Beispiel #1
0
def test_exact():
    @get_body_ast
    def tree():
        if x < 12:
            return True
        else:
            return False
    assert match(useless_if, tree) == [tree[0]]
Beispiel #2
0
def test_constant_mismatch():
    @get_body_ast
    def tree():
        if x < 12:
            return False
        else:
            return False
    assert match(useless_if, tree) == []
Beispiel #3
0
def test_offset():
    @get_body_ast
    def tree():
        y = 1
        if x < 12:
            return True
        else:
            return False
    assert match(useless_if, tree) == [tree[1]]
Beispiel #4
0
def test_extra_statement():
    @get_body_ast
    def tree():
        if x < 12:
            return True
        else:
            return False
            pass
    assert match(useless_if, tree) == []
Beispiel #5
0
def test_nested():
    @get_body_ast
    def tree():
        while cond:
            if x < 12:
                return True
            else:
                return False
    assert match(useless_if, tree) == [tree[0].body[0]]
Beispiel #6
0
def test_subexpr():
    @compile_template
    def expr():
        x + 1

    @get_body_ast
    def tree():
        y = x + 1

    import astor

    assert match(expr, tree) == [tree[0].value]
Beispiel #7
0
def test_more_captures():
    @compile_template
    def map_lambda(var=ast.Name, body=ast.expr, seq=ast.expr):
        map(lambda var: body, seq)

    @get_body_ast
    def tree():
        squares = map(lambda x: x**2, range(10))

    m = match(map_lambda, tree)[0]
    assert astor.to_source(m.captures['body']) == '(x ** 2)'
    assert astor.to_source(m.captures['seq']) == 'range(10)'
Beispiel #8
0
def test_more_captures():
    name_types = (ast.Name, ast.arg) if six.PY3 else ast.Name

    @compile_template
    def map_lambda(var=name_types, body=ast.expr, seq=ast.expr):
        map(lambda var: body, seq)

    @get_body_ast
    def tree():
        squares = map(lambda x: x ** 2, range(10))

    m = match(map_lambda, tree)[0]
    assert astor.to_source(m.captures['body']).strip() == '(x ** 2)'
    assert astor.to_source(m.captures['seq']).strip() == 'range(10)'
Beispiel #9
0
def test_capture():
    @compile_template
    def useless_if(cond=ast.expr):
        if cond:
            return True
        else:
            return False

    @get_body_ast
    def tree():
        if x < 12:
            return True
        else:
            return False

    m = match(useless_if, tree)[0]
    assert astor.to_source(m.captures['cond']) == '(x < 12)'
Beispiel #10
0
def test_capture():
    @compile_template
    def useless_if(cond=ast.expr):
        if cond:
            return True
        else:
            return False

    @get_body_ast
    def tree():
        if x < 12:
            return True
        else:
            return False

    m = match(useless_if, tree)[0]
    assert astor.to_source(m.captures['cond']).strip() == '(x < 12)'
Beispiel #11
0
def test_other_names():
    @get_body_ast
    def tree():
        a = 1
        b = a
    assert match(assignments, tree) == [tree[0]]
Beispiel #12
0
def test_name_mismatch():
    @get_body_ast
    def tree():
        a = 1
        b = c
    assert match(assignments, tree) == []
Beispiel #13
0
def test_irrelevant():
    @get_body_ast
    def tree():
        x = 1
    assert match(useless_if, tree) == []
Beispiel #14
0
def test_partial():
    @get_body_ast
    def tree():
        x = 1
    assert match(assignments, tree) == []
Beispiel #15
0
def test_multi():
    @get_body_ast
    def tree():
        x = 1
        y = x
    assert match(assignments, tree) == [tree[0]]
Beispiel #16
0
def test_incomplete():
    @get_body_ast
    def tree():
        if y:
            return True
    assert match(useless_if, tree) == []
Beispiel #17
0
def test_name_clash():
    @get_body_ast
    def tree():
        a = 1
        a = a
    assert match(assignments, tree) == []