-
-
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 #730 from freerange/improvements-to-keyword-argume…
…nt-matching-tests Improvements to keyword argument matching tests
- Loading branch information
Showing
10 changed files
with
224 additions
and
183 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
103 changes: 103 additions & 0 deletions
103
test/acceptance/loose_keyword_argument_matching_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,103 @@ | ||
# frozen_string_literal: true | ||
|
||
require File.expand_path('../acceptance_test_helper', __FILE__) | ||
|
||
require 'execution_point' | ||
require 'mocha/ruby_version' | ||
|
||
class LooseKeywordArgumentMatchingTest < Mocha::TestCase | ||
include AcceptanceTestHelper | ||
|
||
def setup | ||
setup_acceptance_test | ||
Mocha.configure { |c| c.strict_keyword_argument_matching = false } if Mocha::RUBY_V27_PLUS | ||
end | ||
|
||
def teardown | ||
teardown_acceptance_test | ||
end | ||
|
||
def test_should_match_hash_parameter_with_keyword_args | ||
execution_point = nil | ||
test_result = run_as_test do | ||
mock = mock() | ||
mock.expects(:method).with(key: 42); execution_point = ExecutionPoint.current | ||
capture_deprecation_warnings do | ||
mock.method({ key: 42 }) | ||
end | ||
end | ||
if Mocha::RUBY_V27_PLUS | ||
assert_deprecation_warning_location(test_result, execution_point) | ||
assert_deprecation_warning( | ||
test_result, 'expected keyword arguments (key: 42), but received positional hash ({key: 42})' | ||
) | ||
end | ||
assert_passed(test_result) | ||
end | ||
|
||
def test_should_match_hash_parameter_with_splatted_keyword_args | ||
execution_point = nil | ||
test_result = run_as_test do | ||
mock = mock() | ||
kwargs = { key: 42 } | ||
mock.expects(:method).with(**kwargs); execution_point = ExecutionPoint.current | ||
capture_deprecation_warnings do | ||
mock.method({ key: 42 }) | ||
end | ||
end | ||
if Mocha::RUBY_V27_PLUS | ||
assert_deprecation_warning_location(test_result, execution_point) | ||
assert_deprecation_warning( | ||
test_result, 'expected keyword arguments (key: 42), but received positional hash ({key: 42})' | ||
) | ||
end | ||
assert_passed(test_result) | ||
end | ||
|
||
def test_should_match_positional_and_keyword_args_with_last_positional_hash | ||
execution_point = nil | ||
test_result = run_as_test do | ||
mock = mock() | ||
mock.expects(:method).with(1, { key: 42 }); execution_point = ExecutionPoint.current | ||
capture_deprecation_warnings do | ||
mock.method(1, key: 42) | ||
end | ||
end | ||
if Mocha::RUBY_V27_PLUS | ||
assert_deprecation_warning_location(test_result, execution_point) | ||
assert_deprecation_warning( | ||
test_result, 'expected positional hash ({key: 42}), but received keyword arguments (key: 42)' | ||
) | ||
end | ||
assert_passed(test_result) | ||
end | ||
|
||
def test_should_match_last_positional_hash_with_keyword_args | ||
execution_point = nil | ||
test_result = run_as_test do | ||
mock = mock() | ||
mock.expects(:method).with(1, key: 42); execution_point = ExecutionPoint.current | ||
capture_deprecation_warnings do | ||
mock.method(1, { key: 42 }) | ||
end | ||
end | ||
if Mocha::RUBY_V27_PLUS | ||
assert_deprecation_warning_location(test_result, execution_point) | ||
assert_deprecation_warning( | ||
test_result, 'expected keyword arguments (key: 42), but received positional hash ({key: 42})' | ||
) | ||
end | ||
assert_passed(test_result) | ||
end | ||
|
||
private | ||
|
||
def assert_deprecation_warning(test_result, expected_warning) | ||
assert_includes test_result.last_deprecation_warning, expected_warning | ||
end | ||
|
||
def assert_deprecation_warning_location(test_result, execution_point) | ||
expected_location = "Expectation defined at #{execution_point.location}" | ||
assert_deprecation_warning test_result, expected_location | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require File.expand_path('../acceptance_test_helper', __FILE__) | ||
|
||
require 'mocha/ruby_version' | ||
|
||
if Mocha::RUBY_V27_PLUS | ||
class StrictKeywordArgumentMatchingTest < Mocha::TestCase | ||
include AcceptanceTestHelper | ||
|
||
def setup | ||
setup_acceptance_test | ||
Mocha.configure { |c| c.strict_keyword_argument_matching = true } | ||
end | ||
|
||
def teardown | ||
teardown_acceptance_test | ||
end | ||
|
||
def test_should_not_match_hash_parameter_with_keyword_args_when_strict_keyword_matching_is_enabled | ||
test_result = run_as_test do | ||
mock = mock() | ||
mock.expects(:method).with(key: 42) | ||
mock.method({ key: 42 }) | ||
end | ||
assert_failed(test_result) | ||
end | ||
|
||
def test_should_not_match_hash_parameter_with_splatted_keyword_args_when_strict_keyword_matching_is_enabled | ||
test_result = run_as_test do | ||
mock = mock() | ||
kwargs = { key: 42 } | ||
mock.expects(:method).with(**kwargs) | ||
mock.method({ key: 42 }) | ||
end | ||
assert_failed(test_result) | ||
end | ||
|
||
def test_should_not_match_positional_and_keyword_args_with_last_positional_hash_when_strict_keyword_args_is_enabled | ||
test_result = run_as_test do | ||
mock = mock() | ||
mock.expects(:method).with(1, { key: 42 }) | ||
mock.method(1, key: 42) | ||
end | ||
assert_failed(test_result) | ||
end | ||
|
||
def test_should_not_match_last_positional_hash_with_keyword_args_when_strict_keyword_args_is_enabled | ||
test_result = run_as_test do | ||
mock = mock() | ||
mock.expects(:method).with(1, key: 42) | ||
mock.method(1, { key: 42 }) | ||
end | ||
assert_failed(test_result) | ||
end | ||
|
||
def test_should_not_match_non_hash_args_with_keyword_args | ||
test_result = run_as_test do | ||
mock = mock() | ||
kwargs = { key: 1 } | ||
mock.expects(:method).with(**kwargs) | ||
mock.method([2]) | ||
end | ||
assert_failed(test_result) | ||
end | ||
end | ||
end |
Oops, something went wrong.