Skip to content
Browse files

Warn if the "message" param passed to a should is not a string

  • Loading branch information...
1 parent c8ddb5e commit 518a0f7ff353b1fd5a2a557bd93ab28c7645a909 @samphippen samphippen committed with myronmarston
Showing with 53 additions and 6 deletions.
  1. +20 −6 lib/rspec/expectations/handler.rb
  2. +33 −0 spec/rspec/expectations/syntax_spec.rb
View
26 lib/rspec/expectations/handler.rb
@@ -1,18 +1,31 @@
module RSpec
module Expectations
- class PositiveExpectationHandler
+
+ class ExpectationHandler
+ def self.message_must_be_string
+ "The value passed as the message for the expectation was not a string"
+ end
+
+ def self.check_message(message=nil)
+ ::Kernel.warn message_must_be_string unless message == nil or String === message
+ end
+ end
+
+ class PositiveExpectationHandler < ExpectationHandler
+
def self.handle_matcher(actual, matcher, message=nil, &block)
+ check_message(message)
::RSpec::Matchers.last_should = :should
::RSpec::Matchers.last_matcher = matcher
return ::RSpec::Matchers::BuiltIn::PositiveOperatorMatcher.new(actual) if matcher.nil?
match = matcher.matches?(actual, &block)
return match if match
-
+
message ||= matcher.respond_to?(:failure_message_for_should) ?
matcher.failure_message_for_should :
matcher.failure_message
-
+
if matcher.respond_to?(:diffable?) && matcher.diffable?
::RSpec::Expectations.fail_with message, matcher.expected, matcher.actual
else
@@ -21,17 +34,18 @@ def self.handle_matcher(actual, matcher, message=nil, &block)
end
end
- class NegativeExpectationHandler
+ class NegativeExpectationHandler < ExpectationHandler
def self.handle_matcher(actual, matcher, message=nil, &block)
+ check_message(message)
::RSpec::Matchers.last_should = :should_not
::RSpec::Matchers.last_matcher = matcher
return ::RSpec::Matchers::BuiltIn::NegativeOperatorMatcher.new(actual) if matcher.nil?
-
+
match = matcher.respond_to?(:does_not_match?) ?
!matcher.does_not_match?(actual, &block) :
matcher.matches?(actual, &block)
return match unless match
-
+
message ||= matcher.respond_to?(:failure_message_for_should_not) ?
matcher.failure_message_for_should_not :
matcher.negative_failure_message
View
33 spec/rspec/expectations/syntax_spec.rb
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+module RSpec
+ module Expectations
+ module Syntax
+ describe "the should and should_not expectations" do
+ describe "#should" do
+ it "raises an error when the message object isn't a String" do
+ ::Kernel.should_receive(:warn).with /The value passed as the message/
+ 3.should eq(3), :not_a_string
+ end
+
+ it "doesn't raise an error when message is a String" do
+ ::Kernel.should_not_receive(:warn).with /The value passed as the message/
+ 3.should eq(3), "a string"
+ end
+ end
+
+ describe "#should_not" do
+ it "raises an error when the message object isn't a String" do
+ ::Kernel.should_receive(:warn).with /The value passed as the message/
+ 3.should_not eq(4), :not_a_string
+ end
+
+ it "doesn't raise an error when message is a String" do
+ ::Kernel.should_not_receive(:warn).with /The value passed as the message/
+ 3.should_not eq(4), "a string"
+ end
+ end
+ end
+ end
+ end
+end

0 comments on commit 518a0f7

Please sign in to comment.
Something went wrong with that request. Please try again.