]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
(MODULE-2456) Modify union to accept more than two arrays
authorJetroid <holtjet@gmail.com>
Mon, 24 Aug 2015 11:01:29 +0000 (12:01 +0100)
committerJetroid <holtjet@gmail.com>
Mon, 24 Aug 2015 13:24:10 +0000 (14:24 +0100)
Add spec tests to test the new functionality:
 *Case for 3 arrays.
 *Case for 4 arrays.
Modify README to note new functionality.

This is for issue MODULE-2456, follow the precedent of MODULE-444.

This change allows union to be much more useful, unioning many arrays
in one line rather than in n lines. Additionally, as this is only added
functionality, and does not affect the 2 array case that all modules
currently using array are using, it should not affect any existing
modules utilizing union.

This is now useful, for example, for merging many arrays of resources
(eg: packages.) to generate just one list with no duplicates, to avoid
duplicate resource declarations.

README.markdown
lib/puppet/parser/functions/union.rb
spec/acceptance/union_spec.rb
spec/functions/union_spec.rb

index f949dcaf68559e4ff75b5ad0e2d62ebca0719af2..e71241f2a43d126e28a872adf4892d2fbe83b90c 100644 (file)
@@ -704,7 +704,7 @@ Returns the literal type when passed a value. Requires the new parser. Useful fo
 
 #### `union`
 
-Returns a union of two arrays, without duplicates. For example, `union(["a","b","c"],["b","c","d"])` returns ["a","b","c","d"].
+Returns a union of two or more arrays, without duplicates. For example, `union(["a","b","c"],["b","c","d"])` returns ["a","b","c","d"].
 
 #### `unique`
 
index c91bb805373e6695e34676611b155f77cfde3711..6c5bb8348cb8a2cc923c360c57b4d7a9a4d6efc6 100644 (file)
@@ -4,7 +4,7 @@
 
 module Puppet::Parser::Functions
   newfunction(:union, :type => :rvalue, :doc => <<-EOS
-This function returns a union of two arrays.
+This function returns a union of two or more arrays.
 
 *Examples:*
 
@@ -14,20 +14,15 @@ Would return: ["a","b","c","d"]
     EOS
   ) do |arguments|
 
-    # Two arguments are required
+    # Check that 2 or more arguments have been given ...
     raise(Puppet::ParseError, "union(): Wrong number of arguments " +
-      "given (#{arguments.size} for 2)") if arguments.size != 2
+      "given (#{arguments.size} for < 2)") if arguments.size < 2
 
-    first = arguments[0]
-    second = arguments[1]
-
-    unless first.is_a?(Array) && second.is_a?(Array)
-      raise(Puppet::ParseError, 'union(): Requires 2 arrays')
+    arguments.each do |argument|
+      raise(Puppet::ParseError, 'union(): Every parameter must be an array') unless argument.is_a?(Array)
     end
 
-    result = first | second
-
-    return result
+    arguments.reduce(:|)
   end
 end
 
index 6db8d0cf96f38fc5cf5fdc29d8a667da31b6ddd3..160fd7b09df1c1f199b93432ef73899895e656be 100755 (executable)
@@ -6,9 +6,10 @@ describe 'union function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('opera
     it 'unions arrays' do
       pp = <<-EOS
       $a = ["the","public"]
-      $b = ["art","galleries"]
+      $b = ["art"]
+      $c = ["galleries"]
       # Anagram: Large picture halls, I bet
-      $o = union($a,$b)
+      $o = union($a,$b,$c)
       notice(inline_template('union is <%= @o.inspect %>'))
       EOS
 
index 970e1fe5046dc29a7e7726fdabf8e0d7b623ffae..cfd38b6029024f52998f7fa255a316d6e71de86d 100755 (executable)
@@ -5,10 +5,9 @@ describe 'union' do
     it { is_expected.not_to eq(nil) }
     it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
     it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
-    it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
-    it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError, /Requires 2 arrays/) }
-    it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError, /Requires 2 arrays/) }
-    it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, /Requires 2 arrays/) }
+    it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError, /Every parameter must be an array/) }
+    it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError, /Every parameter must be an array/) }
+    it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, /Every parameter must be an array/) }
   end
 
   it { is_expected.to run.with_params([], []).and_return([]) }
@@ -19,5 +18,7 @@ describe 'union' do
   it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'three']).and_return(['one', 'two', 'three']) }
   it { is_expected.to run.with_params(['one', 'two', 'two', 'three'], ['two', 'three']).and_return(['one', 'two', 'three']) }
   it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'two', 'three']).and_return(['one', 'two', 'three']) }
+  it { is_expected.to run.with_params(['one', 'two'], ['two', 'three'], ['one', 'three']).and_return(['one', 'two', 'three']) }
+  it { is_expected.to run.with_params(['one', 'two'], ['three', 'four'], ['one', 'two', 'three'], ['four']).and_return(['one', 'two', 'three', 'four']) }
   it 'should not confuse types' do is_expected.to run.with_params(['1', '2', '3'], [1, 2]).and_return(['1', '2', '3', 1, 2]) end
 end