improve type tests

This commit is contained in:
Torsten Ruger 2016-12-13 18:46:03 +02:00
parent 357490ff5f
commit 1c28926c6b
5 changed files with 90 additions and 52 deletions

View File

@ -63,7 +63,7 @@ module Parfait
private_add_instance_variable :type ,:Type
self.object_class = object_class
hash.each do |name , type|
private_add_instance_variable name , type
private_add_instance_variable(name , type) unless name == :type
end if hash
end
@ -75,6 +75,8 @@ module Parfait
# Type objects are immutable, so a new object is returned
# As types are also unique, two same adds will result in identical results
def add_instance_variable( name , type )
raise "No nil name" unless name
raise "No nil type" unless type
hash = to_hash
hash[name] = type
code = Type.hash_code_for( hash )
@ -105,14 +107,14 @@ module Parfait
# index of the variable when using get_internal_word
# (get_internal_word is 1 based and 1 is always the type)
def variable_index name
def variable_index( name )
has = super_index(name)
return nil unless has
raise "internal error #{name}:#{has}" if has < 1
(1 + has / 2).to_i # to_i for opal
end
def type_at index
def type_at( index )
type_index = index * 2
get(type_index)
end

View File

@ -1,3 +1,4 @@
require_relative "test_basic"
require_relative "test_message"
require_relative "test_hash"
require_relative "test_message"
require_relative "test_type_api"

View File

@ -4,21 +4,21 @@ class BasicType < MiniTest::Test
def setup
@mess = Register.machine.boot.space.first_message
assert @mess
end
def test_type_index
assert_equal @mess.get_type , @mess.get_internal_word(Parfait::TYPE_INDEX) , "mess"
end
def test_inspect
assert @mess.get_type.inspect.start_with?("Type")
end
def test_type_is_first
type = @mess.get_type
assert_equal 1 , type.variable_index(:type)
end
def test_length
assert @mess
assert @mess.get_type
assert_equal 9 , @mess.get_type.instance_length , @mess.get_type.inspect
end
@ -44,55 +44,11 @@ class BasicType < MiniTest::Test
end
end
def test_class_type
oc = Register.machine.boot.space.get_class_by_name( :Object )
assert_equal Parfait::Class , oc.class
type = oc.instance_type
assert_equal Parfait::Type , type.class
assert_equal 1 , type.instance_names.get_length , type.instance_names.inspect
assert_equal type.first , :type
end
def test_class_space
space = Register.machine.space
assert_equal Parfait::Space , space.class
type = space.get_type
assert_equal Parfait::Type , type.class
assert_equal 4 , type.instance_names.get_length
assert_equal type.object_class.class , Parfait::Class
assert_equal type.object_class.name , :Space
end
def test_attribute_set
@mess.receiver = 55
assert_equal 55 , @mess.receiver
end
def test_add_name
type = Parfait::Type.new Register.machine.space.get_class_by_name(:Type)
type.send(:private_add_instance_variable, :boo , :Object)
assert_equal 2 , type.variable_index(:boo)
assert_equal 4 , type.get_length
assert_equal :type , type.get(1)
assert_equal :boo , type.get(3)
type
end
def test_inspect
type = test_add_name
assert type.inspect.include?("boo") , type.inspect
end
def test_each
type = test_add_name
assert_equal 4 , type.get_length
counter = [:boo , :Object, :type , :Type]
type.each do |item|
assert_equal item , counter.delete(item)
end
assert counter.empty?
end
# not really parfait test, but related and no other place currently
def test_reg_index
message_ind = Register.resolve_index( :message , :receiver )

View File

@ -12,7 +12,7 @@ class TypeHash < MiniTest::Test
end
def test_length
assert_equal 16 , @types.length
assert @types.length > 16
end
def test_two_hashs_not_equal

View File

@ -0,0 +1,79 @@
require_relative "../helper"
class TypeApi < MiniTest::Test
def setup
tc = Register.machine.boot.space.get_class_by_name( :Type )
@type = Parfait::Type.new tc
end
def test_inspect
assert @type.inspect.start_with?("Type")
end
def test_class_type
oc = Register.machine.boot.space.get_class_by_name( :Object )
assert_equal Parfait::Class , oc.class
type = oc.instance_type
assert_equal Parfait::Type , type.class
assert_equal 1 , type.instance_names.get_length , type.instance_names.inspect
assert_equal type.first , :type
end
def test_class_space
space = Register.machine.space
assert_equal Parfait::Space , space.class
type = space.get_type
assert_equal Parfait::Type , type.class
assert_equal 4 , type.instance_names.get_length
assert_equal type.object_class.class , Parfait::Class
assert_equal type.object_class.name , :Space
end
def test_add_name
@type.add_instance_variable( :boo , :Object)
end
def test_added_name_length
type = test_add_name
assert_equal 4 , type.get_length , type.inspect
assert_equal :type , type.get(1)
assert_equal :boo , type.get(3)
end
def test_added_name_index
type = test_add_name
assert_equal 2 , type.variable_index(:boo)
assert_equal :Object , type.type_at(2)
end
def test_basic_var_index
assert_equal 1 , @type.variable_index(:type)
end
def test_basic_type_index
assert_equal :Type , @type.type_at(1)
end
def test_inspect_added
type = test_add_name
assert type.inspect.include?("boo") , type.inspect
end
def test_added_names
type = test_add_name
assert_equal :type , type.instance_names.get(1)
assert_equal :boo , type.instance_names.get(2)
assert_equal 2 , type.instance_names.get_length
end
def test_each
type = test_add_name
assert_equal 4 , type.get_length
counter = [ :boo , :Object , :type , :Type]
type.each do |item|
assert_equal item , counter.delete(item)
end
assert counter.empty? , counter.inspect
end
end