Skip to content

Commit 35bf5fd

Browse files
author
Martin Foot
committed
Allow concat to take non-array second parameters
Also improve and extend concat tests to lock down functionality
1 parent ab98142 commit 35bf5fd

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

README.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ Would result in:
145145

146146
['1','2','3','4','5','6']
147147

148+
concat(['1','2','3'],'4')
149+
150+
Would result in:
151+
152+
['1','2','3','4']
148153

149154
- *Type*: rvalue
150155

lib/puppet/parser/functions/concat.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ module Puppet::Parser::Functions
2323
a = arguments[0]
2424
b = arguments[1]
2525

26-
# Check that both args are arrays.
27-
unless a.is_a?(Array) and b.is_a?(Array)
26+
# Check that the first parameter is an array
27+
unless a.is_a?(Array)
2828
raise(Puppet::ParseError, 'concat(): Requires array to work with')
2929
end
3030

31-
result = a.concat(b)
31+
if b.is_a?(Array)
32+
result = a.concat(b)
33+
else
34+
result = a << b
35+
end
3236

3337
return result
3438
end

spec/unit/puppet/parser/functions/concat_spec.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,27 @@
44
describe "the concat function" do
55
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
66

7-
it "should raise a ParseError if there is less than 1 arguments" do
8-
lambda { scope.function_concat([]) }.should( raise_error(Puppet::ParseError))
7+
it "should raise a ParseError if the client does not provide two arguments" do
8+
lambda { scope.function_concat([]) }.should(raise_error(Puppet::ParseError))
9+
end
10+
11+
it "should raise a ParseError if the first parameter is not an array" do
12+
lambda { scope.function_concat([1, []])}.should(raise_error(Puppet::ParseError))
913
end
1014

1115
it "should be able to concat an array" do
1216
result = scope.function_concat([['1','2','3'],['4','5','6']])
1317
result.should(eq(['1','2','3','4','5','6']))
1418
end
19+
20+
it "should be able to concat a primitive to an array" do
21+
result = scope.function_concat([['1','2','3'],'4'])
22+
result.should(eq(['1','2','3','4']))
23+
end
24+
25+
it "should not accidentally flatten nested arrays" do
26+
result = scope.function_concat([['1','2','3'],[['4','5'],'6']])
27+
result.should(eq(['1','2','3',['4','5'],'6']))
28+
end
29+
1530
end

0 commit comments

Comments
 (0)