-
-
Notifications
You must be signed in to change notification settings - Fork 359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactoring Mocks::Proxy
to make it easier to treat blocks similarly to args for expectations matchers like a_block
#1241
Open
kaiwren
wants to merge
17
commits into
rspec:main
Choose a base branch
from
kaiwren:refactor-extract-ReceivedMessage-from-Proxy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
524637d
rspec-expectations #1065: add support for a_block expectation matcher
kaiwren bfd1b20
Partially refactored out ReceivedMessage from Proxy#message_received
kaiwren 714034b
- Remove premature memoization
kaiwren 930f14a
Move logic from RSpec::Proxy into RSpec::ReceivedMessage
kaiwren 1366c79
Extract concrete ReceivedMessage and ReceivedMessages from Proxy, and…
kaiwren 0db73df
Make ReceivedMessages#partition return ReceivedMessages (and not arra…
kaiwren 3153076
Add TODO to Proxy::message_received to figure out why has_negative_ex…
kaiwren 2a82bde
Improve specs for a_block matcher and handle more edgecases
JonRowe 7848e77
- Merge branch '1065-with-a-block' into refactor-extract-ReceivedMess…
kaiwren 209fb26
Added specs to cover matching mocked and stubbed methods with a &bloc…
kaiwren 2a17542
Adding documentation to a_block
kaiwren 58f44ab
Fixing linting issues to do with whitespaces
kaiwren 6ae1105
Removed space in 'before ()' in specs that was causing the linter to …
kaiwren 9bc5f79
Fixed spelling error in feature name - see https://github.com/rspec/r…
kaiwren 99adb75
Removed space in 'let ()' in specs that was causing the linter to fai…
kaiwren 2ecf7be
replace -> with lambda for 1.8.7
kaiwren 8a27c2a
replace -> with lambda for 1.8.7 in specs - and that's the last -> in…
kaiwren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
module RSpec | ||
module Mocks | ||
# @private | ||
# @see RSpec::Mocks::Proxy | ||
class ReceivedMessage | ||
attr_reader :name, :args, :block | ||
|
||
def initialize(message_name, method_double, *args, &block) | ||
@name = message_name | ||
@method_double = method_double | ||
@args = args | ||
@block = block | ||
end | ||
|
||
def process!(receiving_object, error_generator, is_null_object, has_negative_expectation, messages_arg_list) | ||
message = @name | ||
received_message = self | ||
args = @args | ||
block = @block | ||
expectation = received_message.find_matching_expectation | ||
stub = received_message.find_matching_stub | ||
|
||
if received_message.matching_stub_and_matching_expectation_and_expectation_maxxed? || received_message.only_matching_stub? | ||
expectation.increase_actual_received_count! if expectation && expectation.actual_received_count_matters? | ||
if (expectation = find_almost_matching_expectation) | ||
expectation.advise(*args) unless expectation.expected_messages_received? | ||
end | ||
stub.invoke(nil, *args, &block) | ||
elsif expectation | ||
expectation.unadvise(messages_arg_list) | ||
expectation.invoke(stub, *args, &block) | ||
elsif (expectation = find_almost_matching_expectation) | ||
expectation.advise(*args) if is_null_object unless expectation.expected_messages_received? | ||
|
||
if is_null_object || !has_negative_expectation.call | ||
expectation.raise_unexpected_message_args_error([args]) | ||
end | ||
elsif (stub = find_almost_matching_stub) | ||
stub.advise(*args) | ||
error_generator.raise_missing_default_stub_error(stub, [args]) | ||
elsif Class === receiving_object | ||
receiving_object.superclass.__send__(message, *args, &block) | ||
else | ||
receiving_object.__send__(:method_missing, message, *args, &block) | ||
end | ||
end | ||
|
||
def find_matching_stub | ||
@method_double.stubs.find { |stub| stub.matches?(@name, *@args, &@block) } | ||
end | ||
|
||
def find_matching_expectation | ||
find_best_matching_expectation do |expectation| | ||
expectation.matches?(@name, *@args, &@block) | ||
end | ||
end | ||
|
||
def find_almost_matching_stub | ||
@method_double.stubs.find { |stub| stub.matches_name_but_not_args(@name, *@args) } | ||
end | ||
|
||
def find_almost_matching_expectation | ||
find_best_matching_expectation do |expectation| | ||
expectation.matches_name_but_not_args(@name, *@args) | ||
end | ||
end | ||
|
||
def matching_stub_and_matching_expectation_and_expectation_maxxed? | ||
find_matching_stub && find_matching_expectation && find_matching_expectation.called_max_times? | ||
end | ||
|
||
def only_matching_stub? | ||
find_matching_stub && !find_matching_expectation | ||
end | ||
|
||
private | ||
def find_best_matching_expectation | ||
first_match = nil | ||
|
||
@method_double.expectations.each do |expectation| | ||
next unless yield expectation | ||
return expectation unless expectation.called_max_times? | ||
first_match ||= expectation | ||
end | ||
|
||
first_match | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"invocation"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed in 9bc5f79