1 | require 'spec_helper'
|
---|
2 |
|
---|
3 | describe 'deep_merge' do
|
---|
4 | it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
|
---|
5 | it { is_expected.to run.with_params({ 'key' => 'value' }).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
|
---|
6 | it { is_expected.to run.with_params({}, '2').and_raise_error(Puppet::ParseError, /unexpected argument type String/) }
|
---|
7 | it { is_expected.to run.with_params({}, 2).and_raise_error(Puppet::ParseError, /unexpected argument type Fixnum/) }
|
---|
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 'should prefer values from the last hash' do
|
---|
17 | is_expected.to run \
|
---|
18 | .with_params(
|
---|
19 | {'key1' => 'value1', 'key2' => 'value2' },
|
---|
20 | {'key2' => 'replacement_value', 'key3' => 'value3'}) \
|
---|
21 | .and_return(
|
---|
22 | {'key1' => 'value1', 'key2' => 'replacement_value', 'key3' => 'value3'})
|
---|
23 | end
|
---|
24 | it { is_expected.to run \
|
---|
25 | .with_params({'key1' => 'value1'}, {'key1' => 'value2'}, {'key1' => 'value3'}) \
|
---|
26 | .and_return({'key1' => 'value3' })
|
---|
27 | }
|
---|
28 | end
|
---|
29 |
|
---|
30 | describe 'when arguments have subhashes' do
|
---|
31 | it { is_expected.to run \
|
---|
32 | .with_params({'key1' => 'value1'}, {'key2' => 'value2', 'key3' => {'subkey1' => 'value4'}}) \
|
---|
33 | .and_return( {'key1' => 'value1', 'key2' => 'value2', 'key3' => {'subkey1' => 'value4'}})
|
---|
34 | }
|
---|
35 | it { is_expected.to run \
|
---|
36 | .with_params({'key1' => {'subkey1' => 'value1'}}, {'key1' => {'subkey2' => 'value2'}}) \
|
---|
37 | .and_return( {'key1' => {'subkey1' => 'value1', 'subkey2' => 'value2'}})
|
---|
38 | }
|
---|
39 | it { is_expected.to run \
|
---|
40 | .with_params({'key1' => {'subkey1' => {'subsubkey1' => 'value1'}}}, {'key1' => {'subkey1' => {'subsubkey1' => 'value2'}}}) \
|
---|
41 | .and_return( {'key1' => {'subkey1' => {'subsubkey1' => 'value2'}}})
|
---|
42 | }
|
---|
43 | end
|
---|
44 |
|
---|
45 | it 'should not change the original hashes' do
|
---|
46 | argument1 = { 'key1' => 'value1' }
|
---|
47 | original1 = argument1.dup
|
---|
48 | argument2 = { 'key2' => 'value2' }
|
---|
49 | original2 = argument2.dup
|
---|
50 |
|
---|
51 | subject.call([argument1, argument2])
|
---|
52 | expect(argument1).to eq(original1)
|
---|
53 | expect(argument2).to eq(original2)
|
---|
54 | end
|
---|
55 | end
|
---|