73c1bf5c3028dbbf0a69392f7fe1331c45ee8529
[packages/precise/mcollective.git] / spec / unit / validator_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4 require File.dirname(__FILE__) + '/../../plugins/mcollective/validator/array_validator.rb'
5
6 module MCollective
7   module Validator
8     describe "#load_validators" do
9       it "should not reload the plugins if the plugin cache has not expired" do
10         Validator.instance_variable_set(:@last_load, nil)
11         PluginManager.expects(:find_and_load).with("validator").once
12         Validator.load_validators
13         Validator.load_validators
14       end
15     end
16
17     describe "#[]" do
18       before do
19         Validator.load_validators
20       end
21
22       it "should return the correct class if klass is given as klass" do
23         result = Validator["array"]
24         result.should == ArrayValidator
25       end
26       it "should return the correct class if klass is given as KlassValidator" do
27         result = Validator["ArrayValidator"]
28         result.should == ArrayValidator
29       end
30       it "should return the correct class if klass is given as :klass" do
31         result = Validator[:array]
32         result.should == ArrayValidator
33       end
34     end
35
36     describe "#method_missing" do
37       it "should load a plugin if a validator method is called and the plugin exists" do
38         ArrayValidator.expects(:validate).with(2, [1,2,3])
39         result = Validator.array(2,[1,2,3])
40       end
41
42       it "should call super if a validator method is called and the plugin does not exist" do
43         expect{
44           Validator.rspec(1,2,3)
45         }.to raise_error(ValidatorError)
46       end
47     end
48
49     describe "#validator_class" do
50       it "should return the correct string for a given validator plugin name" do
51         result = Validator.validator_class("test")
52         result.should == "TestValidator"
53       end
54     end
55
56     describe "#has_validator?" do
57       it "should return true if the validator has been loaded" do
58         Validator.const_set(:TestValidator, Class)
59         Validator.has_validator?("test").should == true
60       end
61
62       it "should return false if the validator has not been loaded" do
63         Validator.has_validator?("test2").should == false
64       end
65     end
66   end
67 end