improve tests

This commit is contained in:
Torsten Ruger
2015-10-10 10:02:28 +03:00
parent 530537de1a
commit c20ab21bbd
5 changed files with 43 additions and 70 deletions

View File

@ -1,40 +1,30 @@
require_relative '../helper'
require 'parslet/convenience'
module CompilerHelper
Phisol::Compiler.class_eval do
def check
Virtual.machine.boot.parse_and_compile @string_input
produced = Virtual.machine.space.get_main.source
assert_equal @output , produced
Virtual.machine.run_passes
end
end
class UnusedSofEquality
# simple thought: don't recurse for Blocks, just check their names
def == other
return false unless other.class == self.class
Sof::Util.attributes(self).each do |a|
begin
left = send(a)
rescue NoMethodError
next # not using instance variables that are not defined as attr_readers for equality
end
begin
right = other.send(a)
rescue NoMethodError
return false
end
return false unless left.class == right.class
if( left.is_a? Block)
return false unless left.name == right.name
else
return false unless left == right
end
Phisol::Compiler.class_eval do
def set_main
@method = Virtual.machine.space.get_main
end
return true
end
def check
machine = Virtual.machine.boot
parser = Parser::Salama.new
parser = parser.send @root
syntax = parser.parse_with_debug(@string_input)
parts = Parser::Transform.new.apply(syntax)
#puts parts.inspect
compiler = Phisol::Compiler.new
compiler.set_main
produced = compiler.process( parts )
produced = [produced] unless produced.is_a? Array
assert @output , "No output given"
assert_equal produced.length, @output.length , "Block length"
produced.each_with_index do |b,i|
codes = @output[i]
assert_equal codes , b.class , "Class #{i} "
end
end
end

View File

@ -1,4 +1,3 @@
require_relative "test_methods"
require_relative "test_hello"
require_relative "test_compiler"
require_relative "test_field_access"

View File

@ -2,61 +2,45 @@ require_relative "compiler_helper"
class TestBasic < MiniTest::Test
def check
input = <<HERE
class Object
int main()
#{@string_input}
end
end
HERE
include CompilerHelper
statements = Virtual.machine.boot.parse_and_compile input
if( statements.first.is_a? Virtual::Self )
statements.first.type.instance_variable_set :@of_class , nil
end
is = Sof.write(statements)
#puts is
assert_equal @output , is
def setup
@root = :basic_type
@output = [Virtual::Return]
end
def test_number
@string_input = '42 '
@output = "- Virtual::Return(:type => Virtual::Integer, :value => 42)"
check
end
def test_true
@string_input = 'true '
@output = "- Virtual::Return(:type => Virtual::Reference, :value => true)"
check
end
def test_false
@string_input = 'false '
@output = "- Virtual::Return(:type => Virtual::Reference, :value => false)"
check
end
def test_nil
@string_input = 'nil '
@output = "- Virtual::Return(:type => Virtual::Reference)"
check
end
def test_var
@string_input = 'int foo '
@output = "- Virtual::Return(:type => :int)"
@root = :field_def
check
end
def test_self
@string_input = 'self '
@output = "- Virtual::Self(:type => Virtual::Reference())"
@output = [Virtual::Self]
check
end
def test_string
@string_input = "\"hello\""
@output = "- Virtual::Return(:type => Virtual::Reference, :value => :hello)"
check
end

View File

@ -1,175 +0,0 @@
require_relative "compiler_helper"
require_relative "code_checker"
module Virtual
class TestMethods < MiniTest::Test
include CodeChecker
def test_simplest_function
@string_input = <<HERE
class Object
int main()
return 5
end
end
HERE
@output = [[MethodEnter,Set] ,[MethodReturn]]
check
end
def test_second_simplest_function
@string_input = <<HERE
class Object
int main()
int x = 5
return x
end
end
HERE
@output = [[Virtual::MethodEnter,Set],[Virtual::MethodReturn]]
check
end
def test_puts_string
@string_input = <<HERE
class Object
int puts(ref str)
return str
end
int main()
puts("Hello")
end
end
HERE
@output = [[MethodEnter , NewMessage, Set, Set , Set, Set, MethodCall],[MethodReturn]]
check
end
def test_int_function
@string_input = <<HERE
class Integer < Object
int times(int x)
return x
end
end
HERE
@output = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
check
cla = Virtual.machine.space.get_class_by_name :Integer
assert cla.get_instance_method( :times )
end
def test_function_ops
@string_input = <<HERE
class Object
int foo(int abba)
abba = 5
2 + 5
end
end
HERE
@output = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
check
end
def test_function_if
@string_input = <<HERE
class Object
int main()
if(0)
return 42
else
return 667
end
end
end
HERE
@output = [[MethodEnter,Set,Register::IsZeroBranch] , [Set,Register::AlwaysBranch],
[Set],[],[MethodReturn]]
check
end
def test_while
@string_input = <<HERE
class Object
int foo()
while(1)
3
end
end
end
HERE
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
check
end
def test_function_while
@string_input = <<HERE
class Object
int fibonaccit(int n)
int a = 0
while(n)
int some = 43
int other = some * 4
end
end
end
HERE
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
check
end
def test_function_return
@string_input = <<HERE
class Object
int retvar(int n)
int i = 5
return i
end
end
HERE
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
check
end
def test_function_return_if
@string_input = <<HERE
class Object
int main()
int n = 10
if( n > 5)
return 10
else
return 20
end
end
end
HERE
@output = [[MethodEnter,Set,Set,Register::GetSlot,Register::GetSlot,
Register::OperatorInstruction,Register::IsZeroBranch],
[Set,Register::AlwaysBranch],[Set],[],[MethodReturn]]
check
end
def test_function_return_while
@string_input = <<HERE
class Object
int main()
int n = 10
while( n > 5)
n = n + 1
return n
end
end
end
HERE
@output = [[MethodEnter,Set],
[Set,Register::GetSlot,Register::GetSlot,Register::OperatorInstruction,
Register::IsZeroBranch,Set,Register::GetSlot,Register::GetSlot,
Register::OperatorInstruction,Set,Register::AlwaysBranch] ,
[],[MethodReturn]]
check
end
end
end