-
-
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.
Merge pull request #731 from freerange/improvements-to-keyword-argume…
…nt-matcher-unit-test Improvements to `PositionalOrKeywordHashTest` unit test
- Loading branch information
Showing
5 changed files
with
151 additions
and
100 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
80 changes: 80 additions & 0 deletions
80
test/unit/parameter_matchers/loose_positional_or_keyword_hash_test.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,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 |
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
69 changes: 69 additions & 0 deletions
69
test/unit/parameter_matchers/strict_positional_or_keyword_hash_test.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,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 |