Skip to content

Commit

Permalink
Merge pull request #672 from freerange/fixes-for-ruby-v3.4
Browse files Browse the repository at this point in the history
Fixes for Ruby v3.4
  • Loading branch information
floehopper authored Jul 30, 2024
2 parents 5c7d14c + ce2ae26 commit c4bd4af
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 27 deletions.
10 changes: 5 additions & 5 deletions lib/mocha/mockery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ def sequences
end

def mocha_inspect
message = ''
message << "unsatisfied expectations:\n- #{unsatisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" if unsatisfied_expectations.any?
message << "satisfied expectations:\n- #{satisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" if satisfied_expectations.any?
message << "states:\n- #{state_machines.map(&:mocha_inspect).join("\n- ")}\n" if state_machines.any?
message
lines = []
lines << "unsatisfied expectations:\n- #{unsatisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" if unsatisfied_expectations.any?
lines << "satisfied expectations:\n- #{satisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" if satisfied_expectations.any?
lines << "states:\n- #{state_machines.map(&:mocha_inspect).join("\n- ")}\n" if state_machines.any?
lines.join
end

def on_stubbing(object, method)
Expand Down
1 change: 1 addition & 0 deletions lib/mocha/ruby_version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module Mocha
RUBY_V27_PLUS = Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.7')
RUBY_V34_PLUS = Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('3.4')
end
10 changes: 6 additions & 4 deletions test/acceptance/failure_messages_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ def test_should_display_mock_address_when_expectation_was_on_unnamed_mock
assert_match Regexp.new("#<Mock:#{OBJECT_ADDRESS_PATTERN}>"), test_result.failures[0].message
end

def test_should_display_string_when_expectation_was_on_string
test_result = run_as_test do
'Foo'.expects(:bar)
unless Mocha::RUBY_V34_PLUS
def test_should_display_string_when_expectation_was_on_string
test_result = run_as_test do
'Foo'.expects(:bar)
end
assert_match Regexp.new(%("Foo")), test_result.failures[0].message
end
assert_match Regexp.new(%("Foo")), test_result.failures[0].message
end

def test_should_display_that_block_was_expected
Expand Down
13 changes: 9 additions & 4 deletions test/acceptance/keyword_argument_matching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'deprecation_disabler'
require 'execution_point'
require 'mocha/deprecation'
require 'mocha/ruby_version'

class KeywordArgumentMatchingTest < Mocha::TestCase
include AcceptanceTest
Expand All @@ -24,7 +25,8 @@ def test_should_match_hash_parameter_with_keyword_args
mock.method({ key: 42 }) # rubocop:disable Style/BracesAroundHashParameters
end
if Mocha::RUBY_V27_PLUS
location = "#{execution_point.file_name}:#{execution_point.line_number}:in `block in #{test_name}'"
opening_quote = Mocha::RUBY_V34_PLUS ? "'" : '`'
location = "#{execution_point.file_name}:#{execution_point.line_number}:in #{opening_quote}block in #{test_name}'"
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 @@ -54,7 +56,8 @@ def test_should_match_hash_parameter_with_splatted_keyword_args
mock.method({ key: 42 }) # rubocop:disable Style/BracesAroundHashParameters
end
if Mocha::RUBY_V27_PLUS
location = "#{execution_point.file_name}:#{execution_point.line_number}:in `block in #{test_name}'"
opening_quote = Mocha::RUBY_V34_PLUS ? "'" : '`'
location = "#{execution_point.file_name}:#{execution_point.line_number}:in #{opening_quote}block in #{test_name}'"
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 @@ -102,7 +105,8 @@ def test_should_match_positional_and_keyword_args_with_last_positional_hash
mock.method(1, key: 42)
end
if Mocha::RUBY_V27_PLUS
location = "#{execution_point.file_name}:#{execution_point.line_number}:in `block in #{test_name}'"
opening_quote = Mocha::RUBY_V34_PLUS ? "'" : '`'
location = "#{execution_point.file_name}:#{execution_point.line_number}:in #{opening_quote}block in #{test_name}'"
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 Down Expand Up @@ -132,7 +136,8 @@ 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
location = "#{execution_point.file_name}:#{execution_point.line_number}:in `block in #{test_name}'"
opening_quote = Mocha::RUBY_V34_PLUS ? "'" : '`'
location = "#{execution_point.file_name}:#{execution_point.line_number}:in #{opening_quote}block in #{test_name}'"
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
8 changes: 4 additions & 4 deletions test/acceptance/partial_mocks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def teardown

def test_should_pass_if_all_expectations_are_satisfied
test_result = run_as_test do
partial_mock_one = 'partial_mock_one'
partial_mock_two = 'partial_mock_two'
partial_mock_one = Object.new
partial_mock_two = Object.new

partial_mock_one.expects(:first)
partial_mock_one.expects(:second)
Expand All @@ -29,8 +29,8 @@ def test_should_pass_if_all_expectations_are_satisfied

def test_should_fail_if_all_expectations_are_not_satisfied
test_result = run_as_test do
partial_mock_one = 'partial_mock_one'
partial_mock_two = 'partial_mock_two'
partial_mock_one = Object.new
partial_mock_two = Object.new

partial_mock_one.expects(:first)
partial_mock_one.expects(:second)
Expand Down
8 changes: 4 additions & 4 deletions test/acceptance/sequence_block_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def test_should_allow_invocations_in_sequence_even_if_expected_on_different_mock

def test_should_constrain_invocations_to_occur_in_expected_order_even_if_expected_on_partial_mocks
test_result = run_as_test do
partial_mock_one = '1'
partial_mock_two = '2'
partial_mock_one = Object.new
partial_mock_two = Object.new

sequence('one') do
partial_mock_one.expects(:first)
Expand All @@ -93,8 +93,8 @@ def test_should_constrain_invocations_to_occur_in_expected_order_even_if_expecte

def test_should_allow_invocations_in_sequence_even_if_expected_on_partial_mocks
test_result = run_as_test do
partial_mock_one = '1'
partial_mock_two = '2'
partial_mock_one = Object.new
partial_mock_two = Object.new

sequence('one') do
partial_mock_one.expects(:first)
Expand Down
8 changes: 4 additions & 4 deletions test/acceptance/sequence_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def test_should_allow_invocations_in_sequence_even_if_expected_on_different_mock

def test_should_constrain_invocations_to_occur_in_expected_order_even_if_expected_on_partial_mocks
test_result = run_as_test do
partial_mock_one = '1'
partial_mock_two = '2'
partial_mock_one = Object.new
partial_mock_two = Object.new
sequence = sequence('one')

partial_mock_one.expects(:first).in_sequence(sequence)
Expand All @@ -88,8 +88,8 @@ def test_should_constrain_invocations_to_occur_in_expected_order_even_if_expecte

def test_should_allow_invocations_in_sequence_even_if_expected_on_partial_mocks
test_result = run_as_test do
partial_mock_one = '1'
partial_mock_two = '2'
partial_mock_one = Object.new
partial_mock_two = Object.new
sequence = sequence('one')

partial_mock_one.expects(:first).in_sequence(sequence)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'mocha/parameter_matchers/instance_methods'
require 'mocha/inspect'
require 'mocha/expectation'
require 'mocha/ruby_version'

class PositionalOrKeywordHashTest < Mocha::TestCase
include Mocha::ParameterMatchers
Expand Down Expand Up @@ -69,7 +70,9 @@ def test_should_match_hash_arg_with_keyword_args_but_display_deprecation_warning
return unless Mocha::RUBY_V27_PLUS

message = Mocha::Deprecation.messages.last
location = "#{execution_point.file_name}:#{execution_point.line_number}:in `new'"
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}'"
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.'
Expand All @@ -85,7 +88,9 @@ def test_should_match_keyword_args_with_hash_arg_but_display_deprecation_warning
return unless Mocha::RUBY_V27_PLUS

message = Mocha::Deprecation.messages.last
location = "#{execution_point.file_name}:#{execution_point.line_number}:in `new'"
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}'"
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 c4bd4af

Please sign in to comment.