From 7ad36380c2b4838966b894d14a68b0265f5332d5 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 13 May 2018 13:03:04 +0300 Subject: [PATCH] add next_value for dictionary also next for list and add types attributes to space --- lib/parfait/dictionary.rb | 4 ++++ lib/parfait/list.rb | 10 +++++++++- lib/parfait/space.rb | 2 +- test/parfait/test_dictionary.rb | 18 ++++++++++++++++++ test/parfait/test_list.rb | 18 ++++++++++++++++++ test/parfait/test_space.rb | 3 +++ 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/lib/parfait/dictionary.rb b/lib/parfait/dictionary.rb index f73bea72..9358118f 100644 --- a/lib/parfait/dictionary.rb +++ b/lib/parfait/dictionary.rb @@ -30,6 +30,10 @@ module Parfait return @keys.get_length() end + def next_value(val) + return @values.next_value(val) + end + # get a value fot the given key # key identity is checked with == not === (ie equals not identity) # return nil if no such key diff --git a/lib/parfait/list.rb b/lib/parfait/list.rb index 2dadcd44..c17ab3c5 100644 --- a/lib/parfait/list.rb +++ b/lib/parfait/list.rb @@ -78,7 +78,7 @@ module Parfait end # include? means non nil index - def include? item + def include?( item ) return index_of(item) != nil end @@ -97,6 +97,14 @@ module Parfait return nil end + # return the next of given. Nil if item not in list or there is not next + def next_value(val) + index = index_of(val) + return nil unless index + return nil if index == get_length + return get(index + 1) + end + # push means add to the end # this automatically grows the List def push( value ) diff --git a/lib/parfait/space.rb b/lib/parfait/space.rb index 480544e3..a521f3b9 100644 --- a/lib/parfait/space.rb +++ b/lib/parfait/space.rb @@ -50,7 +50,7 @@ module Parfait @nil_object = Parfait::NilClass.new end - attr_reader :classes , :first_message , :next_integer + attr_reader :classes , :types , :first_message , :next_integer attr_reader :true_object , :false_object , :nil_object def each_type diff --git a/test/parfait/test_dictionary.rb b/test/parfait/test_dictionary.rb index dc08a3d9..c4e17e4f 100644 --- a/test/parfait/test_dictionary.rb +++ b/test/parfait/test_dictionary.rb @@ -76,4 +76,22 @@ module Parfait assert @lookup[2] == :two end end + class TestDictionaryNextValue < ParfaitTest + def setup + super + @lookup = ::Parfait::Dictionary.new + @lookup[:key1] = :value1 + @lookup[:key2] = :value2 + @lookup[:key3] = :value3 + end + def test_next_value_ok + assert_equal :value2 , @lookup.next_value(:value1) + end + def test_next_value_end + assert_equal :value3 , @lookup.next_value(:value2) + end + def test_next_value_not + assert_nil @lookup.next_value(:value3) + end + end end diff --git a/test/parfait/test_list.rb b/test/parfait/test_list.rb index e4c56215..efa7c75f 100644 --- a/test/parfait/test_list.rb +++ b/test/parfait/test_list.rb @@ -198,4 +198,22 @@ module Parfait assert_nil @list.last end end + class TestListContains < ParfaitTest + def setup + super + @list = ::Parfait::List.new + @list.push :one + @list.push :two + end + + def test_next_value_ok + assert_equal :two , @list.next_value(:one) + end + def test_next_value_end + assert_nil @list.next_value(:two) + end + def test_next_value_not_int + assert_nil @list.next_value(:three) + end + end end diff --git a/test/parfait/test_space.rb b/test/parfait/test_space.rb index b30b4c87..9b0131f7 100644 --- a/test/parfait/test_space.rb +++ b/test/parfait/test_space.rb @@ -43,6 +43,9 @@ module Parfait def test_types assert @space.instance_variable_ged("@types").is_a? Parfait::Dictionary end + def test_types_attr + assert @space.types.is_a? Parfait::Dictionary + end def test_types_each @space.each_type do |type|