summaryrefslogtreecommitdiffstats
path: root/spec/unit/util/confine_spec.rb
blob: 147c70d4059e904af4ecf4c0524833cb59fd1b61 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env ruby

require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

require 'facter/util/confine'
require 'facter/util/values'

include Facter::Util::Values

describe Facter::Util::Confine do
    it "should require a fact name" do
        Facter::Util::Confine.new("yay", true).fact.should == "yay"
    end

    it "should accept a value specified individually" do
        Facter::Util::Confine.new("yay", "test").values.should == ["test"]
    end

    it "should accept multiple values specified at once" do
        Facter::Util::Confine.new("yay", "test", "other").values.should == ["test", "other"]
    end

    it "should fail if no fact name is provided" do
        lambda { Facter::Util::Confine.new(nil, :test) }.should raise_error(ArgumentError)
    end

    it "should fail if no values were provided" do
        lambda { Facter::Util::Confine.new("yay") }.should raise_error(ArgumentError)
    end

    it "should have a method for testing whether it matches" do
        Facter::Util::Confine.new("yay", :test).should respond_to(:true?)
    end

    describe "when evaluating" do
        before do
            @confine = Facter::Util::Confine.new("yay", "one", "two", "Four", :xy, true, 1, [3,4])
            @fact = mock 'fact'
            Facter.stubs(:[]).returns @fact
        end

        it "should return false if the fact does not exist" do
            Facter.expects(:[]).with("yay").returns nil

            @confine.true?.should be_false
        end

        it "should use the returned fact to get the value" do
            Facter.expects(:[]).with("yay").returns @fact

            @fact.expects(:value).returns nil

            @confine.true?
        end

        it "should return false if the fact has no value" do
            @fact.stubs(:value).returns nil

            @confine.true?.should be_false
        end

        it "should return true if any of the provided values matches the fact's value" do
            @fact.stubs(:value).returns "two"

            @confine.true?.should be_true
        end

        it "should return true if any of the provided symbol values matches the fact's value" do
            @fact.stubs(:value).returns :xy

            @confine.true?.should be_true
        end

        it "should return true if any of the provided integer values matches the fact's value" do
            @fact.stubs(:value).returns 1

            @confine.true?.should be_true
        end

        it "should return true if any of the provided boolan values matches the fact's value" do
            @fact.stubs(:value).returns true

            @confine.true?.should be_true
        end

        it "should return true if any of the provided array values matches the fact's value" do
            @fact.stubs(:value).returns [3,4]

            @confine.true?.should be_true
        end

        it "should return true if any of the provided symbol values matches the fact's string value" do
            @fact.stubs(:value).returns :one

            @confine.true?.should be_true
        end

        it "should return true if any of the provided string values matches case-insensitive the fact's value" do
            @fact.stubs(:value).returns "four"

            @confine.true?.should be_true
        end

        it "should return true if any of the provided symbol values matches case-insensitive the fact's string value" do
            @fact.stubs(:value).returns :four

            @confine.true?.should be_true
        end

        it "should return true if any of the provided symbol values matches the fact's string value" do
            @fact.stubs(:value).returns :Xy

            @confine.true?.should be_true
        end

        it "should return false if none of the provided values matches the fact's value" do
            @fact.stubs(:value).returns "three"

            @confine.true?.should be_false
        end

        it "should return false if none of the provided integer values matches the fact's value" do
            @fact.stubs(:value).returns 2

            @confine.true?.should be_false
        end

        it "should return false if none of the provided boolan values matches the fact's value" do
            @fact.stubs(:value).returns false

            @confine.true?.should be_false
        end

        it "should return false if none of the provided array values matches the fact's value" do
            @fact.stubs(:value).returns [1,2]

            @confine.true?.should be_false
        end
    end
end