summaryrefslogtreecommitdiff
path: root/test/psych/test_merge_keys.rb
blob: dcf4f1fce3ffaeedd2536246840c8b3c6bb94d71 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# frozen_string_literal: true
require_relative 'helper'

module Psych
  class TestMergeKeys < TestCase
    class Product
      attr_reader :bar
    end

    def test_merge_key_with_bare_hash
      doc = Psych.load <<-eodoc
map:
  <<:
    hello: world
      eodoc
      hash = { "map" => { "hello" => "world" } }
      assert_equal hash, doc
    end

    def test_merge_key_with_bare_hash_symbolized_names
      doc = Psych.load <<-eodoc, symbolize_names: true
map:
  <<:
    hello: world
      eodoc
      hash = { map: { hello: "world" } }
      assert_equal hash, doc
    end

    def test_roundtrip_with_chevron_key
      h = {}
      v = { 'a' => h, '<<' => h }
      assert_cycle v
    end

    def test_explicit_string
      doc = Psych.unsafe_load <<-eoyml
a: &me { hello: world }
b: { !!str '<<': *me }
eoyml
      expected = {
        "a" => { "hello" => "world" },
        "b" => {
          "<<" => { "hello" => "world" }
        }
      }
      assert_equal expected, doc
    end

    def test_mergekey_with_object
      s = <<-eoyml
foo: &foo
  bar: 10
product:
  !ruby/object:#{Product.name}
  <<: *foo
      eoyml
      hash = Psych.unsafe_load s
      assert_equal({"bar" => 10}, hash["foo"])
      product = hash["product"]
      assert_equal 10, product.bar
    end

    def test_merge_nil
      yaml = <<-eoyml
defaults: &defaults
development:
  <<: *defaults
      eoyml
      assert_equal({'<<' => nil }, Psych.unsafe_load(yaml)['development'])
    end

    def test_merge_array
      yaml = <<-eoyml
foo: &hello
- 1
baz:
  <<: *hello
      eoyml
      assert_equal({'<<' => [1]}, Psych.unsafe_load(yaml)['baz'])
    end

    def test_merge_is_not_partial
      yaml = <<-eoyml
default: &default
  hello: world
foo: &hello
- 1
baz:
  <<: [*hello, *default]
      eoyml
      doc = Psych.unsafe_load yaml
      refute doc['baz'].key? 'hello'
      assert_equal({'<<' => [[1], {"hello"=>"world"}]}, Psych.unsafe_load(yaml)['baz'])
    end

    def test_merge_seq_nil
      yaml = <<-eoyml
foo: &hello
baz:
  <<: [*hello]
      eoyml
      assert_equal({'<<' => [nil]}, Psych.unsafe_load(yaml)['baz'])
    end

    def test_bad_seq_merge
      yaml = <<-eoyml
defaults: &defaults [1, 2, 3]
development:
  <<: *defaults
      eoyml
      assert_equal({'<<' => [1,2,3]}, Psych.unsafe_load(yaml)['development'])
    end

    def test_missing_merge_key
      yaml = <<-eoyml
bar:
  << : *foo
      eoyml
      exp = assert_raise(Psych::BadAlias) { Psych.load yaml }
      assert_match 'foo', exp.message
    end

    # [ruby-core:34679]
    def test_merge_key
      yaml = <<-eoyml
foo: &foo
  hello: world
bar:
  << : *foo
  baz: boo
      eoyml

      hash = {
        "foo" => { "hello" => "world"},
        "bar" => { "hello" => "world", "baz" => "boo" } }
      assert_equal hash, Psych.unsafe_load(yaml)
    end

    def test_multiple_maps
      yaml = <<-eoyaml
---
- &CENTER { x: 1, y: 2 }
- &LEFT { x: 0, y: 2 }
- &BIG { r: 10 }
- &SMALL { r: 1 }

# All the following maps are equal:

- # Merge multiple maps
  << : [ *CENTER, *BIG ]
  label: center/big
      eoyaml

      hash = {
        'x' => 1,
        'y' => 2,
        'r' => 10,
        'label' => 'center/big'
      }

      assert_equal hash, Psych.unsafe_load(yaml)[4]
    end

    def test_override
      yaml = <<-eoyaml
---
- &CENTER { x: 1, y: 2 }
- &LEFT { x: 0, y: 2 }
- &BIG { r: 10 }
- &SMALL { r: 1 }

# All the following maps are equal:

- # Override
  << : [ *BIG, *LEFT, *SMALL ]
  x: 1
  label: center/big
      eoyaml

      hash = {
        'x' => 1,
        'y' => 2,
        'r' => 10,
        'label' => 'center/big'
      }

      assert_equal hash, Psych.unsafe_load(yaml)[4]
    end
  end
end