Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / deep_merge_spec.rb
1 require 'spec_helper'
2
3 describe 'deep_merge' do
4   it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
5   it { is_expected.to run.with_params('key' => 'value').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
6   it { is_expected.to run.with_params({}, '2').and_raise_error(Puppet::ParseError, %r{unexpected argument type String}) }
7   it { is_expected.to run.with_params({}, 2).and_raise_error(Puppet::ParseError, %r{unexpected argument}) }
8   it { is_expected.to run.with_params({}, '').and_return({}) }
9   it { is_expected.to run.with_params({}, {}).and_return({}) }
10   it { is_expected.to run.with_params({}, {}, {}).and_return({}) }
11   it { is_expected.to run.with_params({}, {}, {}, {}).and_return({}) }
12   it { is_expected.to run.with_params({ 'key' => 'value' }, '').and_return('key' => 'value') }
13   it { is_expected.to run.with_params({ 'key1' => 'value1' }, 'key2' => 'value2').and_return('key1' => 'value1', 'key2' => 'value2') }
14
15   describe 'when arguments have key collisions' do
16     it 'prefers values from the last hash' do
17       is_expected.to run \
18         .with_params({ 'key1' => 'value1', 'key2' => 'value2' }, 'key2' => 'replacement_value', 'key3' => 'value3') \
19         .and_return('key1' => 'value1', 'key2' => 'replacement_value', 'key3' => 'value3')
20     end
21     it {
22       is_expected.to run \
23         .with_params({ 'key1' => 'value1' }, { 'key1' => 'value2' }, 'key1' => 'value3') \
24         .and_return('key1' => 'value3')
25     }
26   end
27
28   describe 'when arguments have subhashes' do
29     it {
30       is_expected.to run \
31         .with_params({ 'key1' => 'value1' }, 'key2' => 'value2', 'key3' => { 'subkey1' => 'value4' }) \
32         .and_return('key1' => 'value1', 'key2' => 'value2', 'key3' => { 'subkey1' => 'value4' })
33     }
34     it {
35       is_expected.to run \
36         .with_params({ 'key1' => { 'subkey1' => 'value1' } }, 'key1' => { 'subkey2' => 'value2' }) \
37         .and_return('key1' => { 'subkey1' => 'value1', 'subkey2' => 'value2' })
38     }
39     it {
40       is_expected.to run \
41         .with_params({ 'key1' => { 'subkey1' => { 'subsubkey1' => 'value1' } } }, 'key1' => { 'subkey1' => { 'subsubkey1' => 'value2' } }) \
42         .and_return('key1' => { 'subkey1' => { 'subsubkey1' => 'value2' } })
43     }
44   end
45
46   arguments = { 'key1' => 'value1' }, { 'key2' => 'value2' }
47   originals = [arguments[0].dup, arguments[1].dup]
48   it 'does not change the original hashes' do
49     subject.execute(arguments[0], arguments[1])
50     arguments.each_with_index do |argument, index|
51       expect(argument).to eq(originals[index])
52     end
53   end
54
55   context 'with UTF8 and double byte characters' do
56     it { is_expected.to run.with_params({ 'ĸέỹ1' => 'ϋǻļủë1' }, 'この文字列' => '万').and_return('ĸέỹ1' => 'ϋǻļủë1', 'この文字列' => '万') }
57   end
58 end