示例#1
0
def compare_trees(
    actual_tree: ast.AST, file: str, verbose: bool, include_attributes: bool = False,
) -> int:
    with open(file) as f:
        expected_tree = ast.parse(f.read())

    expected_text = ast.dump(expected_tree, include_attributes=include_attributes)
    actual_text = ast.dump(actual_tree, include_attributes=include_attributes)
    if actual_text == expected_text:
        if verbose:
            print("Tree for {file}:")
            print(show_parse.format_tree(actual_tree, include_attributes))
        return 0

    print(f"Diffing ASTs for {file} ...")

    expected = show_parse.format_tree(expected_tree, include_attributes)
    actual = show_parse.format_tree(actual_tree, include_attributes)

    if verbose:
        print("Expected for {file}:")
        print(expected)
        print("Actual for {file}:")
        print(actual)
        print(f"Diff for {file}:")

    diff = show_parse.diff_trees(expected_tree, actual_tree, include_attributes)
    for line in diff:
        print(line)

    return 1
def compare_trees(
    actual_tree: ast.AST,
    file: str,
    verbose: bool,
    include_attributes: bool = False,
) -> int:
    with open(file) as f:
        try:
            expected_tree = ast.parse(f.read())
        except Exception:
            print(f"CPython parser failed on file {file}")
            return 0

    expected_text = ast.dump(expected_tree, include_attributes=include_attributes)
    if actual_tree is None:
        print(f"Pegen generated parser failed to produce any AST for file {file}")
        return 1
    actual_text = ast.dump(actual_tree, include_attributes=include_attributes)
    if actual_text == expected_text:
        if verbose:
            print("AST trees match.\n")
            print(f"Tree for {file}:")
            print(show_parse.format_tree(actual_tree, include_attributes))
        return 0

    print(f"Diffing ASTs for {file} ...")

    expected = show_parse.format_tree(expected_tree, include_attributes)
    actual = show_parse.format_tree(actual_tree, include_attributes)

    if verbose:
        print(f"Expected for {file}:")
        print(expected)
        print(f"Actual for {file}:")
        print(actual)
        print(f"Diff for {file}:")

    diff = show_parse.diff_trees(expected_tree, actual_tree, include_attributes)
    for line in diff:
        print(line)

    return 1