Skip to content

Commit

Permalink
Merge pull request #719 from freerange/use-ruby-v19-hash-syntax-in-ha…
Browse files Browse the repository at this point in the history
…sh-mocha-inspect

Use Ruby v1.9 Hash syntax in Hash#mocha_inspect
  • Loading branch information
floehopper authored Jan 1, 2025
2 parents 7f99db6 + 9b75183 commit a04434f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 38 deletions.
23 changes: 10 additions & 13 deletions lib/mocha/inspect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions test/acceptance/keyword_argument_matching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 12 additions & 12 deletions test/acceptance/positional_and_keyword_has_inspect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def test_single_hash_parameters_in_invocation_and_expectation_print_correctly
mock.method({ key: 42 })
end
assert_equal [
'unexpected invocation: #<Mock:mock>.method({:key => 42})',
'unexpected invocation: #<Mock:mock>.method({key: 42})',
'unsatisfied expectations:',
'- expected exactly once, invoked never: #<Mock:mock>.method({:foo => 42})'
'- expected exactly once, invoked never: #<Mock:mock>.method({foo: 42})'
], test_result.failure_message_lines
end

Expand All @@ -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: #<Mock:mock>.method({:key => 42})',
'unexpected invocation: #<Mock:mock>.method({key: 42})',
'unsatisfied expectations:',
'- expected exactly once, invoked never: #<Mock:mock>.method({:foo => 42})'
'- expected exactly once, invoked never: #<Mock:mock>.method({foo: 42})'
], test_result.failure_message_lines
end
end
Expand All @@ -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: #<Mock:mock>.method(1, {:key => 42})',
'unexpected invocation: #<Mock:mock>.method(1, {key: 42})',
'unsatisfied expectations:',
'- expected exactly once, invoked never: #<Mock:mock>.method(1, {:foo => 42})'
'- expected exactly once, invoked never: #<Mock:mock>.method(1, {foo: 42})'
], test_result.failure_message_lines
end

Expand All @@ -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: #<Mock:mock>.method(1, {:key => 42})',
'unexpected invocation: #<Mock:mock>.method(1, {key: 42})',
'unsatisfied expectations:',
'- expected exactly once, invoked never: #<Mock:mock>.method(1, {:foo => 42})'
'- expected exactly once, invoked never: #<Mock:mock>.method(1, {foo: 42})'
], test_result.failure_message_lines
end
end
Expand All @@ -90,9 +90,9 @@ def test_single_hash_parameters_in_invocation_and_expectation_print_correctly_wh
end
end
assert_equal [
'unexpected invocation: #<Mock:mock>.method({:key => 42})',
'unexpected invocation: #<Mock:mock>.method({key: 42})',
'unsatisfied expectations:',
'- expected exactly once, invoked never: #<Mock:mock>.method({:foo => 42})'
'- expected exactly once, invoked never: #<Mock:mock>.method({foo: 42})'
], test_result.failure_message_lines
end

Expand Down Expand Up @@ -120,9 +120,9 @@ def test_last_hash_parameters_in_invocation_and_expectation_print_correctly_when
end
end
assert_equal [
'unexpected invocation: #<Mock:mock>.method(1, {:key => 42})',
'unexpected invocation: #<Mock:mock>.method(1, {key: 42})',
'unsatisfied expectations:',
'- expected exactly once, invoked never: #<Mock:mock>.method(1, {:foo => 42})'
'- expected exactly once, invoked never: #<Mock:mock>.method(1, {foo: 42})'
], test_result.failure_message_lines
end

Expand Down
2 changes: 1 addition & 1 deletion test/unit/expectation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions test/unit/hash_inspect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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=.'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a04434f

Please sign in to comment.