Blob of rspec/lib/spec/expectations/handler.rb (raw blob data)

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 if :use_operator_matcher == matcher
16 return Spec::Matchers::PositiveOperatorMatcher.new(actual)
17 end
18
19 unless matcher.respond_to?(:matches?)
20 raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
21 end
22
23 match = matcher.matches?(actual, &block)
24 ::Spec::Matchers.generated_description = "should #{describe_matcher(matcher)}"
25 Spec::Expectations.fail_with(matcher.failure_message) unless match
26 end
27 end
28 end
29
30 class NegativeExpectationMatcherHandler
31 class << self
32 include MatcherHandlerHelper
33 def handle_matcher(actual, matcher, &block)
34 if :use_operator_matcher == matcher
35 return Spec::Matchers::NegativeOperatorMatcher.new(actual)
36 end
37
38 unless matcher.respond_to?(:matches?)
39 raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
40 end
41
42 unless matcher.respond_to?(:negative_failure_message)
43 Spec::Expectations.fail_with(
44 <<-EOF
45 Matcher does not support should_not.
46 See Spec::Matchers for more information
47 about matchers.
48 EOF
49 )
50 end
51 match = matcher.matches?(actual, &block)
52 ::Spec::Matchers.generated_description = "should not #{describe_matcher(matcher)}"
53 Spec::Expectations.fail_with(matcher.negative_failure_message) if match
54 end
55 end
56 end
57
58 end
59 end