Permalink
Browse files

handler looks for diffable?

  • Loading branch information...
1 parent 3ebc283 commit 4661decb9b0ef5bcd7e0173d16559c6d6b435da5 @dchelimsky committed Apr 11, 2009
View
@@ -1,4 +1,5 @@
require 'spec/matchers'
+require 'spec/expectations/fail_with'
require 'spec/expectations/errors'
require 'spec/expectations/extensions'
require 'spec/expectations/handler'
@@ -30,27 +31,5 @@ module Spec
# RSpec ships with a standard set of useful matchers, and writing your own
# matchers is quite simple. See Spec::Matchers for details.
module Expectations
- class << self
- attr_accessor :differ
-
- # raises a Spec::Expectations::ExpectationNotMetError with message
- #
- # When a differ has been assigned and fail_with is passed
- # <code>expected</code> and <code>target</code>, passes them
- # to the differ to append a diff message to the failure message.
- def fail_with(message, expected=nil, target=nil) # :nodoc:
- if (Array === message) & (message.length == 3)
- message, expected, target = message[0], message[1], message[2]
- end
- unless (differ.nil? || expected.nil? || target.nil?)
- if expected.is_a?(String)
- message << "\nDiff:" << self.differ.diff_as_string(target.to_s, expected)
- elsif !target.is_a?(Proc)
- message << "\nDiff:" << self.differ.diff_as_object(target, expected)
- end
- end
- Kernel::raise(Spec::Expectations::ExpectationNotMetError.new(message))
- end
- end
end
end
@@ -0,0 +1,29 @@
+module Spec
+ module Expectations
+ class << self
+ attr_accessor :differ
+
+ def fail
+ end
+
+ # raises a Spec::Expectations::ExpectationNotMetError with message
+ #
+ # When a differ has been assigned and fail_with is passed
+ # <code>expected</code> and <code>target</code>, passes them
+ # to the differ to append a diff message to the failure message.
+ def fail_with(message, expected=nil, target=nil) # :nodoc:
+ if (Array === message) & (message.length == 3)
+ message, expected, target = message[0], message[1], message[2]
+ end
+ unless (differ.nil? || expected.nil? || target.nil?)
+ if expected.is_a?(String)
+ message << "\nDiff:" << self.differ.diff_as_string(target.to_s, expected)
+ elsif !target.is_a?(Proc)
+ message << "\nDiff:" << self.differ.diff_as_object(target, expected)
+ end
+ end
+ Kernel::raise(Spec::Expectations::ExpectationNotMetError.new(message))
+ end
+ end
+ end
+end
@@ -10,10 +10,14 @@ def self.handle_matcher(actual, matcher, &block)
match = matcher.matches?(actual, &block)
return match if match
-
- ::Spec::Expectations.fail_with matcher.respond_to?(:failure_message_for_should) ?
- matcher.failure_message_for_should :
- matcher.failure_message
+
+ if matcher.respond_to?(:diffable?) && matcher.diffable?
+ ::Spec::Expectations.fail matcher.failure_message_for_should, matcher.expected.first, matcher.actual
+ else
+ ::Spec::Expectations.fail_with matcher.respond_to?(:failure_message_for_should) ?
+ matcher.failure_message_for_should :
+ matcher.failure_message
+ end
end
end
@@ -28,9 +32,13 @@ def self.handle_matcher(actual, matcher, &block)
matcher.matches?(actual, &block)
return match unless match
- ::Spec::Expectations.fail_with matcher.respond_to?(:failure_message_for_should_not) ?
- matcher.failure_message_for_should_not :
- matcher.negative_failure_message
+ if matcher.respond_to?(:diffable?) && matcher.diffable?
+ ::Spec::Expectations.fail matcher.failure_message_for_should_not, matcher.expected.first, matcher.actual
+ else
+ ::Spec::Expectations.fail_with matcher.respond_to?(:failure_message_for_should_not) ?
+ matcher.failure_message_for_should_not :
+ matcher.negative_failure_message
+ end
end
end
end
@@ -7,9 +7,7 @@ def initialize(*args, &block)
if args.last.is_a? Hash
@expected = args.last[:expected]
end
- if block_given?
- @expected = block.call
- end
+ @expected = block.call if block
@block = block
end
@@ -71,7 +69,21 @@ module Expectations
::Spec::Expectations.should_receive(:fail_with).with("message")
Spec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher)
+ end
+
+ it "calls fail if matcher.diffable?" do
+ matcher = mock("matcher",
+ :diffable? => true,
+ :failure_message_for_should => "message",
+ :matches? => false,
+ :expected => [1],
+ :actual => 2
+ )
+ actual = Object.new
+ ::Spec::Expectations.should_receive(:fail).with("message", 1, 2)
+
+ Spec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher)
end
it "calls failure_message if the matcher does not implement failure_message_for_should" do
@@ -131,6 +143,23 @@ module Expectations
Spec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher)
end
+
+
+ it "calls fail if matcher.diffable?" do
+ matcher = mock("matcher",
+ :diffable? => true,
+ :failure_message_for_should_not => "message",
+ :matches? => true,
+ :expected => [1],
+ :actual => 2
+ )
+ actual = Object.new
+
+ ::Spec::Expectations.should_receive(:fail).with("message", 1, 2)
+
+ Spec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher)
+ end
+
end
end

0 comments on commit 4661dec

Please sign in to comment.