Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions lib/puppet/parser/functions/concat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,35 @@

module Puppet::Parser::Functions
newfunction(:concat, :type => :rvalue, :doc => <<-EOS
Appends the contents of array 2 onto array 1.
Appends the contents of multiple arrays into array 1.

*Example:*

concat(['1','2','3'],['4','5','6'])
concat(['1','2','3'],['4','5','6'],['7','8','9'])

Would result in:

['1','2','3','4','5','6']
['1','2','3','4','5','6','7','8','9']
EOS
) do |arguments|

# Check that 2 arguments have been given ...
# Check that more than 2 arguments have been given ...
raise(Puppet::ParseError, "concat(): Wrong number of arguments " +
"given (#{arguments.size} for 2)") if arguments.size != 2
"given (#{arguments.size} for < 2)") if arguments.size < 2

a = arguments[0]
b = arguments[1]

# Check that the first parameter is an array
unless a.is_a?(Array)
raise(Puppet::ParseError, 'concat(): Requires array to work with')
end
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we even do this check? We can just cast everything to an array... so it's silly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1


result = a + Array(b)
result = a
arguments.shift

arguments.each do |x|
result = result + Array(x)
end

return result
end
Expand Down
22 changes: 22 additions & 0 deletions spec/acceptance/concat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@
}
EOS

apply_manifest(pp, :catch_failures => true)
end
it 'should concat arrays and primitives to array' do
pp = <<-EOS
$output = concat(['1','2','3'],'4','5','6',['7','8','9'])
validate_array($output)
if size($output) != 9 {
fail("${output} should have 9 elements.")
}
EOS

apply_manifest(pp, :catch_failures => true)
end
it 'should concat multiple arrays to one' do
pp = <<-EOS
$output = concat(['1','2','3'],['4','5','6'],['7','8','9'])
validate_array($output)
if size($output) != 6 {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, this should be != 9

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petems yep, just found that, about to PR :)

fail("${output} should have 9 elements.")
}
EOS

apply_manifest(pp, :catch_failures => true)
end
end
Expand Down
17 changes: 16 additions & 1 deletion spec/functions/concat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
describe "the concat function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

it "should raise a ParseError if the client does not provide two arguments" do
it "should raise a ParseError if the client does not provide at least two arguments" do
expect { scope.function_concat([]) }.to(raise_error(Puppet::ParseError))
expect { scope.function_concat([[1]]) }.to(raise_error(Puppet::ParseError))
end

it "should raise a ParseError if the first parameter is not an array" do
expect { scope.function_concat([1, []])}.to(raise_error(Puppet::ParseError))
end

it "should not raise a ParseError if the client provides more than two arguments" do
expect { scope.function_concat([[1],[2],[3]]) }.not_to raise_error
end

it "should be able to concat an array" do
result = scope.function_concat([['1','2','3'],['4','5','6']])
expect(result).to(eq(['1','2','3','4','5','6']))
Expand All @@ -32,4 +37,14 @@
result = scope.function_concat([array_original,['4','5','6']])
array_original.should(eq(['1','2','3']))
end

it "should be able to concat multiple arrays" do
result = scope.function_concat([['1','2','3'],['4','5','6'],['7','8','9']])
expect(result).to(eq(['1','2','3','4','5','6','7','8','9']))
end

it "should be able to concat mix of primitives and arrays to a final array" do
result = scope.function_concat([['1','2','3'],'4',['5','6','7']])
expect(result).to(eq(['1','2','3','4','5','6','7']))
end
end