7 @expected = :satisfy_if
9 @expected = parse_expected(args.shift)
17 if handling_predicate?
19 return @result = actual.__send__(predicate, *@args)
20 rescue => predicate_error
21 # This clause should be empty, but rcov will not report it as covered
22 # unless something (anything) is executed within the clause
23 rcov_error_report = "http://eigenclass.org/hiki.rb?rcov-0.8.0"
26 # This supports should_exist > target.exists? in the old world.
27 # We should consider deprecating that ability as in the new world
28 # you can't write "should exist" unless you have your own custom matcher.
30 return @result = actual.__send__(present_tense_predicate, *@args)
35 return match_or_compare
40 return "expected #{@comparison}#{expected}, got #{@actual.inspect}" unless handling_predicate?
41 return "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}"
44 def negative_failure_message
45 return "expected not #{expected}, got #{@actual.inspect}" unless handling_predicate?
46 return "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
50 return "if to be satisfied" if @expected == :satisfy_if
51 return true if @expected == :true
52 return false if @expected == :false
53 return "nil" if @expected == :nil
54 return @expected.inspect
58 return @actual ? true : false if @expected == :satisfy_if
59 return @actual == true if @expected == :true
60 return @actual == false if @expected == :false
61 return @actual.nil? if @expected == :nil
62 return @actual < @expected if @less_than
63 return @actual <= @expected if @less_than_or_equal
64 return @actual >= @expected if @greater_than_or_equal
65 return @actual > @expected if @greater_than
66 return @actual == @expected if @double_equal
67 return @actual === @expected if @triple_equal
68 return @actual.equal?(@expected)
97 @less_than_or_equal = true
105 @greater_than_or_equal = true
120 "#{prefix_to_sentence}#{comparison}#{expected_to_sentence}#{args_to_sentence}"
124 def parse_expected(expected)
125 if Symbol === expected
126 @handling_predicate = true
127 ["be_an_","be_a_","be_"].each do |prefix|
128 if expected.starts_with?(prefix)
130 return "#{expected.to_s.sub(@prefix,"")}".to_sym
138 def handling_predicate?
139 return false if [:true, :false, :nil].include?(@expected)
140 return @handling_predicate
144 "#{@expected.to_s}?".to_sym
147 def present_tense_predicate
148 "#{@expected.to_s}s?".to_sym
152 return "" if @args.empty?
153 inspected_args = @args.collect{|a| a.inspect}
154 return "(#{inspected_args.join(', ')})"
161 def expected_to_sentence
162 split_words(@expected)
165 def prefix_to_sentence
170 sym.to_s.gsub(/_/,' ')
180 " #{@args[0...-1].join(', ')} and #{@args[-1]}"
191 # should be_arbitrary_predicate(*args)
193 # should_not be_arbitrary_predicate(*args)
195 # Given true, false, or nil, will pass if actual is
196 # true, false or nil (respectively). Given no args means
197 # the caller should satisfy an if condition (to be or not to be).
199 # Predicates are any Ruby method that ends in a "?" and returns true or false.
200 # Given be_ followed by arbitrary_predicate (without the "?"), RSpec will match
201 # convert that into a query against the target object.
203 # The arbitrary_predicate feature will handle any predicate
204 # prefixed with "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of)
205 # or "be_" (e.g. be_empty), letting you choose the prefix that best suits the predicate.
210 # target.should be_true
211 # target.should be_false
212 # target.should be_nil
213 # target.should_not be_nil
215 # collection.should be_empty #passes if target.empty?
216 # "this string".should be_an_intance_of(String)
218 # target.should_not be_empty #passes unless target.empty?
219 # target.should_not be_old_enough(16) #passes unless target.old_enough?(16)
221 Matchers::Be.new(*args)