summaryrefslogtreecommitdiff
path: root/test/visitors
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2009-10-07 11:52:54 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2009-10-07 11:52:54 -0700
commitc330b9e93fdf5eb15aea29ede9dd017c037b3942 (patch)
treef91933139de909a5addde8a39379189ade17f356 /test/visitors
parentd3bfc7a71256f84d4ce1b7e31b032739f02dfb55 (diff)
downloadpsych-c330b9e93fdf5eb15aea29ede9dd017c037b3942.zip
structs will round trip
Diffstat (limited to 'test/visitors')
-rw-r--r--test/visitors/test_to_ruby.rb36
-rw-r--r--test/visitors/test_yast_builder.rb12
2 files changed, 48 insertions, 0 deletions
diff --git a/test/visitors/test_to_ruby.rb b/test/visitors/test_to_ruby.rb
index e8b7a6e..9c37690 100644
--- a/test/visitors/test_to_ruby.rb
+++ b/test/visitors/test_to_ruby.rb
@@ -11,6 +11,42 @@ module Psych
@visitor = ToRuby.new
end
+ A = Struct.new(:foo)
+
+ def test_struct
+ s = A.new('bar')
+
+ mapping = Nodes::Mapping.new nil, "!ruby/struct:#{s.class}"
+ mapping.children << Nodes::Scalar.new('foo')
+ mapping.children << Nodes::Scalar.new('bar')
+
+ ruby = mapping.to_ruby
+
+ assert_equal s.class, ruby.class
+ assert_equal s.foo, ruby.foo
+ assert_equal s, ruby
+ end
+
+ def test_anon_struct_legacy
+ s = Struct.new(:foo).new('bar')
+
+ mapping = Nodes::Mapping.new nil, '!ruby/struct:'
+ mapping.children << Nodes::Scalar.new('foo')
+ mapping.children << Nodes::Scalar.new('bar')
+
+ assert_equal s.foo, mapping.to_ruby.foo
+ end
+
+ def test_anon_struct
+ s = Struct.new(:foo).new('bar')
+
+ mapping = Nodes::Mapping.new nil, '!ruby/struct'
+ mapping.children << Nodes::Scalar.new('foo')
+ mapping.children << Nodes::Scalar.new('bar')
+
+ assert_equal s.foo, mapping.to_ruby.foo
+ end
+
def test_exception
exc = Exception.new 'hello'
diff --git a/test/visitors/test_yast_builder.rb b/test/visitors/test_yast_builder.rb
index d35ff3d..4730e7d 100644
--- a/test/visitors/test_yast_builder.rb
+++ b/test/visitors/test_yast_builder.rb
@@ -8,6 +8,18 @@ module Psych
@v = Visitors::YASTBuilder.new
end
+ A = Struct.new(:foo)
+
+ def test_struct
+ assert_round_trip A.new('bar')
+ end
+
+ def test_struct_anon
+ s = Struct.new(:foo).new('bar')
+ obj = Psych.load(Psych.dump(s))
+ assert_equal s.foo, obj.foo
+ end
+
def test_exception
ex = Exception.new 'foo'
loaded = Psych.load(Psych.dump(ex))