3 class InvalidMatcherError < ArgumentError; end
5 module MatcherHandlerHelper
6 def describe_matcher(matcher)
7 matcher.respond_to?(:description) ? matcher.description : "[#{matcher.class.name} does not provide a description]"
11 class ExpectationMatcherHandler
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}."
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
26 class NegativeExpectationMatcherHandler
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}."
34 unless matcher.respond_to?(:negative_failure_message)
35 Spec::Expectations.fail_with(
37 Matcher does not support should_not.
38 See Spec::Matchers for more information
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