Skip to content

Commit af96187

Browse files
committed
(maint) Fix stylistic issues
1 parent 489b0eb commit af96187

File tree

2 files changed

+62
-42
lines changed

2 files changed

+62
-42
lines changed

lib/puppet/functions/merge.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@
3636
# The iterative `merge()` has an advantage over doing the same with a general `reduce()` in that the constructed hash
3737
# does not have to be copied in each iteration and thus will perform much better with large inputs.
3838
#
39-
Puppet::Functions.create_function(:'merge') do
40-
39+
Puppet::Functions.create_function(:merge) do
4140
dispatch :merge2hashes do
42-
repeated_param 'Variant[Hash, Undef, String[0,0]]', :args # this strange type is backwards compatible
41+
repeated_param 'Variant[Hash, Undef, String[0,0]]', :args # this strange type is backwards compatible
4342
return_type 'Hash'
4443
end
4544

@@ -55,14 +54,13 @@
5554
return_type 'Hash'
5655
end
5756

58-
5957
def merge2hashes(*hashes)
6058
accumulator = {}
61-
hashes.each {|h| accumulator.merge!(h) if h.is_a?(Hash)}
59+
hashes.each { |h| accumulator.merge!(h) if h.is_a?(Hash) }
6260
accumulator
6361
end
6462

65-
def merge_iterable2(iterable, &block)
63+
def merge_iterable2(iterable)
6664
accumulator = {}
6765
enum = Puppet::Pops::Types::Iterable.asserted_iterable(self, iterable)
6866
enum.each do |v|
@@ -72,7 +70,7 @@ def merge_iterable2(iterable, &block)
7270
accumulator
7371
end
7472

75-
def merge_iterable3(iterable, &block)
73+
def merge_iterable3(iterable)
7674
accumulator = {}
7775
enum = Puppet::Pops::Types::Iterable.asserted_iterable(self, iterable)
7876
if enum.hash_style?
@@ -88,7 +86,7 @@ def merge_iterable3(iterable, &block)
8886
accumulator.merge!(r) if r.is_a?(Hash)
8987
index += 1
9088
end
91-
rescue StopIteration
89+
rescue StopIteration # rubocop:disable Lint/HandleExceptions
9290
end
9391
end
9492
accumulator

spec/functions/merge_spec.rb

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,24 @@
22

33
describe 'merge' do
44
it { is_expected.not_to eq(nil) }
5-
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"))) }
6-
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}) }
7-
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') }
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+
}
823

924
it { is_expected.to run.with_params.and_return({}) }
1025
it { is_expected.to run.with_params({}).and_return({}) }
@@ -14,6 +29,7 @@
1429
describe 'should accept empty strings as puppet undef' do
1530
it { is_expected.to run.with_params({}, '').and_return({}) }
1631
end
32+
1733
it { is_expected.to run.with_params({ 'key' => 'value' }, {}).and_return('key' => 'value') }
1834
it { is_expected.to run.with_params({}, 'key' => 'value').and_return('key' => 'value') }
1935
it { is_expected.to run.with_params({ 'key' => 'value1' }, 'key' => 'value2').and_return('key' => 'value2') }
@@ -23,36 +39,42 @@
2339
.and_return('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3')
2440
}
2541
describe 'should accept iterable and merge produced hashes' do
26-
27-
it { is_expected.to run \
28-
.with_params([1,2,3]) \
29-
.with_lambda {|hsh, val| { val => val } } \
30-
.and_return({ 1 => 1, 2 => 2, 3 => 3 }) }
31-
32-
it { is_expected.to run \
33-
.with_params([1,2,3]) \
34-
.with_lambda {|hsh, val| { val => val } unless val == 2} \
35-
.and_return({ 1 => 1, 3 => 3 }) }
36-
37-
it { is_expected.to run \
38-
.with_params([1,2,3]) \
39-
.with_lambda {|hsh, val| raise StopIteration.new if val == 3; { val => val } } \
40-
.and_return({ 1 => 1, 2 => 2 }) }
41-
42-
it { is_expected.to run \
43-
.with_params(['a', 'b', 'b', 'c', 'b']) \
44-
.with_lambda {|hsh, val| { val => (hsh[val] || 0) + 1 } } \
45-
.and_return({ 'a' => 1, 'b' => 3, 'c' => 1 }) }
46-
47-
it { is_expected.to run \
48-
.with_params(['a', 'b', 'c']) \
49-
.with_lambda {|hsh, idx, val| { idx => val } } \
50-
.and_return({ 0 => 'a', 1 => 'b', 2 => 'c'}) }
51-
52-
it { is_expected.to run \
53-
.with_params({'a' => 'A', 'b' => 'B', 'c' => 'C'}) \
54-
.with_lambda {|hsh, key, val| { key => "#{key}#{val}" } } \
55-
.and_return({ 'a' => 'aA', 'b' => 'bB', 'c' => 'cC'}) }
56-
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+
}
5779
end
5880
end

0 commit comments

Comments
 (0)