Example #1
0
def test_path_walker_assignment():
    node = parse("a = 1")
    # Indentation is purely visual sugar
    walker = PathWalkerTester([
        ('>', [0]),
        ('>', [0, 'target']),
        ('>', [0, 'target', 'value']),
        ('<', [0, 'target', 'value']),
        ('<', [0, 'target']),
        ('>', [0, 'first_formatting']),
        ('>', [0, 'first_formatting', 0]),
        ('>', [0, 'first_formatting', 0, 'value']),
        ('<', [0, 'first_formatting', 0, 'value']),
        ('<', [0, 'first_formatting', 0]),
        ('<', [0, 'first_formatting']),
        ('>', [0, '=']),
        ('<', [0, '=']),
        ('>', [0, 'second_formatting']),
        ('>', [0, 'second_formatting', 0]),
        ('>', [0, 'second_formatting', 0, 'value']),
        ('<', [0, 'second_formatting', 0, 'value']),
        ('<', [0, 'second_formatting', 0]),
        ('<', [0, 'second_formatting']),
        ('>', [0, 'value']),
        ('>', [0, 'value', 'value']),
        ('<', [0, 'value', 'value']),
        ('<', [0, 'value']),
        ('<', [0]),
    ])

    walker.walk(node)
Example #2
0
def test_path_walker_assignment():
    node = parse("a = 1")
    # Indentation is purely visual sugar
    walker = PathWalkerTester([
        ('>', [0]),
        ('>', [0, 'target']),
        ('>', [0, 'target', 'value']),
        ('<', [0, 'target', 'value']),
        ('<', [0, 'target']),
        ('>', [0, 'first_formatting']),
        ('>', [0, 'first_formatting', 0]),
        ('>', [0, 'first_formatting', 0, 'value']),
        ('<', [0, 'first_formatting', 0, 'value']),
        ('<', [0, 'first_formatting', 0]),
        ('<', [0, 'first_formatting']),
        ('>', [0, '=']),
        ('<', [0, '=']),
        ('>', [0, 'second_formatting']),
        ('>', [0, 'second_formatting', 0]),
        ('>', [0, 'second_formatting', 0, 'value']),
        ('<', [0, 'second_formatting', 0, 'value']),
        ('<', [0, 'second_formatting', 0]),
        ('<', [0, 'second_formatting']),
        ('>', [0, 'value']),
        ('>', [0, 'value', 'value']),
        ('<', [0, 'value', 'value']),
        ('<', [0, 'value']),
        ('<', [0]),
    ])

    walker.walk(node)
Example #3
0
def test_bc_l1_def_first_formatting():
    tree = parse(bigcode)
    path = [0, "first_formatting"]
    assert path_to_bounding_box(tree, path) == ((1, 4), (1, 4))

    path = [0, "first_formatting", 0]
    assert path_to_bounding_box(tree, path) == ((1, 4), (1, 4))

    path = [0, "first_formatting", 0, "value"]
    check_path(bigcode, [(1, 4)], path)
Example #4
0
def test_bc_l1_def_first_formatting():
    tree = parse(bigcode)
    path = [0, "first_formatting"]
    assert path_to_bounding_box(tree, path) == ((1, 4), (1, 4))

    path = [0, "first_formatting", 0]
    assert path_to_bounding_box(tree, path) == ((1, 4), (1, 4))

    path = [0, "first_formatting", 0, "value"]
    check_path(bigcode, [(1, 4)], path)
Example #5
0
def check_path(code, positions, target_path):
    tree = parse(code)

    for (line, column) in positions:
        path = position_to_path(tree, line, column)
        assert path == target_path

        if path is None:
            assert position_to_node(tree, line, column) is None
            return

        node = path_to_node(tree, path)
        assert isinstance(node, string_instance)

        assert position_to_node(tree, line, column) is node

    if target_path is not None:
        bounding_box = (positions[0], positions[-1])
        assert path_to_bounding_box(tree, path) == bounding_box
Example #6
0
def check_path(code, positions, target_path):
    tree = parse(code)

    for position in positions:
        path = position_to_path(tree, position)
        assert path == target_path

        if path is None:
            assert position_to_node(tree, position) is None
            return

        node = path_to_node(tree, path)
        assert isinstance(node, string_instance)

        assert position_to_node(tree, position) is node

    if target_path is not None:
        bounding_box = (positions[0], positions[-1])
        assert path_to_bounding_box(tree, path) == bounding_box
Example #7
0
def test_print_as_print():
    code = "print(a)"
    assert print_is_parsed_as_a_function(parse(code, False)) == False
Example #8
0
def test_bc_l1_def_second_formatting():
    path = [0, "second_formatting"]
    tree = parse(bigcode)
    assert path_to_bounding_box(tree, path) == ((1, 8), (1, 7))
Example #9
0
def test_print_as_name():
    code = "print(a)"
    assert print_is_parsed_as_a_function(parse(code, True)) == True
Example #10
0
def test_bb_class():
    node = parse(classcode)[0]
    assert node_to_bounding_box(node) == ((1, 1), (7, 0))
Example #11
0
def test_bb_funcdef():
    node = parse(bigcode)[0]
    assert node_to_bounding_box(node) == ((1, 1), (4, 0))
Example #12
0
def test_bb_name():
    node = parse("var")[0]
    assert node_to_bounding_box(node) == ((1, 1), (1, 3))
Example #13
0
def test_bb_string():
    node = parse("\"hello\"")[0]
    assert path_to_bounding_box(node, []) == ((1, 1), (1, 7))
Example #14
0
def test_bb_name():
    node = parse("var")[0]
    assert node_to_bounding_box(node) == ((1, 1), (1, 3))
Example #15
0
def test_print_as_print():
    code = "print(a)"
    assert print_is_parsed_as_a_function(parse(code, False)) == False
Example #16
0
def test_print_as_name():
    code = "print(a)"
    assert print_is_parsed_as_a_function(parse(code, True)) == True
Example #17
0
def test_auto_print_as_name():
    code = "from __future__ import print_function\nprint(a)"
    assert print_is_parsed_as_a_function(parse(code)) == True
Example #18
0
def test_bb_comment():
    node = parse("# comment")[0]
    assert path_to_bounding_box(node, None) == ((1, 1), (1, 9))
Example #19
0
def test_bb_comment():
    node = parse("# comment")[0]
    assert path_to_bounding_box(node, None) == ((1, 1), (1, 9))
Example #20
0
def test_auto_print_as_print():
    code = "print(a)"
    assert not print_is_parsed_as_a_function(parse(code))
Example #21
0
def test_bb_string():
    node = parse("\"hello\"")[0]
    assert path_to_bounding_box(node, []) == ((1, 1), (1, 7))
Example #22
0
def test_bb_funcdef():
    node = parse(funcdefcode)[0]
    assert node_to_bounding_box(node) == ((1, 1), (3, 9))
Example #23
0
def test_bb_funcdef():
    node = parse(bigcode)[0]
    assert node_to_bounding_box(node) == ((1, 1), (4, 0))
Example #24
0
def test_bb_class():
    node = parse(classcode)[0]
    assert node_to_bounding_box(node) == ((1, 1), (6, 21))
Example #25
0
def test_bb_split_assignment():
    node = parse(splitcode)[0]
    assert node_to_bounding_box(node) == ((1, 1), (2, 5))
Example #26
0
def test_bb_split_assignment():
    node = parse(splitcode)[0]
    assert node_to_bounding_box(node) == ((1, 1), (2, 5))
Example #27
0
def test_bc_l1_def_second_formatting():
    path = [0, "second_formatting"]
    tree = parse(bigcode)
    assert path_to_bounding_box(tree, path) == ((1, 8), (1, 7))
Example #28
0
def test_auto_print_as_name():
    code = "from __future__ import print_function\nprint(a)"
    assert print_is_parsed_as_a_function(parse(code)) == True