From 9b751831cc8f0ef2f950791bba5c83a387201cdb Mon Sep 17 00:00:00 2001 From: James Mead Date: Wed, 1 Jan 2025 15:41:09 +0000 Subject: [PATCH] Use Ruby v1.9 Hash syntax in Hash#mocha_inspect Closes #651. --- lib/mocha/inspect.rb | 23 ++++++++---------- .../keyword_argument_matching_test.rb | 8 +++---- ...positional_and_keyword_has_inspect_test.rb | 24 +++++++++---------- test/unit/expectation_test.rb | 2 +- test/unit/hash_inspect_test.rb | 15 ++++++++---- .../positional_or_keyword_hash_test.rb | 8 +++---- 6 files changed, 42 insertions(+), 38 deletions(-) diff --git a/lib/mocha/inspect.rb b/lib/mocha/inspect.rb index 246fe8b8..70272c4a 100755 --- a/lib/mocha/inspect.rb +++ b/lib/mocha/inspect.rb @@ -19,19 +19,16 @@ def mocha_inspect(wrapped: true) module HashMethods def mocha_inspect - if Hash.ruby2_keywords_hash?(self) - collect do |key, value| - case key - when Symbol - "#{key}: #{value.mocha_inspect}" - else - "#{key.mocha_inspect} => #{value.mocha_inspect}" - end - end.join(', ') - else - unwrapped = collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ') - "{#{unwrapped}}" - end + unwrapped = collect do |key, value| + case key + when Symbol + "#{key}: #{value.mocha_inspect}" + else + "#{key.mocha_inspect} => #{value.mocha_inspect}" + end + end.join(', ') + + Hash.ruby2_keywords_hash?(self) ? unwrapped : "{#{unwrapped}}" end end diff --git a/test/acceptance/keyword_argument_matching_test.rb b/test/acceptance/keyword_argument_matching_test.rb index 85ba5b75..14930097 100644 --- a/test/acceptance/keyword_argument_matching_test.rb +++ b/test/acceptance/keyword_argument_matching_test.rb @@ -28,7 +28,7 @@ def test_should_match_hash_parameter_with_keyword_args 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})' + assert_includes Mocha::Deprecation.messages.last, 'but received positional hash ({key: 42})' end end assert_passed(test_result) @@ -60,7 +60,7 @@ def test_should_match_hash_parameter_with_splatted_keyword_args 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})' + assert_includes Mocha::Deprecation.messages.last, 'but received positional hash ({key: 42})' end end assert_passed(test_result) @@ -111,7 +111,7 @@ def test_should_match_positional_and_keyword_args_with_last_positional_hash 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, "Expectation defined at #{location} expected positional hash ({key: 42})" assert_includes Mocha::Deprecation.messages.last, 'but received keyword arguments (key: 42)' end end @@ -143,7 +143,7 @@ def test_should_match_last_positional_hash_with_keyword_args 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})' + assert_includes Mocha::Deprecation.messages.last, 'but received positional hash ({key: 42})' end end assert_passed(test_result) diff --git a/test/acceptance/positional_and_keyword_has_inspect_test.rb b/test/acceptance/positional_and_keyword_has_inspect_test.rb index 171dbaa3..23dd540b 100644 --- a/test/acceptance/positional_and_keyword_has_inspect_test.rb +++ b/test/acceptance/positional_and_keyword_has_inspect_test.rb @@ -19,9 +19,9 @@ def test_single_hash_parameters_in_invocation_and_expectation_print_correctly mock.method({ key: 42 }) end assert_equal [ - 'unexpected invocation: #.method({:key => 42})', + 'unexpected invocation: #.method({key: 42})', 'unsatisfied expectations:', - '- expected exactly once, invoked never: #.method({:foo => 42})' + '- expected exactly once, invoked never: #.method({foo: 42})' ], test_result.failure_message_lines end @@ -39,9 +39,9 @@ def test_unexpected_keyword_arguments_in_invocation_and_expectation_print_correc ], test_result.failure_message_lines else assert_equal [ - 'unexpected invocation: #.method({:key => 42})', + 'unexpected invocation: #.method({key: 42})', 'unsatisfied expectations:', - '- expected exactly once, invoked never: #.method({:foo => 42})' + '- expected exactly once, invoked never: #.method({foo: 42})' ], test_result.failure_message_lines end end @@ -53,9 +53,9 @@ def test_last_hash_parameters_in_invocation_and_expectation_print_correctly mock.method(1, { key: 42 }) end assert_equal [ - 'unexpected invocation: #.method(1, {:key => 42})', + 'unexpected invocation: #.method(1, {key: 42})', 'unsatisfied expectations:', - '- expected exactly once, invoked never: #.method(1, {:foo => 42})' + '- expected exactly once, invoked never: #.method(1, {foo: 42})' ], test_result.failure_message_lines end @@ -73,9 +73,9 @@ def test_unexpected_keyword_arguments_with_other_positionals_in_invocation_and_e ], test_result.failure_message_lines else assert_equal [ - 'unexpected invocation: #.method(1, {:key => 42})', + 'unexpected invocation: #.method(1, {key: 42})', 'unsatisfied expectations:', - '- expected exactly once, invoked never: #.method(1, {:foo => 42})' + '- expected exactly once, invoked never: #.method(1, {foo: 42})' ], test_result.failure_message_lines end end @@ -90,9 +90,9 @@ def test_single_hash_parameters_in_invocation_and_expectation_print_correctly_wh end end assert_equal [ - 'unexpected invocation: #.method({:key => 42})', + 'unexpected invocation: #.method({key: 42})', 'unsatisfied expectations:', - '- expected exactly once, invoked never: #.method({:foo => 42})' + '- expected exactly once, invoked never: #.method({foo: 42})' ], test_result.failure_message_lines end @@ -120,9 +120,9 @@ def test_last_hash_parameters_in_invocation_and_expectation_print_correctly_when end end assert_equal [ - 'unexpected invocation: #.method(1, {:key => 42})', + 'unexpected invocation: #.method(1, {key: 42})', 'unsatisfied expectations:', - '- expected exactly once, invoked never: #.method(1, {:foo => 42})' + '- expected exactly once, invoked never: #.method(1, {foo: 42})' ], test_result.failure_message_lines end diff --git a/test/unit/expectation_test.rb b/test/unit/expectation_test.rb index 4010006c..c14a756d 100644 --- a/test/unit/expectation_test.rb +++ b/test/unit/expectation_test.rb @@ -362,7 +362,7 @@ def test_should_raise_error_with_message_indicating_which_method_was_expected_to sequence_two = Sequence.new('two') expectation = Expectation.new(mock, :expected_method).with(1, 2, { 'a' => true }, { b: false }, [1, 2, 3]).in_sequence(sequence_one, sequence_two) assert !expectation.verified? - assert_match %{mock.expected_method(1, 2, {"a" => true}, {:b => false}, [1, 2, 3]); in sequence "one"; in sequence "two"}, expectation.mocha_inspect + assert_match %{mock.expected_method(1, 2, {"a" => true}, {b: false}, [1, 2, 3]); in sequence "one"; in sequence "two"}, expectation.mocha_inspect end class FakeConstraint diff --git a/test/unit/hash_inspect_test.rb b/test/unit/hash_inspect_test.rb index 3d75a766..3a60edff 100644 --- a/test/unit/hash_inspect_test.rb +++ b/test/unit/hash_inspect_test.rb @@ -3,9 +3,14 @@ require 'mocha/ruby_version' class HashInspectTest < Mocha::TestCase - def test_should_return_string_representation_of_hash + def test_should_return_string_representation_of_hash_with_symbol_keys hash = { a: true, b: false } - assert_equal '{:a => true, :b => false}', hash.mocha_inspect + assert_equal '{a: true, b: false}', hash.mocha_inspect + end + + def test_should_return_string_representation_of_hash_with_string_keys + hash = { 'a' => true, 'b' => false } + assert_equal '{"a" => true, "b" => false}', hash.mocha_inspect end if Mocha::RUBY_V27_PLUS @@ -21,7 +26,9 @@ def test_should_return_unwrapped_hash_when_keyword_hash_keys_are_not_symbols end def test_should_use_mocha_inspect_on_each_key_and_value - hash = { a: 'mocha' } - assert_equal %({:a => "mocha"}), hash.mocha_inspect + object1 = Object.new + object2 = Object.new + hash = { object1 => object2 } + assert_equal "{#{object1.mocha_inspect} => #{object2.mocha_inspect}}", hash.mocha_inspect end end 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 66d7b1dc..6130e13e 100644 --- a/test/unit/parameter_matchers/positional_or_keyword_hash_test.rb +++ b/test/unit/parameter_matchers/positional_or_keyword_hash_test.rb @@ -13,7 +13,7 @@ class PositionalOrKeywordHashTest < Mocha::TestCase def test_should_describe_matcher hash = { key_1: 1, key_2: 2 } matcher = build_matcher(hash) - assert_equal '{:key_1 => 1, :key_2 => 2}', matcher.mocha_inspect + assert_equal '{key_1: 1, key_2: 2}', matcher.mocha_inspect end def test_should_match_non_last_hash_arg_with_hash_arg @@ -73,7 +73,7 @@ def test_should_match_hash_arg_with_keyword_args_but_display_deprecation_warning message = Mocha::Deprecation.messages.last 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, '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 @@ -90,7 +90,7 @@ def test_should_match_keyword_args_with_hash_arg_but_display_deprecation_warning message = Mocha::Deprecation.messages.last location = expectation.definition_location - assert_includes message, "Expectation defined at #{location} expected positional hash ({:key_1 => 1, :key_2 => 2})" + 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=.' @@ -153,7 +153,7 @@ def test_should_display_deprecation_warning_even_if_parent_expectation_is_nil end message = Mocha::Deprecation.messages.last - assert_includes message, 'Expectation expected positional hash ({:key_1 => 1, :key_2 => 2})' + 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 end