Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / rspec / lib / spec / matchers / be.rb
blob0eb1629a64e53dc7ef6707a6a8a3216b26c5e729
1 module Spec
2   module Matchers
3     
4     class Be #:nodoc:
5       def initialize(*args)
6         @expected = parse_expected(args.shift)
7         @args = args
8         @comparison = ""
9       end
10       
11       def matches?(actual)
12         @actual = actual
13         return true if match_or_compare unless handling_predicate?
14         if handling_predicate?
15           begin
16             return @result = actual.__send__(predicate, *@args)
17           rescue => predicate_error
18             # This clause should be empty, but rcov will not report it as covered
19             # unless something (anything) is executed within the clause
20             rcov_error_report = "http://eigenclass.org/hiki.rb?rcov-0.8.0"
21           end
23           # This supports should_exist > target.exists? in the old world.
24           # We should consider deprecating that ability as in the new world
25           # you can't write "should exist" unless you have your own custom matcher.
26           begin
27             return @result = actual.__send__(present_tense_predicate, *@args)
28           rescue
29             raise predicate_error
30           end
31         end
32         return false
33       end
34       
35       def failure_message
36         return "expected #{@comparison}#{expected}, got #{@actual.inspect}" unless handling_predicate?
37         return "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}"
38       end
39       
40       def negative_failure_message
41         return "expected not #{expected}, got #{@actual.inspect}" unless handling_predicate?
42         return "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
43       end
44       
45       def expected
46         return true if @expected == :true
47         return false if @expected == :false
48         return "nil" if @expected == :nil
49         return @expected.inspect
50       end
51       
52       def match_or_compare
53         return @actual == true if @expected == :true
54         return @actual == false if @expected == :false
55         return @actual.nil? if @expected == :nil
56         return @actual < @expected if @less_than
57         return @actual <= @expected if @less_than_or_equal
58         return @actual >= @expected if @greater_than_or_equal
59         return @actual > @expected if @greater_than
60         return @actual == @expected if @double_equal
61         return @actual === @expected if @triple_equal
62         return @actual.equal?(@expected)
63       end
64       
65       def ==(expected)
66         @double_equal = true
67         @comparison = "== "
68         @expected = expected
69         self
70       end
72       def ===(expected)
73         @triple_equal = true
74         @comparison = "=== "
75         @expected = expected
76         self
77       end
79       def <(expected)
80         @less_than = true
81         @comparison = "< "
82         @expected = expected
83         self
84       end
86       def <=(expected)
87         @less_than_or_equal = true
88         @comparison = "<= "
89         @expected = expected
90         self
91       end
93       def >=(expected)
94         @greater_than_or_equal = true
95         @comparison = ">= "
96         @expected = expected
97         self
98       end
100       def >(expected)
101         @greater_than = true
102         @comparison = "> "
103         @expected = expected
104         self
105       end
106       
107       def description
108         "#{prefix_to_sentence}#{comparison}#{expected_to_sentence}#{args_to_sentence}"
109       end
111       private
112         def parse_expected(expected)
113           if Symbol === expected
114             @handling_predicate = true
115             ["be_an_","be_a_","be_"].each do |@prefix|
116               return "#{expected.to_s.sub(@prefix,"")}".to_sym if expected.starts_with?(@prefix)
117             end
118           end
119           @prefix = "be "
120           return expected
121         end
122         
123         def handling_predicate?
124           return false if [:true, :false, :nil].include?(@expected)
125           return @handling_predicate
126         end
128         def predicate
129           "#{@expected.to_s}?".to_sym
130         end
131         
132         def present_tense_predicate
133           "#{@expected.to_s}s?".to_sym
134         end
135         
136         def args_to_s
137           return "" if @args.empty?
138           inspected_args = @args.collect{|a| a.inspect}
139           return "(#{inspected_args.join(', ')})"
140         end
141         
142         def comparison
143           @comparison
144         end
145         
146         def expected_to_sentence
147           split_words(@expected)
148         end
149         
150         def prefix_to_sentence
151           split_words(@prefix)
152         end
154         def split_words(sym)
155           sym.to_s.gsub(/_/,' ')
156         end
158         def args_to_sentence
159           case @args.length
160             when 0
161               ""
162             when 1
163               " #{@args[0]}"
164             else
165               " #{@args[0...-1].join(', ')} and #{@args[-1]}"
166           end
167         end
168         
169     end
171     # :call-seq:
172     #   should be_true
173     #   should be_false
174     #   should be_nil
175     #   should be_arbitrary_predicate(*args)
176     #   should_not be_nil
177     #   should_not be_arbitrary_predicate(*args)
178     #
179     # Given true, false, or nil, will pass if actual is
180     # true, false or nil (respectively).
181     #
182     # Predicates are any Ruby method that ends in a "?" and returns true or false.
183     # Given be_ followed by arbitrary_predicate (without the "?"), RSpec will match
184     # convert that into a query against the target object.
185     #
186     # The arbitrary_predicate feature will handle any predicate
187     # prefixed with "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of)
188     # or "be_" (e.g. be_empty), letting you choose the prefix that best suits the predicate.
189     #
190     # == Examples 
191     #
192     #   target.should be_true
193     #   target.should be_false
194     #   target.should be_nil
195     #   target.should_not be_nil
196     #
197     #   collection.should be_empty #passes if target.empty?
198     #   "this string".should be_an_intance_of(String)
199     #
200     #   target.should_not be_empty #passes unless target.empty?
201     #   target.should_not be_old_enough(16) #passes unless target.old_enough?(16)
202     def be(*args)
203       Matchers::Be.new(*args)
204     end
205   end