-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement optional strict keyword arg matching
When the strict keyword argument option is enabled, an expectation expecting keyword arguments (via Expectation#with) will no longer match an invocation passing a positional Hash argument. Without this option, positional hash and keyword arguments are treated the same during comparison, which can lead to false negatives in Ruby >= v3.0 (see examples below). For more details on keyword arguments in Ruby v3, refer to this article [1]. Note that Hash matchers such as has_value or has_key will still treat positional hash and keyword arguments the same, so false negatives are still possible when they are used. Closes #446. See also #535 & #544 for discussions relating to this change. [1]: https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0
- Loading branch information
1 parent
1b4b6bb
commit 98487db
Showing
4 changed files
with
96 additions
and
4 deletions.
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
31 changes: 31 additions & 0 deletions
31
lib/mocha/parameter_matchers/positional_or_keyword_hash.rb
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,31 @@ | ||
require 'mocha/configuration' | ||
require 'mocha/parameter_matchers/base' | ||
|
||
module Mocha | ||
module ParameterMatchers | ||
# @private | ||
class PositionalOrKeywordHash < Base | ||
def initialize(value) | ||
@value = value | ||
end | ||
|
||
def matches?(available_parameters) | ||
parameter, is_last_parameter = extract_parameter(available_parameters) | ||
if is_last_parameter && Mocha.configuration.strict_keyword_argument_matching? | ||
return false unless ::Hash.ruby2_keywords_hash?(parameter) == ::Hash.ruby2_keywords_hash?(@value) | ||
end | ||
parameter == @value | ||
end | ||
|
||
def mocha_inspect | ||
@value.mocha_inspect | ||
end | ||
|
||
private | ||
|
||
def extract_parameter(available_parameters) | ||
[available_parameters.shift, available_parameters.empty?] | ||
end | ||
end | ||
end | ||
end |
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