Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / merge_spec.rb
1 require 'spec_helper'
2
3 describe 'merge' do
4   it { is_expected.not_to eq(nil) }
5   it {
6     is_expected.to run \
7       .with_params({}, 'two') \
8       .and_raise_error(
9         ArgumentError, \
10         Regexp.new(Regexp.escape("rejected: parameter 'args' expects a value of type Undef, Hash, or String[0, 0], got String")),
11       )
12   }
13   it {
14     is_expected.to run \
15       .with_params({}, 1) \
16       .and_raise_error(ArgumentError, %r{parameter 'args' expects a value of type Undef, Hash, or String, got Integer})
17   }
18   it {
19     is_expected.to run \
20       .with_params({ 'one' => 1, 'three' => { 'four' => 4 } }, 'two' => 'dos', 'three' => { 'five' => 5 }) \
21       .and_return('one' => 1, 'three' => { 'five' => 5 }, 'two' => 'dos')
22   }
23
24   it { is_expected.to run.with_params.and_return({}) }
25   it { is_expected.to run.with_params({}).and_return({}) }
26   it { is_expected.to run.with_params({}, {}).and_return({}) }
27   it { is_expected.to run.with_params({}, {}, {}).and_return({}) }
28
29   describe 'should accept empty strings as puppet undef' do
30     it { is_expected.to run.with_params({}, '').and_return({}) }
31   end
32
33   it { is_expected.to run.with_params({ 'key' => 'value' }, {}).and_return('key' => 'value') }
34   it { is_expected.to run.with_params({}, 'key' => 'value').and_return('key' => 'value') }
35   it { is_expected.to run.with_params({ 'key' => 'value1' }, 'key' => 'value2').and_return('key' => 'value2') }
36   it {
37     is_expected.to run \
38       .with_params({ 'key1' => 'value1' }, { 'key2' => 'value2' }, 'key3' => 'value3') \
39       .and_return('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3')
40   }
41   describe 'should accept iterable and merge produced hashes' do
42     it {
43       is_expected.to run \
44         .with_params([1, 2, 3]) \
45         .with_lambda { |_hsh, val| { val => val } } \
46         .and_return(1 => 1, 2 => 2, 3 => 3)
47     }
48     it {
49       is_expected.to run \
50         .with_params([1, 2, 3]) \
51         .with_lambda { |_hsh, val| { val => val } unless val == 2 } \
52         .and_return(1 => 1, 3 => 3)
53     }
54     it {
55       is_expected.to run \
56         .with_params([1, 2, 3]) \
57         # rubocop:disable Style/Semicolon
58         .with_lambda { |_hsh, val| raise StopIteration if val == 3; { val => val } } \
59         .and_return(1 => 1, 2 => 2)
60     }
61     it {
62       is_expected.to run \
63         .with_params(['a', 'b', 'b', 'c', 'b']) \
64         .with_lambda { |hsh, val| { val => (hsh[val] || 0) + 1 } } \
65         .and_return('a' => 1, 'b' => 3, 'c' => 1)
66     }
67     it {
68       is_expected.to run \
69         .with_params(['a', 'b', 'c']) \
70         .with_lambda { |_hsh, idx, val| { idx => val } } \
71         .and_return(0 => 'a', 1 => 'b', 2 => 'c')
72     }
73     it {
74       is_expected.to run \
75         .with_params('a' => 'A', 'b' => 'B', 'c' => 'C') \
76         .with_lambda { |_hsh, key, val| { key => "#{key}#{val}" } } \
77         .and_return('a' => 'aA', 'b' => 'bB', 'c' => 'cC')
78     }
79   end
80 end