Skip to content

Commit

Permalink
Merge pull request #730 from freerange/improvements-to-keyword-argume…
Browse files Browse the repository at this point in the history
…nt-matching-tests

Improvements to keyword argument matching tests
  • Loading branch information
floehopper authored Jan 3, 2025
2 parents 39d9995 + b7919fd commit 067f057
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 183 deletions.
23 changes: 13 additions & 10 deletions lib/mocha/deprecation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@

module Mocha
class Deprecation
class << self
attr_accessor :mode, :messages

def warning(*messages)
message = messages.join
@messages << message
return if mode == :disabled

class Logger
def warning(message)
filter = BacktraceFilter.new
location = filter.filtered(caller)[0]
warn "Mocha deprecation warning at #{location}: #{message}"
end
end

self.mode = :enabled
self.messages = []
class << self
attr_writer :logger

def warning(message)
logger.call(message)
end

def logger
@logger || Logger.new
end
end
end
end
145 changes: 0 additions & 145 deletions test/acceptance/keyword_argument_matching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

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

require 'deprecation_disabler'
require 'execution_point'
require 'mocha/deprecation'
require 'mocha/ruby_version'

class KeywordArgumentMatchingTest < Mocha::TestCase
include AcceptanceTestHelper

Expand All @@ -18,70 +13,6 @@ def teardown
teardown_acceptance_test
end

def test_should_match_hash_parameter_with_keyword_args
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(key: 42); execution_point = ExecutionPoint.current
Mocha::Configuration.override(strict_keyword_argument_matching: false) do
DeprecationDisabler.disable_deprecations do
mock.method({ key: 42 })
end
end
if Mocha::RUBY_V27_PLUS
location = execution_point.location
assert_includes Mocha::Deprecation.messages.last, "Expectation defined at #{location} expected keyword arguments (key: 42)"
assert_includes Mocha::Deprecation.messages.last, 'but received positional hash ({key: 42})'
end
end
assert_passed(test_result)
end

if Mocha::RUBY_V27_PLUS
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)
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
mock.method({ key: 42 })
end
end
assert_failed(test_result)
end
end

def test_should_match_hash_parameter_with_splatted_keyword_args
test_result = run_as_test do
mock = mock()
kwargs = { key: 42 }
mock.expects(:method).with(**kwargs); execution_point = ExecutionPoint.current
Mocha::Configuration.override(strict_keyword_argument_matching: false) do
DeprecationDisabler.disable_deprecations do
mock.method({ key: 42 })
end
end
if Mocha::RUBY_V27_PLUS
location = execution_point.location
assert_includes Mocha::Deprecation.messages.last, "Expectation defined at #{location} expected keyword arguments (key: 42)"
assert_includes Mocha::Deprecation.messages.last, 'but received positional hash ({key: 42})'
end
end
assert_passed(test_result)
end

if Mocha::RUBY_V27_PLUS
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)
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
mock.method({ key: 42 })
end
end
assert_failed(test_result)
end
end

def test_should_match_splatted_hash_parameter_with_keyword_args
test_result = run_as_test do
mock = mock()
Expand All @@ -102,68 +33,6 @@ def test_should_match_splatted_hash_parameter_with_splatted_hash
assert_passed(test_result)
end

def test_should_match_positional_and_keyword_args_with_last_positional_hash
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, { key: 42 }); execution_point = ExecutionPoint.current
Mocha::Configuration.override(strict_keyword_argument_matching: false) do
DeprecationDisabler.disable_deprecations do
mock.method(1, key: 42)
end
end
if Mocha::RUBY_V27_PLUS
location = execution_point.location
assert_includes Mocha::Deprecation.messages.last, "Expectation defined at #{location} expected positional hash ({key: 42})"
assert_includes Mocha::Deprecation.messages.last, 'but received keyword arguments (key: 42)'
end
end
assert_passed(test_result)
end

if Mocha::RUBY_V27_PLUS
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 })
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
mock.method(1, key: 42)
end
end
assert_failed(test_result)
end
end

def test_should_match_last_positional_hash_with_keyword_args
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, key: 42); execution_point = ExecutionPoint.current
Mocha::Configuration.override(strict_keyword_argument_matching: false) do
DeprecationDisabler.disable_deprecations do
mock.method(1, { key: 42 })
end
end
if Mocha::RUBY_V27_PLUS
location = execution_point.location
assert_includes Mocha::Deprecation.messages.last, "Expectation defined at #{location} expected keyword arguments (key: 42)"
assert_includes Mocha::Deprecation.messages.last, 'but received positional hash ({key: 42})'
end
end
assert_passed(test_result)
end

if Mocha::RUBY_V27_PLUS
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)
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
mock.method(1, { key: 42 })
end
end
assert_failed(test_result)
end
end

def test_should_match_positional_and_keyword_args_with_keyword_args
test_result = run_as_test do
mock = mock()
Expand Down Expand Up @@ -236,18 +105,4 @@ def test_should_match_last_positional_hash_with_hash_matcher
end
assert_passed(test_result)
end

if Mocha::RUBY_V27_PLUS
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)
Mocha::Configuration.override(strict_keyword_argument_matching: true) do
mock.method([2])
end
end
assert_failed(test_result)
end
end
end
103 changes: 103 additions & 0 deletions test/acceptance/loose_keyword_argument_matching_test.rb
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
67 changes: 67 additions & 0 deletions test/acceptance/strict_keyword_argument_matching_test.rb
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
Loading

0 comments on commit 067f057

Please sign in to comment.