require 'test/unit'
require 'ostruct'
$:.unshift File.dirname(__FILE__) + "/../lib/"
require 'model_xml'
class TestStruct < OpenStruct
include ModelXML
end
class Parent < OpenStruct
include ModelXML
model_xml :foo, :child
end
class Child < OpenStruct
include ModelXML
model_xml :bar
end
class ModelXMLTest < Test::Unit::TestCase
def setup
@t = TestStruct.new :foo => 1, :bar => 2
end
def test_class_simple
TestStruct.instance_eval do
model_xml_reset!
model_xml :foo
end
assert_equal [[:foo]], TestStruct.model_xml_generator.field_sets
res = '
1
'
assert_equal res, @t.to_xml
end
def test_block_notation
TestStruct.instance_eval do
model_xml_reset!
model_xml do
foo
bar
end
end
assert_equal [[:foo, :bar]], TestStruct.model_xml_generator.field_sets
res = '
1
2
'
assert_equal res, @t.to_xml
end
def test_inline_procs
TestStruct.instance_eval do
model_xml_reset!
model_xml do
foo
bar
foobar Proc.new {|obj| obj.foo + obj.bar}
end
end
res = '
1
2
3
'
assert_equal res, @t.to_xml
end
def test_skip_instruct
TestStruct.instance_eval do
model_xml_reset!
model_xml :foo, :bar
end
res = '
1
2
'
assert_equal res, @t.to_xml(:skip_instruct => true)
end
def test_embedded_xml
p = Parent.new(:foo => 1, :child => Child.new(:bar => 2))
res = '
1
2
'
assert_equal res, p.to_xml
end
end