move parfait tests to some
after renaming compiler to soml it’s where they wanna be also will allow for unifying test helpers and testing fragments remotely too
This commit is contained in:
33
test/soml/expressions/helper.rb
Normal file
33
test/soml/expressions/helper.rb
Normal file
@ -0,0 +1,33 @@
|
||||
require_relative '../../helper'
|
||||
require 'parslet/convenience'
|
||||
|
||||
Soml::Compiler.class_eval do
|
||||
def set_main main
|
||||
@clazz = Register.machine.space.get_class_by_name :Object
|
||||
@method = main
|
||||
@current = main.instructions.next
|
||||
end
|
||||
end
|
||||
|
||||
module ExpressionHelper
|
||||
|
||||
def set_main compiler
|
||||
compiler.set_main Register.machine.space.get_main
|
||||
end
|
||||
def check
|
||||
machine = Register.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 = Soml::Compiler.new
|
||||
set_main(compiler)
|
||||
produced = compiler.process( parts )
|
||||
assert @output , "No output given"
|
||||
assert_equal produced.class, @output , "Wrong class"
|
||||
produced
|
||||
end
|
||||
|
||||
end
|
4
test/soml/expressions/test_all.rb
Normal file
4
test/soml/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_ops"
|
52
test/soml/expressions/test_basic.rb
Normal file
52
test/soml/expressions/test_basic.rb
Normal file
@ -0,0 +1,52 @@
|
||||
require_relative "helper"
|
||||
|
||||
|
||||
class TestBasic < MiniTest::Test
|
||||
include ExpressionHelper
|
||||
|
||||
def setup
|
||||
@root = :basic_type
|
||||
@output = Register::RegisterValue
|
||||
end
|
||||
|
||||
def test_number
|
||||
@string_input = '42 '
|
||||
assert_equal 42 , check.value
|
||||
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_space
|
||||
@string_input = 'self '
|
||||
check
|
||||
end
|
||||
|
||||
def test_string
|
||||
@string_input = "\"hello\""
|
||||
check
|
||||
end
|
||||
|
||||
end
|
45
test/soml/expressions/test_call.rb
Normal file
45
test/soml/expressions/test_call.rb
Normal file
@ -0,0 +1,45 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Register
|
||||
class TestCall < MiniTest::Test
|
||||
include ExpressionHelper
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@root = :call_site
|
||||
@output = Register::RegisterValue
|
||||
end
|
||||
|
||||
def test_call_main_plain
|
||||
@string_input = 'main()'
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_main_int
|
||||
@string_input = 'main(1)'
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_self_main
|
||||
@string_input = 'self.main()'
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_main_string
|
||||
@string_input = 'main("1")'
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_main_op
|
||||
Register.machine.space.get_main.ensure_local(:bar , :Integer)
|
||||
@string_input = 'main( bar )'
|
||||
check
|
||||
end
|
||||
|
||||
def test_call_string_put
|
||||
@string_input = '"Hello Raisa, I am salama".putstring()'
|
||||
check
|
||||
end
|
||||
|
||||
end
|
||||
end
|
61
test/soml/expressions/test_field_access.rb
Normal file
61
test/soml/expressions/test_field_access.rb
Normal file
@ -0,0 +1,61 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Register
|
||||
class TestFields < MiniTest::Test
|
||||
include ExpressionHelper
|
||||
|
||||
def setup
|
||||
Register.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_not_space
|
||||
@root = :field_access
|
||||
@string_input = <<HERE
|
||||
self.space
|
||||
HERE
|
||||
assert_raises(RuntimeError) { check }
|
||||
end
|
||||
|
||||
def test_field
|
||||
Register.machine.space.get_class_by_name(:Object).object_layout.add_instance_variable(:bro,:Object)
|
||||
@root = :field_access
|
||||
@string_input = <<HERE
|
||||
self.bro
|
||||
HERE
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
def test_local
|
||||
Register.machine.space.get_main.ensure_local(:bar , :Integer)
|
||||
@root = :name
|
||||
@string_input = 'bar '
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
def test_space
|
||||
@root = :name
|
||||
@string_input = 'space '
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
def test_args
|
||||
Register.machine.space.get_main.arguments.push Parfait::Variable.new(:Integer , :bar)
|
||||
@root = :name
|
||||
@string_input = 'bar '
|
||||
@output = Register::RegisterValue
|
||||
check
|
||||
end
|
||||
|
||||
end
|
||||
end
|
45
test/soml/expressions/test_ops.rb
Normal file
45
test/soml/expressions/test_ops.rb
Normal file
@ -0,0 +1,45 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Register
|
||||
class TestOps < MiniTest::Test
|
||||
include ExpressionHelper
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
@root = :operator_value
|
||||
@output = Register::RegisterValue
|
||||
end
|
||||
|
||||
def operators
|
||||
["+" , "-" , "*" , "/" , "=="]
|
||||
end
|
||||
def test_ints
|
||||
operators.each do |op|
|
||||
@string_input = '2 + 3'.sub("+" , op)
|
||||
check
|
||||
end
|
||||
end
|
||||
def test_local_int
|
||||
Register.machine.space.get_main.ensure_local(:bar , :Integer)
|
||||
@string_input = 'bar + 3'
|
||||
check
|
||||
end
|
||||
def test_int_local
|
||||
Register.machine.space.get_main.ensure_local(:bar , :Integer)
|
||||
@string_input = '3 + bar'
|
||||
check
|
||||
end
|
||||
|
||||
def test_field_int
|
||||
Register.machine.space.get_class_by_name(:Object).object_layout.add_instance_variable(:bro,:int)
|
||||
@string_input = "self.bro + 3"
|
||||
check
|
||||
end
|
||||
|
||||
def test_int_field
|
||||
Register.machine.space.get_class_by_name(:Object).object_layout.add_instance_variable(:bro,:int)
|
||||
@string_input = "3 + self.bro"
|
||||
check
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user