Example #1
0
def test_readme_example():
    y = True
    @apply_ast_passes([if_inline()])
    def foo(x):
        if inline(y):
            return x + 1
        else:
            return x - 1
    assert inspect.getsource(foo) == f"""\
Example #2
0
def _do_inline(func, **kwargs):
    for dec in (
            begin_rewrite(),
            debug(**kwargs),
            if_inline(),
            debug(**kwargs),
            end_rewrite()):
        func = dec(func)
    return func
Example #3
0
def test_basic(cond):
    def basic():
        if inline(cond):
            return 0
        else:
            return 1

    inlined = apply_passes([if_inline()])(basic)
    inlined_src = inspect.getsource(inlined)
    assert inlined_src == f'''\
def basic():
    return {0 if cond else 1}
'''
    assert basic() == inlined()
Example #4
0
def test_nested(cond_0, cond_1):
    def nested():
        if inline(cond_0):
            if inline(cond_1):
                return 3
            else:
                return 2
        else:
            if inline(cond_1):
                return 1
            else:
                return 0

    inlined = apply_passes([if_inline()])(nested)
    assert inspect.getsource(inlined) == f'''\
def nested():
    return {nested()}
'''
    assert nested() == inlined()
Example #5
0
def test_outer_inline(cond_0, cond_1):
    def nested(cond):
        if inline(cond_0):
            if cond:
                return 3
            else:
                return 2
        else:
            if cond:
                return 1
            else:
                return 0

    inlined = apply_passes([if_inline()])(nested)
    assert inspect.getsource(inlined) == f'''\
def nested(cond):
    if cond:
        return {3 if cond_0 else 1}
    else:
        return {2 if cond_0 else 0}
'''
    assert nested(cond_1) == inlined(cond_1)