]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/lib/spec/expectations/handler.rb
added RSpec and RSpec on Rails
[rails.git] / vendor / gems / rspec-1.1.2 / lib / spec / expectations / handler.rb
1 module Spec
2   module Expectations
3     class InvalidMatcherError < ArgumentError; end        
4     
5     module MatcherHandlerHelper
6       def describe_matcher(matcher)
7         matcher.respond_to?(:description) ? matcher.description : "[#{matcher.class.name} does not provide a description]"
8       end
9     end
10     
11     class ExpectationMatcherHandler        
12       class << self
13         include MatcherHandlerHelper
14         def handle_matcher(actual, matcher, &block)
15           unless matcher.respond_to?(:matches?)
16             raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
17           end
18           
19           match = matcher.matches?(actual, &block)
20           ::Spec::Matchers.generated_description = "should #{describe_matcher(matcher)}"
21           Spec::Expectations.fail_with(matcher.failure_message) unless match
22         end
23       end
24     end
25
26     class NegativeExpectationMatcherHandler
27       class << self
28         include MatcherHandlerHelper
29         def handle_matcher(actual, matcher, &block)
30           unless matcher.respond_to?(:matches?)
31             raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
32           end
33
34           unless matcher.respond_to?(:negative_failure_message)
35             Spec::Expectations.fail_with(
36 <<-EOF
37 Matcher does not support should_not.
38 See Spec::Matchers for more information
39 about matchers.
40 EOF
41 )
42           end
43           match = matcher.matches?(actual, &block)
44           ::Spec::Matchers.generated_description = "should not #{describe_matcher(matcher)}"
45           Spec::Expectations.fail_with(matcher.negative_failure_message) if match
46         end
47       end
48     end
49
50   end
51 end
52