示例#1
0
def test_unquote_nested():
    source = '''
    $$var (do_stuff)
    '''
    assert parse(source) == [
        Call(Unquote(Unquote(Ident('var'))), [Call(Ident('do_stuff'))])
    ]
示例#2
0
def test_unary_in_call():
    source = '''
    puts (-x)
    '''
    assert parse(source) == [
        Call(Ident('puts'), [Call(Ident('-'), [Ident('x')])])
    ]
示例#3
0
def test_simple_multiple_singleline_statements():
    source = '''
    puts "Hello!"; puts "World!"
    '''
    assert parse(source) == [
        Call(Ident('puts'), [String('Hello!')]),
        Call(Ident('puts'), [String('World!')]),
    ]
示例#4
0
def test_unquote_call():
    source = '''
    $(fn arg) (do_stuff)
    '''
    assert parse(source) == [
        Call(Unquote(Call(Ident('fn'), [Ident('arg')])),
             [Call(Ident('do_stuff'))])
    ]
示例#5
0
def test_comment():
    source = r'''
    puts "Hello!"  # inline
    # prints "Hello\nWorld!"
    puts "World!"
    '''
    assert parse(source) == [
        Call(Ident('puts'), [String('Hello!')]),
        Call(Ident('puts'), [String('World!')]),
    ]
示例#6
0
def test_unquote_compound():
    source = '''
    ${(fn arg).attr} (do_stuff)
    '''
    assert parse(source) == [
        Call(
            Unquote(
                BinarySlurp([
                    Call(Ident('fn'), [Ident('arg')]),
                    Ident('.'),
                    Ident('attr')
                ])), [Call(Ident('do_stuff'))])
    ]
示例#7
0
def test_simple_if():
    source = '''
    if name ~is "John"
        puts "Hello, John!"
    '''
    assert parse(source) == [
        Call(Ident('if'), [
            BinarySlurp([Ident('name'),
                         Ident('is'),
                         String('John')]),
            Block([Call(Ident('puts'), [String('Hello, John!')])])
        ])
    ]
示例#8
0
def test_if_not():
    source = '''
    if (not thing.())
        puts "badness"
    '''
    assert parse(source) == [
        Call(Ident('if'), [
            Call(Ident('not'),
                 [BinarySlurp([Ident('thing'),
                               Ident('.'), Tuple()])]),
            Block([Call(Ident('puts'), [String('badness')])])
        ])
    ]
示例#9
0
def test_single_block():
    source = '''
    run
        thing 1
        thing 2
    '''
    assert parse(source) == [
        Call(Ident('run'), [
            Block([
                Call(Ident('thing'), [Int(1)]),
                Call(Ident('thing'), [Int(2)]),
            ])
        ]),
    ]
示例#10
0
def test_triple_string_interpolation_call():
    source = r'''
    puts """  Hello, $(last name)  ${  "!" }  """
    '''
    assert parse(source) == [
        Call(Ident('puts'), [
            InterpolatedString([
                String('  Hello, '),
                Call(Ident('last'), [Ident('name')]),
                String('  '),
                String('!'),
                String('  ')
            ])
        ])
    ]
示例#11
0
def test_lambda():
    source = '''
    for list x =>
        puts x
    '''
    assert parse(source) == [
        Call(Ident('for'), [
            Ident('list'),
            BinarySlurp([
                Ident('x'),
                Ident('=>'),
                Block([Call(Ident('puts'), [Ident('x')])])
            ])
        ])
    ]
示例#12
0
def test_triple_string_interpolation_single():
    source = r'''
    puts """$greeting"""
    '''
    assert parse(source) == [
        Call(Ident('puts'), [InterpolatedString([Ident('greeting')])])
    ]
示例#13
0
def test_curly_expression_compound_multiline_nosemi():
    source = '''
    puts 3
    *
    {
    x
    =
    4
    x
    +
    1
    }
    '''
    assert parse(source) == [
        Call(Ident('puts'), [
            BinarySlurp([
                Int(3),
                Ident('*'),
                Block([
                    BinarySlurp([Ident('x'), Ident('='),
                                 Int(4)]),
                    BinarySlurp([Ident('x'), Ident('+'),
                                 Int(1)])
                ])
            ])
        ])
    ]
示例#14
0
def test_simple_binary_slurp():
    source = '''
    puts 1+2
    '''
    assert parse(source) == [
        Call(Ident('puts'),
             [BinarySlurp([Int(1), Ident('+'), Int(2)])])
    ]
示例#15
0
def test_triple_single_quote():
    source = r"""
    puts '''Hello,
    ''\'World'\'!'''
    """
    assert parse(source) == [
        Call(Ident('puts'), [String("Hello,\n'''World''!")])
    ]
示例#16
0
def test_int_nonsigil():
    source = r'''
    puts 10 + 3 i
    '''
    assert parse(source) == [
        Call(Ident('puts'),
             [BinarySlurp([Int(10), Ident('+'), Int(3)]),
              Ident('i')])
    ]
示例#17
0
def test_triple_string_interpolation_leading_space():
    source = r'''
    puts """  Hello, ${"John"}"""
    '''
    assert parse(source) == [
        Call(Ident('puts'),
             [InterpolatedString([String('  Hello, '),
                                  String('John')])])
    ]
示例#18
0
def test_whitespace_call():
    source = '''
    (
    fn
    arg1
    arg2
    )
    '''
    assert parse(source) == [Call(Ident('fn'), [Ident('arg1'), Ident('arg2')])]
示例#19
0
def test_triple_string_interpolation_empty():
    source = r'''
    puts """Hello${""}!"""
    '''
    assert parse(source) == [
        Call(Ident('puts'),
             [InterpolatedString([String('Hello'),
                                  String(''),
                                  String('!')])])
    ]
示例#20
0
def test_triple_string_interpolation_nested():
    source = r'''
    puts """Hello, ${1 + {2}}"""
    '''
    assert parse(source) == [
        Call(Ident('puts'), [
            InterpolatedString(
                [String('Hello, '),
                 BinarySlurp([Int(1), Ident('+'), Int(2)])])
        ])
    ]
示例#21
0
def test_compound_binary_slurp():
    source = '''
    puts 1+2 * 3
    '''
    assert parse(source) == [
        Call(Ident('puts'),
             [BinarySlurp(
                 [Int(1), Ident('+'),
                  Int(2), Ident('*'),
                  Int(3)])])
    ]
示例#22
0
def test_op_def():
    source = '''
    def (+ x: int y: int)
        x.add_int y
    '''
    assert parse(source) == [
        Call(Ident('def'), [
            Call(Ident('+'), [
                BinarySlurp([Ident('x'), Ident(':'),
                             Ident('int')]),
                BinarySlurp([Ident('y'), Ident(':'),
                             Ident('int')]),
            ]),
            Block([
                Call(BinarySlurp([Ident('x'),
                                  Ident('.'),
                                  Ident('add_int')]), [Ident('y')])
            ])
        ])
    ]
示例#23
0
def test_string_interpolation_single_inner():
    source = r'''
    puts "Hello, $'John'!"
    '''
    assert parse(source) == [
        Call(Ident('puts'), [
            InterpolatedString([
                String('Hello, '),
                String('John'), String('!')
            ])
        ])
    ]
示例#24
0
def test_triple_string_interpolation_two_quotes_before_dollar():
    source = r'''
    puts """Hello, ""${"John"}!"""
    '''
    assert parse(source) == [
        Call(Ident('puts'), [
            InterpolatedString(
                [String('Hello, ""'),
                 String('John'),
                 String('!')])
        ])
    ]
示例#25
0
def test_class():
    source = '''
    class Dog
        def (new breed)
            self.breed = breed

        def (bark)
            puts "Woof!"
    '''
    assert parse(source) == [
        Call(Ident('class'), [
            Ident('Dog'),
            Block([
                Call(Ident('def'), [
                    Call(Ident('new'), [Ident('breed')]),
                    Block([
                        BinarySlurp([
                            Ident('self'),
                            Ident('.'),
                            Ident('breed'),
                            Ident('='),
                            Ident('breed')
                        ])
                    ])
                ]),
                Call(Ident('def'), [
                    Call(Ident('bark')),
                    Block([Call(Ident('puts'), [String('Woof!')])])
                ])
            ])
        ])
    ]
示例#26
0
def test_list_dot_slice():
    source = '''
    puts list.{3..5}
    '''
    assert parse(source) == [
        Call(Ident('puts'), [
            BinarySlurp([
                Ident('list'),
                Ident('.'),
                BinarySlurp([Int(3), Ident('..'), Int(5)])
            ])
        ])
    ]
示例#27
0
def test_curly_expression():
    source = '''
    puts 3 * {4 + 1}
    '''
    assert parse(source) == [
        Call(Ident('puts'), [
            BinarySlurp([
                Int(3),
                Ident('*'),
                BinarySlurp([Int(4), Ident('+'), Int(1)])
            ])
        ])
    ]
示例#28
0
def test_kwargs():
    source = '''
    train model l2: 0.5 alpha: 0.01
    '''
    assert parse(source) == [
        Call(Ident('train'), [
            Ident('model'),
            BinarySlurp([Ident('l2'), Ident(':'),
                         Float(0.5)]),
            BinarySlurp([Ident('alpha'),
                         Ident(':'), Float(0.01)]),
        ])
    ]
示例#29
0
def test_list():
    source = '''
    list = [1, 2, (compute "number")]
    '''
    assert parse(source) == [
        BinarySlurp([
            Ident('list'),
            Ident('='),
            List([Int(1),
                  Int(2),
                  Call(Ident('compute'), [String('number')])])
        ])
    ]
示例#30
0
def test_defop():
    source = '''
    defop {+} assoc: @left priority: 5
    '''
    assert parse(source) == [
        Call(Ident('defop'), [
            Ident('+'),
            BinarySlurp([Ident('assoc'),
                         Ident(':'),
                         Symbol('left')]),
            BinarySlurp([Ident('priority'),
                         Ident(':'), Int(5)])
        ])
    ]