From 56b2ce0c1ed1da61691d74471c90856e5ab52b29 Mon Sep 17 00:00:00 2001 From: Eric Vergnaud Date: Thu, 3 Oct 2024 16:36:42 +0200 Subject: [PATCH 1/2] more tests --- .../source_code/python/test_python_ast.py | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/tests/unit/source_code/python/test_python_ast.py b/tests/unit/source_code/python/test_python_ast.py index 7d9f27e73e..f5c553c823 100644 --- a/tests/unit/source_code/python/test_python_ast.py +++ b/tests/unit/source_code/python/test_python_ast.py @@ -233,3 +233,84 @@ def test_renumbers_negatively(): def test_counts_lines(source: str, line_count: int): tree = Tree.normalize_and_parse(source) assert tree.line_count() == line_count + + +@pytest.mark.parametrize( + "source, name, is_builtin", + [ + ("x = open()", "open", True), + ("import datetime; x = datetime.datetime.now()", "now", True), + ("import stuff; x = stuff()", "stuff", False), + ( + """def stuff(): + pass +x = stuff()""", + "stuff", + False, + ), + ], +) +def test_is_builtin(source, name, is_builtin): + tree = Tree.normalize_and_parse(source) + nodes = list(tree.node.get_children()) + for node in nodes: + if isinstance(node, Assign): + call = node.value + assert isinstance(call, Call) + func_name = TreeHelper.get_call_name(call) + assert func_name == name + assert Tree(call).is_builtin() == is_builtin + return + assert False # could not locate call + + +def test_first_statement_is_none(): + node = Const("xyz") + assert not Tree(node).first_statement() + + +def test_repr_is_truncated(): + assert len(repr(Tree(Const("xyz")))) <= (32 + len("...") + len("")) + + +def test_append_tree_fails(): + with pytest.raises(NotImplementedError): + Tree(Const("xyz")).append_tree(Tree(Const("xyz"))) + + +def test_append_node_fails(): + with pytest.raises(NotImplementedError): + Tree(Const("xyz")).append_nodes([]) + + +def test_nodes_between_fails(): + with pytest.raises(NotImplementedError): + Tree(Const("xyz")).nodes_between(0, 100) + + +def test_has_global_fails(): + assert not Tree.new_module().has_global("xyz") + + +def test_append_globals_fails(): + with pytest.raises(NotImplementedError): + Tree(Const("xyz")).append_globals({}) + + +def test_globals_between_fails(): + with pytest.raises(NotImplementedError): + Tree(Const("xyz")).line_count() + + +def test_line_count_fails(): + with pytest.raises(NotImplementedError): + Tree(Const("xyz")).globals_between(0, 100) + + +def test_renumber_fails(): + with pytest.raises(NotImplementedError): + Tree(Const("xyz")).renumber(100) + + +def test_const_is_not_from_module(): + assert not Tree(Const("xyz")).is_from_module("spark") From 9ddfcf66fee1e76da82d86a189f3967247adb47b Mon Sep 17 00:00:00 2001 From: Eric Vergnaud Date: Thu, 3 Oct 2024 16:47:45 +0200 Subject: [PATCH 2/2] fix merge issue --- .../source_code/python/test_python_ast.py | 29 ------------------- 1 file changed, 29 deletions(-) diff --git a/tests/unit/source_code/python/test_python_ast.py b/tests/unit/source_code/python/test_python_ast.py index f5c553c823..047af8c988 100644 --- a/tests/unit/source_code/python/test_python_ast.py +++ b/tests/unit/source_code/python/test_python_ast.py @@ -235,35 +235,6 @@ def test_counts_lines(source: str, line_count: int): assert tree.line_count() == line_count -@pytest.mark.parametrize( - "source, name, is_builtin", - [ - ("x = open()", "open", True), - ("import datetime; x = datetime.datetime.now()", "now", True), - ("import stuff; x = stuff()", "stuff", False), - ( - """def stuff(): - pass -x = stuff()""", - "stuff", - False, - ), - ], -) -def test_is_builtin(source, name, is_builtin): - tree = Tree.normalize_and_parse(source) - nodes = list(tree.node.get_children()) - for node in nodes: - if isinstance(node, Assign): - call = node.value - assert isinstance(call, Call) - func_name = TreeHelper.get_call_name(call) - assert func_name == name - assert Tree(call).is_builtin() == is_builtin - return - assert False # could not locate call - - def test_first_statement_is_none(): node = Const("xyz") assert not Tree(node).first_statement()