Exemple #1
0
def test_format_func_call_notes():
    before = """\
foo(x = 12 # Comment.
)"""
    with pytest.raises(FormattingError,
                       match='Comments inside expressions are not supported'):
        parse_code_element(before).format(allowed_line_length=100)
Exemple #2
0
def test_import():
    # Test module names without periods.
    res = parse_code_element('from   a    import  b')
    assert res == CodeElementImport(path=ExprIdentifier(name='a'),
                                    orig_identifier=ExprIdentifier(name='b'),
                                    local_name=None)
    assert res.format(allowed_line_length=100) == 'from a import b'

    # Test module names without periods, with aliasing.
    res = parse_code_element('from   a    import  b   as   c')
    assert res == CodeElementImport(path=ExprIdentifier(name='a'),
                                    orig_identifier=ExprIdentifier(name='b'),
                                    local_name=ExprIdentifier(name='c'))
    assert res.format(allowed_line_length=100) == 'from a import b as c'

    # Test module names with periods.
    res = parse_code_element('from   a.b12.c4    import  lib345')
    assert res == CodeElementImport(
        path=ExprIdentifier(name='a.b12.c4'),
        orig_identifier=ExprIdentifier(name='lib345'))
    assert res.format(allowed_line_length=100) == 'from a.b12.c4 import lib345'

    # Test module with bad identifier (with periods).
    with pytest.raises(ParserError):
        parse_expr('from a.b import c.d')

    # Test module with bad local name (with periods).
    with pytest.raises(ParserError):
        parse_expr('from a.b import c as d.d')
Exemple #3
0
def test_func_expr():
    res = parse_code_element('let x = f()')
    assert isinstance(res, CodeElementReturnValueReference)
    assert res.format(allowed_line_length=100) == 'let x = f()'

    res = parse_code_element('let x = (f())')
    assert isinstance(res, CodeElementReference)
    assert res.format(allowed_line_length=100) == 'let x = (f())'
Exemple #4
0
def test_import():
    # Test module names without periods.
    res = parse_code_element('from   a    import  b')
    assert res == CodeElementImport(
        path=ExprIdentifier(name='a'),
        import_items=[
            AliasedIdentifier(orig_identifier=ExprIdentifier(name='b'),
                              local_name=None)
        ])
    assert res.format(allowed_line_length=100) == 'from a import b'

    # Test module names without periods, with aliasing.
    res = parse_code_element('from   a    import  b   as   c')
    assert res == CodeElementImport(
        path=ExprIdentifier(name='a'),
        import_items=[
            AliasedIdentifier(orig_identifier=ExprIdentifier(name='b'),
                              local_name=ExprIdentifier(name='c'))
        ])
    assert res.format(allowed_line_length=100) == 'from a import b as c'

    # Test module names with periods.
    res = parse_code_element('from   a.b12.c4    import  lib345')
    assert res == CodeElementImport(
        path=ExprIdentifier(name='a.b12.c4'),
        import_items=[
            AliasedIdentifier(orig_identifier=ExprIdentifier(name='lib345'),
                              local_name=None)
        ])
    assert res.format(allowed_line_length=100) == 'from a.b12.c4 import lib345'

    # Test multiple imports.
    res = parse_code_element('from  lib    import  a,b as    b2,   c')

    assert res == CodeElementImport(
        path=ExprIdentifier(name='lib'),
        import_items=[
            AliasedIdentifier(orig_identifier=ExprIdentifier(name='a'),
                              local_name=None),
            AliasedIdentifier(orig_identifier=ExprIdentifier(name='b'),
                              local_name=ExprIdentifier(name='b2')),
            AliasedIdentifier(orig_identifier=ExprIdentifier(name='c'),
                              local_name=None),
        ])
    assert res.format(
        allowed_line_length=100) == 'from lib import a, b as b2, c'
    assert res.format(
        allowed_line_length=20) == 'from lib import (\n    a, b as b2, c)'

    assert res == parse_code_element('from lib import (\n    a, b as b2, c)')

    # Test module with bad identifier (with periods).
    with pytest.raises(ParserError):
        parse_expr('from a.b import c.d')

    # Test module with bad local name (with periods).
    with pytest.raises(ParserError):
        parse_expr('from a.b import c as d.d')
Exemple #5
0
def test_func_call():
    res = parse_code_element('fibonacci(  1, \na= 2  )')
    assert res.format(allowed_line_length=100) == 'fibonacci(1, a=2)'

    res = parse_code_element('fibonacci  {a=b,c = d}(  1, \na= 2  )')
    assert res.format(allowed_line_length=100) == 'fibonacci{a=b, c=d}(1, a=2)'
    assert res.format(
        allowed_line_length=20) == 'fibonacci{a=b, c=d}(\n    1, a=2)'
    assert res.format(
        allowed_line_length=15) == 'fibonacci{\n    a=b, c=d}(\n    1, a=2)'
Exemple #6
0
def test_parent_location():
    parent_location = (parse_expr('1 + 2').location,
                       'An error ocurred while processing:')

    location = parse_code_element(
        'let x = 3 + 4',
        parser_context=ParserContext(
            parent_location=parent_location)).expr.location
    location_err = LocationError(message='Error', location=location)
    assert str(location_err) == """\
Exemple #7
0
def test_decoractor():
    code = """\
@hello @world


@external func myfunc():
     return ()
end"""

    assert parse_code_element(code=code).format(
        allowed_line_length=100) == """\
Exemple #8
0
def test_return_value_reference():
    res = parse_code_element('let   z=call  x')
    assert res.format(allowed_line_length=100) == 'let z = call x'

    res = parse_code_element('let   z:y.z=call  x')
    assert res.format(allowed_line_length=100) == 'let z : y.z = call x'

    res = parse_code_element('let   z:y.z=call rel x')
    assert res.format(allowed_line_length=100) == 'let z : y.z = call rel x'

    res = parse_code_element(
        'let very_long_prefix = foo(a=1, b=  1,     very_long_arg_1=1, very_long_arg_2   =1)'
    )
    assert res.format(allowed_line_length=40) == """\
let very_long_prefix = foo(
    a=1,
    b=1,
    very_long_arg_1=1,
    very_long_arg_2=1)"""

    res = parse_code_element(
        'let (very_long_prefix ,b,c:   T) = foo(a=1, b=  1, very_long_arg_1=1, very_long_arg_2 =1)'
    )
    assert res.format(allowed_line_length=40) == """\
let (very_long_prefix, b, c : T) = foo(
    a=1,
    b=1,
    very_long_arg_1=1,
    very_long_arg_2=1)"""

    with pytest.raises(ParserError):
        # Const in the unpacking tuple.
        parse_expr('let (1,b,c) = foo(a=1, b=  1)')

    with pytest.raises(ParserError):
        # Missing identifier after call.
        parse_expr('let z = call')

    with pytest.raises(ParserError):
        # 'ap++' cannot be used in the return value reference syntax.
        parse_expr('let z = call x; ap++')
Exemple #9
0
def test_addressof():
    res = parse_code_element('static_assert &     s.SIZE ==  ap   ')
    assert res.format(allowed_line_length=100) == 'static_assert &s.SIZE == ap'
Exemple #10
0
def test_reference_type_annotation():
    res = parse_code_element('let s : T *   = ap')
    assert res.format(allowed_line_length=100) == 'let s : T* = ap'

    with pytest.raises(ParserError):
        parse_expr('local x : = 0')
Exemple #11
0
 def test_format(args_str_wrong, args_str_right=''):
     assert parse_code_element(def_func(args_str_wrong)).format(
         allowed_line_length=100) == def_func(args_str_right)
Exemple #12
0
def test_func_call():
    res = parse_code_element('fibonacci(   ... ,  1, \na= 2  )')
    assert res.format(allowed_line_length=100) == 'fibonacci(..., 1, a=2)'
Exemple #13
0
def test_return():
    res = parse_code_element('return(  1, \na= 2  )')
    assert res.format(allowed_line_length=100) == 'return (1, a=2)'
Exemple #14
0
def test_tail_call():
    res = parse_code_element('return    fibonacci(  1, \na= 2  )')
    assert res.format(allowed_line_length=100) == 'return fibonacci(1, a=2)'