2013-03-26 18:55:04 +04:00
|
|
|
require_relative 'helper'
|
2010-03-29 01: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-17 05:42:54 +04:00
|
|
|
assert_equal(ss, loaded)
|
2010-03-29 01: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 15:55:52 +04:00
|
|
|
--- !ruby/struct:PsychStructWithIvar
|
2010-03-29 01:49:37 +04:00
|
|
|
:foo: bar
|
|
|
|
:@bar: hello
|
|
|
|
eoyml
|
|
|
|
|
|
|
|
assert_equal 'hello', obj.bar
|
|
|
|
assert_equal 'bar', obj.foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|