Skip to content

Commit

Permalink
Change to_matcher method to take keyword arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
floehopper committed Jul 16, 2024
1 parent e9de64e commit 3b60b7d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/mocha/parameter_matchers/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module ParameterMatchers
# @private
module InstanceMethods
# @private
def to_matcher(expectation = nil, top_level = false)
def to_matcher(expectation: nil, top_level: false)
if is_a?(Base)
self
elsif is_a?(Hash) && top_level
Expand Down
2 changes: 1 addition & 1 deletion lib/mocha/parameters_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def mocha_inspect
end

def matchers
@expected_parameters.map { |p| p.to_matcher(@expectation, true) }
@expected_parameters.map { |p| p.to_matcher(expectation: @expectation, top_level: true) }
end
end
end
12 changes: 6 additions & 6 deletions test/unit/parameter_matchers/positional_or_keyword_hash_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ def test_should_match_keyword_args_with_keyword_args
end

def test_should_match_keyword_args_with_matchers_using_keyword_args
matcher = Hash.ruby2_keywords_hash({ key_1: is_a(String), key_2: is_a(Integer) }).to_matcher(nil, true) # rubocop:disable Style/BracesAroundHashParameters
matcher = Hash.ruby2_keywords_hash({ key_1: is_a(String), key_2: is_a(Integer) }).to_matcher(top_level: true) # rubocop:disable Style/BracesAroundHashParameters
assert matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 'foo', key_2: 2 })]) # rubocop:disable Style/BracesAroundHashParameters
end

def test_should_match_hash_arg_with_keyword_args_but_display_deprecation_warning_if_appropriate
expectation = Mocha::Expectation.new(self, :foo); execution_point = ExecutionPoint.current
matcher = Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 }).to_matcher(expectation, true) # rubocop:disable Style/BracesAroundHashParameters
matcher = Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 }).to_matcher(expectation: expectation, top_level: true) # rubocop:disable Style/BracesAroundHashParameters
DeprecationDisabler.disable_deprecations do
assert matcher.matches?([{ key_1: 1, key_2: 2 }])
end
Expand All @@ -58,7 +58,7 @@ def test_should_match_hash_arg_with_keyword_args_but_display_deprecation_warning

def test_should_match_keyword_args_with_hash_arg_but_display_deprecation_warning_if_appropriate
expectation = Mocha::Expectation.new(self, :foo); execution_point = ExecutionPoint.current
matcher = { key_1: 1, key_2: 2 }.to_matcher(expectation, true)
matcher = { key_1: 1, key_2: 2 }.to_matcher(expectation: expectation, top_level: true)
DeprecationDisabler.disable_deprecations do
assert matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 })]) # rubocop:disable Style/BracesAroundHashParameters
end
Expand Down Expand Up @@ -102,22 +102,22 @@ def test_should_match_keyword_args_with_keyword_args_when_strict_keyword_args_is
end

def test_should_not_match_hash_arg_with_keyword_args_when_strict_keyword_args_is_enabled
matcher = Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 }).to_matcher(nil, true) # rubocop:disable Style/BracesAroundHashParameters
matcher = Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 }).to_matcher(top_level: true) # rubocop:disable Style/BracesAroundHashParameters
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
assert !matcher.matches?([{ key_1: 1, key_2: 2 }])
end
end

def test_should_not_match_keyword_args_with_hash_arg_when_strict_keyword_args_is_enabled
matcher = { key_1: 1, key_2: 2 }.to_matcher(nil, true)
matcher = { key_1: 1, key_2: 2 }.to_matcher(top_level: true)
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
assert !matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 })]) # rubocop:disable Style/BracesAroundHashParameters
end
end

def test_should_display_deprecation_warning_even_if_parent_expectation_is_nil
expectation = nil
matcher = { key_1: 1, key_2: 2 }.to_matcher(expectation, true)
matcher = { key_1: 1, key_2: 2 }.to_matcher(expectation: expectation, top_level: true)
DeprecationDisabler.disable_deprecations do
matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 })]) # rubocop:disable Style/BracesAroundHashParameters
end
Expand Down

0 comments on commit 3b60b7d

Please sign in to comment.