Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix and deprecate `expect { }.should` syntax.

Fixes #170.
  • Loading branch information...
commit 4808a0b4b1235bd4d5afa57146a30d0e9e6009cb 1 parent d40da72
@myronmarston myronmarston authored
View
10 Changelog.md
@@ -1,3 +1,13 @@
+### dev
+[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.11.2...master)
+
+Bug fixes
+
+* Fix (and deprecate) `expect { }.should` syntax so that it works even
+ though it was never a documented or intended syntax. It worked as a
+ consequence of the implementation of `expect` in RSpec 2.10 and
+ earlier. (Myron Marston)
+
### 2.11.2 / 2012-07-25
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.11.1...v2.11.2)
View
2  lib/rspec/expectations.rb
@@ -1,7 +1,7 @@
require 'rspec/expectations/extensions'
require 'rspec/matchers'
-require 'rspec/matchers/configuration'
require 'rspec/expectations/expectation_target'
+require 'rspec/matchers/configuration'
require 'rspec/expectations/fail_with'
require 'rspec/expectations/errors'
require 'rspec/expectations/deprecation'
View
30 lib/rspec/expectations/expectation_target.rb
@@ -10,6 +10,11 @@ module Expectations
# # with `to_not`
# expect(actual).to_not eq(3)
class ExpectationTarget
+ class << self
+ attr_accessor :deprecated_should_enabled
+ alias deprecated_should_enabled? deprecated_should_enabled
+ end
+
# @api private
def initialize(target)
@target = target
@@ -44,6 +49,31 @@ def to_not(matcher=nil, message=nil, &block)
end
alias not_to to_not
+ def self.enable_deprecated_should
+ return if deprecated_should_enabled?
+
+ def should(*args)
+ RSpec.deprecate "`expect { }.should`", "`expect { }.to`", 3
+ @target.should(*args)
+ end
+
+ def should_not(*args)
+ RSpec.deprecate "`expect { }.should_not`", "`expect { }.to_not`", 3
+ @target.should_not(*args)
+ end
+
+ self.deprecated_should_enabled = true
+ end
+
+ def self.disable_deprecated_should
+ return unless deprecated_should_enabled?
+
+ remove_method :should
+ remove_method :should_not
+
+ self.deprecated_should_enabled = false
+ end
+
private
def prevent_operator_matchers(verb, matcher)
View
8 lib/rspec/expectations/syntax.rb
@@ -57,6 +57,8 @@ def should_not(matcher=nil, message=nil, &block)
::RSpec::Expectations::NegativeExpectationHandler.handle_matcher(self, matcher, message, &block)
end
end
+
+ ::RSpec::Expectations::ExpectationTarget.enable_deprecated_should if expect_enabled?
end
# @api private
@@ -68,6 +70,8 @@ def disable_should(syntax_host = default_should_host)
undef should
undef should_not
end
+
+ ::RSpec::Expectations::ExpectationTarget.disable_deprecated_should
end
# @api private
@@ -82,6 +86,8 @@ def expect(*target, &target_block)
::RSpec::Expectations::ExpectationTarget.new(target.first)
end
end
+
+ ::RSpec::Expectations::ExpectationTarget.enable_deprecated_should if should_enabled?
end
# @api private
@@ -92,6 +98,8 @@ def disable_expect(syntax_host = ::RSpec::Matchers)
syntax_host.module_eval do
undef expect
end
+
+ ::RSpec::Expectations::ExpectationTarget.disable_deprecated_should
end
# @api private
View
42 spec/rspec/matchers/configuration_spec.rb
@@ -121,6 +121,48 @@ def sandboxed
expect(3).to eq(3)
end
end
+
+ it 'does not add the deprecated #should to ExpectationTarget when only :should is enabled' do
+ et = Expectations::ExpectationTarget
+
+ sandboxed do
+ configure_syntax :should
+ et.new(Proc.new {}).should be_an(et)
+ et.new(Proc.new {}).should_not be_a(Proc)
+ end
+ end
+
+ it 'does not add the deprecated #should to ExpectationTarget when only :expect is enabled' do
+ sandboxed do
+ configure_syntax :expect
+ expect(expect(3)).not_to respond_to(:should)
+ expect(expect(3)).not_to respond_to(:should_not)
+ end
+ end
+
+ context 'when both :expect and :should are enabled' do
+ before { RSpec.stub(:warn) }
+
+ it 'allows `expect {}.should` to be used' do
+ sandboxed do
+ configure_syntax [:should, :expect]
+ expect { raise "boom" }.should raise_error("boom")
+ expect { }.should_not raise_error
+ end
+ end
+
+ it 'prints a deprecation notice when `expect {}.should` is used' do
+ sandboxed do
+ configure_syntax [:should, :expect]
+
+ RSpec.should_receive(:warn).with(/please use `expect \{ \}.to.*instead/)
+ expect { raise "boom" }.should raise_error("boom")
+
+ RSpec.should_receive(:warn).with(/please use `expect \{ \}.to_not.*instead/)
+ expect { }.should_not raise_error
+ end
+ end
+ end
end
describe "configuring rspec-expectations directly" do
Please sign in to comment.
Something went wrong with that request. Please try again.