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 authored and JonRowe committed Dec 31, 2020
1 parent c033456 commit e8e8bfc
Show file tree
Hide file tree
Showing 13 changed files with 19 additions and 435 deletions.
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,6 @@ expect(
).to match([a_hash_including(:a => 'hash'), a_hash_including(:a => 'another')])
```

## `should` syntax

In addition to the `expect` syntax, rspec-expectations continues to support the
non-monkey patching `should` syntax:

```ruby
subject(:number) { 4 }
it { should eq(4) }
it { should be > 3 }
it { should_not be_odd }
```

## Compound Matcher Expressions

Expand Down
1 change: 0 additions & 1 deletion features/.nav
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
- have_attributes.feature
- include.feature
- match.feature
- operators.feature
- raise_error.feature
- respond_to.feature
- satisfy.feature
Expand Down
24 changes: 12 additions & 12 deletions features/built_in_matchers/predicates.feature
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ Feature: Predicate matchers

Any arguments passed to the matcher will be passed on to the predicate method.

Scenario: should be_zero (based on Integer#zero?)
Given a file named "should_be_zero_spec.rb" with:
Scenario: is_expected.to be_zero (based on Integer#zero?)
Given a file named "be_zero_spec.rb" with:
"""ruby
RSpec.describe 0 do
it { is_expected.to be_zero }
Expand All @@ -58,12 +58,12 @@ Feature: Predicate matchers
it { is_expected.to be_zero } # deliberate failure
end
"""
When I run `rspec should_be_zero_spec.rb`
When I run `rspec be_zero_spec.rb`
Then the output should contain "2 examples, 1 failure"
And the output should contain "expected `7.zero?` to be truthy, got false"

Scenario: should_not be_empty (based on Array#empty?)
Given a file named "should_not_be_empty_spec.rb" with:
Scenario: is_expected.not_to be_empty (based on Array#empty?)
Given a file named "not_to_be_empty_spec.rb" with:
"""ruby
RSpec.describe [1, 2, 3] do
it { is_expected.not_to be_empty }
Expand All @@ -73,25 +73,25 @@ Feature: Predicate matchers
it { is_expected.not_to be_empty } # deliberate failure
end
"""
When I run `rspec should_not_be_empty_spec.rb`
When I run `rspec not_to_be_empty_spec.rb`
Then the output should contain "2 examples, 1 failure"
And the output should contain "expected `[].empty?` to be falsey, got true"

Scenario: should have_key (based on Hash#has_key?)
Given a file named "should_have_key_spec.rb" with:
Scenario: is_expected.to have_key (based on Hash#has_key?)
Given a file named "have_key_spec.rb" with:
"""ruby
RSpec.describe Hash do
subject { { :foo => 7 } }
it { is_expected.to have_key(:foo) }
it { is_expected.to have_key(:bar) } # deliberate failure
end
"""
When I run `rspec should_have_key_spec.rb`
When I run `rspec have_key_spec.rb`
Then the output should contain "2 examples, 1 failure"
And the output should contain "expected `{:foo=>7}.has_key?(:bar)` to be truthy, got false"

Scenario: should_not have_all_string_keys (based on custom #has_all_string_keys? method)
Given a file named "should_not_have_all_string_keys_spec.rb" with:
Scenario: is_expected.to have_decimals (based on custom #have_decimals? method)
Given a file named "have_decimals_spec.rb" with:
"""ruby
class Float
def has_decimals?
Expand All @@ -112,7 +112,7 @@ Feature: Predicate matchers
end
end
"""
When I run `rspec should_not_have_all_string_keys_spec.rb`
When I run `rspec have_decimals_spec.rb`
Then the output should contain "2 examples, 1 failure"
And the output should contain "expected `42.0.has_decimals?` to be truthy, got false"

Expand Down
6 changes: 3 additions & 3 deletions features/custom_matchers/define_matcher.feature
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Feature: Define a custom matcher
end
RSpec.describe "these two arrays" do
specify "should be similar" do
it "is similar" do
expect([1,2,3]).to have_same_elements_as([2,3,1])
end
end
Expand Down Expand Up @@ -278,7 +278,7 @@ Feature: Define a custom matcher
Then the output should contain "3 examples, 0 failures"

Scenario: Matcher with separate logic for expect().to and expect().not_to
Given a file named "matcher_with_separate_should_not_logic_spec.rb" with:
Given a file named "matcher_with_separate_not_to_logic_spec.rb" with:
"""ruby
RSpec::Matchers.define :contain do |*expected|
match do |actual|
Expand All @@ -299,7 +299,7 @@ Feature: Define a custom matcher
it { is_expected.not_to contain(1, 4) }
end
"""
When I run `rspec matcher_with_separate_should_not_logic_spec.rb`
When I run `rspec matcher_with_separate_not_to_logic_spec.rb`
Then the output should contain all of these:
| 4 examples, 2 failures |
| expected [1, 2, 3] to contain 1 and 4 |
Expand Down
3 changes: 1 addition & 2 deletions features/customized_message.feature
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
Feature: customized message

RSpec tries to provide useful failure messages, but for cases in which you want more
specific information, you can define your own message right in the example.This works for
any matcher _other than the operator matchers_.
specific information, you can define your own message right in the example.

Scenario: customize failure message
Given a file named "example_spec.rb" with:
Expand Down
4 changes: 0 additions & 4 deletions lib/rspec/expectations/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ def self.handle_failure(matcher, message, failure_message_method)
class PositiveExpectationHandler
def self.handle_matcher(actual, initial_matcher, custom_message=nil, &block)
ExpectationHelper.with_matcher(self, initial_matcher, custom_message) do |matcher|
return ::RSpec::Matchers::BuiltIn::PositiveOperatorMatcher.new(actual) unless initial_matcher

match_result = matcher.matches?(actual, &block)
if custom_message && match_result.respond_to?(:error_generator)
match_result.error_generator.opts[:message] = custom_message
Expand All @@ -74,8 +72,6 @@ def self.opposite_should_method
class NegativeExpectationHandler
def self.handle_matcher(actual, initial_matcher, custom_message=nil, &block)
ExpectationHelper.with_matcher(self, initial_matcher, custom_message) do |matcher|
return ::RSpec::Matchers::BuiltIn::NegativeOperatorMatcher.new(actual) unless initial_matcher

negated_match_result = does_not_match?(matcher, actual, &block)
if custom_message && negated_match_result.respond_to?(:error_generator)
negated_match_result.error_generator.opts[:message] = custom_message
Expand Down
3 changes: 0 additions & 3 deletions lib/rspec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,6 @@ def change(receiver=nil, message=nil, &block)
# This works for collections. Pass in multiple args and it will only
# pass if all args are found in collection.
#
# @note This is also available using the `=~` operator with `should`,
# but `=~` is not supported with `expect`.
#
# @example
# expect([1, 2, 3]).to contain_exactly(1, 2, 3)
# expect([1, 2, 3]).to contain_exactly(1, 3, 2)
Expand Down
3 changes: 0 additions & 3 deletions lib/rspec/matchers/built_in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ module BuiltIn
autoload :Include, 'rspec/matchers/built_in/include'
autoload :All, 'rspec/matchers/built_in/all'
autoload :Match, 'rspec/matchers/built_in/match'
autoload :NegativeOperatorMatcher, 'rspec/matchers/built_in/operators'
autoload :OperatorMatcher, 'rspec/matchers/built_in/operators'
autoload :Output, 'rspec/matchers/built_in/output'
autoload :PositiveOperatorMatcher, 'rspec/matchers/built_in/operators'
autoload :RaiseError, 'rspec/matchers/built_in/raise_error'
autoload :RespondTo, 'rspec/matchers/built_in/respond_to'
autoload :Satisfy, 'rspec/matchers/built_in/satisfy'
Expand Down
128 changes: 0 additions & 128 deletions lib/rspec/matchers/built_in/operators.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/rspec/matchers/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ module DefaultImplementations
include BuiltIn::BaseMatcher::DefaultFailureMessages

# @api private
# Used internally by objects returns by `should` and `should_not`.
# Used internally by handlers and compound matchers.
def diffable?
false
end
Expand Down
4 changes: 2 additions & 2 deletions spec/rspec/expectations/expectation_target_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ module Expectations
it 'does not support operator matchers from #to' do
expect {
expect(3).to == 3
}.to raise_error(ArgumentError)
}.to raise_error(ArgumentError, /The expect syntax does not support operator matchers, so you must pass a matcher to `#to`/)
end

it 'does not support operator matchers from #not_to' do
expect {
expect(3).not_to == 4
}.to raise_error(ArgumentError)
}.to raise_error(ArgumentError, /The expect syntax does not support operator matchers, so you must pass a matcher to `#not_to`/)
end
end

Expand Down
14 changes: 0 additions & 14 deletions spec/rspec/matchers/built_in/be_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -746,20 +746,6 @@ def send
end
end

RSpec.describe "should be =~" do
subject { "a string" }

it "passes when =~ operator returns true" do
should be =~ /str/
end

it "fails when =~ operator returns false" do
expect {
should be =~ /blah/
}.to fail_with(%(expected: =~ /blah/\n got: "a string"))
end
end

RSpec.describe "expect(...).to be ===" do
it "passes when === operator returns true" do
expect(Hash).to be === {}
Expand Down
Loading

0 comments on commit e8e8bfc

Please sign in to comment.