9ea22cdddab826be5954a205ce1b868d1707358e
[packages/precise/mcollective.git] / spec / unit / data / result_spec.rb
1 #!/usr/bin/env rspec
2
3 require 'spec_helper'
4
5 module MCollective
6   module Data
7     describe Result do
8       before(:each) do
9         @result = Result.new
10       end
11
12       describe "#[]=" do
13         it "should only allow trusted types of data to be saved" do
14           expect { @result["rspec"] = Time.now }.to raise_error
15           @result["rspec"] = 1
16           @result["rspec"] = 1.1
17           @result["rspec"] = "rspec"
18           @result["rspec"] = true
19           @result["rspec"] = false
20         end
21
22         it "should set the correct value" do
23           @result["rspec"] = "rspec value"
24           @result.instance_variable_get("@data").should == {:rspec => "rspec value"}
25         end
26
27         it "should only allow valid data types" do
28           expect { @result["rspec"] = Time.now }.to raise_error(/Can only store .+ data but got Time for key rspec/)
29         end
30       end
31
32       describe "#include" do
33         it "should return the correct list of keys" do
34           @result["x"] = "1"
35           @result[:y] = "2"
36           @result.keys.sort.should == [:x, :y]
37         end
38       end
39
40       describe "#include?" do
41         it "should correctly report that a key is present or absent" do
42           @result.include?("rspec").should == false
43           @result.include?(:rspec).should == false
44           @result["rspec"] = "rspec"
45           @result.include?("rspec").should == true
46           @result.include?(:rspec).should == true
47         end
48       end
49
50       describe "#[]" do
51         it "should retrieve the correct information" do
52           @result["rspec"].should == nil
53           @result[:rspec].should == nil
54           @result["rspec"] = "rspec value"
55           @result["rspec"].should == "rspec value"
56           @result[:rspec].should == "rspec value"
57         end
58       end
59
60       describe "#method_missing" do
61         it "should raise the correct exception for unknown keys" do
62           expect { @result.nosuchdata }.to raise_error(NoMethodError)
63         end
64
65         it "should retrieve the correct data" do
66           @result["rspec"] = "rspec value"
67           @result.rspec.should == "rspec value"
68         end
69       end
70     end
71   end
72 end