Skip to content

Commit

Permalink
Merge pull request #731 from freerange/improvements-to-keyword-argume…
Browse files Browse the repository at this point in the history
…nt-matcher-unit-test

Improvements to `PositionalOrKeywordHashTest` unit test
  • Loading branch information
floehopper authored Jan 4, 2025
2 parents 067f057 + 9bbeb32 commit 3c85cad
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 100 deletions.
1 change: 1 addition & 0 deletions test/acceptance/loose_keyword_argument_matching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require 'execution_point'
require 'mocha/ruby_version'
require 'mocha/configuration'

class LooseKeywordArgumentMatchingTest < Mocha::TestCase
include AcceptanceTestHelper
Expand Down
1 change: 1 addition & 0 deletions test/acceptance/strict_keyword_argument_matching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require File.expand_path('../acceptance_test_helper', __FILE__)

require 'mocha/ruby_version'
require 'mocha/configuration'

if Mocha::RUBY_V27_PLUS
class StrictKeywordArgumentMatchingTest < Mocha::TestCase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# frozen_string_literal: true

require File.expand_path('../../../test_helper', __FILE__)

require 'deprecation_capture'
require 'mocha/parameter_matchers/positional_or_keyword_hash'
require 'mocha/parameter_matchers/instance_methods'
require 'mocha/inspect'
require 'mocha/expectation'
require 'mocha/ruby_version'
require 'mocha/configuration'

class LoosePositionalOrKeywordHashTest < Mocha::TestCase
include Mocha::ParameterMatchers
include DeprecationCapture

def setup
return unless Mocha::RUBY_V27_PLUS

@original = Mocha.configuration.strict_keyword_argument_matching?
Mocha.configure { |c| c.strict_keyword_argument_matching = false }
end

def teardown
return unless Mocha::RUBY_V27_PLUS

Mocha.configure { |c| c.strict_keyword_argument_matching = @original }
end

def test_should_match_hash_arg_with_keyword_args_but_display_deprecation_warning_if_appropriate
expectation = Mocha::Expectation.new(self, :foo)
matcher = build_matcher(Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 }), expectation)
capture_deprecation_warnings do
assert matcher.matches?([{ key_1: 1, key_2: 2 }])
end
return unless Mocha::RUBY_V27_PLUS

message = last_deprecation_warning
location = expectation.definition_location
assert_includes message, "Expectation defined at #{location} expected keyword arguments (key_1: 1, key_2: 2)"
assert_includes message, 'but received positional hash ({key_1: 1, key_2: 2})'
assert_includes message, 'These will stop matching when strict keyword argument matching is enabled.'
assert_includes message, 'See the documentation for Mocha::Configuration#strict_keyword_argument_matching=.'
end

def test_should_match_keyword_args_with_hash_arg_but_display_deprecation_warning_if_appropriate
expectation = Mocha::Expectation.new(self, :foo)
matcher = build_matcher({ key_1: 1, key_2: 2 }, expectation)
capture_deprecation_warnings do
assert matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 })])
end
return unless Mocha::RUBY_V27_PLUS

message = last_deprecation_warning
location = expectation.definition_location
assert_includes message, "Expectation defined at #{location} expected positional hash ({key_1: 1, key_2: 2})"
assert_includes message, 'but received keyword arguments (key_1: 1, key_2: 2)'
assert_includes message, 'These will stop matching when strict keyword argument matching is enabled.'
assert_includes message, 'See the documentation for Mocha::Configuration#strict_keyword_argument_matching=.'
end

def test_should_display_deprecation_warning_even_if_parent_expectation_is_nil
expectation = nil
matcher = build_matcher({ key_1: 1, key_2: 2 }, expectation)
capture_deprecation_warnings do
matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 })])
end
return unless Mocha::RUBY_V27_PLUS

message = last_deprecation_warning
assert_includes message, 'Expectation expected positional hash ({key_1: 1, key_2: 2})'
assert_includes message, 'but received keyword arguments (key_1: 1, key_2: 2)'
end

private

def build_matcher(hash, expectation = nil)
Mocha::ParameterMatchers::PositionalOrKeywordHash.new(hash, expectation)
end
end
100 changes: 0 additions & 100 deletions test/unit/parameter_matchers/positional_or_keyword_hash_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require File.expand_path('../../../test_helper', __FILE__)

require 'deprecation_capture'
require 'mocha/parameter_matchers/positional_or_keyword_hash'
require 'mocha/parameter_matchers/instance_methods'
require 'mocha/inspect'
Expand All @@ -11,7 +10,6 @@

class PositionalOrKeywordHashTest < Mocha::TestCase
include Mocha::ParameterMatchers
include DeprecationCapture

def test_should_describe_matcher
hash = { key_1: 1, key_2: 2 }
Expand Down Expand Up @@ -63,104 +61,6 @@ def test_should_not_match_keyword_args_with_matchers_using_keyword_args_when_not
assert !matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 'foo', key_2: 2 })])
end

def test_should_match_hash_arg_with_keyword_args_but_display_deprecation_warning_if_appropriate
expectation = Mocha::Expectation.new(self, :foo)
matcher = build_matcher(Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 }), expectation)
Mocha::Configuration.override(strict_keyword_argument_matching: false) do
capture_deprecation_warnings do
assert matcher.matches?([{ key_1: 1, key_2: 2 }])
end
end
return unless Mocha::RUBY_V27_PLUS

message = last_deprecation_warning
location = expectation.definition_location
assert_includes message, "Expectation defined at #{location} expected keyword arguments (key_1: 1, key_2: 2)"
assert_includes message, 'but received positional hash ({key_1: 1, key_2: 2})'
assert_includes message, 'These will stop matching when strict keyword argument matching is enabled.'
assert_includes message, 'See the documentation for Mocha::Configuration#strict_keyword_argument_matching=.'
end

def test_should_match_keyword_args_with_hash_arg_but_display_deprecation_warning_if_appropriate
expectation = Mocha::Expectation.new(self, :foo)
matcher = build_matcher({ key_1: 1, key_2: 2 }, expectation)
Mocha::Configuration.override(strict_keyword_argument_matching: false) do
capture_deprecation_warnings do
assert matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 })])
end
end
return unless Mocha::RUBY_V27_PLUS

message = last_deprecation_warning
location = expectation.definition_location
assert_includes message, "Expectation defined at #{location} expected positional hash ({key_1: 1, key_2: 2})"
assert_includes message, 'but received keyword arguments (key_1: 1, key_2: 2)'
assert_includes message, 'These will stop matching when strict keyword argument matching is enabled.'
assert_includes message, 'See the documentation for Mocha::Configuration#strict_keyword_argument_matching=.'
end

if Mocha::RUBY_V27_PLUS
def test_should_match_non_last_hash_arg_with_hash_arg_when_strict_keyword_args_is_enabled
hash = { key_1: 1, key_2: 2 }
matcher = build_matcher(hash)
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
assert matcher.matches?([{ key_1: 1, key_2: 2 }, %w[a b]])
end
end

def test_should_not_match_non_hash_arg_with_hash_arg_when_strict_keyword_args_is_enabled
hash = { key_1: 1, key_2: 2 }
matcher = build_matcher(hash)
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
assert !matcher.matches?([%w[a b]])
end
end

def test_should_match_hash_arg_with_hash_arg_when_strict_keyword_args_is_enabled
hash = { key_1: 1, key_2: 2 }
matcher = build_matcher(hash)
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
assert matcher.matches?([{ key_1: 1, key_2: 2 }])
end
end

def test_should_match_keyword_args_with_keyword_args_when_strict_keyword_args_is_enabled
matcher = build_matcher(Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 }))
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
assert matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 })])
end
end

def test_should_not_match_hash_arg_with_keyword_args_when_strict_keyword_args_is_enabled
matcher = build_matcher(Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 }))
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
hash = { key_1: 1, key_2: 2 }
matcher = build_matcher(hash)
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
assert !matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 })])
end
end

def test_should_display_deprecation_warning_even_if_parent_expectation_is_nil
expectation = nil
matcher = build_matcher({ key_1: 1, key_2: 2 }, expectation)
Mocha::Configuration.override(strict_keyword_argument_matching: false) do
capture_deprecation_warnings do
matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 })])
end
end

message = last_deprecation_warning
assert_includes message, 'Expectation expected positional hash ({key_1: 1, key_2: 2})'
assert_includes message, 'but received keyword arguments (key_1: 1, key_2: 2)'
end
end

private

def build_matcher(hash, expectation = nil)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

require File.expand_path('../../../test_helper', __FILE__)

require 'mocha/parameter_matchers/positional_or_keyword_hash'
require 'mocha/parameter_matchers/instance_methods'
require 'mocha/inspect'
require 'mocha/expectation'
require 'mocha/ruby_version'
require 'mocha/configuration'

class StrictPositionalOrKeywordHashTest < Mocha::TestCase
include Mocha::ParameterMatchers

def setup
return unless Mocha::RUBY_V27_PLUS

@original = Mocha.configuration.strict_keyword_argument_matching?
Mocha.configure { |c| c.strict_keyword_argument_matching = true }
end

def teardown
return unless Mocha::RUBY_V27_PLUS

Mocha.configure { |c| c.strict_keyword_argument_matching = @original } if Mocha::RUBY_V27_PLUS
end

if Mocha::RUBY_V27_PLUS
def test_should_match_non_last_hash_arg_with_hash_arg
hash = { key_1: 1, key_2: 2 }
matcher = build_matcher(hash)
assert matcher.matches?([{ key_1: 1, key_2: 2 }, %w[a b]])
end

def test_should_not_match_non_hash_arg_with_hash_arg
hash = { key_1: 1, key_2: 2 }
matcher = build_matcher(hash)
assert !matcher.matches?([%w[a b]])
end

def test_should_match_hash_arg_with_hash_arg
hash = { key_1: 1, key_2: 2 }
matcher = build_matcher(hash)
assert matcher.matches?([{ key_1: 1, key_2: 2 }])
end

def test_should_match_keyword_args_with_keyword_args
matcher = build_matcher(Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 }))
assert matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 })])
end

def test_should_not_match_hash_arg_with_keyword_args
matcher = build_matcher(Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 }))
assert !matcher.matches?([{ key_1: 1, key_2: 2 }])
end

def test_should_not_match_keyword_args_with_hash_arg
hash = { key_1: 1, key_2: 2 }
matcher = build_matcher(hash)
assert !matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 })])
end
end

private

def build_matcher(hash, expectation = nil)
Mocha::ParameterMatchers::PositionalOrKeywordHash.new(hash, expectation)
end
end

0 comments on commit 3c85cad

Please sign in to comment.