Skip to content

Commit

Permalink
[expectations] Revert "Merge pull request rspec/rspec-expectations#1125
Browse files Browse the repository at this point in the history
… from pirj/prevent-block-matchers-from-being-used-with-value-expectation-target"

This reverts commit f5bf9d8264ffd786a66fe25c3bb1125c93b59d89.

---
This commit was imported from rspec/rspec-expectations@9423442.
  • Loading branch information
JonRowe committed Oct 7, 2019
1 parent 4515ec6 commit 10ac688
Show file tree
Hide file tree
Showing 46 changed files with 242 additions and 456 deletions.
9 changes: 8 additions & 1 deletion rspec-expectations/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
### Development

Bug Fixes:

* Revert #1125 due to the change being incompatible with our semantic versioning
policy.

### 3.8.5 / 2019-10-02
[Full Changelog](http://github.com/rspec/rspec-expectations/compare/v3.8.4...v3.8.5)

Expand All @@ -22,7 +29,7 @@ Bug Fixes:
* Prevent composed `all` matchers from leaking into their siblings leading to duplicate
failures. (Jamie English, #1086)
* Prevent objects which change their hash on comparison from failing change checks.
(Phil Pirozhkov, #1100)
(Phil Pirozhkov, #1110)
* Issue an `ArgumentError` rather than a `NoMethodError` when `be_an_instance_of` and
`be_kind_of` matchers encounter objects not supporting those methods.
(Taichi Ishitani, #1107)
Expand Down
50 changes: 7 additions & 43 deletions rspec-expectations/lib/rspec/expectations/expectation_target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ def initialize(value)
end

# @private
def self.for(value, &block)
def self.for(value, block)
if UndefinedValue.equal?(value)
unless block_given?
unless block
raise ArgumentError, "You must pass either an argument or a block to `expect`."
end
BlockExpectationTarget.new(block)
elsif block_given?
elsif block
raise ArgumentError, "You cannot pass both an argument and a block to `expect`."
else
ValueExpectationTarget.new(value)
new(value)
end
end

Expand Down Expand Up @@ -90,40 +90,6 @@ def prevent_operator_matchers(verb)
include InstanceMethods
end

# @private
# Validates the provided matcher to ensure it supports block
# expectations, in order to avoid user confusion when they
# use a block thinking the expectation will be on the return
# value of the block rather than the block itself.
class ValueExpectationTarget < ExpectationTarget
def to(matcher=nil, message=nil, &block)
enforce_value_expectation(matcher)
super
end

def not_to(matcher=nil, message=nil, &block)
enforce_value_expectation(matcher)
super
end

private

def enforce_value_expectation(matcher)
return if supports_value_expectations?(matcher)

raise ExpectationNotMetError, "You must pass a block rather than an argument to `expect` to use the provided " \
"block expectation matcher (#{RSpec::Support::ObjectFormatter.format(matcher)})."
end

def supports_value_expectations?(matcher)
if matcher.respond_to?(:supports_value_expectations?)
matcher.supports_value_expectations?
else
true
end
end
end

# @private
# Validates the provided matcher to ensure it supports block
# expectations, in order to avoid user confusion when they
Expand Down Expand Up @@ -152,11 +118,9 @@ def enforce_block_expectation(matcher)
end

def supports_block_expectations?(matcher)
if matcher.respond_to?(:supports_block_expectations?)
matcher.supports_block_expectations?
else
false
end
matcher.supports_block_expectations?
rescue NoMethodError
false
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion rspec-expectations/lib/rspec/expectations/syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def enable_expect(syntax_host=::RSpec::Matchers)

syntax_host.module_exec do
def expect(value=::RSpec::Expectations::ExpectationTarget::UndefinedValue, &block)
::RSpec::Expectations::ExpectationTarget.for(value, &block)
::RSpec::Expectations::ExpectationTarget.for(value, block)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ def supports_block_expectations?
false
end

# @private
def supports_value_expectations?
true
end

# @api private
def expects_call_stack_jump?
false
Expand Down
25 changes: 10 additions & 15 deletions rspec-expectations/lib/rspec/matchers/built_in/change.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ def supports_block_expectations?
true
end

# @private
def supports_value_expectations?
false
end

private

def initialize(receiver=nil, message=nil, &block)
Expand Down Expand Up @@ -112,10 +107,12 @@ def raise_block_syntax_error
end

def positive_failure_reason
return "was not given a block" unless Proc === @event_proc
"is still #{@actual_before_description}"
end

def negative_failure_reason
return "was not given a block" unless Proc === @event_proc
"did change from #{@actual_before_description} " \
"to #{description_of change_details.actual_after}"
end
Expand Down Expand Up @@ -161,14 +158,10 @@ def supports_block_expectations?
true
end

# @private
def supports_value_expectations?
false
end

private

def failure_reason
return "was not given a block" unless Proc === @event_proc
"was changed by #{description_of @change_details.actual_delta}"
end
end
Expand Down Expand Up @@ -197,6 +190,7 @@ def description

# @private
def failure_message
return not_given_a_block_failure unless Proc === @event_proc
return before_value_failure unless @matches_before
return did_not_change_failure unless @change_details.changed?
after_value_failure
Expand All @@ -207,11 +201,6 @@ def supports_block_expectations?
true
end

# @private
def supports_value_expectations?
false
end

private

def perform_change(event_proc)
Expand Down Expand Up @@ -253,6 +242,11 @@ def did_change_failure
"did change from #{@actual_before_description} " \
"to #{description_of @change_details.actual_after}"
end

def not_given_a_block_failure
"expected #{@change_details.value_representation} to have changed " \
"#{change_description}, but was not given a block"
end
end

# @api private
Expand Down Expand Up @@ -284,6 +278,7 @@ def does_not_match?(event_proc)

# @private
def failure_message_when_negated
return not_given_a_block_failure unless Proc === @event_proc
return before_value_failure unless @matches_before
did_change_failure
end
Expand Down
14 changes: 0 additions & 14 deletions rspec-expectations/lib/rspec/matchers/built_in/compound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,11 @@ def description
"#{matcher_1.description} #{conjunction} #{matcher_2.description}"
end

# @api private
def supports_block_expectations?
matcher_supports_block_expectations?(matcher_1) &&
matcher_supports_block_expectations?(matcher_2)
end

# @api private
def supports_value_expectations?
matcher_supports_value_expectations?(matcher_1) &&
matcher_supports_value_expectations?(matcher_2)
end

# @api private
def expects_call_stack_jump?
NestedEvaluator.matcher_expects_call_stack_jump?(matcher_1) ||
NestedEvaluator.matcher_expects_call_stack_jump?(matcher_2)
Expand Down Expand Up @@ -110,12 +102,6 @@ def matcher_supports_block_expectations?(matcher)
false
end

def matcher_supports_value_expectations?(matcher)
matcher.supports_value_expectations?
rescue NoMethodError
true
end

def matcher_is_diffable?(matcher)
matcher.diffable?
rescue NoMethodError
Expand Down
9 changes: 2 additions & 7 deletions rspec-expectations/lib/rspec/matchers/built_in/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,20 @@ def supports_block_expectations?
true
end

# @api private
# Indicates this matcher matches against a block only.
# @return [False]
def supports_value_expectations?
false
end

private

def captured?
@actual.length > 0
end

def positive_failure_reason
return "was not a block" unless Proc === @block
return "output #{actual_output_description}" if @expected
"did not"
end

def negative_failure_reason
return "was not a block" unless Proc === @block
"output #{actual_output_description}"
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ def supports_block_expectations?
true
end

# @private
def supports_value_expectations?
false
end

# @private
def expects_call_stack_jump?
true
end
Expand Down Expand Up @@ -205,6 +199,7 @@ def format_backtrace(backtrace)
end

def given_error
return " but was not given a block" unless Proc === @given_proc
return " but nothing was raised" unless @actual_error

backtrace = format_backtrace(@actual_error.backtrace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,20 @@ def description
end

# @api private
# Indicates this matcher matches against a block.
# @return [True]
def supports_block_expectations?
true
end

# @api private
def supports_value_expectations?
false
end

# @api private
def expects_call_stack_jump?
true
end

private

def actual_result
return "but was not a block" unless Proc === @block
"got #{caught}"
end

Expand Down

0 comments on commit 10ac688

Please sign in to comment.