def test_unquote_nested(): source = ''' $$var (do_stuff) ''' assert parse(source) == [ Call(Unquote(Unquote(Ident('var'))), [Call(Ident('do_stuff'))]) ]
def test_unary_in_call(): source = ''' puts (-x) ''' assert parse(source) == [ Call(Ident('puts'), [Call(Ident('-'), [Ident('x')])]) ]
def test_simple_multiple_singleline_statements(): source = ''' puts "Hello!"; puts "World!" ''' assert parse(source) == [ Call(Ident('puts'), [String('Hello!')]), Call(Ident('puts'), [String('World!')]), ]
def test_unquote_call(): source = ''' $(fn arg) (do_stuff) ''' assert parse(source) == [ Call(Unquote(Call(Ident('fn'), [Ident('arg')])), [Call(Ident('do_stuff'))]) ]
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!')]), ]
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'))]) ]
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!')])]) ]) ]
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')])]) ]) ]
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)]), ]) ]), ]
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(' ') ]) ]) ]
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')])]) ]) ]) ]
def test_triple_string_interpolation_single(): source = r''' puts """$greeting""" ''' assert parse(source) == [ Call(Ident('puts'), [InterpolatedString([Ident('greeting')])]) ]
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)]) ]) ]) ]) ]
def test_simple_binary_slurp(): source = ''' puts 1+2 ''' assert parse(source) == [ Call(Ident('puts'), [BinarySlurp([Int(1), Ident('+'), Int(2)])]) ]
def test_triple_single_quote(): source = r""" puts '''Hello, ''\'World'\'!''' """ assert parse(source) == [ Call(Ident('puts'), [String("Hello,\n'''World''!")]) ]
def test_int_nonsigil(): source = r''' puts 10 + 3 i ''' assert parse(source) == [ Call(Ident('puts'), [BinarySlurp([Int(10), Ident('+'), Int(3)]), Ident('i')]) ]
def test_triple_string_interpolation_leading_space(): source = r''' puts """ Hello, ${"John"}""" ''' assert parse(source) == [ Call(Ident('puts'), [InterpolatedString([String(' Hello, '), String('John')])]) ]
def test_whitespace_call(): source = ''' ( fn arg1 arg2 ) ''' assert parse(source) == [Call(Ident('fn'), [Ident('arg1'), Ident('arg2')])]
def test_triple_string_interpolation_empty(): source = r''' puts """Hello${""}!""" ''' assert parse(source) == [ Call(Ident('puts'), [InterpolatedString([String('Hello'), String(''), String('!')])]) ]
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)])]) ]) ]
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)])]) ]
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')]) ]) ]) ]
def test_string_interpolation_single_inner(): source = r''' puts "Hello, $'John'!" ''' assert parse(source) == [ Call(Ident('puts'), [ InterpolatedString([ String('Hello, '), String('John'), String('!') ]) ]) ]
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('!')]) ]) ]
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!')])]) ]) ]) ]) ]
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)]) ]) ]) ]
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)]) ]) ]) ]
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)]), ]) ]
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')])]) ]) ]
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)]) ]) ]