Beispiel #1
0
def test_parse_global_simple_func():
    "A oneline function defined at global scope"

    f = parse_as_ast(global_doit)

    assert isinstance(f, ast.Lambda)
    assert len(f.args.args) == 1
    assert isinstance(f.body, ast.BinOp)
Beispiel #2
0
def test_known_local_function():
    "function that is declared locally"

    def doit(x):
        ...

    f = parse_as_ast(lambda a: doit(a))  # type: ignore # NOQA
    assert "Name(id='doit'" in ast.dump(f)
Beispiel #3
0
def test_parse_simple_func():
    "A oneline function defined at local scope"

    def doit(x):
        return x + 1

    f = parse_as_ast(doit)

    assert isinstance(f, ast.Lambda)
    assert len(f.args.args) == 1
    assert isinstance(f.body, ast.BinOp)
Beispiel #4
0
def test_indent_parse():
    "More general case"

    seen_funcs = []

    class h:
        @staticmethod
        def dec_func(x: Callable):
            def make_it(y: Callable):
                return y

            seen_funcs.append(x)
            return make_it

    class yo_baby:
        @h.dec_func(lambda y: y + 2)
        def doit(self, x: int):
            ...

    assert len(seen_funcs) == 1
    l1 = parse_as_ast(seen_funcs[0], "dec_func")
    assert isinstance(l1.body, ast.BinOp)
    assert isinstance(l1.body.op, ast.Add)
Beispiel #5
0
    def dec_func(x: Callable):
        def make_it(y: Callable):
            return y

        seen_lambdas.append(parse_as_ast(x))
        return make_it
Beispiel #6
0
def test_parse_lambda_capture_nested_local():
    cut_value = 30
    r = parse_as_ast(lambda x: (lambda y: y > cut_value)(x))
    r_true = parse_as_ast(lambda x: (lambda y: y > 30)(x))
    assert ast.dump(r) == ast.dump(r_true)
Beispiel #7
0
def test_parse_as_callable_simple():
    r = parse_as_ast(lambda x: x + 1)
    assert isinstance(r, ast.Lambda)
Beispiel #8
0
def test_known_global_function():
    "function that is declared locally"

    f = parse_as_ast(lambda a: global_doit_non_func(a))  # type: ignore # NOQA
    assert "Name(id='global_doit_non_func'" in ast.dump(f)
Beispiel #9
0
def test_parse_global_capture():
    "Global function, which includes variable capture"
    f = parse_as_ast(global_doit_capture)
    f_true = parse_as_ast(global_doit_capture_true)
    assert ast.dump(f) == ast.dump(f_true)
Beispiel #10
0
def test_unknown_function():
    "function that isn't declared"
    f = parse_as_ast(lambda a: unknown(a))  # type: ignore # NOQA
    assert "Name(id='unknown'" in ast.dump(f)
Beispiel #11
0
def test_parse_as_str():
    r = parse_as_ast("lambda x: x + 1")
    assert isinstance(r, ast.Lambda)
Beispiel #12
0
def test_parse_nested_lambda():
    r = parse_as_ast(lambda x: (lambda y: y + 1)(x))
    assert isinstance(r, ast.Lambda)
    assert isinstance(r.body, ast.Call)
Beispiel #13
0
 def func_bottom(x: Callable):
     seen_lambdas.append(parse_as_ast(x))
Beispiel #14
0
def test_parse_lambda_capture_nested_global():
    r = parse_as_ast(lambda x: (lambda y: y > g_cut_value)(x))
    r_true = parse_as_ast(lambda x: (lambda y: y > 30)(x))
    assert ast.dump(r) == ast.dump(r_true)
Beispiel #15
0
def test_parse_lambda_capture_ignore_global():
    x = 30  # NOQA type: ignore
    r = parse_as_ast(lambda g_cut_value: g_cut_value > 20)
    r_true = parse_as_ast(lambda y: y > 20)
    assert ast.dump(r) == ast.dump(r_true).replace("'y'", "'g_cut_value'")
Beispiel #16
0
def test_parse_lambda_capture_ignore_local():
    x = 30  # NOQA type: ignore
    r = parse_as_ast(lambda x: x > 20)
    r_true = parse_as_ast(lambda y: y > 20)
    assert ast.dump(r) == ast.dump(r_true).replace("'y'", "'x'")
Beispiel #17
0
def test_parse_lambda_capture():
    cut_value = 30
    r = parse_as_ast(lambda x: x > cut_value)
    r_true = parse_as_ast(lambda x: x > 30)
    assert ast.dump(r) == ast.dump(r_true)
Beispiel #18
0
 def do_it(self, x: Callable):
     found.append(parse_as_ast(x))
     return self
Beispiel #19
0
def test_parse_as_ast_lambda():
    ln = lambda_unwrap(ast.parse("lambda x: x + 1"))
    r = parse_as_ast(ln)
    assert isinstance(r, ast.Lambda)