def check_add_comments(code, exp_tree, debug_level=0): code = dedent(code).lstrip() tree, comments, lines = parse(code, debug_level=debug_level) tree = add_comments(tree, comments, lines) try: assert nodes_equal(tree, exp_tree) except AssertionError: pprint_ast(tree, include_attributes=True) raise return tree
def check_ast(inp, run=True, mode='eval'): __tracebackhide__ = True # expect a Python AST exp = ast.parse(inp, mode=mode) # observe something from xonsh obs = PARSER.parse(inp, debug_level=0) # Check that they are equal assert nodes_equal(exp, obs) # round trip by running xonsh AST via Python if run: exec(compile(obs, '<test-ast>', mode))
def test_formatting(inp, exp): execer = builtins.__xonsh__.execer # first check that we get what we expect try: obs = reformat(inp) except TypeError: print("Formatter failed!") pprint_ast(execer.parse(inp, {})) raise assert exp == obs, "Bad Tree:\n" + pdump(execer.parse(inp, {})) # next check that the transformation is stable obs2 = reformat(obs) assert obs == obs2 # last, check that the initial AST is the same as the AST we produced # using the normal xonsh parser, barring line & column numbers exp_tree = execer.parse(exp, {}) obs_tree = execer.parse(obs, {}) assert nodes_equal(exp_tree, obs_tree, check_attributes=False)
def test_unmodified(inp): # Context sensitive parsing should not modify AST exp = pyast.parse(inp) obs = check_parse(inp) assert nodes_equal(exp, obs)
def test_parse_inline_comment(): tree, comments, lines = parse("True # I'm a comment\n", debug_level=0) exp = Module(body=[Expr(value=NameConstant(value=True), lineno=1, col_offset=0)]) assert nodes_equal(tree, exp) assert comments == [Comment(s="# I'm a comment", lineno=1, col_offset=6)]
def test_args_in_scope(inp): # Context sensitive parsing should not modify AST exp = pyast.parse(inp) obs = check_parse(inp) assert nodes_equal(exp, obs)