From 338c669a00880387945ef0a85f0a130403ef60d4 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 18 Aug 2014 13:33:40 +0300 Subject: [PATCH] fix level bug that became obvous with recursion --- lib/sof/members.rb | 1 + lib/sof/writer.rb | 7 ++++--- test/sof.rb | 15 ++++++++++----- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/sof/members.rb b/lib/sof/members.rb index e80fc421..33d0ff15 100644 --- a/lib/sof/members.rb +++ b/lib/sof/members.rb @@ -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 diff --git a/lib/sof/writer.rb b/lib/sof/writer.rb index c907fe96..3220749a 100644 --- a/lib/sof/writer.rb +++ b/lib/sof/writer.rb @@ -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 diff --git a/test/sof.rb b/test/sof.rb index f7e84c41..5a4fcf19 100644 --- a/test/sof.rb +++ b/test/sof.rb @@ -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 \ No newline at end of file