1require 'psych/helper' 2 3module Psych 4 class TestMergeKeys < TestCase 5 def test_merge_nil 6 yaml = <<-eoyml 7defaults: &defaults 8development: 9 <<: *defaults 10 eoyml 11 assert_equal({'<<' => nil }, Psych.load(yaml)['development']) 12 end 13 14 def test_merge_array 15 yaml = <<-eoyml 16foo: &hello 17- 1 18baz: 19 <<: *hello 20 eoyml 21 assert_equal({'<<' => [1]}, Psych.load(yaml)['baz']) 22 end 23 24 def test_merge_is_not_partial 25 yaml = <<-eoyml 26default: &default 27 hello: world 28foo: &hello 29- 1 30baz: 31 <<: [*hello, *default] 32 eoyml 33 doc = Psych.load yaml 34 refute doc['baz'].key? 'hello' 35 assert_equal({'<<' => [[1], {"hello"=>"world"}]}, Psych.load(yaml)['baz']) 36 end 37 38 def test_merge_seq_nil 39 yaml = <<-eoyml 40foo: &hello 41baz: 42 <<: [*hello] 43 eoyml 44 assert_equal({'<<' => [nil]}, Psych.load(yaml)['baz']) 45 end 46 47 def test_bad_seq_merge 48 yaml = <<-eoyml 49defaults: &defaults [1, 2, 3] 50development: 51 <<: *defaults 52 eoyml 53 assert_equal({'<<' => [1,2,3]}, Psych.load(yaml)['development']) 54 end 55 56 def test_missing_merge_key 57 yaml = <<-eoyml 58bar: 59 << : *foo 60 eoyml 61 exp = assert_raises(Psych::BadAlias) { Psych.load yaml } 62 assert_match 'foo', exp.message 63 end 64 65 # [ruby-core:34679] 66 def test_merge_key 67 yaml = <<-eoyml 68foo: &foo 69 hello: world 70bar: 71 << : *foo 72 baz: boo 73 eoyml 74 75 hash = { 76 "foo" => { "hello" => "world"}, 77 "bar" => { "hello" => "world", "baz" => "boo" } } 78 assert_equal hash, Psych.load(yaml) 79 end 80 81 def test_multiple_maps 82 yaml = <<-eoyaml 83--- 84- &CENTER { x: 1, y: 2 } 85- &LEFT { x: 0, y: 2 } 86- &BIG { r: 10 } 87- &SMALL { r: 1 } 88 89# All the following maps are equal: 90 91- # Merge multiple maps 92 << : [ *CENTER, *BIG ] 93 label: center/big 94 eoyaml 95 96 hash = { 97 'x' => 1, 98 'y' => 2, 99 'r' => 10, 100 'label' => 'center/big' 101 } 102 103 assert_equal hash, Psych.load(yaml)[4] 104 end 105 106 def test_override 107 yaml = <<-eoyaml 108--- 109- &CENTER { x: 1, y: 2 } 110- &LEFT { x: 0, y: 2 } 111- &BIG { r: 10 } 112- &SMALL { r: 1 } 113 114# All the following maps are equal: 115 116- # Override 117 << : [ *BIG, *LEFT, *SMALL ] 118 x: 1 119 label: center/big 120 eoyaml 121 122 hash = { 123 'x' => 1, 124 'y' => 2, 125 'r' => 10, 126 'label' => 'center/big' 127 } 128 129 assert_equal hash, Psych.load(yaml)[4] 130 end 131 end 132end 133