Skip to content
This repository has been archived by the owner on Oct 27, 2021. It is now read-only.

Commit

Permalink
handler looks for diffable?
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Apr 11, 2009
1 parent 3ebc283 commit 4661dec
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 32 deletions.
23 changes: 1 addition & 22 deletions lib/spec/expectations.rb
@@ -1,4 +1,5 @@
require 'spec/matchers' require 'spec/matchers'
require 'spec/expectations/fail_with'
require 'spec/expectations/errors' require 'spec/expectations/errors'
require 'spec/expectations/extensions' require 'spec/expectations/extensions'
require 'spec/expectations/handler' require 'spec/expectations/handler'
Expand Down Expand Up @@ -30,27 +31,5 @@ module Spec
# RSpec ships with a standard set of useful matchers, and writing your own # RSpec ships with a standard set of useful matchers, and writing your own
# matchers is quite simple. See Spec::Matchers for details. # matchers is quite simple. See Spec::Matchers for details.
module Expectations 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
end end
29 changes: 29 additions & 0 deletions lib/spec/expectations/fail_with.rb
@@ -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
22 changes: 15 additions & 7 deletions lib/spec/expectations/handler.rb
Expand Up @@ -10,10 +10,14 @@ def self.handle_matcher(actual, matcher, &block)


match = matcher.matches?(actual, &block) match = matcher.matches?(actual, &block)
return match if match return match if match


::Spec::Expectations.fail_with matcher.respond_to?(:failure_message_for_should) ? if matcher.respond_to?(:diffable?) && matcher.diffable?
matcher.failure_message_for_should : ::Spec::Expectations.fail matcher.failure_message_for_should, matcher.expected.first, matcher.actual
matcher.failure_message else
::Spec::Expectations.fail_with matcher.respond_to?(:failure_message_for_should) ?
matcher.failure_message_for_should :
matcher.failure_message
end
end end
end end


Expand All @@ -28,9 +32,13 @@ def self.handle_matcher(actual, matcher, &block)
matcher.matches?(actual, &block) matcher.matches?(actual, &block)
return match unless match return match unless match


::Spec::Expectations.fail_with matcher.respond_to?(:failure_message_for_should_not) ? if matcher.respond_to?(:diffable?) && matcher.diffable?
matcher.failure_message_for_should_not : ::Spec::Expectations.fail matcher.failure_message_for_should_not, matcher.expected.first, matcher.actual
matcher.negative_failure_message 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 end
end end
Expand Down
35 changes: 32 additions & 3 deletions spec/spec/expectations/handler_spec.rb
Expand Up @@ -7,9 +7,7 @@ def initialize(*args, &block)
if args.last.is_a? Hash if args.last.is_a? Hash
@expected = args.last[:expected] @expected = args.last[:expected]
end end
if block_given? @expected = block.call if block
@expected = block.call
end
@block = block @block = block
end end


Expand Down Expand Up @@ -71,7 +69,21 @@ module Expectations
::Spec::Expectations.should_receive(:fail_with).with("message") ::Spec::Expectations.should_receive(:fail_with).with("message")


Spec::Expectations::PositiveExpectationHandler.handle_matcher(actual, matcher) 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 end


it "calls failure_message if the matcher does not implement failure_message_for_should" do it "calls failure_message if the matcher does not implement failure_message_for_should" do
Expand Down Expand Up @@ -131,6 +143,23 @@ module Expectations
Spec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher) Spec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher)


end 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
end end


Expand Down

0 comments on commit 4661dec

Please sign in to comment.