summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMarc-Andre Lafortune <github@marc-andre.ca>2020-09-26 01:27:23 -0400
committerMarc-André Lafortune <github@marc-andre.ca>2020-09-30 18:11:24 -0400
commitb36a45c05cafc227ade3b59349482953321d6a89 (patch)
treed166ac4aa87cab243740e599dc186c23e2c4ce1a /test
parent0e93118c44fc4128bcacfe1dc6702c84a84b862b (diff)
[ruby/ostruct] Improved YAML serialization.
Patch adapted from Pietro Monteiro [Fixes bug#8382]
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3593
Diffstat (limited to 'test')
-rw-r--r--test/ostruct/test_ostruct.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb
index 5de6f278d0..7dd1ac626b 100644
--- a/test/ostruct/test_ostruct.rb
+++ b/test/ostruct/test_ostruct.rb
@@ -1,6 +1,7 @@
# frozen_string_literal: true
require 'test/unit'
require 'ostruct'
+require 'yaml'
class TC_OpenStruct < Test::Unit::TestCase
def test_initialize
@@ -309,4 +310,24 @@ class TC_OpenStruct < Test::Unit::TestCase
end.take
assert obj1.object_id == obj2.object_id
end if defined?(Ractor)
+
+ def test_legacy_yaml
+ s = "--- !ruby/object:OpenStruct\ntable:\n :foo: 42\n"
+ o = YAML.load(s)
+ assert_equal(42, o.foo)
+
+ o = OpenStruct.new(table: {foo: 42})
+ assert_equal({foo: 42}, YAML.load(YAML.dump(o)).table)
+ end
+
+ def test_yaml
+ h = {name: "John Smith", age: 70, pension: 300.42}
+ yaml = "--- !ruby/object:OpenStruct\nname: John Smith\nage: 70\npension: 300.42\n"
+ os1 = OpenStruct.new(h)
+ os2 = YAML.load(os1.to_yaml)
+ assert_equal yaml, os1.to_yaml
+ assert_equal os1, os2
+ assert_equal true, os1.eql?(os2)
+ assert_equal 300.42, os2.pension
+ end
end