summaryrefslogtreecommitdiffstats
path: root/spec/unit/fact.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/unit/fact.rb')
-rwxr-xr-xspec/unit/fact.rb129
1 files changed, 129 insertions, 0 deletions
diff --git a/spec/unit/fact.rb b/spec/unit/fact.rb
new file mode 100755
index 0000000..3772d0f
--- /dev/null
+++ b/spec/unit/fact.rb
@@ -0,0 +1,129 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../spec_helper'
+
+require 'facter/fact'
+
+describe Facter::Fact do
+ it "should require a name" do
+ lambda { Facter::Fact.new }.should raise_error(ArgumentError)
+ end
+
+ it "should always downcase the name and convert it to a symbol" do
+ Facter::Fact.new("YayNess").name.should == :yayness
+ end
+
+ it "should default to its name converted to a string as its ldapname" do
+ Facter::Fact.new("YayNess").ldapname.should == "yayness"
+ end
+
+ it "should allow specifying the ldap name at initialization" do
+ Facter::Fact.new("YayNess", :ldapname => "fooness").ldapname.should == "fooness"
+ end
+
+ it "should fail if an unknown option is provided" do
+ lambda { Facter::Fact.new('yay', :foo => :bar) }.should raise_error(ArgumentError)
+ end
+
+ it "should have a method for adding resolution mechanisms" do
+ Facter::Fact.new("yay").should respond_to(:add)
+ end
+
+ describe "when adding resolution mechanisms" do
+ before do
+ @fact = Facter::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::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::Resolution.stubs(:new).returns @resolution
+
+ @fact.add { }
+ end
+
+ it "should re-sort the resolutions by length, so the most restricted resolutions are first" do
+ r1 = stub 'r1', :length => 1
+ r2 = stub 'r2', :length => 2
+ r3 = stub 'r3', :length => 0
+ Facter::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::Fact.new("yay").should respond_to(:value)
+ end
+
+ describe "when returning a value" do
+ before do
+ @fact = Facter::Fact.new("yay")
+ end
+
+ it "should return nil if there are no resolutions" do
+ Facter::Fact.new("yay").value.should be_nil
+ end
+
+ it "should return the first value returned by a resolution" do
+ r1 = stub 'r1', :length => 2, :value => nil, :suitable? => true
+ r2 = stub 'r2', :length => 1, :value => "yay", :suitable? => true
+ r3 = stub 'r3', :length => 0, :value => "foo", :suitable? => true
+ Facter::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', :length => 2, :value => "foo", :suitable? => true
+ r2 = stub 'r2', :length => 1, :suitable? => true # would fail if 'value' were asked for
+ Facter::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', :length => 2, :suitable? => false # would fail if 'value' were asked for'
+ r2 = stub 'r2', :length => 1, :value => "yay", :suitable? => true
+ Facter::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::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::Fact.new(:foo).should respond_to(:flush)
+ end
+end