Skip to content

Commit

Permalink
Remove non-monkey-patching should syntax, too
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed Dec 26, 2020
1 parent 54421ad commit 712689e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 82 deletions.
4 changes: 3 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
Breaking Changes:

* Ruby < 2.3 is no longer supported. (Phil Pirozhkov, #2787)
* Remove monkey-patching syntax. (Phil Pirozhkov, #2803)
* Extract `should` syntax (including the non-monkey-patching one liner). (Phil Pirozhkov, #2803)
* Remove globally-exposed DSL (example and shared group methods
in the root scope and on Module). (Phil Pirozhkov, #2803)

Enhancements:

Expand Down
20 changes: 4 additions & 16 deletions features/subject/one_liner_syntax.feature
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
Feature: One-liner syntax

RSpec supports a one-liner syntax for setting an expectation on the
`subject`. RSpec will give the examples a doc string that is auto-
RSpec supports a one-liner syntax, `is_expected`, for setting an expectation
on the `subject`. RSpec will give the examples a doc string that is auto-
generated from the matcher used in the example. This is designed specifically
to help avoid duplication in situations where the doc string and the matcher
used in the example mirror each other exactly. When used excessively, it can
produce documentation output that does not read well or contribute to
understanding the object you are describing.

This comes in two flavors:

* `is_expected` is defined simply as `expect(subject)` and is designed for
when you are using rspec-expectations with its newer expect-based syntax.
* `should` was designed back when rspec-expectations only had a should-based
syntax.
understanding the object you are describing. This syntax is a shorthand for
`expect(subject)`.

Notes:

Expand All @@ -26,8 +20,6 @@ Feature: One-liner syntax
"""ruby
RSpec.describe Array do
describe "when first created" do
it { should be_empty }
# or
it { is_expected.to be_empty }
end
end
Expand All @@ -39,7 +31,6 @@ Feature: One-liner syntax
Array
when first created
is expected to be empty
is expected to be empty
"""

Scenario: Explicit subject
Expand All @@ -48,8 +39,6 @@ Feature: One-liner syntax
RSpec.describe Array do
describe "with 3 items" do
subject { [1,2,3] }
it { should_not be_empty }
# or
it { is_expected.not_to be_empty }
end
end
Expand All @@ -61,5 +50,4 @@ Feature: One-liner syntax
Array
with 3 items
is expected not to be empty
is expected not to be empty
"""
57 changes: 4 additions & 53 deletions lib/rspec/core/memoized_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ module MemoizedHelpers
#
# RSpec.describe Widget do
# it { is_expected.to validate_presence_of(:name) }
# # or
# it { should validate_presence_of(:name) }
# end
#
# While the examples below demonstrate how to use `subject`
Expand All @@ -25,32 +23,28 @@ module MemoizedHelpers
# # Explicit declaration of subject.
# RSpec.describe Person do
# subject { Person.new(:birthdate => 19.years.ago) }
# it "should be eligible to vote" do
# should be_eligible_to_vote
# it "is eligible to vote" do
# is_expected.to be_eligible_to_vote
# end
# end
#
# # Implicit subject => { Person.new }.
# RSpec.describe Person do
# it "should be eligible to vote" do
# should be_eligible_to_vote
# it "is eligible to vote" do
# is_expected.to be_eligible_to_vote
# end
# end
#
# # One-liner syntax - expectation is set on the subject.
# RSpec.describe Person do
# it { is_expected.to be_eligible_to_vote }
# # or
# it { should be_eligible_to_vote }
# end
#
# @note Because `subject` is designed to create state that is reset
# between each example, and `before(:context)` is designed to setup
# state that is shared across _all_ examples in an example group,
# `subject` is _not_ intended to be used in a `before(:context)` hook.
#
# @see #should
# @see #should_not
# @see #is_expected
def subject
__memoized.fetch_or_store(:subject) do
Expand All @@ -59,45 +53,6 @@ def subject
end
end

# When `should` is called with no explicit receiver, the call is
# delegated to the object returned by `subject`. Combined with an
# implicit subject this supports very concise expressions.
#
# @example
#
# RSpec.describe Person do
# it { should be_eligible_to_vote }
# end
#
# @see #subject
# @see #is_expected
#
# @note This only works if you are using rspec-expectations.
# @note If you are using RSpec's newer expect-based syntax you may
# want to use `is_expected.to` instead of `should`.
def should(matcher=nil, message=nil)
RSpec::Expectations::PositiveExpectationHandler.handle_matcher(subject, matcher, message)
end

# Just like `should`, `should_not` delegates to the subject (implicit or
# explicit) of the example group.
#
# @example
#
# RSpec.describe Person do
# it { should_not be_eligible_to_vote }
# end
#
# @see #subject
# @see #is_expected
#
# @note This only works if you are using rspec-expectations.
# @note If you are using RSpec's newer expect-based syntax you may
# want to use `is_expected.to_not` instead of `should_not`.
def should_not(matcher=nil, message=nil)
RSpec::Expectations::NegativeExpectationHandler.handle_matcher(subject, matcher, message)
end

# Wraps the `subject` in `expect` to make it the target of an expectation.
# Designed to read nicely for one-liners.
#
Expand All @@ -109,8 +64,6 @@ def should_not(matcher=nil, message=nil)
# end
#
# @see #subject
# @see #should
# @see #should_not
#
# @note This only works if you are using rspec-expectations.
def is_expected
Expand Down Expand Up @@ -410,8 +363,6 @@ def let!(name, &block)
# end
# end
#
# @see MemoizedHelpers#should
# @see MemoizedHelpers#should_not
# @see MemoizedHelpers#is_expected
def subject(name=nil, &block)
if name
Expand Down
2 changes: 1 addition & 1 deletion spec/rspec/core/example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1873,7 +1873,7 @@ def foo; end
# for users. RSpec internals should not add methods here, though.
expect(rspec_core_methods.map(&:to_sym)).to contain_exactly(
:described_class, :subject,
:is_expected, :should, :should_not,
:is_expected,
:pending, :skip,
:setup_mocks_for_rspec, :teardown_mocks_for_rspec, :verify_mocks_for_rspec
)
Expand Down
8 changes: 4 additions & 4 deletions spec/rspec/core/memoized_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def working_with?(double)
end.new 1
end

it { should be_working_with double(:value => 10) }
it { is_expected.to be_working_with double(:value => 10) }
end

[false, nil].each do |falsy_value|
Expand Down Expand Up @@ -331,9 +331,9 @@ def should_raise_not_supported_error(&block)
def ok?; true; end
def not_ok?; false; end

it { should eq(self) }
it { should be_ok }
it { should_not be_not_ok }
it { is_expected.to eq(self) }
it { is_expected.to be_ok }
it { is_expected.not_to be_not_ok }
end

expect(group.run).to be true
Expand Down
2 changes: 1 addition & 1 deletion spec/rspec/core/metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module Core
expect([nil, "."]).to include(value)
end

it 'should not transform directories beginning with the same prefix' do
it 'does not transform directories beginning with the same prefix' do
#E.g. /foo/bar_baz is not relative to /foo/bar !!

similar_directory = "#{File.expand_path(".")}_similar"
Expand Down
12 changes: 6 additions & 6 deletions spec/rspec/core/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ def interrupt
instance_double(::DRb::DRbServer, :uri => "druby://127.0.0.1:0000/", :alive? => true)
end

it { should be_truthy }
it { is_expected.to be_truthy }
end

context "when drb server is started with localhost" do
let(:drb_server) do
instance_double(::DRb::DRbServer, :uri => "druby://localhost:0000/", :alive? => true)
end

it { should be_truthy }
it { is_expected.to be_truthy }
end

context "when drb server is started with another local ip address" do
Expand All @@ -197,15 +197,15 @@ def interrupt
allow(::IPSocket).to receive(:getaddress).and_return("192.168.0.1")
end

it { should be_truthy }
it { is_expected.to be_truthy }
end

context "when drb server is started with 127.0.0.1 but not alive" do
let(:drb_server) do
instance_double(::DRb::DRbServer, :uri => "druby://127.0.0.1:0000/", :alive? => false)
end

it { should be_falsey }
it { is_expected.to be_falsey }
end

context "when IPSocket cannot resolve the current hostname" do
Expand All @@ -219,15 +219,15 @@ def interrupt
)
end

it { should be_falsey }
it { is_expected.to be_falsey }
end

context "when no drb server is running" do
let(:drb_server) do
raise ::DRb::DRbServerNotFound
end

it { should be_falsey }
it { is_expected.to be_falsey }
end
end

Expand Down

0 comments on commit 712689e

Please sign in to comment.