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 set_main
@method = Virtual.machine.space.get_main
end
end
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
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
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
end
return true
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

@ -7,3 +7,4 @@ require_relative "test_putint"
require_relative "test_functions"
require_relative "test_recursive_fibo"
require_relative "test_while_fibo"
require_relative "test_methods"

View File

@ -1,9 +1,8 @@
require_relative "compiler_helper"
require_relative "code_checker"
require_relative 'helper'
module Virtual
class TestMethods < MiniTest::Test
include CodeChecker
include Fragments
def test_simplest_function
@string_input = <<HERE
@ -13,7 +12,7 @@ class Object
end
end
HERE
@output = [[MethodEnter,Set] ,[MethodReturn]]
@expect = [[MethodEnter,Set] ,[MethodReturn]]
check
end
@ -27,7 +26,7 @@ class Object
end
end
HERE
@output = [[Virtual::MethodEnter,Set],[Virtual::MethodReturn]]
@expect = [[Virtual::MethodEnter,Set],[Virtual::MethodReturn]]
check
end
@ -42,7 +41,7 @@ class Object
end
end
HERE
@output = [[MethodEnter , NewMessage, Set, Set , Set, Set, MethodCall],[MethodReturn]]
@expect = [[MethodEnter , NewMessage, Set, Set , Set, Set, MethodCall],[MethodReturn]]
check
end
@ -54,7 +53,7 @@ class Integer < Object
end
end
HERE
@output = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
@expect = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
check
cla = Virtual.machine.space.get_class_by_name :Integer
assert cla.get_instance_method( :times )
@ -69,7 +68,7 @@ class Object
end
end
HERE
@output = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
@expect = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
check
end
@ -85,7 +84,7 @@ class Object
end
end
HERE
@output = [[MethodEnter,Set,Register::IsZeroBranch] , [Set,Register::AlwaysBranch],
@expect = [[MethodEnter,Set,Register::IsZeroBranch] , [Set,Register::AlwaysBranch],
[Set],[],[MethodReturn]]
check
end
@ -100,7 +99,7 @@ class Object
end
end
HERE
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
@expect = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
check
end
@ -116,7 +115,7 @@ class Object
end
end
HERE
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
@expect = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
check
end
@ -129,7 +128,7 @@ class Object
end
end
HERE
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
@expect = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
check
end
@ -146,7 +145,7 @@ class Object
end
end
HERE
@output = [[MethodEnter,Set,Set,Register::GetSlot,Register::GetSlot,
@expect = [[MethodEnter,Set,Set,Register::GetSlot,Register::GetSlot,
Register::OperatorInstruction,Register::IsZeroBranch],
[Set,Register::AlwaysBranch],[Set],[],[MethodReturn]]
check
@ -164,7 +163,7 @@ class Object
end
end
HERE
@output = [[MethodEnter,Set],
@expect = [[MethodEnter,Set],
[Set,Register::GetSlot,Register::GetSlot,Register::OperatorInstruction,
Register::IsZeroBranch,Set,Register::GetSlot,Register::GetSlot,
Register::OperatorInstruction,Set,Register::AlwaysBranch] ,