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
def to_sof_node(writer , level)
node = Sof::NodeList.new()
node = Sof::ArrayNode.new()
each do |object|
node.add writer.to_sof_node( object )
end

View File

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

View File

@ -15,36 +15,67 @@ module Sof
end
attr_accessor :data
def out io , level
io.write(data) if data
io.write(data)
end
end
class NodeList < SimpleNode
def initialize data = nil
super(data)
class ArrayNode < Node
def initialize
@children = []
end
attr_accessor :children
def add child
child = SimpleNode.new(child) if(child.is_a? String)
@children << child
attr_reader :children
def add c
@children << c
end
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|
next if i == 0 # done already
io.write "\n"
io.write indent
io.write "\n#{indent}" unless i == 0
io.write "-"
child.out(io , level + 1)
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

View File

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

View File

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