clearer organization for compiler tests
was slightly messy with small/large now needed different test for expression and statements
This commit is contained in:
17
test/compiler/expressions/code_checker.rb
Normal file
17
test/compiler/expressions/code_checker.rb
Normal file
@ -0,0 +1,17 @@
|
||||
module CodeChecker
|
||||
def check
|
||||
machine = Virtual.machine.boot
|
||||
machine.parse_and_compile @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
|
||||
end
|
31
test/compiler/expressions/compiler_helper.rb
Normal file
31
test/compiler/expressions/compiler_helper.rb
Normal file
@ -0,0 +1,31 @@
|
||||
require_relative '../../helper'
|
||||
require 'parslet/convenience'
|
||||
|
||||
Phisol::Compiler.class_eval do
|
||||
def set_main main
|
||||
@clazz = Virtual.machine.space.get_class_by_name :Object
|
||||
@method = main
|
||||
end
|
||||
end
|
||||
|
||||
module CompilerHelper
|
||||
|
||||
def set_main compiler
|
||||
compiler.set_main Virtual.machine.space.get_main
|
||||
end
|
||||
def check
|
||||
machine = Virtual.machine
|
||||
machine.boot unless machine.booted
|
||||
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
|
||||
set_main(compiler)
|
||||
produced = compiler.process( parts )
|
||||
assert @output , "No output given"
|
||||
assert_equal produced.class, @output , "Wrong class"
|
||||
end
|
||||
|
||||
end
|
4
test/compiler/expressions/test_all.rb
Normal file
4
test/compiler/expressions/test_all.rb
Normal file
@ -0,0 +1,4 @@
|
||||
require_relative "test_basic"
|
||||
require_relative "test_call"
|
||||
require_relative "test_field_access"
|
||||
require_relative "test_function"
|
47
test/compiler/expressions/test_basic.rb
Normal file
47
test/compiler/expressions/test_basic.rb
Normal file
@ -0,0 +1,47 @@
|
||||
require_relative "compiler_helper"
|
||||
|
||||
|
||||
class TestBasic < MiniTest::Test
|
||||
include CompilerHelper
|
||||
|
||||
def setup
|
||||
@root = :basic_type
|
||||
@output = Register::RegisterValue
|
||||
end
|
||||
|
||||
def test_number
|
||||
@string_input = '42 '
|
||||
check
|
||||
end
|
||||
|
||||
def test_true
|
||||
@string_input = 'true'
|
||||
check
|
||||
end
|
||||
def test_false
|
||||
@string_input = 'false '
|
||||
check
|
||||
end
|
||||
def test_nil
|
||||
@string_input = 'nil '
|
||||
check
|
||||
end
|
||||
|
||||
def test_var
|
||||
@string_input = 'int foo '
|
||||
@root = :field_def
|
||||
@output = AST::Node
|
||||
check
|
||||
end
|
||||
|
||||
def test_self
|
||||
@string_input = 'self '
|
||||
check
|
||||
end
|
||||
|
||||
def test_string
|
||||
@string_input = "\"hello\""
|
||||
check
|
||||
end
|
||||
|
||||
end
|
55
test/compiler/expressions/test_call.rb
Normal file
55
test/compiler/expressions/test_call.rb
Normal file
@ -0,0 +1,55 @@
|
||||
require_relative "compiler_helper"
|
||||
|
||||
module Virtual
|
||||
class TestCall < MiniTest::Test
|
||||
include CompilerHelper
|
||||
|
||||
def setup
|
||||
Virtual.machine.boot
|
||||
end
|
||||
|
||||
def test_call_main_plain
|
||||
@root = :call_site
|
||||
@string_input = 'main()'
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_main_int
|
||||
@root = :call_site
|
||||
@string_input = 'main(1)'
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
def ttest_call_self_main
|
||||
@root = :call_site
|
||||
@string_input = 'self.main()'
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_main_string
|
||||
@root = :call_site
|
||||
@string_input = 'main("1")'
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_main_op
|
||||
Virtual.machine.space.get_main.ensure_local(:bar , :Integer)
|
||||
@root = :call_site
|
||||
@string_input = 'main( bar )'
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_string_put
|
||||
@root = :call_site
|
||||
@string_input = '"Hello Raisa, I am salama".putstring()'
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
end
|
||||
end
|
46
test/compiler/expressions/test_field_access.rb
Normal file
46
test/compiler/expressions/test_field_access.rb
Normal file
@ -0,0 +1,46 @@
|
||||
require_relative "compiler_helper"
|
||||
|
||||
module Virtual
|
||||
class TestFields < MiniTest::Test
|
||||
include CompilerHelper
|
||||
|
||||
def setup
|
||||
Virtual.machine.boot
|
||||
end
|
||||
|
||||
def test_field_not_defined
|
||||
@root = :field_access
|
||||
@string_input = <<HERE
|
||||
self.a
|
||||
HERE
|
||||
assert_raises(RuntimeError) { check }
|
||||
end
|
||||
|
||||
def test_field
|
||||
Virtual.machine.space.get_class_by_name(:Object).object_layout.add_instance_variable(:bro)
|
||||
@root = :field_access
|
||||
@string_input = <<HERE
|
||||
self.bro
|
||||
HERE
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
def test_local
|
||||
Virtual.machine.space.get_main.ensure_local(:bar , :Integer)
|
||||
@root = :name
|
||||
@string_input = 'bar '
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
def test_args
|
||||
Virtual.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :bar)
|
||||
@root = :name
|
||||
@string_input = 'bar '
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
end
|
||||
end
|
27
test/compiler/expressions/test_function.rb
Normal file
27
test/compiler/expressions/test_function.rb
Normal file
@ -0,0 +1,27 @@
|
||||
require_relative "compiler_helper"
|
||||
|
||||
module Virtual
|
||||
class TestFunctions < MiniTest::Test
|
||||
include CompilerHelper
|
||||
|
||||
def setup
|
||||
Virtual.machine.boot
|
||||
end
|
||||
|
||||
#reset the compiler (other than other tests that need to fake their inside a method)
|
||||
def set_main compiler
|
||||
compiler.set_main(nil)
|
||||
end
|
||||
|
||||
def test_puts
|
||||
@root = :function_definition
|
||||
@string_input = <<HERE
|
||||
int puts(Word str)
|
||||
main()
|
||||
end
|
||||
HERE
|
||||
@output = AST::Node
|
||||
check
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user