Skip to content

Commit

Permalink
Merge pull request #680 from freerange/simplify-backtrace-related-ass…
Browse files Browse the repository at this point in the history
…ertions

Simplify backtrace-related assertions
  • Loading branch information
floehopper authored Nov 9, 2024
2 parents 20595f5 + 468a313 commit f899c03
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
16 changes: 4 additions & 12 deletions test/acceptance/keyword_argument_matching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ def teardown
end

def test_should_match_hash_parameter_with_keyword_args
test_name = __method__
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(key: 42); execution_point = ExecutionPoint.current
DeprecationDisabler.disable_deprecations do
mock.method({ key: 42 }) # rubocop:disable Style/BracesAroundHashParameters
end
if Mocha::RUBY_V27_PLUS
opening_quote = Mocha::RUBY_V34_PLUS ? "'" : '`'
location = "#{execution_point.file_name}:#{execution_point.line_number}:in #{opening_quote}block in #{test_name}'"
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
Expand All @@ -48,16 +46,14 @@ def test_should_not_match_hash_parameter_with_keyword_args_when_strict_keyword_m
end

def test_should_match_hash_parameter_with_splatted_keyword_args
test_name = __method__
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(**{ key: 42 }); execution_point = ExecutionPoint.current
DeprecationDisabler.disable_deprecations do
mock.method({ key: 42 }) # rubocop:disable Style/BracesAroundHashParameters
end
if Mocha::RUBY_V27_PLUS
opening_quote = Mocha::RUBY_V34_PLUS ? "'" : '`'
location = "#{execution_point.file_name}:#{execution_point.line_number}:in #{opening_quote}block in #{test_name}'"
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
Expand Down Expand Up @@ -97,16 +93,14 @@ def test_should_match_splatted_hash_parameter_with_splatted_hash
end

def test_should_match_positional_and_keyword_args_with_last_positional_hash
test_name = __method__
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, { key: 42 }); execution_point = ExecutionPoint.current # rubocop:disable Style/BracesAroundHashParameters
DeprecationDisabler.disable_deprecations do
mock.method(1, key: 42)
end
if Mocha::RUBY_V27_PLUS
opening_quote = Mocha::RUBY_V34_PLUS ? "'" : '`'
location = "#{execution_point.file_name}:#{execution_point.line_number}:in #{opening_quote}block in #{test_name}'"
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
Expand All @@ -128,16 +122,14 @@ def test_should_not_match_positional_and_keyword_args_with_last_positional_hash_
end

def test_should_match_last_positional_hash_with_keyword_args
test_name = __method__
test_result = run_as_test do
mock = mock()
mock.expects(:method).with(1, key: 42); execution_point = ExecutionPoint.current
DeprecationDisabler.disable_deprecations do
mock.method(1, { key: 42 }) # rubocop:disable Style/BracesAroundHashParameters
end
if Mocha::RUBY_V27_PLUS
opening_quote = Mocha::RUBY_V34_PLUS ? "'" : '`'
location = "#{execution_point.file_name}:#{execution_point.line_number}:in #{opening_quote}block in #{test_name}'"
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
Expand Down
4 changes: 4 additions & 0 deletions test/execution_point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def to_s
"file: #{file_name}; line: #{line_number}"
end

def location
@backtrace.first
end

def inspect
to_s
end
Expand Down
13 changes: 4 additions & 9 deletions test/unit/parameter_matchers/positional_or_keyword_hash_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require File.expand_path('../../../test_helper', __FILE__)

require 'deprecation_disabler'
require 'execution_point'
require 'mocha/parameter_matchers/positional_or_keyword_hash'
require 'mocha/parameter_matchers/instance_methods'
require 'mocha/inspect'
Expand Down Expand Up @@ -62,35 +61,31 @@ def test_should_not_match_keyword_args_with_matchers_using_keyword_args_when_not
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
expectation = Mocha::Expectation.new(self, :foo)
matcher = build_matcher(Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 }), expectation) # rubocop:disable Style/BracesAroundHashParameters
DeprecationDisabler.disable_deprecations do
assert matcher.matches?([{ key_1: 1, key_2: 2 }])
end
return unless Mocha::RUBY_V27_PLUS

message = Mocha::Deprecation.messages.last
opening_quote = Mocha::RUBY_V34_PLUS ? "'" : '`'
method_description = Mocha::RUBY_V34_PLUS ? 'Class#new' : 'new'
location = "#{execution_point.file_name}:#{execution_point.line_number}:in #{opening_quote}#{method_description}'"
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); execution_point = ExecutionPoint.current
expectation = Mocha::Expectation.new(self, :foo)
matcher = build_matcher({ key_1: 1, key_2: 2 }, expectation)
DeprecationDisabler.disable_deprecations do
assert matcher.matches?([Hash.ruby2_keywords_hash({ key_1: 1, key_2: 2 })]) # rubocop:disable Style/BracesAroundHashParameters
end
return unless Mocha::RUBY_V27_PLUS

message = Mocha::Deprecation.messages.last
opening_quote = Mocha::RUBY_V34_PLUS ? "'" : '`'
method_description = Mocha::RUBY_V34_PLUS ? 'Class#new' : 'new'
location = "#{execution_point.file_name}:#{execution_point.line_number}:in #{opening_quote}#{method_description}'"
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.'
Expand Down

0 comments on commit f899c03

Please sign in to comment.