1require 'psych/helper' 2 3class PsychStructWithIvar < Struct.new(:foo) 4 attr_reader :bar 5 def initialize *args 6 super 7 @bar = 'hello' 8 end 9end 10 11module Psych 12 class TestStruct < TestCase 13 class StructSubclass < Struct.new(:foo) 14 def initialize foo, bar 15 super(foo) 16 @bar = bar 17 end 18 end 19 20 def test_self_referential_struct 21 ss = StructSubclass.new(nil, 'foo') 22 ss.foo = ss 23 24 loaded = Psych.load(Psych.dump(ss)) 25 assert_instance_of(StructSubclass, loaded.foo) 26 27 assert_equal(ss, loaded) 28 end 29 30 def test_roundtrip 31 thing = PsychStructWithIvar.new('bar') 32 struct = Psych.load(Psych.dump(thing)) 33 34 assert_equal 'hello', struct.bar 35 assert_equal 'bar', struct.foo 36 end 37 38 def test_load 39 obj = Psych.load(<<-eoyml) 40--- !ruby/struct:PsychStructWithIvar 41:foo: bar 42:@bar: hello 43 eoyml 44 45 assert_equal 'hello', obj.bar 46 assert_equal 'bar', obj.foo 47 end 48 end 49end 50