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_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_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_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_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_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_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_inline_def(): source = ''' (Dog name) = self.name = name ''' assert parse(source) == [ BinarySlurp([ Call(Ident('Dog'), [Ident('name')]), Ident('='), Block([ BinarySlurp([ Ident('self'), Ident('.'), Ident('name'), Ident('='), Ident('name') ]) ]) ]) ]
def test_varargs(): source = ''' def (variadic first(...rest)) (stuff) (variadic arg1) variadic arg1 arg2 (...args) ''' assert parse(source) == [ Call(Ident('def'), [ Call(Ident('variadic'), [Ident('first'), Call(Ident('...'), [Ident('rest')])]), Block([Call(Ident('stuff'))]) ]), Call(Ident('variadic'), [Ident('arg1')]), Call(Ident('variadic'), [ Ident('arg1'), Ident('arg2'), Call(Ident('...'), [Ident('args')]) ]), ]
def test_match(): source = ''' match x Dog -> (puts "I'm a dog") Cat -> (puts "I'm a cat") ''' assert parse(source) == [ Call(Ident('match'), [ Ident('x'), Block([ BinarySlurp([ Ident('Dog'), Ident('->'), Call(Ident('puts'), [String("I'm a dog")]) ]), BinarySlurp([ Ident('Cat'), Ident('->'), Call(Ident('puts'), [String("I'm a cat")]) ]), ]) ]) ]
def test_record(): source = ''' record Dog breed: [@golden_retriever, @beagle] name: String ''' truth = [ Call(Ident('record'), [ Ident('Dog'), Block([ BinarySlurp([ Ident('breed'), Ident(':'), List([Symbol('golden_retriever'), Symbol('beagle')]) ]), BinarySlurp([Ident('name'), Ident(':'), Ident('String')]) ]) ]) ] print('Truth: ', truth) assert parse(source) == truth