2017-07-14 02:15:58 -04:00
|
|
|
# frozen_string_literal: true
|
2013-03-26 10:55:04 -04:00
|
|
|
require_relative 'helper'
|
2010-03-28 17:49:37 -04:00
|
|
|
|
|
|
|
class PsychStructWithIvar < Struct.new(:foo)
|
|
|
|
attr_reader :bar
|
|
|
|
def initialize *args
|
|
|
|
super
|
|
|
|
@bar = 'hello'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Psych
|
|
|
|
class TestStruct < TestCase
|
|
|
|
class StructSubclass < Struct.new(:foo)
|
|
|
|
def initialize foo, bar
|
|
|
|
super(foo)
|
|
|
|
@bar = bar
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_self_referential_struct
|
|
|
|
ss = StructSubclass.new(nil, 'foo')
|
|
|
|
ss.foo = ss
|
|
|
|
|
2021-05-10 12:50:06 -04:00
|
|
|
loaded = Psych.unsafe_load(Psych.dump(ss))
|
2010-03-28 17:49:37 -04:00
|
|
|
assert_instance_of(StructSubclass, loaded.foo)
|
|
|
|
|
2011-11-16 20:42:54 -05:00
|
|
|
assert_equal(ss, loaded)
|
2010-03-28 17:49:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_roundtrip
|
|
|
|
thing = PsychStructWithIvar.new('bar')
|
2021-05-10 12:50:06 -04:00
|
|
|
struct = Psych.unsafe_load(Psych.dump(thing))
|
2010-03-28 17:49:37 -04:00
|
|
|
|
|
|
|
assert_equal 'hello', struct.bar
|
|
|
|
assert_equal 'bar', struct.foo
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_load
|
2021-05-10 12:50:06 -04:00
|
|
|
obj = Psych.unsafe_load(<<-eoyml)
|
2011-05-15 07:55:52 -04:00
|
|
|
--- !ruby/struct:PsychStructWithIvar
|
2010-03-28 17:49:37 -04:00
|
|
|
:foo: bar
|
|
|
|
:@bar: hello
|
|
|
|
eoyml
|
|
|
|
|
|
|
|
assert_equal 'hello', obj.bar
|
|
|
|
assert_equal 'bar', obj.foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|