6 @expected = parse_expected(args.shift)
13 return true if match_or_compare unless handling_predicate?
14 if handling_predicate?
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"
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.
27 return @result = actual.__send__(present_tense_predicate, *@args)
36 return "expected #{@comparison}#{expected}, got #{@actual.inspect}" unless handling_predicate?
37 return "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}"
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}"
46 return true if @expected == :true
47 return false if @expected == :false
48 return "nil" if @expected == :nil
49 return @expected.inspect
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)
87 @less_than_or_equal = true
94 @greater_than_or_equal = true
108 "#{prefix_to_sentence}#{comparison}#{expected_to_sentence}#{args_to_sentence}"
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)
123 def handling_predicate?
124 return false if [:true, :false, :nil].include?(@expected)
125 return @handling_predicate
129 "#{@expected.to_s}?".to_sym
132 def present_tense_predicate
133 "#{@expected.to_s}s?".to_sym
137 return "" if @args.empty?
138 inspected_args = @args.collect{|a| a.inspect}
139 return "(#{inspected_args.join(', ')})"
146 def expected_to_sentence
147 split_words(@expected)
150 def prefix_to_sentence
155 sym.to_s.gsub(/_/,' ')
165 " #{@args[0...-1].join(', ')} and #{@args[-1]}"
175 # should be_arbitrary_predicate(*args)
177 # should_not be_arbitrary_predicate(*args)
179 # Given true, false, or nil, will pass if actual is
180 # true, false or nil (respectively).
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.
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.
192 # target.should be_true
193 # target.should be_false
194 # target.should be_nil
195 # target.should_not be_nil
197 # collection.should be_empty #passes if target.empty?
198 # "this string".should be_an_intance_of(String)
200 # target.should_not be_empty #passes unless target.empty?
201 # target.should_not be_old_enough(16) #passes unless target.old_enough?(16)
203 Matchers::Be.new(*args)