summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHunter Haugen <hunter@puppetlabs.com>2014-12-17 09:24:51 -0800
committerHunter Haugen <hunter@puppetlabs.com>2014-12-17 09:24:51 -0800
commit8726caf996472c9c8822d1f697ff02a51abc54df (patch)
tree4d3da0a493ab38273d7c05272450ddeb2e3e604b
parent6237446582d71aa722b71ad8873f63c0662b20f7 (diff)
parent84bd98645f248a1dc3e0ed791e3af6f2ba9996fa (diff)
downloadpuppet-stdlib-8726caf996472c9c8822d1f697ff02a51abc54df.tar.gz
puppet-stdlib-8726caf996472c9c8822d1f697ff02a51abc54df.tar.bz2
Merge pull request #374 from petems/MODULES-444-add_concat_multiple
MODULES-444-Add concat multiple
-rw-r--r--lib/puppet/parser/functions/concat.rb18
-rwxr-xr-xspec/acceptance/concat_spec.rb22
-rwxr-xr-xspec/functions/concat_spec.rb17
3 files changed, 49 insertions, 8 deletions
diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb
index 0d35b07..618e62d 100644
--- a/lib/puppet/parser/functions/concat.rb
+++ b/lib/puppet/parser/functions/concat.rb
@@ -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
- result = a + Array(b)
+ result = a
+ arguments.shift
+
+ arguments.each do |x|
+ result = result + Array(x)
+ end
return result
end
diff --git a/spec/acceptance/concat_spec.rb b/spec/acceptance/concat_spec.rb
index 7bda365..caf2f7d 100755
--- a/spec/acceptance/concat_spec.rb
+++ b/spec/acceptance/concat_spec.rb
@@ -14,5 +14,27 @@ describe 'concat function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('oper
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 {
+ fail("${output} should have 9 elements.")
+ }
+ EOS
+
+ apply_manifest(pp, :catch_failures => true)
+ end
end
end
diff --git a/spec/functions/concat_spec.rb b/spec/functions/concat_spec.rb
index 49cb2ad..49fa6bb 100755
--- a/spec/functions/concat_spec.rb
+++ b/spec/functions/concat_spec.rb
@@ -4,14 +4,19 @@ require 'spec_helper'
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']))
@@ -32,4 +37,14 @@ describe "the concat function" do
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