rubyx/test/virtual/test_methods.rb

189 lines
2.8 KiB
Ruby
Raw Normal View History

2014-07-14 10:29:38 +02:00
require_relative "virtual_helper"
2015-07-18 14:28:57 +02:00
module Virtual
class TestMethods < MiniTest::Test
def check
Virtual.machine.boot.compile_main @string_input
produced = Virtual.machine.space.get_main.source
assert @output , "No output given"
assert_equal @output.length , produced.blocks.length , "Block length"
produced.blocks.each_with_index do |b,i|
codes = @output[i]
assert codes , "No codes for block #{i}"
assert_equal b.codes.length , codes.length , "Code length for block #{i}"
b.codes.each_with_index do |c , ii |
assert_equal codes[ii] , c.class , "Block #{i} , code #{ii}"
end
end
end
2015-07-19 09:36:06 +02:00
def test_module
@string_input = <<HERE
class Some
def foo()
5
end
end
HERE
@output = [[MethodEnter] ,[MethodReturn]]
check
end
2015-07-18 14:28:57 +02:00
def test_simplest_function
@string_input = <<HERE
2015-05-06 07:38:29 +02:00
def foo(x)
2014-07-14 10:29:38 +02:00
5
end
2015-07-18 14:28:57 +02:00
HERE
@output = [[MethodEnter] ,[MethodReturn]]
check
end
def ttest_second_simplest_function
@string_input = <<HERE
def foo(x)
x
end
2014-07-14 10:29:38 +02:00
HERE
2015-07-18 18:02:54 +02:00
@output = [[1,2,3,4],[],[],[]]
check
end
def ttest_puts_string
@string_input = <<HERE
def foo()
puts("Hello")
end
foo()
HERE
@output = nil
2014-07-14 10:29:38 +02:00
check
end
def ttest_class_function
2014-07-14 10:29:38 +02:00
@string_input = <<HERE
def String.length(x)
@length
end
HERE
@output = nil
2014-07-14 13:06:09 +02:00
check
2014-07-14 10:29:38 +02:00
end
def ttest_function_ops
2014-07-14 10:29:38 +02:00
@string_input = <<HERE
2015-05-06 07:38:29 +02:00
def foo(x)
abba = 5
2014-07-14 10:29:38 +02:00
2 + 5
end
HERE
@output = nil
2014-07-15 17:27:13 +02:00
check
end
def ttest_function_ops_simple
2014-07-15 17:27:13 +02:00
@string_input = <<HERE
def foo()
2 + 5
end
HERE
@output = nil
2014-07-14 15:19:47 +02:00
check
2014-07-14 10:29:38 +02:00
end
def ttest_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
@output = nil
2015-05-06 14:15:53 +02:00
check
2014-07-14 10:29:38 +02:00
end
2015-07-18 18:02:54 +02:00
def test_while
@string_input = <<HERE
while(1) do
3
end
HERE
@output = [[MethodEnter],[Set,IsTrueBranch,Set,UnconditionalBranch],[],[MethodReturn]]
check
end
def ttest_function_while
@string_input = <<HERE
def fibonaccit(n)
a = 0
while (n) do
some = 43
other = some * 4
end
end
HERE
@output = nil
check
end
2015-05-06 14:15:53 +02:00
def pest_function_return
2014-07-14 10:29:38 +02:00
@string_input = <<HERE
def retvar(n)
i = 5
2015-05-06 07:38:29 +02:00
return i
2014-07-14 10:29:38 +02:00
end
HERE
@output = ""
2014-07-14 15:19:47 +02:00
check
2014-07-14 10:29:38 +02:00
end
2015-05-06 14:15:53 +02:00
def pest_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
2015-05-06 07:38:29 +02:00
end
2014-07-14 10:29:38 +02:00
end
HERE
@output = ""
2014-07-14 15:19:47 +02:00
check
2014-07-14 10:29:38 +02:00
end
2015-05-06 14:15:53 +02:00
def est_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
2015-05-06 07:38:29 +02:00
end
2014-07-14 10:29:38 +02:00
end
HERE
@output = ""
2014-07-14 15:19:47 +02:00
check
2014-07-14 10:29:38 +02:00
end
2015-05-06 14:15:53 +02:00
def pest_function_big_while
2014-07-14 10:29:38 +02:00
@string_input = <<HERE
def fibonaccit(n)
2015-05-06 07:38:29 +02:00
a = 0
2014-07-14 10:29:38 +02:00
b = 1
while( n > 1 ) do
tmp = a
a = b
b = tmp + b
puts(b)
n = n - 1
end
end
HERE
@output = ""
2014-07-14 15:19:47 +02:00
check
2015-05-06 07:38:29 +02:00
end
end
2015-07-18 14:28:57 +02:00
end