2017-07-14 02:15:58 -04:00
|
|
|
# frozen_string_literal: true
|
2013-03-26 10:55:04 -04:00
|
|
|
require_relative 'helper'
|
2011-01-21 21:34:50 -05:00
|
|
|
|
|
|
|
module Psych
|
|
|
|
class TestMergeKeys < TestCase
|
2013-11-05 14:15:40 -05:00
|
|
|
class Product
|
|
|
|
attr_reader :bar
|
|
|
|
end
|
|
|
|
|
2014-08-29 17:11:47 -04:00
|
|
|
def test_merge_key_with_bare_hash
|
|
|
|
doc = Psych.load <<-eodoc
|
|
|
|
map:
|
|
|
|
<<:
|
|
|
|
hello: world
|
|
|
|
eodoc
|
|
|
|
hash = { "map" => { "hello" => "world" } }
|
|
|
|
assert_equal hash, doc
|
2020-06-08 11:52:41 -04:00
|
|
|
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
|
2014-08-29 17:11:47 -04:00
|
|
|
end
|
|
|
|
|
2014-08-29 17:02:19 -04:00
|
|
|
def test_roundtrip_with_chevron_key
|
|
|
|
h = {}
|
|
|
|
v = { 'a' => h, '<<' => h }
|
|
|
|
assert_cycle v
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_explicit_string
|
2021-05-10 12:50:06 -04:00
|
|
|
doc = Psych.unsafe_load <<-eoyml
|
2014-08-29 17:02:19 -04:00
|
|
|
a: &me { hello: world }
|
|
|
|
b: { !!str '<<': *me }
|
|
|
|
eoyml
|
|
|
|
expected = {
|
|
|
|
"a" => { "hello" => "world" },
|
|
|
|
"b" => {
|
|
|
|
"<<" => { "hello" => "world" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_equal expected, doc
|
|
|
|
end
|
|
|
|
|
2013-11-05 14:15:40 -05:00
|
|
|
def test_mergekey_with_object
|
|
|
|
s = <<-eoyml
|
|
|
|
foo: &foo
|
|
|
|
bar: 10
|
|
|
|
product:
|
|
|
|
!ruby/object:#{Product.name}
|
|
|
|
<<: *foo
|
|
|
|
eoyml
|
2021-05-10 12:50:06 -04:00
|
|
|
hash = Psych.unsafe_load s
|
2013-11-05 14:15:40 -05:00
|
|
|
assert_equal({"bar" => 10}, hash["foo"])
|
|
|
|
product = hash["product"]
|
|
|
|
assert_equal 10, product.bar
|
|
|
|
end
|
|
|
|
|
2013-01-11 19:00:12 -05:00
|
|
|
def test_merge_nil
|
|
|
|
yaml = <<-eoyml
|
|
|
|
defaults: &defaults
|
|
|
|
development:
|
|
|
|
<<: *defaults
|
|
|
|
eoyml
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal({'<<' => nil }, Psych.unsafe_load(yaml)['development'])
|
2013-01-11 19:00:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_merge_array
|
|
|
|
yaml = <<-eoyml
|
|
|
|
foo: &hello
|
|
|
|
- 1
|
|
|
|
baz:
|
|
|
|
<<: *hello
|
|
|
|
eoyml
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal({'<<' => [1]}, Psych.unsafe_load(yaml)['baz'])
|
2013-01-11 19:00:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_merge_is_not_partial
|
|
|
|
yaml = <<-eoyml
|
|
|
|
default: &default
|
|
|
|
hello: world
|
|
|
|
foo: &hello
|
|
|
|
- 1
|
|
|
|
baz:
|
|
|
|
<<: [*hello, *default]
|
|
|
|
eoyml
|
2021-05-10 12:50:06 -04:00
|
|
|
doc = Psych.unsafe_load yaml
|
2013-01-11 19:00:12 -05:00
|
|
|
refute doc['baz'].key? 'hello'
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal({'<<' => [[1], {"hello"=>"world"}]}, Psych.unsafe_load(yaml)['baz'])
|
2013-01-11 19:00:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_merge_seq_nil
|
|
|
|
yaml = <<-eoyml
|
|
|
|
foo: &hello
|
|
|
|
baz:
|
|
|
|
<<: [*hello]
|
|
|
|
eoyml
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal({'<<' => [nil]}, Psych.unsafe_load(yaml)['baz'])
|
2013-01-11 19:00:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_bad_seq_merge
|
|
|
|
yaml = <<-eoyml
|
|
|
|
defaults: &defaults [1, 2, 3]
|
|
|
|
development:
|
|
|
|
<<: *defaults
|
|
|
|
eoyml
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal({'<<' => [1,2,3]}, Psych.unsafe_load(yaml)['development'])
|
2013-01-11 19:00:12 -05:00
|
|
|
end
|
|
|
|
|
2011-07-18 00:38:37 -04:00
|
|
|
def test_missing_merge_key
|
|
|
|
yaml = <<-eoyml
|
|
|
|
bar:
|
|
|
|
<< : *foo
|
|
|
|
eoyml
|
2021-05-10 06:09:17 -04:00
|
|
|
exp = assert_raise(Psych::BadAlias) { Psych.load yaml }
|
2011-07-18 00:38:37 -04:00
|
|
|
assert_match 'foo', exp.message
|
|
|
|
end
|
|
|
|
|
2011-01-21 21:34:50 -05:00
|
|
|
# [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" } }
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal hash, Psych.unsafe_load(yaml)
|
2011-01-21 21:34:50 -05:00
|
|
|
end
|
2011-01-21 21:51:14 -05:00
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal hash, Psych.unsafe_load(yaml)[4]
|
2011-01-21 21:51:14 -05:00
|
|
|
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'
|
|
|
|
}
|
|
|
|
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal hash, Psych.unsafe_load(yaml)[4]
|
2011-01-21 21:51:14 -05:00
|
|
|
end
|
2011-01-21 21:34:50 -05:00
|
|
|
end
|
|
|
|
end
|