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
|
#!/usr/bin/env ruby
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'facter/util/fact'
describe Facter::Util::Fact do
it "should require a name" do
lambda { Facter::Util::Fact.new }.should raise_error(ArgumentError)
end
it "should always downcase the name and convert it to a symbol" do
Facter::Util::Fact.new("YayNess").name.should == :yayness
end
it "should default to its name converted to a string as its ldapname" do
Facter::Util::Fact.new("YayNess").ldapname.should == "yayness"
end
it "should allow specifying the ldap name at initialization" do
Facter::Util::Fact.new("YayNess", :ldapname => "fooness").ldapname.should == "fooness"
end
it "should fail if an unknown option is provided" do
lambda { Facter::Util::Fact.new('yay', :foo => :bar) }.should raise_error(ArgumentError)
end
it "should have a method for adding resolution mechanisms" do
Facter::Util::Fact.new("yay").should respond_to(:add)
end
describe "when adding resolution mechanisms" do
before do
@fact = Facter::Util::Fact.new("yay")
@resolution = mock 'resolution'
@resolution.stub_everything
end
it "should fail if no block is given" do
lambda { @fact.add }.should raise_error(ArgumentError)
end
it "should create a new resolution instance" do
Facter::Util::Resolution.expects(:new).returns @resolution
@fact.add { }
end
it "should instance_eval the passed block on the new resolution" do
@resolution.expects(:instance_eval)
Facter::Util::Resolution.stubs(:new).returns @resolution
@fact.add { }
end
it "should re-sort the resolutions by weight, so the most restricted resolutions are first" do
r1 = stub 'r1', :weight => 1
r2 = stub 'r2', :weight => 2
r3 = stub 'r3', :weight => 0
Facter::Util::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3)
@fact.add { }
@fact.add { }
@fact.add { }
@fact.instance_variable_get("@resolves").should == [r2, r1, r3]
end
end
it "should be able to return a value" do
Facter::Util::Fact.new("yay").should respond_to(:value)
end
describe "when returning a value" do
before do
@fact = Facter::Util::Fact.new("yay")
end
it "should return nil if there are no resolutions" do
Facter::Util::Fact.new("yay").value.should be_nil
end
it "should return the first value returned by a resolution" do
r1 = stub 'r1', :weight => 2, :value => nil, :suitable? => true
r2 = stub 'r2', :weight => 1, :value => "yay", :suitable? => true
r3 = stub 'r3', :weight => 0, :value => "foo", :suitable? => true
Facter::Util::Resolution.expects(:new).times(3).returns(r1).returns(r2).returns(r3)
@fact.add { }
@fact.add { }
@fact.add { }
@fact.value.should == "yay"
end
it "should short-cut returning the value once one is found" do
r1 = stub 'r1', :weight => 2, :value => "foo", :suitable? => true
r2 = stub 'r2', :weight => 1, :suitable? => true # would fail if 'value' were asked for
Facter::Util::Resolution.expects(:new).times(2).returns(r1).returns(r2)
@fact.add { }
@fact.add { }
@fact.value
end
it "should skip unsuitable resolutions" do
r1 = stub 'r1', :weight => 2, :suitable? => false # would fail if 'value' were asked for'
r2 = stub 'r2', :weight => 1, :value => "yay", :suitable? => true
Facter::Util::Resolution.expects(:new).times(2).returns(r1).returns(r2)
@fact.add { }
@fact.add { }
@fact.value.should == "yay"
end
it "should return nil if the value is the empty string" do
r1 = stub 'r1', :suitable? => true, :value => ""
Facter::Util::Resolution.expects(:new).returns r1
@fact.add { }
@fact.value.should be_nil
end
end
it "should have a method for flushing the cached fact" do
Facter::Util::Fact.new(:foo).should respond_to(:flush)
end
end
|