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