Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

should and should_not now return true on success

  • Loading branch information...
commit 63d2edcf4c1625b57ac27029287104ce973eb7b6 1 parent b323584
@dchelimsky authored
View
2  CHANGES
@@ -17,6 +17,8 @@ mechanism.
* 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
* added autospec executable
+* added configurable messages to simple_matcher
+* should and should_not return true on success
== Version 1.1.4
View
2  lib/spec/expectations/handler.rb
@@ -23,6 +23,7 @@ def handle_matcher(actual, matcher, &block)
match = matcher.matches?(actual, &block)
::Spec::Matchers.generated_description = "should #{describe_matcher(matcher)}"
Spec::Expectations.fail_with(matcher.failure_message) unless match
+ match
end
end
end
@@ -51,6 +52,7 @@ def handle_matcher(actual, matcher, &block)
match = matcher.matches?(actual, &block)
::Spec::Matchers.generated_description = "should not #{describe_matcher(matcher)}"
Spec::Expectations.fail_with(matcher.negative_failure_message) if match
+ match
end
end
end
View
4 lib/spec/matchers/operator_matcher.rb
@@ -52,7 +52,7 @@ class PositiveOperatorMatcher < BaseOperatorMatcher #:nodoc:
def __delegate_method_missing_to_target(operator, expected)
::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: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{@target.inspect}")
end
@@ -63,7 +63,7 @@ class NegativeOperatorMatcher < BaseOperatorMatcher #:nodoc:
def __delegate_method_missing_to_target(operator, expected)
::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}")
end
View
22 lib/spec/matchers/simple_matcher.rb
@@ -10,9 +10,12 @@ def initialize(description, &match_block)
def matches?(actual)
@actual = actual
- @match_block.arity == 2 ?
- @match_block.call(@actual, self) :
+ case @match_block.arity
+ when 2
+ @match_block.call(@actual, self)
+ else
@match_block.call(@actual)
+ end
end
def description
@@ -50,15 +53,20 @@ def explanation
#
# 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
- # indicates no match, which will do the reverse: fail if you use +should+
- # and pass if you use +should_not+.
+ # (or nil) indicates no match, which will do the reverse: fail if you use
+ # +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
#
# def be_even
# simple_matcher("an even number") { |given| given % 2 == 0 }
# end
- #
+ #
# describe 2 do
# it "should be even" do
# 2.should be_even
@@ -82,8 +90,8 @@ def explanation
# describe "pecan" do
# it "should rhyme with 'be gone'" do
# nut = "pecan"
- # reed.extend Rhymer
- # reed.should rhyme_with("be gone")
+ # nut.extend Rhymer
+ # nut.should rhyme_with("be gone")
# end
# end
#
View
95 spec/spec/matchers/handler_spec.rb
@@ -48,53 +48,73 @@ def positive_only_matcher(*args, &block)
module Spec
module Expectations
- describe ExpectationMatcherHandler, ".handle_matcher" do
- it "should ask the matcher if it matches" do
- matcher = mock("matcher")
- actual = Object.new
- 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")
+ describe ExpectationMatcherHandler do
+ describe "#handle_matcher" do
+ it "should ask the matcher if it matches" do
+ matcher = mock("matcher")
actual = Object.new
- ExpectationMatcherHandler.handle_matcher(actual, nonmatcher)
- rescue Spec::Expectations::InvalidMatcherError => e
+ 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
+ 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
- describe NegativeExpectationMatcherHandler, ".handle_matcher" do
- it "should explain when matcher does not support should_not" do
- matcher = mock("matcher")
- matcher.stub!(:matches?)
- actual = Object.new
- lambda {
- NegativeExpectationMatcherHandler.handle_matcher(actual, matcher)
- }.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
+ describe NegativeExpectationMatcherHandler do
+ describe "#handle_matcher" do
+ it "should explain when matcher does not support should_not" do
+ matcher = mock("matcher")
+ matcher.stub!(:matches?)
+ actual = Object.new
+ lambda {
+ NegativeExpectationMatcherHandler.handle_matcher(actual, matcher)
+ }.should fail_with(/Matcher does not support should_not.\n/)
+ end
- it "should explain when the matcher parameter is not a matcher" do
- begin
- nonmatcher = mock("nonmatcher")
+ it "should ask the matcher if it matches" do
+ matcher = mock("matcher")
actual = Object.new
- NegativeExpectationMatcherHandler.handle_matcher(actual, nonmatcher)
- rescue Spec::Expectations::InvalidMatcherError => e
+ 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
+ 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
- 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
@@ -124,6 +144,7 @@ module Expectations
}.should fail_with(/Matcher does not support should_not.\n/)
end
+
end
end
end
View
12 spec/spec/matchers/operator_matcher_spec.rb
@@ -10,6 +10,11 @@
subject.should == "apple"
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
subject = "apple"
Spec::Expectations.should_receive(:fail_with).with(%[expected: "orange",\n got: "apple" (using ==)], "orange", "apple")
@@ -26,12 +31,17 @@
subject.should_not == "apple"
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
subject = "apple"
Spec::Expectations.should_receive(:fail_with).with(%[expected not: == "apple",\n got: "apple"], "apple", "apple")
subject.should_not == "apple"
end
-
+
end
describe "should ===" do
View
9 spec/spec/matchers/simple_matcher_spec.rb
@@ -26,6 +26,15 @@ module Matchers
matcher = simple_matcher("thing") do end
matcher.description.should =="thing"
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
describe "with arity of 2" do
Please sign in to comment.
Something went wrong with that request. Please try again.