From 08350ad13a25f7352ff45266a45d7c4b9a44d233 Mon Sep 17 00:00:00 2001 From: Pierre Penninckx Date: Fri, 23 Jan 2015 17:15:48 +0100 Subject: [PATCH 1/6] [fix] shows abnormal whitespace production on insert Furthermore, this shows an unexpected behaviour with the index in a LineProxyList. The .insert(2, ...) should insert just before "class B" but instead inserts after it. This is because the \n after "class A" is merged with that node's value but it shouldn't. --- tests/test_insert.py | 76 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 tests/test_insert.py diff --git a/tests/test_insert.py b/tests/test_insert.py new file mode 100644 index 00000000..664eae6a --- /dev/null +++ b/tests/test_insert.py @@ -0,0 +1,76 @@ +#!/usr/bin/python +# -*- coding:Utf-8 -*- + +""" Tests the code insertion features """ + +import pytest +# pylint: disable=redefined-outer-name +from redbaron import RedBaron + + +@pytest.fixture +def red(): + return RedBaron("""\ +class A: + pass + +class B: + pass +""") + + +def test_insert_with_class_0(red): + red.insert(0, "a = 1") + + print(red.dumps()) + assert red.dumps() == """\ +a = 1 +class A: + pass + +class B: + pass +""" + + +def test_insert_with_class_1(red): + red.insert(1, "a = 1") + + print(red.dumps()) + assert red.dumps() == """\ +class A: + pass +a = 1 + +class B: + pass +""" + + +def test_insert_with_class_2(red): + red.insert(2, "a = 1") + + print(red.dumps()) + assert red.dumps() == """\ +class A: + pass + +a = 1 +class B: + pass +""" + + +def test_insert_with_class_3(red): + red.insert(3, "a = 1") + + print(red.dumps()) + assert red.dumps() == """\ +class A: + pass + +class B: + pass +a = 1 +""" + From 30d1436f0e509e1fdc3893dd92bff9274e5de1ef Mon Sep 17 00:00:00 2001 From: Pierre Penninckx Date: Fri, 23 Jan 2015 17:19:42 +0100 Subject: [PATCH 2/6] [fix] shows class node too greedy with last newline --- tests/test_bounding_box.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/tests/test_bounding_box.py b/tests/test_bounding_box.py index 4e15df81..9930c3b3 100644 --- a/tests/test_bounding_box.py +++ b/tests/test_bounding_box.py @@ -121,11 +121,13 @@ def test_bounding_box_with_proxy_list(): assert ((2, 5), (5, 7)) == RED.class_.value[0].absolute_bounding_box assert ((6, 5), (9, 17)) == RED.class_.value[1].absolute_bounding_box assert ((9, 18), (10, 0)) == RED.class_.value[2].absolute_bounding_box - assert ((11, 5), (18, 4)) == RED.class_.value[3].absolute_bounding_box - assert ((18, 5), (18, 16)) == RED.class_.value[4].absolute_bounding_box - assert ((18, 17), (19, 0)) == RED.class_.value[5].absolute_bounding_box - assert ((20, 5), (28, 4)) == RED.class_.value[6].absolute_bounding_box - assert ((28, 5), (31, 17)) == RED.class_.value[7].absolute_bounding_box + assert ((11, 5), (16, 12)) == RED.class_.value[3].absolute_bounding_box + assert ((16, 13), (17, 0)) == RED.class_.value[4].absolute_bounding_box + assert ((18, 5), (18, 16)) == RED.class_.value[5].absolute_bounding_box + assert ((18, 17), (19, 0)) == RED.class_.value[6].absolute_bounding_box + assert ((20, 5), (26, 12)) == RED.class_.value[7].absolute_bounding_box + assert ((26, 13), (27, 0)) == RED.class_.value[8].absolute_bounding_box + assert ((28, 5), (31, 17)) == RED.class_.value[9].absolute_bounding_box with pytest.raises(IndexError): RED.class_.value[8] @@ -136,10 +138,12 @@ def test_bounding_box_of_attribute_with_proxy_list(): assert ((2, 5), (5, 7)) == RED.class_.value.get_absolute_bounding_box_of_attribute(0) assert ((6, 5), (9, 17)) == RED.class_.value.get_absolute_bounding_box_of_attribute(1) assert ((9, 18), (10, 0)) == RED.class_.value.get_absolute_bounding_box_of_attribute(2) - assert ((11, 5), (18, 4)) == RED.class_.value.get_absolute_bounding_box_of_attribute(3) - assert ((18, 5), (18, 16)) == RED.class_.value.get_absolute_bounding_box_of_attribute(4) - assert ((18, 17), (19, 0)) == RED.class_.value.get_absolute_bounding_box_of_attribute(5) - assert ((20, 5), (28, 4)) == RED.class_.value.get_absolute_bounding_box_of_attribute(6) - assert ((28, 5), (31, 17)) == RED.class_.value.get_absolute_bounding_box_of_attribute(7) + assert ((11, 5), (16, 12)) == RED.class_.value.get_absolute_bounding_box_of_attribute(3) + assert ((16, 13), (17, 0)) == RED.class_.value.get_absolute_bounding_box_of_attribute(4) + assert ((18, 5), (18, 16)) == RED.class_.value.get_absolute_bounding_box_of_attribute(5) + assert ((18, 17), (19, 0)) == RED.class_.value.get_absolute_bounding_box_of_attribute(6) + assert ((20, 5), (26, 12)) == RED.class_.value.get_absolute_bounding_box_of_attribute(7) + assert ((26, 13), (27, 0)) == RED.class_.value.get_absolute_bounding_box_of_attribute(8) + assert ((28, 5), (31, 17)) == RED.class_.value.get_absolute_bounding_box_of_attribute(9) with pytest.raises(IndexError): RED.class_.value.get_absolute_bounding_box_of_attribute(8) From a81c26375e2a59a2fe15b7465d51fcc6086c297e Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 30 Jan 2015 05:14:14 +0100 Subject: [PATCH 3/6] [mod] test aren't exactly correct --- tests/test_insert.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_insert.py b/tests/test_insert.py index 664eae6a..6c20af1a 100644 --- a/tests/test_insert.py +++ b/tests/test_insert.py @@ -40,8 +40,8 @@ def test_insert_with_class_1(red): assert red.dumps() == """\ class A: pass -a = 1 +a = 1 class B: pass """ @@ -55,9 +55,9 @@ def test_insert_with_class_2(red): class A: pass -a = 1 class B: pass +a = 1 """ @@ -73,4 +73,3 @@ class B: pass a = 1 """ - From 6a022fdfa83dd3e6a110065371b6a8cafc046137 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 30 Jan 2015 05:15:29 +0100 Subject: [PATCH 4/6] [fix] class and functions handle inner whitespaces so I don't have to take care of it --- redbaron.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/redbaron.py b/redbaron.py index 92af29aa..eec63e19 100644 --- a/redbaron.py +++ b/redbaron.py @@ -1525,7 +1525,8 @@ def generate_separator(): expected_list.pop() expected_list.append(i) - expected_list.append(generate_separator()) + if i.type not in ('function', 'class'): + expected_list.append(generate_separator()) if expected_list: if self.parent and self.parent.next: @@ -2745,7 +2746,7 @@ def generate_separator(): expected_list.append(i) - if not (i.type == "endl" and position == 0): + if not (i.type == "endl" and position == 0) and (i.type not in ('function', 'class')): expected_list.append(generate_separator()) return expected_list From aea6f64b2f96521628c8eb710a7950a7cc7df427 Mon Sep 17 00:00:00 2001 From: Pierre Penninckx Date: Mon, 2 Feb 2015 11:44:53 +0100 Subject: [PATCH 5/6] [mod] remove part of test to split PR in two --- tests/test_bounding_box.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/tests/test_bounding_box.py b/tests/test_bounding_box.py index 9930c3b3..5d42462b 100644 --- a/tests/test_bounding_box.py +++ b/tests/test_bounding_box.py @@ -121,29 +121,25 @@ def test_bounding_box_with_proxy_list(): assert ((2, 5), (5, 7)) == RED.class_.value[0].absolute_bounding_box assert ((6, 5), (9, 17)) == RED.class_.value[1].absolute_bounding_box assert ((9, 18), (10, 0)) == RED.class_.value[2].absolute_bounding_box - assert ((11, 5), (16, 12)) == RED.class_.value[3].absolute_bounding_box - assert ((16, 13), (17, 0)) == RED.class_.value[4].absolute_bounding_box - assert ((18, 5), (18, 16)) == RED.class_.value[5].absolute_bounding_box - assert ((18, 17), (19, 0)) == RED.class_.value[6].absolute_bounding_box - assert ((20, 5), (26, 12)) == RED.class_.value[7].absolute_bounding_box - assert ((26, 13), (27, 0)) == RED.class_.value[8].absolute_bounding_box - assert ((28, 5), (31, 17)) == RED.class_.value[9].absolute_bounding_box + assert ((11, 5), (18, 4)) == RED.class_.value[3].absolute_bounding_box + assert ((18, 5), (18, 16)) == RED.class_.value[4].absolute_bounding_box + assert ((18, 17), (19, 0)) == RED.class_.value[5].absolute_bounding_box + assert ((20, 5), (28, 4)) == RED.class_.value[6].absolute_bounding_box + assert ((28, 5), (31, 17)) == RED.class_.value[7].absolute_bounding_box with pytest.raises(IndexError): RED.class_.value[8] def test_bounding_box_of_attribute_with_proxy_list(): - # assert ((1, 1), (32, 0)) == RED.absolute_bounding_box - # assert ((1, 1), (32, 0)) == RED.class_.absolute_bounding_box + assert ((1, 1), (32, 0)) == RED.absolute_bounding_box + assert ((1, 1), (32, 0)) == RED.class_.absolute_bounding_box assert ((2, 5), (5, 7)) == RED.class_.value.get_absolute_bounding_box_of_attribute(0) assert ((6, 5), (9, 17)) == RED.class_.value.get_absolute_bounding_box_of_attribute(1) assert ((9, 18), (10, 0)) == RED.class_.value.get_absolute_bounding_box_of_attribute(2) - assert ((11, 5), (16, 12)) == RED.class_.value.get_absolute_bounding_box_of_attribute(3) - assert ((16, 13), (17, 0)) == RED.class_.value.get_absolute_bounding_box_of_attribute(4) - assert ((18, 5), (18, 16)) == RED.class_.value.get_absolute_bounding_box_of_attribute(5) - assert ((18, 17), (19, 0)) == RED.class_.value.get_absolute_bounding_box_of_attribute(6) - assert ((20, 5), (26, 12)) == RED.class_.value.get_absolute_bounding_box_of_attribute(7) - assert ((26, 13), (27, 0)) == RED.class_.value.get_absolute_bounding_box_of_attribute(8) - assert ((28, 5), (31, 17)) == RED.class_.value.get_absolute_bounding_box_of_attribute(9) + assert ((11, 5), (18, 4)) == RED.class_.value.get_absolute_bounding_box_of_attribute(3) + assert ((18, 5), (18, 16)) == RED.class_.value.get_absolute_bounding_box_of_attribute(4) + assert ((18, 17), (19, 0)) == RED.class_.value.get_absolute_bounding_box_of_attribute(5) + assert ((20, 5), (28, 4)) == RED.class_.value.get_absolute_bounding_box_of_attribute(6) + assert ((28, 5), (31, 17)) == RED.class_.value.get_absolute_bounding_box_of_attribute(7) with pytest.raises(IndexError): RED.class_.value.get_absolute_bounding_box_of_attribute(8) From c5d310353050644d0a83659793f9cbb9bec2f29d Mon Sep 17 00:00:00 2001 From: Pierre Penninckx Date: Mon, 2 Feb 2015 12:06:04 +0100 Subject: [PATCH 6/6] [doc] update changelog --- CHANGELOG | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 2895f808..d6c06539 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,11 @@ Changelog ========= +0.5.1 (unreleased) +------------------ + +- fix whitespace duplication when using .insert() + 0.5 (2015-01-31) ----------------