From 174e86ab3d34e6434918b6b15539086e3f42357a Mon Sep 17 00:00:00 2001 From: James Mead Date: Sat, 9 Nov 2024 13:47:22 +0000 Subject: [PATCH 1/2] Use ExecutionPoint#location to simplify assertions Since the `ExecutionPoint` already stores the full backtrace, we should already have it in the format for the relevant Ruby version. Using this is a lot simpler than having to generate the line from scratch. --- .../acceptance/keyword_argument_matching_test.rb | 16 ++++------------ test/execution_point.rb | 4 ++++ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/test/acceptance/keyword_argument_matching_test.rb b/test/acceptance/keyword_argument_matching_test.rb index e3372104a..67dda752d 100644 --- a/test/acceptance/keyword_argument_matching_test.rb +++ b/test/acceptance/keyword_argument_matching_test.rb @@ -17,7 +17,6 @@ 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 @@ -25,8 +24,7 @@ def test_should_match_hash_parameter_with_keyword_args 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 @@ -48,7 +46,6 @@ 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 @@ -56,8 +53,7 @@ def test_should_match_hash_parameter_with_splatted_keyword_args 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 @@ -97,7 +93,6 @@ 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 @@ -105,8 +100,7 @@ def test_should_match_positional_and_keyword_args_with_last_positional_hash 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 @@ -128,7 +122,6 @@ 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 @@ -136,8 +129,7 @@ def test_should_match_last_positional_hash_with_keyword_args 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 diff --git a/test/execution_point.rb b/test/execution_point.rb index 83309a03a..42e9b9120 100644 --- a/test/execution_point.rb +++ b/test/execution_point.rb @@ -30,6 +30,10 @@ def to_s "file: #{file_name}; line: #{line_number}" end + def location + @backtrace.first + end + def inspect to_s end From 468a313bbf26af698b4f0fd2f88fa02644c6bd46 Mon Sep 17 00:00:00 2001 From: James Mead Date: Sat, 9 Nov 2024 13:49:45 +0000 Subject: [PATCH 2/2] Use Expectation#definition_location to simplify assertions Since the `Expectation` instance already "knows" where it was defined, it's simpler to use `Expectation#definition_location` to make these assertions. And again, since this is obtained directly from the backtrace, it should be in the format for the relevant Ruby version. --- .../positional_or_keyword_hash_test.rb | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/test/unit/parameter_matchers/positional_or_keyword_hash_test.rb b/test/unit/parameter_matchers/positional_or_keyword_hash_test.rb index 41b709eef..14dc2a250 100644 --- a/test/unit/parameter_matchers/positional_or_keyword_hash_test.rb +++ b/test/unit/parameter_matchers/positional_or_keyword_hash_test.rb @@ -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' @@ -62,7 +61,7 @@ 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 }]) @@ -70,9 +69,7 @@ def test_should_match_hash_arg_with_keyword_args_but_display_deprecation_warning 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.' @@ -80,7 +77,7 @@ def test_should_match_hash_arg_with_keyword_args_but_display_deprecation_warning 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 @@ -88,9 +85,7 @@ def test_should_match_keyword_args_with_hash_arg_but_display_deprecation_warning 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.'