Skip to content

Commit

Permalink
Merge pull request #52 from Psycojoker/proxy_list_index
Browse files Browse the repository at this point in the history
Fix discrepancy between indexes in a proxy list
  • Loading branch information
Psycojoker committed Jan 23, 2015
2 parents 6ceacba + 6b64a57 commit 07eeb17
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

0.4.1 (not released)
--------------------

- fix index handling in get_absolute_bounding_box_of_attribute method in
a LineProxyList

0.4 (2014-12-11)
----------------

Expand Down
7 changes: 7 additions & 0 deletions redbaron.py
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,13 @@ def _diff_reduced_list(self):
else:
self.node_list.pop(i)

def get_absolute_bounding_box_of_attribute(self, index):
if index >= len(self.data) or index < 0:
raise IndexError()
index = self[index].index_on_parent
path = self.path().to_baron_path() + [index]
return baron.path.path_to_bounding_box(self.root.fst(), path)


class DecoratorsLineProxyList(LineProxyList):
def _convert_input_to_node_object_list(self, value, parent, on_attribute):
Expand Down
65 changes: 65 additions & 0 deletions tests/test_bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,68 @@ def test_bounding_box_empty():
red = RedBaron("a()")
assert ((1, 3), (1, 2)) == red.atomtrailers.value[1].value.absolute_bounding_box


RED = RedBaron("""\
class A:
''' Class docstring
Class description
'''
attrA = [ a,
b,
c,
d]
def a(self):
''' Function a docstring
Function description
'''
pass
attrB = valB
@myDecorator
def b(self):
''' Function b docstring
Function description
'''
pass
attrC = [ a,
b,
c,
d]\
""")


def test_bounding_box_with_proxy_list():
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[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
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 ((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)
with pytest.raises(IndexError):
RED.class_.value.get_absolute_bounding_box_of_attribute(8)

0 comments on commit 07eeb17

Please sign in to comment.