예제 #1
0
def test_dispatched_walker():

    @ast_inspector
    class collect_numbers_with_default:
        @staticmethod
        def handle_Num(node, state, **kwds):
            return state.update(numbers=state.numbers.add(node.n))

        @staticmethod
        def handle(node, state, **kwds):
            return state

    @ast_inspector
    class collect_numbers:
        @staticmethod
        def handle_Num(node, state, **kwds):
            return state.update(numbers=state.numbers.add(node.n))

    node = get_ast(dummy)

    state = collect_numbers(node, state=dict(numbers=immutableset()))
    assert state.numbers == set([1, 4])

    state = collect_numbers_with_default(node, state=dict(numbers=immutableset()))
    assert state.numbers == set([1, 4])
예제 #2
0
def test_set_add():
    s = immutableset([1])
    ns = s.add(1)
    assert ns is s

    s = immutableset([1])
    ns = s.add(2)
    assert ns == set([1, 2])
    assert s == set([1])
예제 #3
0
def test_set_discard():

    s = immutableset([1])
    ns = s.discard(3)
    assert ns is s

    s = immutableset([1, 2])
    ns = s.discard(2)
    assert ns == set([1])
    assert s == set([1, 2])
예제 #4
0
def test_intersection():
    s1 = immutableset([1, 2])
    s2 = immutableset([1, 2])
    s3 = immutableset([1])

    ns = s1 & s3
    assert ns == set([1])
    assert type(ns) == immutableset

    ns = s1 & s2
    assert ns is s1

    ns = s1.intersection_update(s3)
    assert ns == set([1])
    assert type(ns) == immutableset

    with pytest.raises(AttributeError):
        s1 &= s3
예제 #5
0
def test_union():
    s1 = immutableset([1, 2])
    s2 = immutableset([2])
    s3 = immutableset([3])

    ns = s1 | s3
    assert ns == set([1, 2, 3])
    assert type(ns) == immutableset

    ns = s1 | s2
    assert ns is s1

    ns = s1.update(s3)
    assert ns == set([1, 2, 3])
    assert type(ns) == immutableset

    with pytest.raises(AttributeError):
        s1 |= s3
예제 #6
0
def test_difference():
    s1 = immutableset([1, 2])
    s2 = immutableset([2])
    s3 = immutableset([3])

    ns = s1 - s2
    assert ns == set([1])
    assert type(ns) == immutableset

    ns = s1 - s3
    assert ns is s1

    ns = s1.difference_update(s2)
    assert ns == set([1])
    assert type(ns) == immutableset

    with pytest.raises(AttributeError):
        s1 -= s2
예제 #7
0
def test_symmetric_difference():
    s1 = immutableset([1, 2])
    s2 = immutableset([2, 3])
    s3 = immutableset([3])

    ns = s1 ^ s2
    assert ns == set([1, 3])
    assert type(ns) == immutableset

    ns = s1 ^ set()
    assert ns is s1

    ns = s1.symmetric_difference_update(s2)
    assert ns == set([1, 3])
    assert type(ns) == immutableset

    with pytest.raises(AttributeError):
        s1 ^= s2
예제 #8
0
def test_walk_list():

    @ast_inspector
    def collect_numbers(node, state, **kwds):
        if isinstance(node, ast.Num):
            return state.update(numbers=state.numbers.add(node.n))
        else:
            return state

    node = get_ast(dummy)
    state = collect_numbers(node.body, state=dict(numbers=immutableset()))
    assert state.numbers == set([1, 4])
예제 #9
0
def test_walk_field_inspect():

    @ast_inspector
    def names_and_nums(node, state, walk_field, **kwds):
        if isinstance(node, ast.Assign):
            state = walk_field(node.value, state)
            return state.update(objs=state.objs.add(node.targets[0].id))
        elif isinstance(node, ast.Num):
            return state.update(objs=state.objs.add(node.n))
        else:
            return state

    node = get_ast(dummy)
    state = names_and_nums(node, state=dict(objs=immutableset()))
    assert state.objs == set(['a', 'c', 1, 4])
예제 #10
0
def test_walker():

    @ast_walker
    def process_numbers(node, state, **kwds):
        if isinstance(node, ast.Num):
            return ast.Num(n=node.n + 1), state.update(numbers=state.numbers.add(node.n))
        else:
            return node, state

    node = get_ast(dummy)
    new_node, state = process_numbers(node, state=dict(numbers=immutableset()))

    assert state.numbers == set([1, 4])
    assert_ast_equal(new_node, get_ast("""
        def dummy(x, y):
            c = 5
            a = 2
        """))
예제 #11
0
def test_walk_field_transform_inspect():

    @ast_walker
    def names_and_incremented_nums(node, state, walk_field, **kwds):
        if isinstance(node, ast.Assign):
            value_node, state = walk_field(node.value, state)
            new_node = replace_fields(node, targets=node.targets, value=value_node)
            new_state = state.update(objs=state.objs.add(node.targets[0].id))
            return new_node, new_state
        elif isinstance(node, ast.Num):
            return ast.Num(n=node.n + 1), state.update(objs=state.objs.add(node.n))
        else:
            return node, state

    node = get_ast(dummy)
    new_node, state = names_and_incremented_nums(node, state=dict(objs=immutableset()))
    assert state.objs == set(['a', 'c', 1, 4])
    assert_ast_equal(new_node, get_ast(
        """
        def dummy(x, y):
            c = 5
            a = 2
        """))
예제 #12
0
def test_set_pop():
    s = immutableset([1])
    elem, ns = s.pop()
    assert ns == set()
    assert s == set([1])
    assert elem == 1
예제 #13
0
def test_set_remove():
    s = immutableset([1, 2])
    ns = s.remove(2)
    assert ns == set([1])
    assert s == set([1, 2])
예제 #14
0
def test_set_copy():
    s = immutableset([1])
    ns = s.copy()
    assert ns is s
예제 #15
0
파일: scope.py 프로젝트: fjarri/peval
def analyze_scope(node):
    state = _analyze_scope(dict(locals=immutableset(), locals_used=immutableset(), globals=immutableset()), node)
    return Scope(locals=state.locals, locals_used=state.locals_used, globals=state.globals)
예제 #16
0
def find_symbol_usages(tree):
    ''' Return a set of all variables used in ast tree
    '''
    state = _find_symbol_usages(tree, state=dict(names=immutableset()))
    return state.names
예제 #17
0
def find_symbol_creations(tree):
    ''' Return a set of all local variable names in ast tree
    '''
    state = _find_symbol_creations(tree, state=dict(names=immutableset()))
    return state.names
예제 #18
0
def test_set_repr():
    s = immutableset([1])
    ns = eval(repr(s))
    assert type(ns) == type(s)
    assert ns == s
예제 #19
0
def test_set_clear():
    s = immutableset([1])
    ns = s.clear()
    assert ns == set()
    assert s == set([1])