def test_exclude_types(): parser = Parser(exclude=[LOCAL]) add, clear = parser.parse(dedent(''' a = 1 def f(): b, c = 1 a + b ''')) assert [n.name for n in add] == ['a'] assert clear == [] add, clear = parser.parse(dedent(''' a = 1 def f(): b, c = 1 a + c ''')) assert add == [] assert clear == [] add, clear = parser.parse(dedent(''' a = 1 def f(): b, c = 1 g + c ''')) assert [n.name for n in add] == ['g'] assert [n.name for n in clear] == ['a'] add, clear = parser.parse(dedent(''' a = 1 def f(): b, c = 1 0 + c ''')) assert add == [] assert [n.name for n in clear] == ['g']
def test_diff(): """The id of a saved name should remain the same so that we can remove it later by ID.""" parser = Parser() add0, rem = parser.parse('foo') add, rem = parser.parse('foo ') add, rem = parser.parse('foo = 1') assert add0[0].id == rem[0].id
def test_detect_symtable_syntax_error(): """Some syntax errors (such as duplicate parameter names) aren't directly raised when compile() is called on the code, but cause problems later. """ parser = Parser() with pytest.raises(UnparsableError): parser.parse('def foo(x, x): pass') assert parser.syntax_errors[-1].lineno == 1
def test_syntax_error_fail(): """Syntax errors which can't be fixed with a single change.""" parser = Parser() with pytest.raises(UnparsableError): parser.parse('(\n(') with pytest.raises(UnparsableError): parser.parse(')\n(') # Intentionally no difference to previous one with pytest.raises(UnparsableError): parser.parse(')\n(')
def test_refresh_names(): """Clear everything if more than one line changes.""" parser = Parser() add, clear = parser.parse(dedent(''' def foo(): x = y ''')) assert len(add) == 3 assert len(clear) == 0 add, clear = parser.parse(dedent(''' def foo(): x = y ''')) assert len(add) == 0 assert len(clear) == 0 add, clear = parser.parse(dedent(''' def foo(): z = y ''')) assert len(add) == 1 assert len(clear) == 1 add, clear = parser.parse( dedent(''' def foo(): z = y a, b ''')) assert len(add) == 5 assert len(clear) == 3 add, clear = parser.parse( dedent(''' def foo(): z = y c, d ''')) assert len(add) == 2 assert len(clear) == 2 add, clear = parser.parse( dedent(''' def foo(): z = y, k 1, 1 ''')) assert len(add) == 4 assert len(clear) == 5
def test_make_nodes(): """parser._make_nodes should work without a `lines` argument.""" parser = Parser() parser._make_nodes('x')
def test_exclude_types_same_nodes(): parser = Parser(exclude=[UNRESOLVED]) add, clear = parser.parse('a, a') assert len(add) == 0 assert [n.pos for n in parser.same_nodes((1, 0))] == [(1, 0), (1, 3)]
def make_parser(code): parser = Parser() add, remove = parser.parse(dedent(code)) return parser
def parse(code): add, remove = Parser().parse(dedent(code)) assert len(remove) == 0 for node in add: node.base_table() return add