diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-01-26 23:11:09 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-01-26 23:11:09 -0800 |
commit | b79d64dd0bf00c4092b798f8b993d2ba780cc868 (patch) | |
tree | d1c8c99d33a6c63a29e8a16a425af4520d20eb0d /test | |
parent | 2fa4eb511d63d2f0c0924664c0eb646f76f4d9dd (diff) | |
download | psych-b79d64dd0bf00c4092b798f8b993d2ba780cc868.zip |
classes can be emitted as scalar and map
Diffstat (limited to 'test')
-rw-r--r-- | test/psych/test_coder.rb | 79 | ||||
-rw-r--r-- | test/psych/test_to_yaml_properties.rb | 55 | ||||
-rw-r--r-- | test/yaml/test_object.rb | 27 |
3 files changed, 106 insertions, 55 deletions
diff --git a/test/psych/test_coder.rb b/test/psych/test_coder.rb new file mode 100644 index 0000000..1ce3c4c --- /dev/null +++ b/test/psych/test_coder.rb @@ -0,0 +1,79 @@ +require 'minitest/autorun' +require 'psych' + +module Psych + class TestCoder < MiniTest::Unit::TestCase + class InitApi + attr_accessor :implicit + attr_accessor :style + attr_accessor :tag + attr_accessor :a, :b, :c + + def initialize + @a = 1 + @b = 2 + @c = 3 + end + + def init_with coder + @a = coder['aa'] + @b = coder['bb'] + @implicit = coder.implicit + @tag = coder.tag + @style = coder.style + end + + def encode_with coder + coder['aa'] = @a + coder['bb'] = @b + end + end + + class TaggingCoder < InitApi + def encode_with coder + super + coder.tag = coder.tag.sub(/!/, '!hello') + coder.implicit = false + coder.style = Psych::Nodes::Mapping::FLOW + end + end + + class ScalarCoder + def encode_with coder + coder.scalar = "foo" + end + end + + def test_scalar_coder + foo = Psych.load(Psych.dump(ScalarCoder.new)) + assert_equal 'foo', foo + end + + def test_load_dumped_tagging + foo = InitApi.new + bar = Psych.load(Psych.dump(foo)) + assert_equal false, bar.implicit + assert_equal "!ruby/object:Psych::TestCoder::InitApi", bar.tag + assert_equal Psych::Nodes::Mapping::BLOCK, bar.style + end + + def test_dump_with_tag + foo = TaggingCoder.new + assert_match(/hello/, Psych.dump(foo)) + assert_match(/\{aa/, Psych.dump(foo)) + end + + def test_dump_encode_with + foo = InitApi.new + assert_match(/aa/, Psych.dump(foo)) + end + + def test_dump_init_with + foo = InitApi.new + bar = Psych.load(Psych.dump(foo)) + assert_equal foo.a, bar.a + assert_equal foo.b, bar.b + assert_nil bar.c + end + end +end diff --git a/test/psych/test_to_yaml_properties.rb b/test/psych/test_to_yaml_properties.rb index 65b8e45..ff35fcd 100644 --- a/test/psych/test_to_yaml_properties.rb +++ b/test/psych/test_to_yaml_properties.rb @@ -16,61 +16,6 @@ module Psych end end - class InitApi < Foo - attr_accessor :implicit - attr_accessor :style - attr_accessor :tag - - def init_with coder - @a = coder['aa'] - @b = coder['bb'] - @implicit = coder.implicit - @tag = coder.tag - @style = coder.style - end - - def encode_with coder - coder['aa'] = @a - coder['bb'] = @b - end - end - - class TaggingCoder < InitApi - def encode_with coder - super - coder.tag = coder.tag.sub(/!/, '!hello') - coder.implicit = false - coder.style = Psych::Nodes::Mapping::FLOW - end - end - - def test_load_dumped_tagging - foo = InitApi.new - bar = Psych.load(Psych.dump(foo)) - assert_equal false, bar.implicit - assert_equal "!ruby/object:Psych::TestToYamlProperties::InitApi", bar.tag - assert_equal Psych::Nodes::Mapping::BLOCK, bar.style - end - - def test_dump_with_tag - foo = TaggingCoder.new - assert_match(/hello/, Psych.dump(foo)) - assert_match(/{aa/, Psych.dump(foo)) - end - - def test_dump_encode_with - foo = InitApi.new - assert_match(/aa/, Psych.dump(foo)) - end - - def test_dump_init_with - foo = InitApi.new - bar = Psych.load(Psych.dump(foo)) - assert_equal foo.a, bar.a - assert_equal foo.b, bar.b - assert_nil bar.c - end - def test_object_dump_yaml_properties foo = Psych.load(Psych.dump(Foo.new)) assert_equal 1, foo.a diff --git a/test/yaml/test_object.rb b/test/yaml/test_object.rb new file mode 100644 index 0000000..4f63d0e --- /dev/null +++ b/test/yaml/test_object.rb @@ -0,0 +1,27 @@ +require 'helper' + +module YAML + class Tagged + yaml_tag '!foo' + + attr_accessor :baz + + def initialize + @baz = 'bar' + end + end + + class TestObject < MiniTest::Unit::TestCase + def test_dump_with_tag + tag = Tagged.new + assert_match('foo', Psych.dump(tag)) + end + + def test_tag_round_trip + tag = Tagged.new + tag2 = Psych.load(Psych.dump(tag)) + assert_equal tag.baz, tag2.baz + assert_instance_of(Tagged, tag2) + end + end +end |