From d2c670b31aa12635632775bf1e8873425ef874ad Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 26 Oct 2015 12:57:54 +0200 Subject: [PATCH] fixing offset bug found by tests that overwrite the attribute --- lib/parfait/indexed.rb | 1 + lib/parfait/layout.rb | 14 ++++---------- test/parfait/test_list.rb | 11 ++++++++++- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/parfait/indexed.rb b/lib/parfait/indexed.rb index fcd2994b..e39197a6 100644 --- a/lib/parfait/indexed.rb +++ b/lib/parfait/indexed.rb @@ -140,6 +140,7 @@ module Parfait module Methods def offset( offset ) + offset += 1 # for the attribute we add (indexed_length) define_method :get_offset do offset diff --git a/lib/parfait/layout.rb b/lib/parfait/layout.rb index 658894a5..8f323f72 100644 --- a/lib/parfait/layout.rb +++ b/lib/parfait/layout.rb @@ -25,7 +25,7 @@ module Parfait attribute :object_class include Indexed - self.offset(2) + self.offset(1) def initialize( object_class ) super() @@ -47,7 +47,7 @@ module Parfait self.get_length end - def object_instance_names + def instance_names names = List.new each do |item| names.push item @@ -55,20 +55,14 @@ module Parfait names end - def object_instance_length + def instance_length self.get_length end - alias :list_index :index_of - # private inheritance is something to think off, we don't really want the list api exported - def index_of name - raise "should not rely on layout internal structure, use variable_index" - end - # index of the variable when using internal_object_get # (internal_object_get is 1 based and 1 is always the layout) def variable_index name - has = list_index(name) + has = index_of(name) return nil unless has raise "internal error #{name}:#{has}" if has < 1 has diff --git a/test/parfait/test_list.rb b/test/parfait/test_list.rb index 4926bb61..32c6bba9 100644 --- a/test/parfait/test_list.rb +++ b/test/parfait/test_list.rb @@ -33,6 +33,15 @@ class TestList < MiniTest::Test assert_equal 1 , layout.variable_index(:layout) end + def test_length0 + assert_equal 0 , @list.get_length + assert_equal nil , @list.indexed_length + end + def test_length1 + @list.push :one + assert_equal 1 , @list.get_length + assert_equal 1 , @list.indexed_length + end def test_list_inspect @list.set(1,1) assert_equal "1" , @list.inspect @@ -54,7 +63,7 @@ class TestList < MiniTest::Test end def test_one_set1 assert_equal 2 , @list.set(1,2) - assert_equal 1 , @list.internal_object_get(1) + assert_equal 1 , @list.internal_object_get(2) end def test_set1_len @list.set(1,1)