add a each_pair to indexed and use in types

This commit is contained in:
Torsten Ruger 2016-12-07 23:35:51 +02:00
parent 266a04040b
commit 2741f35380
5 changed files with 47 additions and 8 deletions

View File

@ -103,8 +103,6 @@ module Parfait
end
def each
# not sure how to do this with define_method, because of the double block issue.
# probably some clever way around that, but not important
index = 1
while index <= self.get_length
item = get(index)
@ -114,6 +112,17 @@ module Parfait
self
end
def each_pair
index = 1
while index <= self.get_length
key = get( index )
value = get(index + 1)
yield key , value
index = index + 2
end
self
end
def set_length len
was = self.get_length
return if was == len

View File

@ -46,6 +46,7 @@ module Parfait
def get_internal_word(index)
@memory[index]
end
# 1 -based index
def set_internal_word(index , value)
raise "failed init for #{self.class}" unless @memory
@ -132,6 +133,7 @@ module Parfait
def instance_variables
get_instance_variables.to_a.collect{ |n| "@#{n}".to_sym }
end
# name comes in as a ruby @var name
def instance_variable_get name
var = get_instance_variable name.to_s[1 .. -1].to_sym

View File

@ -66,10 +66,8 @@ module Parfait
def instance_names
names = List.new
name = true
each do |item|
names.push(item) if name
name = ! name
each_pair do |name , type|
names.push(name)
end
names
end
@ -110,5 +108,8 @@ module Parfait
nil # stop resolve recursing up metaclasses
end
def hash
h = name.hash
end
end
end

View File

@ -17,7 +17,7 @@ class TestAttributes < MiniTest::Test
end
def test_message_name_nil
last = @type.instance_names.last
assert_equal :indexed_length , last
assert_equal :indexed_length , last , @type.instance_names.inspect
assert_nil @mess.name
end
def test_message_next_set

View File

@ -95,15 +95,42 @@ class TestList < MiniTest::Test
test_one_set2
assert_equal :some , @list.get(2)
end
def test_many_get
def set_shouldda
shouldda = { 1 => :one , 2 => :two , 3 => :three}
shouldda.each do |k,v|
@list.set(k,v)
end
shouldda
end
def test_many_get
shouldda = set_shouldda
shouldda.each do |k,v|
assert_equal v , @list.get(k)
end
end
def test_each
shouldda_values = set_shouldda.values
@list.each do |val|
shouldda_values.delete val
end
assert_equal 0 , shouldda_values.length
end
def test_each_pair_length
shouldda_values = set_shouldda.values
@list.each_pair do |key,val|
shouldda_values.delete key
shouldda_values.delete val
end
assert_equal 0 , shouldda_values.length
end
def test_each_pair_count
set_shouldda.values
counter = 0
@list.each_pair do |key,val|
counter += 1
end
assert_equal 2 , counter
end
def test_delete_at
test_many_get
assert @list.delete_at 2