diff --git a/lib/typed/parfait/dictionary.rb b/lib/typed/parfait/dictionary.rb index fb07a83d..a96bd404 100644 --- a/lib/typed/parfait/dictionary.rb +++ b/lib/typed/parfait/dictionary.rb @@ -15,7 +15,13 @@ module Parfait @values = List.new() end - attr_reader :values , :keys #FIXME these should be dupped, not handed out + def keys + @keys.dup + end + + def values + @values.dup + end # are there any key/value items in the list def empty? @@ -46,24 +52,14 @@ module Parfait # private method def key_index(key) - len = @keys.get_length() - index = 1 - found = nil - while(index <= len) - if( @keys.get(index) == key) - found = index - break - end - index += 1 - end - found + @keys.index_of(key) end # set key with value, returns value def set(key , value) index = key_index(key) if( index ) - @keys.set(index , value) + @values.set(index , value) else @keys.push(key) @values.push(value) diff --git a/lib/typed/parfait/list.rb b/lib/typed/parfait/list.rb index a46076f5..01e199cc 100644 --- a/lib/typed/parfait/list.rb +++ b/lib/typed/parfait/list.rb @@ -33,6 +33,15 @@ module Parfait def to_sof_node(writer , level , ref ) Sof.array_to_sof_node(self , writer , level , ref ) end + + def dup + list = List.new + each do |item| + list.push(item) + end + list + end + def to_a array = [] index = 1 diff --git a/test/typed/parfait/test_dictionary.rb b/test/typed/parfait/test_dictionary.rb index 0f502612..835da68b 100644 --- a/test/typed/parfait/test_dictionary.rb +++ b/test/typed/parfait/test_dictionary.rb @@ -60,11 +60,17 @@ class TestDictionary < MiniTest::Test assert_equal v , shouldda[k] end end - def test_values - assert @lookup.values + @lookup[2] = 2 + assert @lookup.values.get_length == 1 end def test_keys - assert @lookup.keys + @lookup[2] = 2 + assert @lookup.keys.get_length == 1 + end + def test_override_exising + @lookup[2] = 2 + @lookup[2] = :two + assert @lookup[2] == :two end end