1require 'psych/helper'
2
3class ObjectWithInstanceVariables
4  attr_accessor :var1, :var2
5end
6
7class SubStringWithInstanceVariables < String
8  attr_accessor :var1
9end
10
11module Psych
12 class TestAliasAndAnchor < TestCase
13   def test_mri_compatibility
14     yaml = <<EOYAML
15---
16- &id001 !ruby/object {}
17
18- *id001
19- *id001
20EOYAML
21     result = Psych.load yaml
22     result.each {|el| assert_same(result[0], el) }
23   end
24
25   def test_mri_compatibility_object_with_ivars
26  yaml = <<EOYAML
27---
28- &id001 !ruby/object:ObjectWithInstanceVariables
29  var1: test1
30  var2: test2
31- *id001
32- *id001
33EOYAML
34
35     result = Psych.load yaml
36     result.each do |el|
37      assert_same(result[0], el)
38      assert_equal('test1', el.var1)
39      assert_equal('test2', el.var2)
40    end
41   end
42
43   def test_mri_compatibility_substring_with_ivars
44    yaml = <<EOYAML
45---
46- &id001 !str:SubStringWithInstanceVariables
47  str: test
48  "@var1": test
49- *id001
50- *id001
51EOYAML
52     result = Psych.load yaml
53     result.each do |el|
54      assert_same(result[0], el)
55      assert_equal('test', el.var1)
56    end
57   end
58
59   def test_anchor_alias_round_trip
60     o = Object.new
61     original = [o,o,o]
62
63     yaml = Psych.dump original
64     result = Psych.load yaml
65     result.each {|el| assert_same(result[0], el) }
66   end
67
68   def test_anchor_alias_round_trip_object_with_ivars
69     o = ObjectWithInstanceVariables.new
70     o.var1 = 'test1'
71     o.var2 = 'test2'
72     original = [o,o,o]
73
74     yaml = Psych.dump original
75     result = Psych.load yaml
76     result.each do |el|
77      assert_same(result[0], el)
78      assert_equal('test1', el.var1)
79      assert_equal('test2', el.var2)
80    end
81   end
82
83   def test_anchor_alias_round_trip_substring_with_ivars
84     o = SubStringWithInstanceVariables.new
85     o.var1 = 'test'
86     original = [o,o,o]
87
88     yaml = Psych.dump original
89     result = Psych.load yaml
90     result.each do |el|
91      assert_same(result[0], el)
92      assert_equal('test', el.var1)
93    end
94   end
95 end
96end
97