diff --git a/lib/sof/hash.rb b/lib/sof/hash.rb index 6ab77b59..14e88aec 100644 --- a/lib/sof/hash.rb +++ b/lib/sof/hash.rb @@ -1,9 +1,12 @@ Hash.class_eval do def to_sof_node(members , level) - node = Sof::NodeList.new(nil) + node = Sof::NodeList.new() each do |key , object| k = key.to_sof() + ": " v = members.to_sof_node( object ) + v.data = "#{k}#{v.data}" + node.add v end + node end end diff --git a/lib/sof/node.rb b/lib/sof/node.rb index 059ebf58..a3606631 100644 --- a/lib/sof/node.rb +++ b/lib/sof/node.rb @@ -10,17 +10,18 @@ module Sof end class SimpleNode < Node - def initialize head - @head = head + def initialize data + @data = data end - attr_accessor :head + attr_accessor :data def out io , level - io.write(head) if head + io.write(data) if data end end - class NodeList < Node + class NodeList < SimpleNode def initialize + super(nil) @children = [] end attr_accessor :children @@ -31,6 +32,7 @@ module Sof end def out io , level = 0 + super return if @children.empty? first = @children.first io.write "-" diff --git a/test/sof.rb b/test/sof.rb index 4607b777..a4d29950 100644 --- a/test/sof.rb +++ b/test/sof.rb @@ -46,20 +46,16 @@ class BasicSof < MiniTest::Test out = Sof::Writer.write([true, 1 , [true , 12 , ObjectWithAttributes.new]]) assert_equal "-true\n-1\n--true\n -12\n -#{OBJECT_STRING}" , out end - def ttest_simple_hash + def test_simple_hash out = Sof::Writer.write({ one: 1 , tru: true }) - puts out - assert_equal "-:one 1\n-:tru true\n" , out + assert_equal "-:one: 1\n-:tru: true" , out end - def ttest_hash_object + def test_hash_object out = Sof::Writer.write({ one: 1 , two: ObjectWithAttributes.new }) - assert_equal "-:one 1\n-:two #{OBJECT_STRING}\n" , out + assert_equal "-:one: 1\n-:two: #{OBJECT_STRING}" , out end - def ttest_hash_array + def test_hash_array out = Sof::Writer.write({ one: [1 , ObjectWithAttributes.new] , two: true }) - puts "\n#{out}" - s = { :one => [1 , ObjectWithAttributes.new] , :two => true }.to_yaml - puts s - assert_equal "-:one 1\n-:two #{OBJECT_STRING}\n\n" , out + assert_equal "-:one: -1\n -#{OBJECT_STRING}\n-:two: true" , out end end \ No newline at end of file