2014-07-14 10:29:38 +02:00
|
|
|
require_relative "virtual_helper"
|
|
|
|
|
2014-07-14 23:00:00 +02:00
|
|
|
class TestMethods < MiniTest::Test
|
2014-07-14 10:29:38 +02:00
|
|
|
include VirtualHelper
|
|
|
|
|
|
|
|
def test_simplest_function
|
|
|
|
@string_input = <<HERE
|
|
|
|
def foo(x)
|
|
|
|
5
|
|
|
|
end
|
|
|
|
HERE
|
2014-07-16 18:24:41 +02:00
|
|
|
@output = [Virtual::MethodDefinition.new(:foo,[Virtual::Argument.new(:x,Virtual::Mystery.new())],Virtual::SelfReference.new(nil),Virtual::IntegerConstant.new(5),Virtual::MethodDefinitionEnter.new(nil))]
|
2014-07-14 10:29:38 +02:00
|
|
|
check
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_class_function
|
|
|
|
@string_input = <<HERE
|
|
|
|
def String.length(x)
|
|
|
|
@length
|
|
|
|
end
|
|
|
|
HERE
|
2014-07-16 18:24:41 +02:00
|
|
|
@output = [Virtual::MethodDefinition.new(:length,[Virtual::Argument.new(:x,Virtual::Mystery.new())],Boot::BootClass.new(:String,:Object),Virtual::Return.new(Virtual::Mystery.new()),Virtual::MethodDefinitionEnter.new(Virtual::ObjectGet.new(:length,nil)))]
|
2014-07-14 13:06:09 +02:00
|
|
|
check
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_function_ops
|
|
|
|
@string_input = <<HERE
|
|
|
|
def foo(x)
|
|
|
|
abba = 5
|
|
|
|
2 + 5
|
|
|
|
end
|
|
|
|
HERE
|
2014-07-16 18:24:41 +02:00
|
|
|
@output = [Virtual::MethodDefinition.new(:foo,[Virtual::Argument.new(:x,Virtual::Mystery.new())],Virtual::SelfReference.new(nil),Virtual::Return.new(Virtual::Mystery),Virtual::MethodDefinitionEnter.new(Virtual::FrameSet.new(:abba,Virtual::IntegerConstant.new(5),Virtual::LoadSelf.new(Virtual::IntegerConstant.new(2),Virtual::FrameSend.new(:+,[Virtual::IntegerConstant.new(5)],nil)))))]
|
2014-07-15 17:27:13 +02:00
|
|
|
check
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_function_ops_simple
|
|
|
|
@string_input = <<HERE
|
|
|
|
def foo()
|
|
|
|
2 + 5
|
|
|
|
end
|
|
|
|
HERE
|
2014-07-16 18:24:41 +02:00
|
|
|
@output = [Virtual::MethodDefinition.new(:foo,[],Virtual::SelfReference.new(nil),Virtual::Return.new(Virtual::Mystery),Virtual::MethodDefinitionEnter.new(Virtual::LoadSelf.new(Virtual::IntegerConstant.new(2),Virtual::FrameSend.new(:+,[Virtual::IntegerConstant.new(5)],nil))))]
|
2014-07-14 15:19:47 +02:00
|
|
|
check
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
|
|
|
|
2014-07-16 12:20:47 +02:00
|
|
|
def test_function_if
|
2014-07-14 10:29:38 +02:00
|
|
|
@string_input = <<HERE
|
|
|
|
def ofthen(n)
|
|
|
|
if(0)
|
|
|
|
isit = 42
|
|
|
|
else
|
|
|
|
maybenot = 667
|
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
2014-07-16 12:20:47 +02:00
|
|
|
@output = nil
|
2014-07-14 15:19:47 +02:00
|
|
|
check
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
|
|
|
|
2014-07-14 20:25:53 +02:00
|
|
|
def ttest_function_return
|
2014-07-14 10:29:38 +02:00
|
|
|
@string_input = <<HERE
|
|
|
|
def retvar(n)
|
|
|
|
i = 5
|
|
|
|
return i
|
|
|
|
end
|
|
|
|
HERE
|
2014-07-16 18:24:41 +02:00
|
|
|
@output = [Virtual::MethodDefinition.new(:foo,[Ast::NameExpression.new(:x)])]
|
2014-07-14 15:19:47 +02:00
|
|
|
check
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
|
|
|
|
2014-07-14 20:25:53 +02:00
|
|
|
def ttest_function_return_if
|
2014-07-14 10:29:38 +02:00
|
|
|
@string_input = <<HERE
|
|
|
|
def retvar(n)
|
|
|
|
if( n > 5)
|
|
|
|
return 10
|
|
|
|
else
|
|
|
|
return 20
|
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
2014-07-16 18:24:41 +02:00
|
|
|
@output = [Virtual::MethodDefinition.new(:foo,[Ast::NameExpression.new(:x)])]
|
2014-07-14 15:19:47 +02:00
|
|
|
check
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
|
|
|
|
2014-07-14 20:25:53 +02:00
|
|
|
def ttest_function_return_while
|
2014-07-14 10:29:38 +02:00
|
|
|
@string_input = <<HERE
|
|
|
|
def retvar(n)
|
|
|
|
while( n > 5) do
|
|
|
|
n = n + 1
|
|
|
|
return n
|
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
2014-07-16 18:24:41 +02:00
|
|
|
@output = [Virtual::MethodDefinition.new(:foo,[Ast::NameExpression.new(:x)])]
|
2014-07-14 15:19:47 +02:00
|
|
|
check
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
|
|
|
|
2014-07-14 20:25:53 +02:00
|
|
|
def ttest_function_while
|
2014-07-14 10:29:38 +02:00
|
|
|
@string_input = <<HERE
|
|
|
|
def fibonaccit(n)
|
|
|
|
a = 0
|
|
|
|
while (n) do
|
|
|
|
some = 43
|
|
|
|
other = some * 4
|
|
|
|
end
|
|
|
|
end
|
|
|
|
HERE
|
2014-07-16 18:24:41 +02:00
|
|
|
@output = [Virtual::MethodDefinition.new(:foo,[Ast::NameExpression.new(:x)])]
|
2014-07-14 15:19:47 +02:00
|
|
|
check
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
|
|
|
|
2014-07-14 20:25:53 +02:00
|
|
|
def ttest_function_big_while
|
2014-07-14 10:29:38 +02:00
|
|
|
@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
|
2014-07-16 18:24:41 +02:00
|
|
|
@output = [Virtual::MethodDefinition.new(:foo,[Ast::NameExpression.new(:x)])]
|
2014-07-14 15:19:47 +02:00
|
|
|
check
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
|
|
|
end
|