2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
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
|
|
|
|
|
|
|
|
loaded = Psych.load(Psych.dump(ss))
|
|
|
|
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')
|
|
|
|
struct = Psych.load(Psych.dump(thing))
|
|
|
|
|
|
|
|
assert_equal 'hello', struct.bar
|
|
|
|
assert_equal 'bar', struct.foo
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_load
|
|
|
|
obj = Psych.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
|