more class tests

This commit is contained in:
Torsten Ruger 2015-10-25 13:19:18 +02:00
parent 12e31f28c2
commit 7de2f913a0
3 changed files with 87 additions and 3 deletions

View File

@ -60,6 +60,7 @@ module Parfait
def add_instance_method method
raise "not a method #{method.class} #{method.inspect}" unless method.is_a? Method
raise "syserr #{method.name.class}" unless method.name.is_a? Symbol
raise "Adding to wrong class, should be #{method.for_class}" if method.for_class != self
found = get_instance_method( method.name )
if found
self.instance_methods.delete(found)
@ -77,7 +78,7 @@ module Parfait
else
raise "No such method #{method_name} in #{self.name}"
end
self.instance_methods.delete found
return true
end
def create_instance_method method_name , arguments
@ -85,7 +86,7 @@ module Parfait
clazz = object_layout().object_class()
raise "??? #{method_name}" unless clazz
#puts "Self: #{self.class} clazz: #{clazz.name}"
Method.new( clazz , method_name , arguments )
add_instance_method Method.new( clazz , method_name , arguments )
end
# this needs to be done during booting as we can't have all the classes and superclassses
@ -110,7 +111,7 @@ module Parfait
method = get_instance_method(m_name)
return method if method
if( self.super_class )
method = self.super_class.resolve_method(m_name)
method = Parfait::Space.object_space.get_class_by_name(self.super_class).resolve_method(m_name)
raise "Method not found #{m_name}, for \n#{self}" unless method
end
method

View File

@ -0,0 +1,24 @@
require_relative 'helper'
module Register
class TestBasicClass < MiniTest::Test
include Statements
def test_class_def
@string_input = <<HERE
class Bar
int buh()
return 1
end
end
class Object
int main()
return 1
end
end
HERE
@expect = [Label, SaveReturn,LoadConstant,Label,RegisterTransfer,GetSlot,FunctionReturn]
check
end
end
end

View File

@ -0,0 +1,59 @@
require_relative "../helper"
class TestClass < MiniTest::Test
def setup
@space = Register.machine.boot.space
@try = @space.create_class :Try , :Object
end
def foo_method for_class = :Try
args = Register.new_list [ Parfait::Variable.new(:Integer , :bar )]
::Parfait::Method.new @space.get_class_by_name(for_class) , :foo , args
end
def test_layout_forclass
assert_equal "Class(Space)" , @space.get_layout.object_class.inspect
assert_equal :Space , @space.get_layout.object_class.name
end
def test_new_superclass
assert_equal :Object , @try.super_class
end
def test_new_methods
assert_equal @try.method_names.class, @try.instance_methods.class
assert_equal @try.method_names.get_length , @try.instance_methods.get_length
end
def test_add_method
foo = foo_method
assert_equal foo , @try.add_instance_method(foo)
assert_equal 1 , @try.instance_methods.get_length
assert_equal ":foo" , @try.method_names.inspect
end
def test_remove_method
test_add_method
assert_equal true , @try.remove_instance_method(:foo)
end
def test_remove_nothere
assert_raises RuntimeError do
@try.remove_instance_method(:foo)
end
end
def test_create_method
@try.create_instance_method :bar, Register.new_list( [ Parfait::Variable.new(:Integer , :bar )])
assert_equal ":bar" , @try.method_names.inspect
end
def test_method_get
test_add_method
assert_equal Parfait::Method , @try.get_instance_method(:foo).class
end
def test_method_get_nothere
assert_equal nil , @try.get_instance_method(:foo)
test_remove_method
assert_equal nil , @try.get_instance_method(:foo)
end
def test_resolve
foo = foo_method :Object
@space.get_class_by_name(:Object).add_instance_method(foo)
assert_equal :foo , @try.resolve_method(:foo).name
end
end