fix level bug that became obvous with recursion

This commit is contained in:
Torsten Ruger 2014-08-18 13:33:40 +03:00
parent ad4aaaff8a
commit 338c669a00
3 changed files with 15 additions and 8 deletions

View File

@ -14,6 +14,7 @@ module Sof
def add object , level
return if is_value?(object)
if( occurence = @objects[object] )
#puts "reset level #{level} at #{occurence.level}"
occurence.level = level if occurence.level > level
return
end

View File

@ -19,12 +19,13 @@ module Sof
occurence = @members.objects[object]
raise "no object #{object}" unless occurence
if(level > occurence.level )
#puts "level #{level} at #{occurence.level}"
return SimpleNode.new("*#{occurence.number}")
end
if(object.respond_to? :to_sof_node) #mainly meant for arrays and hashes
object.to_sof_node(self , level + 1)
object.to_sof_node(self , level )
else
object_sof_node(object , level + 1 )
object_sof_node(object , level )
end
end
@ -36,7 +37,7 @@ module Sof
node = ObjectNode.new(head)
extended.each do |a|
val = get_value(object , a)
node.add( to_sof_node(a) , to_sof_node(val) )
node.add( to_sof_node(a,level + 1) , to_sof_node(val, level + 1) )
end
node
end

View File

@ -6,10 +6,7 @@ class ObjectWithAttributes
@name = "some name"
@number = 1234
end
def extra_array
@extra = [:sym , 123]
self
end
attr_accessor :extra
end
OBJECT_STRING = "ObjectWithAttributes(name: 'some name', number: 1234)"
@ -32,7 +29,9 @@ class BasicSof < MiniTest::Test
check "#{OBJECT_STRING}"
end
def test_object_extra_array
@out = Sof::Writer.write(ObjectWithAttributes.new.extra_array)
object = ObjectWithAttributes.new
object.extra = [:sym , 123]
@out = Sof::Writer.write(object)
check "#{OBJECT_STRING}\n :extra -:sym\n -123"
end
def test_simple_array
@ -81,4 +80,10 @@ class BasicSof < MiniTest::Test
@out = Sof::Writer.write(ar)
check "-true\n-1\n-*1"
end
def test_object_recursive
ar = [true, 1 ]
ar << ar
@out = Sof::Writer.write(ar)
check "-true\n-1\n-*1"
end
end