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

Commit

Permalink
should and should_not now return true on success
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Jun 20, 2008
1 parent b323584 commit 63d2edc
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 47 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -17,6 +17,8 @@ mechanism.
* plain text story formatter correctly colors story/scenario red if step fails (Patch from Joseph Wilk). Closes #439 * plain text story formatter correctly colors story/scenario red if step fails (Patch from Joseph Wilk). Closes #439
* quiet deprecation warning on inflector - patch from RSL. Closes #430 * quiet deprecation warning on inflector - patch from RSL. Closes #430
* added autospec executable * added autospec executable
* added configurable messages to simple_matcher
* should and should_not return true on success


== Version 1.1.4 == Version 1.1.4


Expand Down
2 changes: 2 additions & 0 deletions lib/spec/expectations/handler.rb
Expand Up @@ -23,6 +23,7 @@ def handle_matcher(actual, matcher, &block)
match = matcher.matches?(actual, &block) match = matcher.matches?(actual, &block)
::Spec::Matchers.generated_description = "should #{describe_matcher(matcher)}" ::Spec::Matchers.generated_description = "should #{describe_matcher(matcher)}"
Spec::Expectations.fail_with(matcher.failure_message) unless match Spec::Expectations.fail_with(matcher.failure_message) unless match
match
end end
end end
end end
Expand Down Expand Up @@ -51,6 +52,7 @@ def handle_matcher(actual, matcher, &block)
match = matcher.matches?(actual, &block) match = matcher.matches?(actual, &block)
::Spec::Matchers.generated_description = "should not #{describe_matcher(matcher)}" ::Spec::Matchers.generated_description = "should not #{describe_matcher(matcher)}"
Spec::Expectations.fail_with(matcher.negative_failure_message) if match Spec::Expectations.fail_with(matcher.negative_failure_message) if match
match
end end
end end
end end
Expand Down
4 changes: 2 additions & 2 deletions lib/spec/matchers/operator_matcher.rb
Expand Up @@ -52,7 +52,7 @@ class PositiveOperatorMatcher < BaseOperatorMatcher #:nodoc:


def __delegate_method_missing_to_target(operator, expected) def __delegate_method_missing_to_target(operator, expected)
::Spec::Matchers.generated_description = "should #{operator} #{expected.inspect}" ::Spec::Matchers.generated_description = "should #{operator} #{expected.inspect}"
return if @target.send(operator, expected) return true if @target.send(operator, expected)
return fail_with_message("expected: #{expected.inspect},\n got: #{@target.inspect} (using #{operator})") if ['==','===', '=~'].include?(operator) return fail_with_message("expected: #{expected.inspect},\n got: #{@target.inspect} (using #{operator})") if ['==','===', '=~'].include?(operator)
return fail_with_message("expected: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{@target.inspect}") return fail_with_message("expected: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{@target.inspect}")
end end
Expand All @@ -63,7 +63,7 @@ class NegativeOperatorMatcher < BaseOperatorMatcher #:nodoc:


def __delegate_method_missing_to_target(operator, expected) def __delegate_method_missing_to_target(operator, expected)
::Spec::Matchers.generated_description = "should not #{operator} #{expected.inspect}" ::Spec::Matchers.generated_description = "should not #{operator} #{expected.inspect}"
return unless @target.send(operator, expected) return true unless @target.send(operator, expected)
return fail_with_message("expected not: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{@target.inspect}") return fail_with_message("expected not: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{@target.inspect}")
end end


Expand Down
22 changes: 15 additions & 7 deletions lib/spec/matchers/simple_matcher.rb
Expand Up @@ -10,9 +10,12 @@ def initialize(description, &match_block)


def matches?(actual) def matches?(actual)
@actual = actual @actual = actual
@match_block.arity == 2 ? case @match_block.arity
@match_block.call(@actual, self) : when 2
@match_block.call(@actual, self)
else
@match_block.call(@actual) @match_block.call(@actual)
end
end end


def description def description
Expand Down Expand Up @@ -50,15 +53,20 @@ def explanation
# #
# The match_block should return a boolean: true indicates a match, which # The match_block should return a boolean: true indicates a match, which
# will pass if you use +should+ and fail if you use +should_not+. false # will pass if you use +should+ and fail if you use +should_not+. false
# indicates no match, which will do the reverse: fail if you use +should+ # (or nil) indicates no match, which will do the reverse: fail if you use
# and pass if you use +should_not+. # +should+ and pass if you use +should_not+.
#
# An error in the +match_block+ will bubble up, resulting in a failure.
# This includes ExpectationNotMet errors (raised by +should+ and
# +should_not+), so you can wrap other expectations in a +simple_matcher+
# and they'll "do the right thing."
# #
# == Example with default messages # == Example with default messages
# #
# def be_even # def be_even
# simple_matcher("an even number") { |given| given % 2 == 0 } # simple_matcher("an even number") { |given| given % 2 == 0 }
# end # end
# #
# describe 2 do # describe 2 do
# it "should be even" do # it "should be even" do
# 2.should be_even # 2.should be_even
Expand All @@ -82,8 +90,8 @@ def explanation
# describe "pecan" do # describe "pecan" do
# it "should rhyme with 'be gone'" do # it "should rhyme with 'be gone'" do
# nut = "pecan" # nut = "pecan"
# reed.extend Rhymer # nut.extend Rhymer
# reed.should rhyme_with("be gone") # nut.should rhyme_with("be gone")
# end # end
# end # end
# #
Expand Down
95 changes: 58 additions & 37 deletions spec/spec/matchers/handler_spec.rb
Expand Up @@ -48,53 +48,73 @@ def positive_only_matcher(*args, &block)


module Spec module Spec
module Expectations module Expectations
describe ExpectationMatcherHandler, ".handle_matcher" do describe ExpectationMatcherHandler do
it "should ask the matcher if it matches" do describe "#handle_matcher" do
matcher = mock("matcher") it "should ask the matcher if it matches" do
actual = Object.new matcher = mock("matcher")
matcher.should_receive(:matches?).with(actual).and_return(true)
ExpectationMatcherHandler.handle_matcher(actual, matcher)
end

it "should explain when the matcher parameter is not a matcher" do
begin
nonmatcher = mock("nonmatcher")
actual = Object.new actual = Object.new
ExpectationMatcherHandler.handle_matcher(actual, nonmatcher) matcher.should_receive(:matches?).with(actual).and_return(true)
rescue Spec::Expectations::InvalidMatcherError => e ExpectationMatcherHandler.handle_matcher(actual, matcher)
end end

it "should explain when the matcher parameter is not a matcher" do
begin
nonmatcher = mock("nonmatcher")
actual = Object.new
ExpectationMatcherHandler.handle_matcher(actual, nonmatcher)
rescue Spec::Expectations::InvalidMatcherError => e
end


e.message.should =~ /^Expected a matcher, got / e.message.should =~ /^Expected a matcher, got /
end

it "should return the match value" do
matcher = mock("matcher")
actual = Object.new
matcher.should_receive(:matches?).with(actual).and_return(:this_value)
ExpectationMatcherHandler.handle_matcher(actual, matcher).should == :this_value
end
end end
end end


describe NegativeExpectationMatcherHandler, ".handle_matcher" do describe NegativeExpectationMatcherHandler do
it "should explain when matcher does not support should_not" do describe "#handle_matcher" do
matcher = mock("matcher") it "should explain when matcher does not support should_not" do
matcher.stub!(:matches?) matcher = mock("matcher")
actual = Object.new matcher.stub!(:matches?)
lambda { actual = Object.new
NegativeExpectationMatcherHandler.handle_matcher(actual, matcher) lambda {
}.should fail_with(/Matcher does not support should_not.\n/) NegativeExpectationMatcherHandler.handle_matcher(actual, matcher)
end }.should fail_with(/Matcher does not support should_not.\n/)

end
it "should ask the matcher if it matches" do
matcher = mock("matcher")
actual = Object.new
matcher.stub!(:negative_failure_message)
matcher.should_receive(:matches?).with(actual).and_return(false)
NegativeExpectationMatcherHandler.handle_matcher(actual, matcher)
end


it "should explain when the matcher parameter is not a matcher" do it "should ask the matcher if it matches" do
begin matcher = mock("matcher")
nonmatcher = mock("nonmatcher")
actual = Object.new actual = Object.new
NegativeExpectationMatcherHandler.handle_matcher(actual, nonmatcher) matcher.stub!(:negative_failure_message)
rescue Spec::Expectations::InvalidMatcherError => e matcher.should_receive(:matches?).with(actual).and_return(false)
NegativeExpectationMatcherHandler.handle_matcher(actual, matcher)
end

it "should explain when the matcher parameter is not a matcher" do
begin
nonmatcher = mock("nonmatcher")
actual = Object.new
NegativeExpectationMatcherHandler.handle_matcher(actual, nonmatcher)
rescue Spec::Expectations::InvalidMatcherError => e
end

e.message.should =~ /^Expected a matcher, got /
end end


e.message.should =~ /^Expected a matcher, got /
it "should return the match value" do
matcher = mock("matcher")
actual = Object.new
matcher.should_receive(:matches?).with(actual).and_return(false)
matcher.stub!(:negative_failure_message).and_return("ignore")
NegativeExpectationMatcherHandler.handle_matcher(actual, matcher).should be_false
end
end end
end end


Expand Down Expand Up @@ -124,6 +144,7 @@ module Expectations
}.should fail_with(/Matcher does not support should_not.\n/) }.should fail_with(/Matcher does not support should_not.\n/)
end end



end end
end end
end end
12 changes: 11 additions & 1 deletion spec/spec/matchers/operator_matcher_spec.rb
Expand Up @@ -10,6 +10,11 @@
subject.should == "apple" subject.should == "apple"
end end


it "should return true on success" do
subject = "apple"
(subject.should == "apple").should be_true
end

it "should fail when target.==(actual) returns false" do it "should fail when target.==(actual) returns false" do
subject = "apple" subject = "apple"
Spec::Expectations.should_receive(:fail_with).with(%[expected: "orange",\n got: "apple" (using ==)], "orange", "apple") Spec::Expectations.should_receive(:fail_with).with(%[expected: "orange",\n got: "apple" (using ==)], "orange", "apple")
Expand All @@ -26,12 +31,17 @@
subject.should_not == "apple" subject.should_not == "apple"
end end


it "should return true on success" do
subject = "apple"
(subject.should_not == "orange").should be_true
end

it "should fail when target.==(actual) returns false" do it "should fail when target.==(actual) returns false" do
subject = "apple" subject = "apple"
Spec::Expectations.should_receive(:fail_with).with(%[expected not: == "apple",\n got: "apple"], "apple", "apple") Spec::Expectations.should_receive(:fail_with).with(%[expected not: == "apple",\n got: "apple"], "apple", "apple")
subject.should_not == "apple" subject.should_not == "apple"
end end

end end


describe "should ===" do describe "should ===" do
Expand Down
9 changes: 9 additions & 0 deletions spec/spec/matchers/simple_matcher_spec.rb
Expand Up @@ -26,6 +26,15 @@ module Matchers
matcher = simple_matcher("thing") do end matcher = simple_matcher("thing") do end
matcher.description.should =="thing" matcher.description.should =="thing"
end end

it "should fail if a wrapped 'should' fails" do
matcher = simple_matcher("should fail") do
2.should == 3
end
lambda do
matcher.matches?("anything").should be_true
end.should fail_with(/expected: 3/)
end
end end


describe "with arity of 2" do describe "with arity of 2" do
Expand Down

0 comments on commit 63d2edc

Please sign in to comment.