Beispiel #1
0
def test_same_nodes_use_target():
    parser = make_parser('''
    class Foo:
        def foo(self):
            self.x, self.x
    ''')
    node = parser._nodes[-1]
    assert [n.name for n in list(parser.same_nodes(node, use_target=True))
            ] == ['x', 'x']
    assert [n.name for n in list(parser.same_nodes(node, use_target=False))
            ] == ['self', 'self', 'self']
Beispiel #2
0
def test_attributes():
    parser = make_parser('''
    aa.bb
    cc.self.dd
    self.ee
    def a(self):
        self.ff
    class A:
        def b(self):
            self.gg
    class B:
        def c(self):
            self.gg
        def d(self):
            self.gg
        def e(self):
            self.hh
        def f(foo):
            self.gg
    ''')
    names = parser._nodes
    names = [n for n in names if n.hl_group == ATTRIBUTE]
    b_gg, c_gg, d_gg, e_hh = names
    same_nodes = set(parser.same_nodes(c_gg))
    assert same_nodes == {c_gg, d_gg}
Beispiel #3
0
def test_base_scope_class():
    parser = make_parser('''
    class A:
        x = 1
        x
    ''')
    names = parser._nodes
    A, x1, x2 = names
    same_nodes = set(parser.same_nodes(x1))
    assert same_nodes == {x1, x2}
Beispiel #4
0
def test_base_scope_nonlocal_free():
    parser = make_parser('''
    def foo():
        a = 1
        def bar():
            nonlocal a
            a = 1
    ''')
    foo, foo_a, bar, bar_nonlocal_a, bar_a = parser._nodes
    assert set(parser.same_nodes(foo_a)) == {foo_a, bar_nonlocal_a, bar_a}
Beispiel #5
0
def test_base_scope_free():
    parser = make_parser('''
    def a():
        x = 1
        def b():
            x
    ''')
    names = parser._nodes
    a, a_x, b, b_x = names
    same_nodes = set(parser.same_nodes(a_x))
    assert same_nodes == {a_x, b_x}
Beispiel #6
0
def test_same_nodes():
    parser = make_parser('''
    x = 1
    class A:
        x
        def B():
            x
    ''')
    names = parser._nodes
    x, A, A_x, B, B_x = names
    same_nodes = set(parser.same_nodes(x))
    assert same_nodes == {x, A_x, B_x}
Beispiel #7
0
def test_base_scope_class_nested():
    parser = make_parser('''
    def z():
        x = 1
        class A():
            x = 2
            def b():
                return x
    ''')
    names = parser._nodes
    z, z_x, A, A_x, b, b_x = names
    same_nodes = set(parser.same_nodes(z_x))
    assert same_nodes == {z_x, b_x}
Beispiel #8
0
def test_base_scope_global():
    parser = make_parser('''
    x = 1
    def a():
        x = 2
        def b():
            global x
            x
    ''')
    names = parser._nodes
    x, a, a_x, b, b_global_x, b_x = names
    same_nodes = set(parser.same_nodes(x))
    assert same_nodes == {x, b_global_x, b_x}
Beispiel #9
0
def test_self_target():
    """The target of a self with an attribute should be the attribute node."""
    parser = make_parser('''
    self.abc
    class Foo:
        def x(self):
            self.abc
    ''')
    names = parser._nodes
    assert names[0].target is None
    last_self = names[-1]
    abc = names[-2]
    assert last_self.target is abc
    assert last_self.target.name == 'abc'
    assert list(parser.same_nodes(last_self)) == [abc]
Beispiel #10
0
def test_exclude_types_same_nodes():
    parser = Parser(exclude=[UNRESOLVED])
    add, clear = parser.parse('a, a')
    assert len(add) == 0
    assert [n.pos for n in parser.same_nodes((1, 0))] == [(1, 0), (1, 3)]
Beispiel #11
0
def test_same_nodes_empty():
    parser = make_parser('0, 1')
    assert parser.same_nodes((1, 0)) == []
Beispiel #12
0
def test_same_nodes_exclude_current():
    parser = make_parser('a, a, a')
    a0, a1, a2 = parser._nodes
    assert set(parser.same_nodes(a0, mark_original=False)) == {a1, a2}