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