Example #1
0
def test_function_decorators():
    source = '''
    @quux.okay
    @baz(True and False or {1, 2, 3})
    @bar(-1, 2.3, nope={True: None})
    def foo(bar, *baz, **quux):
        pass
    '''

    root = ast.parse(textwrap.dedent(source))

    node = root.body[0]

    assert isinstance(node, ast.FunctionDef)
    assert make_python(node.args) == "bar, *baz, **quux"
    assert make_python(node.decorator_list[0]) == "quux.okay"
    assert make_python(
        node.decorator_list[1]) == ("baz(((True and False) or {1, 2, 3}))")
    assert make_python(
        node.decorator_list[2]) == ("bar(-1, 2.3, nope={True: None})")
Example #2
0
def test_function_varargs():
    source = '''
    def foo(*bar):
        pass
    '''

    root = ast.parse(textwrap.dedent(source))

    node = root.body[0]

    assert isinstance(node, ast.FunctionDef)
    assert make_python(node.args) == "*bar"
Example #3
0
def test_function_default_comp():
    source = '''
    def foo(bar=('baz',) == b'asdf'):
        pass
    '''

    root = ast.parse(textwrap.dedent(source))

    node = root.body[0]

    assert isinstance(node, ast.FunctionDef)
    assert make_python(node.args) == "bar=('baz', ) == b'...'"
Example #4
0
def test_function_default_lambda():
    source = '''
    def foo(bar=lambda x: x * 2):
        pass
    '''

    root = ast.parse(textwrap.dedent(source))

    node = root.body[0]

    assert isinstance(node, ast.FunctionDef)
    assert make_python(node.args) == "bar=lambda x: x * 2"
Example #5
0
def test_function_combined():
    source = '''
    def foo(foo, bar=[baz], *quux):
        pass
    '''

    root = ast.parse(textwrap.dedent(source))

    node = root.body[0]

    assert isinstance(node, ast.FunctionDef)
    assert make_python(node.args) == "foo, bar=[baz], *quux"
Example #6
0
def test_unsupported(capsys):
    root = ast.parse('')

    assert make_python(None) == "!!!"
    assert make_python(root) == "???"