add next_value for dictionary

also next for list
and add types attributes to space
This commit is contained in:
Torsten Ruger 2018-05-13 13:03:04 +03:00
parent 2d901bf7b6
commit 7ad36380c2
6 changed files with 53 additions and 2 deletions

View File

@ -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

View File

@ -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 )

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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|