[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, experimental, updated. debian/2.6.8-1-844-g7ec39d5

Matt Robinson matt at puppetlabs.com
Tue May 10 08:11:46 UTC 2011


The following commit has been merged in the experimental branch:
commit 3094d423c498640f01d267ca7589533f862e9ac4
Author: Matt Robinson <matt at puppetlabs.com>
Date:   Fri Apr 8 11:55:33 2011 -0700

    maint: Add Array combinations method
    
    Ruby < 1.8.7 doesn't have this method and we're using it in a test, so
    tests won't run on 1.8.6 until this is in place.
    
    It's probably a good thing to use much in implementation since it's
    written in pure Ruby when using < 1.8.7 and in C when in > 1.8.7, but
    then if you're using older Rubies you're probably not expecting much for
    performance anyway.
    
    Reviewed-by: Daniel Pittman <daniel at puppetlabs.com>

diff --git a/lib/puppet/util/monkey_patches.rb b/lib/puppet/util/monkey_patches.rb
index a93c66b..10a2684 100644
--- a/lib/puppet/util/monkey_patches.rb
+++ b/lib/puppet/util/monkey_patches.rb
@@ -90,3 +90,17 @@ if RUBY_VERSION == '1.8.1' || RUBY_VERSION == '1.8.2'
     r
   }
 end
+
+class Array
+  # Ruby < 1.8.7 doesn't have this method but we use it in tests
+  def combination(num)
+    return [] if num < 0 || num > size
+    return [[]] if num == 0
+    return map{|e| [e] } if num == 1
+    tmp = self.dup
+    self[0, size - (num - 1)].inject([]) do |ret, e|
+      tmp.shift
+      ret += tmp.combination(num - 1).map{|a| a.unshift(e) }
+    end
+  end unless method_defined? :combination
+end
diff --git a/spec/unit/util/monkey_patches_spec.rb b/spec/unit/util/monkey_patches_spec.rb
index 8bfcd90..fb2103e 100644
--- a/spec/unit/util/monkey_patches_spec.rb
+++ b/spec/unit/util/monkey_patches_spec.rb
@@ -31,3 +31,26 @@ describe "yaml deserialization" do
     obj.foo.should == 100
   end
 end
+
+# In Ruby > 1.8.7 this is a builtin, otherwise we monkey patch the method in
+describe "Array#combination" do
+  it "should fail if wrong number of arguments given" do
+    lambda { [1,2,3].combination() }.should raise_error(ArgumentError, /wrong number/)
+    lambda { [1,2,3].combination(1,2) }.should raise_error(ArgumentError, /wrong number/)
+  end
+
+  it "should return an empty array if combo size than array size or negative" do
+    [1,2,3].combination(4).to_a.should == []
+    [1,2,3].combination(-1).to_a.should == []
+  end
+
+  it "should return an empty array with an empty array if combo size == 0" do
+    [1,2,3].combination(0).to_a.should == [[]]
+  end
+
+  it "should all provide all combinations of size passed in" do
+    [1,2,3,4].combination(1).to_a.should == [[1], [2], [3], [4]]
+    [1,2,3,4].combination(2).to_a.should == [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
+    [1,2,3,4].combination(3).to_a.should == [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
+  end
+end

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list