summaryrefslogtreecommitdiff
path: root/test/psych/test_struct.rb
blob: 39e38f7dfd930a0f1f503884bc16f9dc9d05f761 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require 'psych/helper'

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)

      # FIXME: This seems to cause an infinite loop.  wtf.  Must report a bug
      # in ruby.
      # assert_equal(ss, loaded)
    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)
--- !ruby/struct:PsychStructWithIvar
:foo: bar
:@bar: hello
      eoyml

      assert_equal 'hello', obj.bar
      assert_equal 'bar', obj.foo
    end
  end
end