create compilers directory, found and renamed salama.rb
This commit is contained in:
13
test/melon/compilers/helper.rb
Normal file
13
test/melon/compilers/helper.rb
Normal file
@ -0,0 +1,13 @@
|
||||
require_relative '../helper'
|
||||
|
||||
module Melon
|
||||
module CompilerHelper
|
||||
def in_Space(statements)
|
||||
"class Space ; #{statements} ; end"
|
||||
end
|
||||
|
||||
def as_main(statements)
|
||||
in_Space("def main ; #{statements}; end")
|
||||
end
|
||||
end
|
||||
end
|
4
test/melon/compilers/test_all.rb
Normal file
4
test/melon/compilers/test_all.rb
Normal file
@ -0,0 +1,4 @@
|
||||
require_relative "test_type_collector"
|
||||
require_relative "test_method_collector"
|
||||
require_relative "test_locals_collector"
|
||||
require_relative "test_class_creation"
|
25
test/melon/compilers/test_class_creation.rb
Normal file
25
test/melon/compilers/test_class_creation.rb
Normal file
@ -0,0 +1,25 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Melon
|
||||
class TestClass < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
end
|
||||
|
||||
def test_creates_class_without_deriviation
|
||||
Compiler.compile "class Testing ; end"
|
||||
clazz = Parfait.object_space.get_class_by_name(:Testing)
|
||||
assert clazz , "No classes created"
|
||||
assert_equal :Object , clazz.super_class_name
|
||||
end
|
||||
|
||||
def test_creates_class_with_deriviation
|
||||
Compiler.compile "class Test2 < List ;end"
|
||||
clazz = Parfait.object_space.get_class_by_name(:Test2)
|
||||
assert clazz, "No classes created"
|
||||
assert_equal :List , clazz.super_class_name
|
||||
end
|
||||
|
||||
end
|
||||
end
|
37
test/melon/compilers/test_locals_collector.rb
Normal file
37
test/melon/compilers/test_locals_collector.rb
Normal file
@ -0,0 +1,37 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Melon
|
||||
module Compilers
|
||||
class TestLocalsCollector < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot unless Register.machine.booted
|
||||
end
|
||||
|
||||
def parse_collect( input )
|
||||
ast = Parser::Ruby22.parse input
|
||||
LocalsCollector.new.collect(ast)
|
||||
end
|
||||
|
||||
def test_no_locals
|
||||
locals = parse_collect "def meth; end"
|
||||
assert locals.empty?
|
||||
end
|
||||
|
||||
def test_method_is_not_local
|
||||
locals = parse_collect("def meth2(arg1); foo ;end")
|
||||
assert locals.empty?
|
||||
end
|
||||
|
||||
def test_local_assign_one
|
||||
locals = parse_collect("def meth2(arg1); foo = bar ;end")
|
||||
assert locals[:foo] , locals.inspect
|
||||
end
|
||||
def test_local_assign_two
|
||||
locals = parse_collect("def meth2(arg1); foo = bar ; groove = 1 + 2 ;end")
|
||||
assert locals.length == 2 , locals.inspect
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
39
test/melon/compilers/test_method_collector.rb
Normal file
39
test/melon/compilers/test_method_collector.rb
Normal file
@ -0,0 +1,39 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Melon
|
||||
module Compilers
|
||||
class TestMethodCollector < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot unless Register.machine.booted
|
||||
end
|
||||
|
||||
def parse_collect( input )
|
||||
ast = Parser::Ruby22.parse input
|
||||
MethodCollector.new.collect(ast)
|
||||
end
|
||||
|
||||
def test_no_args
|
||||
methods = parse_collect "def meth; @ivar;end"
|
||||
assert methods.find{|m| m.name == :meth }
|
||||
end
|
||||
|
||||
def test_one_arg
|
||||
method = parse_collect("def meth2(arg1); 1;end").first
|
||||
assert method.name == :meth2
|
||||
assert_equal 2 , method.args_type.variable_index(:arg1) , method.args_type.inspect
|
||||
end
|
||||
|
||||
def test_three_args
|
||||
method = parse_collect("def meth3(yksi,kaksi,kolme); 1;end").first
|
||||
assert method.args_type.variable_index(:kolme) , method.args_type.inspect
|
||||
end
|
||||
|
||||
def test_one_local
|
||||
method = parse_collect("def meth2(arg1); foo = 2 ;end").first
|
||||
assert method.locals_type.variable_index(:foo) , method.locals_type.inspect
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
40
test/melon/compilers/test_type_collector.rb
Normal file
40
test/melon/compilers/test_type_collector.rb
Normal file
@ -0,0 +1,40 @@
|
||||
require_relative "helper"
|
||||
|
||||
module Melon
|
||||
module Compilers
|
||||
class TestTypeCollector < MiniTest::Test
|
||||
|
||||
def setup
|
||||
Register.machine.boot
|
||||
end
|
||||
|
||||
def parse_collect( input )
|
||||
ast = Parser::Ruby22.parse input
|
||||
TypeCollector.new.collect(ast)
|
||||
end
|
||||
|
||||
def test_ivar_name
|
||||
hash = parse_collect "def meth; @ivar;end"
|
||||
assert hash[:ivar] , hash
|
||||
end
|
||||
|
||||
def test_ivar_assign
|
||||
hash = parse_collect "def meth; @ivar = 5;end"
|
||||
assert hash[:ivar] , hash
|
||||
end
|
||||
|
||||
def test_ivar_operator_assign
|
||||
hash = parse_collect "def meth; @ivar += 5;end"
|
||||
assert hash[:ivar] , hash
|
||||
end
|
||||
|
||||
def test_compile_class
|
||||
Compiler.compile "class TestIvar < Object ; def meth; @ivar;end; end"
|
||||
itest = Parfait.object_space.get_class_by_name(:TestIvar)
|
||||
assert itest
|
||||
assert itest.instance_type.names.include?(:ivar) , itest.instance_type.names.inspect
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user