2014-04-29 15:57:36 +02:00
|
|
|
require_relative "helper"
|
2014-04-24 14:43:20 +02:00
|
|
|
|
2014-04-29 15:57:36 +02:00
|
|
|
class TestFunctionDefinition < MiniTest::Test
|
|
|
|
# include the magic (setup and parse -> test method translation), see there
|
|
|
|
include ParserHelper
|
2014-04-27 20:12:42 +02:00
|
|
|
|
2014-04-29 15:57:36 +02:00
|
|
|
def test_simplest_function
|
|
|
|
@string_input = <<HERE
|
|
|
|
def foo(x)
|
|
|
|
5
|
|
|
|
end
|
|
|
|
HERE
|
|
|
|
@parse_output = {:function_definition => {:name => 'foo'},
|
|
|
|
:parmeter_list => [{:parmeter => {:name => 'x'}}],
|
2014-04-28 15:07:34 +02:00
|
|
|
:expressions => [{:integer => '5'}]}
|
2014-05-05 08:51:16 +02:00
|
|
|
@transform_output = Ast::FunctionExpression.new('foo',
|
|
|
|
[Ast::NameExpression.new('x')],
|
|
|
|
[Ast::IntegerExpression.new(5)])
|
2014-05-10 10:18:39 +02:00
|
|
|
@parser = @parser.function_definition
|
2014-04-28 21:07:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_function_assignment
|
2014-04-29 15:57:36 +02:00
|
|
|
@string_input = <<HERE
|
|
|
|
def foo(x)
|
|
|
|
abba = 5
|
|
|
|
end
|
|
|
|
HERE
|
|
|
|
@parse_output = { :function_definition => { :name => "foo" } ,
|
|
|
|
:parmeter_list => [{ :parmeter => { :name => "x" } }],
|
2014-04-28 21:07:35 +02:00
|
|
|
:expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ]
|
|
|
|
}
|
2014-05-10 18:11:32 +02:00
|
|
|
@transform_output = Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] ,
|
|
|
|
[Ast::AssignmentExpression.new(Ast::NameExpression.new("abba"), Ast::IntegerExpression.new(5))] )
|
2014-05-10 10:18:39 +02:00
|
|
|
@parser = @parser.function_definition
|
2014-04-28 20:21:12 +02:00
|
|
|
end
|
|
|
|
|
2014-05-10 18:02:51 +02:00
|
|
|
def test_function_while
|
|
|
|
@string_input = <<HERE
|
|
|
|
def fibonaccit(n)
|
|
|
|
a = 0
|
|
|
|
b = 1
|
|
|
|
while n > 1 do
|
|
|
|
tmp = a
|
|
|
|
a = b
|
|
|
|
b = tmp + b
|
|
|
|
puts b
|
|
|
|
n = n - 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
|
|
|
@parse_output = { :function_definition => { :name => "foo" } ,
|
|
|
|
:parmeter_list => [{ :parmeter => { :name => "x" } }],
|
|
|
|
:expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ]
|
|
|
|
}
|
|
|
|
@transform_output = Ast::FunctionExpression.new( "foo", [Ast::NameExpression.new("x")],
|
|
|
|
[Ast::AssignmentExpression.new( "abba", Ast::IntegerExpression.new(5) ) ])
|
|
|
|
@parser = @parser.function_definition
|
|
|
|
end
|
2014-04-29 15:57:36 +02:00
|
|
|
end
|