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 def add object , level
return if is_value?(object) return if is_value?(object)
if( occurence = @objects[object] ) if( occurence = @objects[object] )
#puts "reset level #{level} at #{occurence.level}"
occurence.level = level if occurence.level > level occurence.level = level if occurence.level > level
return return
end end

View File

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

View File

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