def test_comments(): code = """ # cm0 class Foo: # cm1 def foo(self): # cm2 if 1: # cm3 a = 3 else: # cm4 a = 2 for i in x: # cm5 a += i else: # cm6 pass while x: # cm7 pass else: # cm8 pass try: # cm9 pass except Exception: # cm10 pass else: # cm10 pass finally: # cm11 pass with foo: # cm12 bar """ code = '\n'.join(x[4:] for x in code.splitlines()) root = commonast.parse(code, comments=False) assert 'cm' not in root.tojson() root = commonast.parse(code, comments=True) for i in range(13): assert ('cm%i' % i) in root.tojson()
def test_consistent_ast3(): # Parse the sample file and export as a json string code = open(filename3, 'rb').read().decode() root = commonast.parse(code) js = root.tojson() # Compare with ref _compare_large_strings(_get_ref_json(filename3), js)
def test_python_33_plus(): # Yield from code = "def foo():\n yield from x" root = commonast.parse(code).body_nodes[0] node = root.body_nodes[0].value_node assert isinstance(node, commonast.YieldFrom)
def test_compare_print(): ast = commonast.parse('print(foo, bar)') n = ast.body_nodes[0].value_node assert isinstance(n, commonast.Call) assert n.func_node.name == 'print' assert len(n.arg_nodes) == 2 assert n.arg_nodes[0].name == 'foo' assert n.arg_nodes[1].name == 'bar'
def test_python2_old_syntax(): # We do not support tuple function arg; it would complicate things with raises(RuntimeError): commonast.parse('def foo((a,)=c):pass') # Print statement becomes print function assert commonast.parse('print(foo)') == commonast.parse('print foo') # Exec statement becomes a d function assert commonast.parse('exec(foo)') == commonast.parse('exec foo') # Backticks becomes repr function assert commonast.parse('repr(foo)') == commonast.parse('`foo`')
def test_call_even_some_more(): from commonast import Name, Num, Starred, Keyword code = "foo(a, *b, c, *d, **e, **f)" node = commonast.parse(code).body_nodes[0].value_node assert len(node.arg_nodes) == 4 assert len(node.kwarg_nodes) == 2 for arg, cls in zip(node.arg_nodes + node.kwarg_nodes, [Name, Starred, Name, Starred, Keyword, Keyword]): isinstance(arg, cls)
def test_call_some_more(): from commonast import Name, Num, Starred, Keyword code = "foo(1, a, *b, c=3, **d)" node = commonast.parse(code).body_nodes[0].value_node # Call is in an Expr assert isinstance(node, commonast.Call) assert len(node.arg_nodes) == 3 assert len(node.kwarg_nodes) == 2 for arg, cls in zip(node.arg_nodes + node.kwarg_nodes, [Num, Name, Starred, Num, None.__class__]): isinstance(arg, cls) assert node.arg_nodes[2].value_node.name == 'b' assert node.kwarg_nodes[1].name is None assert node.kwarg_nodes[1].value_node.name == 'd' # keyword with emtpy name
def _export_python_sample_ast(): # Get what to export if sys.version_info > (3, ): filenames = filename1, filename3 else: filenames = filename2, # Write for filename in filenames: filename_bz2 = filename[:-2] + 'bz2' code = open(filename, 'rb').read().decode() root = commonast.parse(code) ast_bin = bz2.compress(root.tojson(indent=None).encode()) with open(filename_bz2, 'wb') as f: n = f.write(ast_bin) or 'some' print('wrote %s bytes to %s' % (n, filename_bz2))
def test_classdef_some_more(): code = "class Foo(Bar, *bases, metaclass=X, **extra_kwargs): pass" node = commonast.parse(code).body_nodes[0] assert isinstance(node, commonast.ClassDef) assert len(node.arg_nodes) == 2 assert len(node.kwarg_nodes) == 2 # assert node.arg_nodes[0].name == 'Bar' assert isinstance(node.arg_nodes[1], commonast.Starred) assert node.arg_nodes[1].value_node.name == 'bases' # kwarg nodes are Keywords assert node.kwarg_nodes[0].name == 'metaclass' assert node.kwarg_nodes[0].value_node.name == 'X' # Name node assert node.kwarg_nodes[1].name is None assert node.kwarg_nodes[1].value_node.name == 'extra_kwargs'
def test_functiondef_some_more(): code = """ def foo(a, b=3, *, c=4, d): pass def bar(a:[], b:(1,2), *c:'xx', **d:'yy') -> 'returns': pass """ code = '\n'.join(x[4:] for x in code.splitlines()) root = commonast.parse(code) assert len(root.body_nodes) == 2 f1, f2 = root.body_nodes assert isinstance(f1, commonast.FunctionDef) assert isinstance(f2, commonast.FunctionDef) # Test args of foo (focus on default values) assert len(f1.arg_nodes) == 2 assert len(f1.kwarg_nodes) == 2 for arg, name in zip(f1.arg_nodes + f1.kwarg_nodes, 'abcd'): assert isinstance(arg, commonast.Arg) assert arg.name == name assert arg.annotation_node is None assert f1.arg_nodes[0].value_node is None assert f1.arg_nodes[1].value_node.value == 3 # Num node assert f1.kwarg_nodes[0].value_node.value == 4 assert f1.kwarg_nodes[1].value_node is None # Test args of bar (focus on annotations) assert len(f2.arg_nodes) == 2 assert len(f2.kwarg_nodes) == 0 for arg, name in zip(f2.arg_nodes + [f2.args_node, f2.kwargs_node], 'abcd'): assert isinstance(arg, commonast.Arg) assert arg.name == name assert f2.args_node.value_node is None assert f2.kwargs_node.value_node is None # assert isinstance(f2.arg_nodes[0].annotation_node, commonast.List) assert isinstance(f2.arg_nodes[1].annotation_node, commonast.Tuple) assert isinstance(f2.args_node.annotation_node, commonast.Str) assert isinstance(f2.kwargs_node.annotation_node, commonast.Str)