simpler but more verbose node model fixes everything but one

This commit is contained in:
Torsten Ruger 2014-08-18 08:19:48 +03:00
parent b0472753f4
commit a3c9ab7e29
5 changed files with 90 additions and 57 deletions

View File

@ -1,6 +1,6 @@
Array.class_eval do Array.class_eval do
def to_sof_node(writer , level) def to_sof_node(writer , level)
node = Sof::NodeList.new() node = Sof::ArrayNode.new()
each do |object| each do |object|
node.add writer.to_sof_node( object ) node.add writer.to_sof_node( object )
end end

View File

@ -1,11 +1,10 @@
Hash.class_eval do Hash.class_eval do
def to_sof_node(writer , level) def to_sof_node(writer , level)
node = Sof::NodeList.new() node = Sof::HashNode.new()
each do |key , object| each do |key , object|
k = key.to_sof() + ": " k = writer.to_sof_node(key )
v = writer.to_sof_node( object ) v = writer.to_sof_node( object )
v.data = "#{k}#{v.data}" node.add(k , v)
node.add v
end end
node node
end end

View File

@ -15,36 +15,67 @@ module Sof
end end
attr_accessor :data attr_accessor :data
def out io , level def out io , level
io.write(data) if data io.write(data)
end end
end end
class NodeList < SimpleNode class ArrayNode < Node
def initialize data = nil def initialize
super(data)
@children = [] @children = []
end end
attr_accessor :children attr_reader :children
def add c
def add child @children << c
child = SimpleNode.new(child) if(child.is_a? String)
@children << child
end end
def out io , level = 0 def out io , level = 0
super
return if @children.empty?
first = @children.first
io.write "-"
first.out(io , level + 1)
indent = " " * level indent = " " * level
@children.each_with_index do |child , i| @children.each_with_index do |child , i|
next if i == 0 # done already io.write "\n#{indent}" unless i == 0
io.write "\n"
io.write indent
io.write "-" io.write "-"
child.out(io , level + 1) child.out(io , level + 1)
end end
end end
end end
class ObjectNode < Node
def initialize data
@data = data
@children = []
end
attr_reader :children
attr_accessor :data
def add k , v
@children << [k,v]
end
def out io , level = 0
io.write(@data)
indent = " " * level
@children.each_with_index do |child , i|
k , v = child
io.write "\n#{indent}"
io.write ".."
k.out(io , level + 1)
v.out(io , level + 1)
end
end
end
class HashNode < Node
def initialize
@children = []
end
attr_reader :children
def add key , val
@children << [key,val]
end
def out io , level = 0
indent = " " * level
@children.each_with_index do |child , i|
key , val = child
io.write "\n#{indent}" unless i == 0
io.write "-"
key.out(io , level + 1)
io.write ": "
val.out(io , level + 1)
end
end
end
end end

View File

@ -30,13 +30,11 @@ module Sof
attributes = attributes_for(object) attributes = attributes_for(object)
immediate , extended = attributes.partition {|a| is_value?(get_value(object , a) ) } immediate , extended = attributes.partition {|a| is_value?(get_value(object , a) ) }
head += immediate.collect {|a| "#{a}: #{get_value(object , a).to_sof()}"}.join(", ") + ")" head += immediate.collect {|a| "#{a}: #{get_value(object , a).to_sof()}"}.join(", ") + ")"
ex = {} node = ObjectNode.new(head)
extended.each do |a| extended.each do |a|
val = get_value(object , a) val = get_value(object , a)
ex[a] = val node.add( write.to_sof_node(a) , writer.to_sof_node(val) )
end end
node = ex.to_sof_node(self,level+1)
node.data = head
node node
end end

View File

@ -14,60 +14,65 @@ end
OBJECT_STRING = "ObjectWithAttributes(name: 'some name', number: 1234)" OBJECT_STRING = "ObjectWithAttributes(name: 'some name', number: 1234)"
class BasicSof < MiniTest::Test class BasicSof < MiniTest::Test
def check should
same = (should == @out)
puts "Shouldda\n#{@out}" unless same
assert_equal should , @out
end
def test_true def test_true
out = Sof::Writer.write(true) @out = Sof::Writer.write(true)
assert_equal "true" , out check "true"
end end
def test_num def test_num
out = Sof::Writer.write(124) @out = Sof::Writer.write(124)
assert_equal "124" , out check "124"
end end
def test_object def test_simple_object
out = Sof::Writer.write(ObjectWithAttributes.new) @out = Sof::Writer.write(ObjectWithAttributes.new)
assert_equal "#{OBJECT_STRING}" , out check "#{OBJECT_STRING}"
end end
def test_object_extra_array def test_object_extra_array
out = Sof::Writer.write(ObjectWithAttributes.new.extra_array) @out = Sof::Writer.write(ObjectWithAttributes.new.extra_array)
assert_equal "#{OBJECT_STRING}" , out check "#{OBJECT_STRING}"
end end
def test_simple_array def test_simple_array
out = Sof::Writer.write([true, 1234]) @out = Sof::Writer.write([true, 1234])
assert_equal "-true\n-1234" , out check "-true\n-1234"
end end
def test_array_object def test_array_object
out = Sof::Writer.write([true, 1234 , ObjectWithAttributes.new]) @out = Sof::Writer.write([true, 1234 , ObjectWithAttributes.new])
assert_equal "-true\n-1234\n-#{OBJECT_STRING}" , out check "-true\n-1234\n-#{OBJECT_STRING}"
end end
def test_array_array def test_array_array
out = Sof::Writer.write([true, 1 , [true , 12 ]]) @out = Sof::Writer.write([true, 1 , [true , 12 ]])
assert_equal "-true\n-1\n--true\n -12" , out check "-true\n-1\n--true\n -12"
end end
def test_array_array_reverse def test_array_array_reverse
out = Sof::Writer.write([ [true , 12 ], true, 1]) @out = Sof::Writer.write([ [true , 12 ], true, 1])
assert_equal "--true\n -12\n-true\n-1" , out check "--true\n -12\n-true\n-1"
end end
def test_array_array_array def test_array_array_array
out = Sof::Writer.write([true, 1 , [true , 12 , [true , 123 ]]]) @out = Sof::Writer.write([true, 1 , [true , 12 , [true , 123 ]]])
assert_equal "-true\n-1\n--true\n -12\n --true\n -123" , out check "-true\n-1\n--true\n -12\n --true\n -123"
end end
def test_array_array_object def test_array_array_object
out = Sof::Writer.write([true, 1 , [true , 12 , ObjectWithAttributes.new]]) @out = Sof::Writer.write([true, 1 , [true , 12 , ObjectWithAttributes.new]])
assert_equal "-true\n-1\n--true\n -12\n -#{OBJECT_STRING}" , out check "-true\n-1\n--true\n -12\n -#{OBJECT_STRING}"
end end
def test_simple_hash def test_simple_hash
out = Sof::Writer.write({ one: 1 , tru: true }) @out = Sof::Writer.write({ one: 1 , tru: true })
assert_equal "-:one: 1\n-:tru: true" , out check "-:one: 1\n-:tru: true"
end end
def test_hash_object def test_hash_object
out = Sof::Writer.write({ one: 1 , two: ObjectWithAttributes.new }) @out = Sof::Writer.write({ one: 1 , two: ObjectWithAttributes.new })
assert_equal "-:one: 1\n-:two: #{OBJECT_STRING}" , out check "-:one: 1\n-:two: #{OBJECT_STRING}"
end end
def test_array_hash def test_array_hash
out = Sof::Writer.write([true, 1 , { one: 1 , tru: true }]) @out = Sof::Writer.write([true, 1 , { one: 1 , tru: true }])
assert_equal "-true\n-1\n--:one: 1\n -:tru: true" , out check "-true\n-1\n--:one: 1\n -:tru: true"
end end
def test_hash_array def test_hash_array
out = Sof::Writer.write({ one: [1 , ObjectWithAttributes.new] , two: true }) @out = Sof::Writer.write({ one: [1 , ObjectWithAttributes.new] , two: true })
assert_equal "-:one: -1\n -#{OBJECT_STRING}\n-:two: true" , out check "-:one: -1\n -#{OBJECT_STRING}\n-:two: true"
end end
end end