Skip to content
Snippets Groups Projects
Commit 4b72bad7 authored by bey's avatar bey
Browse files

Add more tests for at method. Fix bug. Update CHANGELOG and documentation.

parent dbb9bc95
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -4,6 +4,7 @@ Changelog
0.6.2 (unreleased)
----------------
 
- add at method
- fix some old call to log() weren't lazy, that could cause a crash in some situations by an infinite recursive call and also reduce performances
- fix in _iter_in_rendering_order method to avoid bug in edge cases (issue #107)
 
Loading
Loading
Loading
Loading
@@ -476,6 +476,17 @@ You can find which node is located at a given line and column:
red.find_by_position((1, 5))
red.find_by_position((1, 6)) # '(' is not a redbaron node
 
.at()
-------------------
Returns first node at specific line
.. ipython:: python
red = RedBaron("def a():\n return 42")
red.at(1) # Gives DefNode
red.at(2) # Gives ReturnNode
.. _Node.from_fst:
 
Node.from_fst()
Loading
Loading
Loading
Loading
@@ -215,11 +215,13 @@ class GenericNodesUtils(object):
def at(self, line_no):
if line_no > self.absolute_bounding_box.bottom_right.line or \
line_no < self.absolute_bounding_box.top_left.line:
raise IndexError()
raise IndexError("Line number {0} is outside of the file".format(line_no))
path = Path.from_baron_path(self, baron.path.position_to_path(self.fst(), (line_no, 1)))
node = path.node if path else None
if node is not None and hasattr(node, 'type') and node.type == 'endl':
return node.next
# I am not sure that it is the best solution from design point,
# but it is the first idea which is come to mind
return node.next_recursive
return node
 
def _string_to_node_list(self, string, parent, on_attribute):
Loading
Loading
Loading
Loading
@@ -4,11 +4,11 @@
 
import pytest
import redbaron
from redbaron import RedBaron, DecoratorNode, AssignmentNode, DefNode, EndlNode
from redbaron import RedBaron
 
redbaron.DEBUG = True
 
fst = RedBaron("""\
fst_def = RedBaron("""\
@deco
 
def foo(a, b):
Loading
Loading
@@ -17,9 +17,26 @@ def foo(a, b):
""")
 
 
def test_at():
assert isinstance(fst.at(1), DecoratorNode) is True
assert isinstance(fst.at(2), RedBaron) is True
assert isinstance(fst.at(3), DefNode) is True
assert isinstance(fst.at(4), AssignmentNode) is True
assert isinstance(fst.at(5), AssignmentNode) is True
def test_at_def():
assert fst_def.at(1) is fst_def.find('DecoratorNode')
assert fst_def.at(2) is fst_def
assert fst_def.at(3) is fst_def.def_
assert fst_def.at(4) is fst_def.find_all('AssignmentNode')[0]
assert fst_def.at(5) is fst_def.find_all('AssignmentNode')[1]
fst_class = RedBaron("""\
class Foo(object):
def __init__(self):
self.a = None
def bar(self):
return self.a + 5
""")
def test_at_class():
assert fst_class.at(1) is fst_class.class_
assert fst_class.at(2) is fst_class.find_all('DefNode')[0]
assert fst_class.at(3) is fst_class.find('AssignmentNode')
assert fst_class.at(4) is fst_class.find_all('DefNode')[1]
assert fst_class.at(5) is fst_class.find('ReturnNode')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment