diff --git a/lib/typed/parfait/type.rb b/lib/typed/parfait/type.rb index d9a7877c..de84ee70 100644 --- a/lib/typed/parfait/type.rb +++ b/lib/typed/parfait/type.rb @@ -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 diff --git a/test/typed/type/test_all.rb b/test/typed/type/test_all.rb index ecc17c4c..5b33cffd 100644 --- a/test/typed/type/test_all.rb +++ b/test/typed/type/test_all.rb @@ -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" diff --git a/test/typed/type/test_basic.rb b/test/typed/type/test_basic.rb index cf585007..56e2f951 100644 --- a/test/typed/type/test_basic.rb +++ b/test/typed/type/test_basic.rb @@ -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 ) diff --git a/test/typed/type/test_hash.rb b/test/typed/type/test_hash.rb index 8c86be6f..44688024 100644 --- a/test/typed/type/test_hash.rb +++ b/test/typed/type/test_hash.rb @@ -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 diff --git a/test/typed/type/test_type_api.rb b/test/typed/type/test_type_api.rb new file mode 100644 index 00000000..e1f77172 --- /dev/null +++ b/test/typed/type/test_type_api.rb @@ -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