summaryrefslogtreecommitdiffstats
path: root/spec/unit/util/monkey_patches_spec.rb
blob: 4ff6438ce2205eb7e22de38610aa1c8db3090776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env ruby

require 'spec_helper'

require 'puppet/util/monkey_patches'



describe "yaml deserialization" do
  it "should call yaml_initialize when deserializing objects that have that method defined" do
    class Puppet::TestYamlInitializeClass
      attr_reader :foo

      def yaml_initialize(tag, var)
        var.should == {'foo' => 100}
        instance_variables.should == []
        @foo = 200
      end
    end

    obj = YAML.load("--- !ruby/object:Puppet::TestYamlInitializeClass\n  foo: 100")
    obj.foo.should == 200
  end

  it "should not call yaml_initialize if not defined" do
    class Puppet::TestYamlNonInitializeClass
      attr_reader :foo
    end

    obj = YAML.load("--- !ruby/object:Puppet::TestYamlNonInitializeClass\n  foo: 100")
    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