improve tests
This commit is contained in:
parent
530537de1a
commit
c20ab21bbd
@ -1,40 +1,30 @@
|
|||||||
require_relative '../helper'
|
require_relative '../helper'
|
||||||
require 'parslet/convenience'
|
require 'parslet/convenience'
|
||||||
|
|
||||||
|
module CompilerHelper
|
||||||
|
|
||||||
Phisol::Compiler.class_eval do
|
Phisol::Compiler.class_eval do
|
||||||
|
def set_main
|
||||||
def check
|
@method = Virtual.machine.space.get_main
|
||||||
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
|
|
||||||
end
|
end
|
||||||
return true
|
|
||||||
end
|
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
|
end
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
require_relative "test_methods"
|
|
||||||
require_relative "test_hello"
|
require_relative "test_hello"
|
||||||
require_relative "test_compiler"
|
require_relative "test_compiler"
|
||||||
require_relative "test_field_access"
|
require_relative "test_field_access"
|
||||||
|
@ -2,61 +2,45 @@ require_relative "compiler_helper"
|
|||||||
|
|
||||||
|
|
||||||
class TestBasic < MiniTest::Test
|
class TestBasic < MiniTest::Test
|
||||||
def check
|
include CompilerHelper
|
||||||
input = <<HERE
|
|
||||||
class Object
|
|
||||||
int main()
|
|
||||||
#{@string_input}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
HERE
|
|
||||||
|
|
||||||
statements = Virtual.machine.boot.parse_and_compile input
|
def setup
|
||||||
if( statements.first.is_a? Virtual::Self )
|
@root = :basic_type
|
||||||
statements.first.type.instance_variable_set :@of_class , nil
|
@output = [Virtual::Return]
|
||||||
end
|
|
||||||
is = Sof.write(statements)
|
|
||||||
#puts is
|
|
||||||
assert_equal @output , is
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_number
|
def test_number
|
||||||
@string_input = '42 '
|
@string_input = '42 '
|
||||||
@output = "- Virtual::Return(:type => Virtual::Integer, :value => 42)"
|
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_true
|
def test_true
|
||||||
@string_input = 'true '
|
@string_input = 'true '
|
||||||
@output = "- Virtual::Return(:type => Virtual::Reference, :value => true)"
|
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
def test_false
|
def test_false
|
||||||
@string_input = 'false '
|
@string_input = 'false '
|
||||||
@output = "- Virtual::Return(:type => Virtual::Reference, :value => false)"
|
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
def test_nil
|
def test_nil
|
||||||
@string_input = 'nil '
|
@string_input = 'nil '
|
||||||
@output = "- Virtual::Return(:type => Virtual::Reference)"
|
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_var
|
def test_var
|
||||||
@string_input = 'int foo '
|
@string_input = 'int foo '
|
||||||
@output = "- Virtual::Return(:type => :int)"
|
@root = :field_def
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_self
|
def test_self
|
||||||
@string_input = 'self '
|
@string_input = 'self '
|
||||||
@output = "- Virtual::Self(:type => Virtual::Reference())"
|
@output = [Virtual::Self]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_string
|
def test_string
|
||||||
@string_input = "\"hello\""
|
@string_input = "\"hello\""
|
||||||
@output = "- Virtual::Return(:type => Virtual::Reference, :value => :hello)"
|
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -7,3 +7,4 @@ require_relative "test_putint"
|
|||||||
require_relative "test_functions"
|
require_relative "test_functions"
|
||||||
require_relative "test_recursive_fibo"
|
require_relative "test_recursive_fibo"
|
||||||
require_relative "test_while_fibo"
|
require_relative "test_while_fibo"
|
||||||
|
require_relative "test_methods"
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
require_relative "compiler_helper"
|
require_relative 'helper'
|
||||||
require_relative "code_checker"
|
|
||||||
|
|
||||||
module Virtual
|
module Virtual
|
||||||
class TestMethods < MiniTest::Test
|
class TestMethods < MiniTest::Test
|
||||||
include CodeChecker
|
include Fragments
|
||||||
|
|
||||||
def test_simplest_function
|
def test_simplest_function
|
||||||
@string_input = <<HERE
|
@string_input = <<HERE
|
||||||
@ -13,7 +12,7 @@ class Object
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [[MethodEnter,Set] ,[MethodReturn]]
|
@expect = [[MethodEnter,Set] ,[MethodReturn]]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -27,7 +26,7 @@ class Object
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [[Virtual::MethodEnter,Set],[Virtual::MethodReturn]]
|
@expect = [[Virtual::MethodEnter,Set],[Virtual::MethodReturn]]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -42,7 +41,7 @@ class Object
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [[MethodEnter , NewMessage, Set, Set , Set, Set, MethodCall],[MethodReturn]]
|
@expect = [[MethodEnter , NewMessage, Set, Set , Set, Set, MethodCall],[MethodReturn]]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -54,7 +53,7 @@ class Integer < Object
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
|
@expect = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
|
||||||
check
|
check
|
||||||
cla = Virtual.machine.space.get_class_by_name :Integer
|
cla = Virtual.machine.space.get_class_by_name :Integer
|
||||||
assert cla.get_instance_method( :times )
|
assert cla.get_instance_method( :times )
|
||||||
@ -69,7 +68,7 @@ class Object
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
|
@expect = [[Virtual::MethodEnter] , [Virtual::MethodReturn]]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -85,7 +84,7 @@ class Object
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [[MethodEnter,Set,Register::IsZeroBranch] , [Set,Register::AlwaysBranch],
|
@expect = [[MethodEnter,Set,Register::IsZeroBranch] , [Set,Register::AlwaysBranch],
|
||||||
[Set],[],[MethodReturn]]
|
[Set],[],[MethodReturn]]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
@ -100,7 +99,7 @@ class Object
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
|
@expect = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -116,7 +115,7 @@ class Object
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
|
@expect = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -129,7 +128,7 @@ class Object
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
|
@expect = [[Virtual::MethodEnter],[Virtual::MethodReturn]]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -146,7 +145,7 @@ class Object
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [[MethodEnter,Set,Set,Register::GetSlot,Register::GetSlot,
|
@expect = [[MethodEnter,Set,Set,Register::GetSlot,Register::GetSlot,
|
||||||
Register::OperatorInstruction,Register::IsZeroBranch],
|
Register::OperatorInstruction,Register::IsZeroBranch],
|
||||||
[Set,Register::AlwaysBranch],[Set],[],[MethodReturn]]
|
[Set,Register::AlwaysBranch],[Set],[],[MethodReturn]]
|
||||||
check
|
check
|
||||||
@ -164,7 +163,7 @@ class Object
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = [[MethodEnter,Set],
|
@expect = [[MethodEnter,Set],
|
||||||
[Set,Register::GetSlot,Register::GetSlot,Register::OperatorInstruction,
|
[Set,Register::GetSlot,Register::GetSlot,Register::OperatorInstruction,
|
||||||
Register::IsZeroBranch,Set,Register::GetSlot,Register::GetSlot,
|
Register::IsZeroBranch,Set,Register::GetSlot,Register::GetSlot,
|
||||||
Register::OperatorInstruction,Set,Register::AlwaysBranch] ,
|
Register::OperatorInstruction,Set,Register::AlwaysBranch] ,
|
Loading…
Reference in New Issue
Block a user