Skip to content

Commit

Permalink
Remove support for legacy RSpec 1&2 matchers
Browse files Browse the repository at this point in the history
As per http://rspec.info/blog/2013/07/the-plan-for-rspec-3/:

> compatibility layer so that existing matchers will continue to work, with the plan to remove that compatibility layer in RSpec 4
  • Loading branch information
pirj committed Dec 26, 2020
1 parent b043852 commit 3a55a2d
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 290 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Breaking Changes:
* Ruby < 2.3 is no longer supported. (Phil Pirozhkov, #1231)
* Extract `should*` (both monkey-patching and non-monkey-patching). (Phil Pirozhkov, #1245)
* Remove deprecated `LegacyMacherAdapter`. (Phil Pirozhkov, #1253)
* Remove support for legacy RSpec matchers (pre 3). (Phil Pirozhkov, #1253)

Enhancements:

Expand Down
77 changes: 0 additions & 77 deletions lib/rspec/expectations/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,8 @@ def self.check_message(msg)
end
end

# Returns an RSpec-3+ compatible matcher, wrapping a legacy one
# in an adapter if necessary.
#
# @private
def self.modern_matcher_from(matcher)
LegacyMatcherAdapter::RSpec2.wrap(matcher) ||
LegacyMatcherAdapter::RSpec1.wrap(matcher) || matcher
end

def self.with_matcher(handler, matcher, message)
check_message(message)
matcher = modern_matcher_from(matcher)
yield matcher
ensure
::RSpec::Matchers.last_expectation_handler = handler
Expand Down Expand Up @@ -101,72 +91,5 @@ def self.opposite_should_method
:should
end
end

# Wraps a matcher written against one of the legacy protocols in
# order to present the current protocol.
#
# @private
class LegacyMatcherAdapter < Matchers::MatcherDelegator
def initialize(matcher)
super
::RSpec.warn_deprecation(<<-EOS.gsub(/^\s+\|/, ''), :type => "legacy_matcher")
|#{matcher.class.name || matcher.inspect} implements a legacy RSpec matcher
|protocol. For the current protocol you should expose the failure messages
|via the `failure_message` and `failure_message_when_negated` methods.
|(Used from #{CallerFilter.first_non_rspec_line})
EOS
end

def self.wrap(matcher)
new(matcher) if interface_matches?(matcher)
end

# Starting in RSpec 1.2 (and continuing through all 2.x releases),
# the failure message protocol was:
# * `failure_message_for_should`
# * `failure_message_for_should_not`
# @private
class RSpec2 < self
def failure_message
base_matcher.failure_message_for_should
end

def failure_message_when_negated
base_matcher.failure_message_for_should_not
end

def self.interface_matches?(matcher)
(
!matcher.respond_to?(:failure_message) &&
matcher.respond_to?(:failure_message_for_should)
) || (
!matcher.respond_to?(:failure_message_when_negated) &&
matcher.respond_to?(:failure_message_for_should_not)
)
end
end

# Before RSpec 1.2, the failure message protocol was:
# * `failure_message`
# * `negative_failure_message`
# @private
class RSpec1 < self
def failure_message
base_matcher.failure_message
end

def failure_message_when_negated
base_matcher.negative_failure_message
end

# Note: `failure_message` is part of the RSpec 3 protocol
# (paired with `failure_message_when_negated`), so we don't check
# for `failure_message` here.
def self.interface_matches?(matcher)
!matcher.respond_to?(:failure_message_when_negated) &&
matcher.respond_to?(:negative_failure_message)
end
end
end
end
end
3 changes: 1 addition & 2 deletions lib/rspec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -981,8 +981,7 @@ def self.is_a_matcher?(obj)
end
return false unless obj.respond_to?(:matches?)

obj.respond_to?(:failure_message) ||
obj.respond_to?(:failure_message_for_should) # support legacy matchers
obj.respond_to?(:failure_message)
end

::RSpec::Support.register_matcher_definition do |obj|
Expand Down
29 changes: 0 additions & 29 deletions lib/rspec/matchers/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,34 +335,6 @@ def define_user_override(method_name, user_def, &our_def)
our_def ||= lambda { super(*actual_arg_for(user_def)) }
define_method(method_name, &our_def)
end

# Defines deprecated macro methods from RSpec 2 for backwards compatibility.
# @deprecated Use the methods from {Macros} instead.
module Deprecated
# @deprecated Use {Macros#match} instead.
def match_for_should(&definition)
RSpec.deprecate("`match_for_should`", :replacement => "`match`")
match(&definition)
end

# @deprecated Use {Macros#match_when_negated} instead.
def match_for_should_not(&definition)
RSpec.deprecate("`match_for_should_not`", :replacement => "`match_when_negated`")
match_when_negated(&definition)
end

# @deprecated Use {Macros#failure_message} instead.
def failure_message_for_should(&definition)
RSpec.deprecate("`failure_message_for_should`", :replacement => "`failure_message`")
failure_message(&definition)
end

# @deprecated Use {Macros#failure_message_when_negated} instead.
def failure_message_for_should_not(&definition)
RSpec.deprecate("`failure_message_for_should_not`", :replacement => "`failure_message_when_negated`")
failure_message_when_negated(&definition)
end
end
end

# Defines default implementations of the matcher
Expand Down Expand Up @@ -425,7 +397,6 @@ class Matcher

# Makes the macro methods available to an `RSpec::Matchers.define` block.
extend Macros
extend Macros::Deprecated

# Exposes the value being matched against -- generally the object
# object wrapped by `expect`.
Expand Down
84 changes: 0 additions & 84 deletions spec/rspec/matchers/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,90 +199,6 @@ def new_matcher(name, *expected, &block)
expect(m2.matches?(2)).to be_truthy
end

context 'using deprecated APIs' do
before { allow_deprecation }

describe "failure_message_for_should" do
let(:matcher) do
new_matcher(:foo) do
match { false }
failure_message_for_should { "failed" }
end
end
line = __LINE__ - 3

it 'defines the failure message for a positive expectation' do
expect {
expect(nil).to matcher
}.to fail_with("failed")
end

it 'prints a deprecation warning' do
expect_deprecation_with_call_site(__FILE__, line, /failure_message_for_should/)
matcher
end
end

describe "failure_message_for_should_not" do
let(:matcher) do
new_matcher(:foo) do
match { true }
failure_message_for_should_not { "failed" }
end
end
line = __LINE__ - 3

it 'defines the failure message for a negative expectation' do
expect {
expect(nil).not_to matcher
}.to fail_with("failed")
end

it 'prints a deprecation warning' do
expect_deprecation_with_call_site(__FILE__, line, /failure_message_for_should_not/)
matcher
end
end

describe "match_for_should" do
let(:matcher) do
new_matcher(:foo) do
match_for_should { |arg| arg }
end
end
line = __LINE__ - 3

it 'defines the positive expectation match logic' do
expect(true).to matcher
expect { expect(false).to matcher }.to fail_with(/foo/)
end

it 'prints a deprecation warning' do
expect_deprecation_with_call_site(__FILE__, line, /match_for_should/)
matcher
end
end

describe "match_for_should_not" do
let(:matcher) do
new_matcher(:foo) do
match_for_should_not { |arg| !arg }
end
end
line = __LINE__ - 3

it 'defines the positive expectation match logic' do
expect(false).not_to matcher
expect { expect(true).not_to matcher }.to fail_with(/foo/)
end

it 'prints a deprecation warning' do
expect_deprecation_with_call_site(__FILE__, line, /match_for_should_not/)
matcher
end
end
end

context "with an included module" do
let(:matcher) do
new_matcher(:be_a_greeting) do
Expand Down
98 changes: 0 additions & 98 deletions spec/rspec/matchers/legacy_spec.rb

This file was deleted.

0 comments on commit 3a55a2d

Please sign in to comment.