Skip to content

Commit

Permalink
Add deprecation warnings for the removal of should syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
benoittgt committed Jun 1, 2021
1 parent 040d1b0 commit ed390ed
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 26 deletions.
4 changes: 2 additions & 2 deletions features/syntax_configuration.feature
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Feature: Syntax Configuration
"""
When I run `rspec`
Then the examples should all pass
And the output should contain "Using `should` from rspec-expectations' old `:should` syntax without explicitly enabling the syntax is deprecated"
And the output should contain "Using `should` from rspec-expectations' old `:should` will be removed in RSpec 4"

Scenario: Disable should syntax
Given a file named "spec/spec_helper.rb" with:
Expand Down Expand Up @@ -88,5 +88,5 @@ Feature: Syntax Configuration
"""
When I run `rspec`
Then the examples should all pass
And the output should not contain "deprecated"
And the output should contain "Using `should` from rspec-expectations' old `:should` will be removed in RSpec 4"

1 change: 0 additions & 1 deletion lib/rspec/expectations/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ def include_chain_clauses_in_custom_matcher_descriptions?
# @private
def reset_syntaxes_to_default
self.syntax = [:should, :expect]
RSpec::Expectations::Syntax.warn_about_should!
end

# @api private
Expand Down
25 changes: 6 additions & 19 deletions lib/rspec/expectations/syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,27 @@ def default_should_host
end

# @api private
# Instructs rspec-expectations to warn on first usage of `should` or `should_not`.
# Enabled by default. This is largely here to facilitate testing.
def warn_about_should!
@warn_about_should = true
end

# @api private
# Generates a deprecation warning for the given method if no warning
# has already been issued.
def warn_about_should_unless_configured(method_name)
return unless @warn_about_should

# Generates a deprecation warning for the given method
def warn_about_should(method_name)
RSpec.deprecate(
"Using `#{method_name}` from rspec-expectations' old `:should` syntax without explicitly enabling the syntax",
:replacement => "the new `:expect` syntax or explicitly enable `:should` with `config.expect_with(:rspec) { |c| c.syntax = :should }`"
"Using `#{method_name}` from rspec-expectations' old `:should` will be removed in RSpec 4",
:replacement => "the new `:expect` syntax"
)

@warn_about_should = false
end

# @api private
# Enables the `should` syntax.
def enable_should(syntax_host=default_should_host)
@warn_about_should = false if syntax_host == default_should_host
return if should_enabled?(syntax_host)

syntax_host.module_exec do
def should(matcher=nil, message=nil, &block)
::RSpec::Expectations::Syntax.warn_about_should_unless_configured(::Kernel.__method__)
::RSpec::Expectations::Syntax.warn_about_should(::Kernel.__method__)
::RSpec::Expectations::PositiveExpectationHandler.handle_matcher(self, matcher, message, &block)
end

def should_not(matcher=nil, message=nil, &block)
::RSpec::Expectations::Syntax.warn_about_should_unless_configured(::Kernel.__method__)
::RSpec::Expectations::Syntax.warn_about_should(::Kernel.__method__)
::RSpec::Expectations::NegativeExpectationHandler.handle_matcher(self, matcher, message, &block)
end
end
Expand Down
6 changes: 2 additions & 4 deletions spec/rspec/expectations/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ def delegated?; true; end

it "warns when the should syntax is called by default" do
expected_arguments = [
/Using.*without explicitly enabling/,
{ :replacement => "the new `:expect` syntax or explicitly enable `:should` with `config.expect_with(:rspec) { |c| c.syntax = :should }`" }
/.*will be removed in RSpec 4/,
{ :replacement => "the new `:expect` syntax" }
]

expect(RSpec).to receive(:deprecate).with(*expected_arguments)
Expand All @@ -211,13 +211,11 @@ def delegated?; true; end

it "does not warn when only the should syntax is explicitly configured" do
configure_syntax(:should)
RSpec.should_not receive(:deprecate)
3.should eq(3)
end

it "does not warn when both the should and expect syntaxes are explicitly configured" do
configure_syntax([:should, :expect])
expect(RSpec).not_to receive(:deprecate)
3.should eq(3)
end
end
Expand Down

0 comments on commit ed390ed

Please sign in to comment.