From ed192a04648db7786d072bef23ed72849115d9de Mon Sep 17 00:00:00 2001
From: Peter Souter
Date: Thu, 4 Dec 2014 14:12:55 +0000
Subject: (MODULES-444) Add specs for new behaviour
`concat` can now take multiple arguments
---
spec/functions/concat_spec.rb | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/spec/functions/concat_spec.rb b/spec/functions/concat_spec.rb
index 49cb2ad..4a18cd9 100755
--- a/spec/functions/concat_spec.rb
+++ b/spec/functions/concat_spec.rb
@@ -32,4 +32,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
--
cgit v1.2.3
From 7c570f75a5b88b1eb057bce3f7c4cad9cac83496 Mon Sep 17 00:00:00 2001
From: Peter Souter
Date: Thu, 4 Dec 2014 14:15:03 +0000
Subject: (MODULES-444) Acceptance test for primitives
`concat` should be able to concat arrays and primitives
---
spec/acceptance/concat_spec.rb | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/spec/acceptance/concat_spec.rb b/spec/acceptance/concat_spec.rb
index 7bda365..0d5e831 100755
--- a/spec/acceptance/concat_spec.rb
+++ b/spec/acceptance/concat_spec.rb
@@ -12,6 +12,17 @@ describe 'concat function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('oper
}
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
end
--
cgit v1.2.3
From 5e49c504580bf06353c841c51f1319a5bda893a8 Mon Sep 17 00:00:00 2001
From: Peter Souter
Date: Thu, 4 Dec 2014 14:15:33 +0000
Subject: (MODULES-444) Acceptance for multiple arrays
Acceptance test to take multiple arrays for concatenation
---
spec/acceptance/concat_spec.rb | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/spec/acceptance/concat_spec.rb b/spec/acceptance/concat_spec.rb
index 0d5e831..caf2f7d 100755
--- a/spec/acceptance/concat_spec.rb
+++ b/spec/acceptance/concat_spec.rb
@@ -23,6 +23,17 @@ describe 'concat function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('oper
}
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
--
cgit v1.2.3
From 7a1c4a6d9e4123a59fa85be18bd1b86ed5539b56 Mon Sep 17 00:00:00 2001
From: Peter Souter
Date: Thu, 4 Dec 2014 14:27:38 +0000
Subject: (MODULES-444) Change test to > 2 arguments
Also add extra test for just 1 argument
---
spec/functions/concat_spec.rb | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/spec/functions/concat_spec.rb b/spec/functions/concat_spec.rb
index 4a18cd9..d443c4b 100755
--- a/spec/functions/concat_spec.rb
+++ b/spec/functions/concat_spec.rb
@@ -4,8 +4,9 @@ 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
--
cgit v1.2.3
From 368c97f08046696d453afca6e1f8001b5bfc88a5 Mon Sep 17 00:00:00 2001
From: Peter Souter
Date: Thu, 4 Dec 2014 14:27:55 +0000
Subject: (MODULES-444) - Check for accepting > 2 args
---
spec/functions/concat_spec.rb | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/spec/functions/concat_spec.rb b/spec/functions/concat_spec.rb
index d443c4b..49fa6bb 100755
--- a/spec/functions/concat_spec.rb
+++ b/spec/functions/concat_spec.rb
@@ -13,6 +13,10 @@ describe "the concat function" 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']))
--
cgit v1.2.3
From 75a6186512b0d4d02593453fcc9ffe5e1b253107 Mon Sep 17 00:00:00 2001
From: Peter Souter
Date: Thu, 4 Dec 2014 14:32:23 +0000
Subject: (MODULES-444) Update docs with new functionality
---
lib/puppet/parser/functions/concat.rb | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb
index 0d35b07..4b53f59 100644
--- a/lib/puppet/parser/functions/concat.rb
+++ b/lib/puppet/parser/functions/concat.rb
@@ -4,15 +4,15 @@
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|
--
cgit v1.2.3
From 594c2dd38dc35a4f458ce511be9b7dd875915b44 Mon Sep 17 00:00:00 2001
From: Peter Souter
Date: Thu, 4 Dec 2014 14:33:23 +0000
Subject: (MODULES-444) Change argument restriction to < 2
---
lib/puppet/parser/functions/concat.rb | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb
index 4b53f59..8400f7b 100644
--- a/lib/puppet/parser/functions/concat.rb
+++ b/lib/puppet/parser/functions/concat.rb
@@ -16,9 +16,9 @@ Would result in:
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]
--
cgit v1.2.3
From 84bd98645f248a1dc3e0ed791e3af6f2ba9996fa Mon Sep 17 00:00:00 2001
From: Peter Souter
Date: Thu, 4 Dec 2014 14:34:25 +0000
Subject: (MODULES-444) - Real meat of the change
This is the core change, we now go through the array and add it to the first element, instead of just two arguments.
---
lib/puppet/parser/functions/concat.rb | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb
index 8400f7b..618e62d 100644
--- a/lib/puppet/parser/functions/concat.rb
+++ b/lib/puppet/parser/functions/concat.rb
@@ -21,14 +21,18 @@ Would result in:
"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
--
cgit v1.2.3