From 6fcaf9472ea71e10fd12a2e1ea005618c985c28e Mon Sep 17 00:00:00 2001 From: James Mead Date: Tue, 18 Oct 2022 15:00:19 +0100 Subject: [PATCH] Default reinstate_undocumented_behaviour_from_v1_9 to false Behaviour which relies on the Mocha v1.9 behaviour will have been highlighted by deprecation warnings and if these have not been fixed it's still possible to set the reinstate_undocumented_behaviour_from_v1_9 configuration option to true. --- lib/mocha/configuration.rb | 4 +- ...h_first_argument_type_being_string_test.rb | 54 ++++++++++++------- test/acceptance/mock_test.rb | 34 +++++------- test/unit/expectation_test.rb | 16 +++--- 4 files changed, 57 insertions(+), 51 deletions(-) diff --git a/lib/mocha/configuration.rb b/lib/mocha/configuration.rb index 352959af6..b82ab1672 100644 --- a/lib/mocha/configuration.rb +++ b/lib/mocha/configuration.rb @@ -43,7 +43,7 @@ class Configuration :stubbing_non_public_method => :allow, :stubbing_method_on_nil => :prevent, :display_matching_invocations_on_failure => false, - :reinstate_undocumented_behaviour_from_v1_9 => true + :reinstate_undocumented_behaviour_from_v1_9 => false }.freeze attr_reader :options @@ -258,7 +258,7 @@ def display_matching_invocations_on_failure? # # Enabling this configuration option reinstates the previous behaviour, but displays a deprecation warning. # - # @param [Boolean] value +true+ to reinstate undocumented behaviour; enabled by default. + # @param [Boolean] value +true+ to reinstate undocumented behaviour; disabled by default. # # @example Reinstate undocumented behaviour for {API#mock} # Mocha.configure do |c| diff --git a/test/acceptance/mock_built_with_first_argument_type_being_string_test.rb b/test/acceptance/mock_built_with_first_argument_type_being_string_test.rb index b6523a869..aa35792c0 100644 --- a/test/acceptance/mock_built_with_first_argument_type_being_string_test.rb +++ b/test/acceptance/mock_built_with_first_argument_type_being_string_test.rb @@ -14,9 +14,11 @@ def teardown def test_mock_built_with_single_symbol_argument_with_satisfied_expectation test_result = run_as_test do - DeprecationDisabler.disable_deprecations do - m = mock(:my_method) - assert_nil m.my_method + Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do + DeprecationDisabler.disable_deprecations do + m = mock(:my_method) + assert_nil m.my_method + end end expected_warning = 'Explicitly include `my_method` in Hash of expected methods vs return values, e.g. `mock(:my_method => nil)`.' assert_equal expected_warning, Mocha::Deprecation.messages.last @@ -26,8 +28,10 @@ def test_mock_built_with_single_symbol_argument_with_satisfied_expectation def test_mock_built_with_single_symbol_argument_with_unsatisfied_expectation test_result = run_as_test do - DeprecationDisabler.disable_deprecations do - mock(:my_method) + Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do + DeprecationDisabler.disable_deprecations do + mock(:my_method) + end end end assert_failed(test_result) @@ -38,9 +42,11 @@ def test_mock_built_with_single_symbol_argument_with_unsatisfied_expectation def test_stub_built_with_single_symbol_argument test_result = run_as_test do - DeprecationDisabler.disable_deprecations do - s = stub(:my_method) - assert_nil s.my_method + Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do + DeprecationDisabler.disable_deprecations do + s = stub(:my_method) + assert_nil s.my_method + end end expected_warning = 'Explicitly include `my_method` in Hash of stubbed methods vs return values, e.g. `stub(:my_method => nil)`.' assert_equal expected_warning, Mocha::Deprecation.messages.last @@ -50,9 +56,11 @@ def test_stub_built_with_single_symbol_argument def test_mock_built_with_first_argument_a_symbol_and_second_argument_a_hash test_result = run_as_test do - DeprecationDisabler.disable_deprecations do - s = mock(:my_method, :another_method => 123) - assert_nil s.my_method + Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do + DeprecationDisabler.disable_deprecations do + s = mock(:my_method, :another_method => 123) + assert_nil s.my_method + end end expected_warning = 'In this case the 2nd argument for `mock(:#my_method, ...)` is ignored, but in the future a Hash of expected methods vs return values will be respected.' assert Mocha::Deprecation.messages.last(2).include?(expected_warning) @@ -62,9 +70,11 @@ def test_mock_built_with_first_argument_a_symbol_and_second_argument_a_hash def test_stub_built_with_first_argument_a_symbol_and_second_argument_a_hash test_result = run_as_test do - DeprecationDisabler.disable_deprecations do - s = stub(:my_method, :another_method => 123) - assert_nil s.my_method + Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do + DeprecationDisabler.disable_deprecations do + s = stub(:my_method, :another_method => 123) + assert_nil s.my_method + end end expected_warning = 'In this case the 2nd argument for `stub(:#my_method, ...)` is ignored, but in the future a Hash of stubbed methods vs return values will be respected.' assert Mocha::Deprecation.messages.last(2).include?(expected_warning) @@ -74,9 +84,11 @@ def test_stub_built_with_first_argument_a_symbol_and_second_argument_a_hash def test_stub_everything_built_with_single_symbol_argument test_result = run_as_test do - DeprecationDisabler.disable_deprecations do - s = stub_everything(:my_method) - assert_nil s.my_method + Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do + DeprecationDisabler.disable_deprecations do + s = stub_everything(:my_method) + assert_nil s.my_method + end end expected_warning = 'Explicitly include `my_method` in Hash of stubbed methods vs return values, e.g. `stub_everything(:my_method => nil)`.' assert_equal expected_warning, Mocha::Deprecation.messages.last @@ -86,9 +98,11 @@ def test_stub_everything_built_with_single_symbol_argument def test_stub_everything_built_with_first_argument_a_symbol_and_second_argument_a_hash test_result = run_as_test do - DeprecationDisabler.disable_deprecations do - s = stub_everything(:my_method, :another_method => 123) - assert_nil s.my_method + Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do + DeprecationDisabler.disable_deprecations do + s = stub_everything(:my_method, :another_method => 123) + assert_nil s.my_method + end end expected_warning = 'In this case the 2nd argument for `stub_everything(:#my_method, ...)` is ignored, but in the future a Hash of stubbed methods vs return values will be respected.' # rubocop:disable Metrics/LineLength assert Mocha::Deprecation.messages.last(2).include?(expected_warning) diff --git a/test/acceptance/mock_test.rb b/test/acceptance/mock_test.rb index 30b177c51..2454e761e 100644 --- a/test/acceptance/mock_test.rb +++ b/test/acceptance/mock_test.rb @@ -43,11 +43,9 @@ def test_should_build_string_named_mock_and_explicitly_add_an_expectation_which_ def test_should_build_symbol_named_mock_and_explicitly_add_an_expectation_which_is_satisfied test_result = run_as_test do - Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => false) do - foo = mock(:foo) - foo.expects(:bar) - foo.bar - end + foo = mock(:foo) + foo.expects(:bar) + foo.bar end assert_passed(test_result) end @@ -62,10 +60,8 @@ def test_should_build_string_named_mock_and_explicitly_add_an_expectation_which_ def test_should_build_symbol_named_mock_and_explicitly_add_an_expectation_which_is_not_satisfied test_result = run_as_test do - DeprecationDisabler.disable_deprecations do - foo = mock(:foo) - foo.expects(:bar) - end + foo = mock(:foo) + foo.expects(:bar) end assert_failed(test_result) end @@ -106,11 +102,9 @@ def test_should_build_string_named_mock_incorporating_two_expectations_which_are def test_should_build_symbol_named_mock_incorporating_two_expectations_which_are_satisifed test_result = run_as_test do - Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => false) do - foo = mock(:foo, :bar => 'bar', :baz => 'baz') - foo.bar - foo.baz - end + foo = mock(:foo, :bar => 'bar', :baz => 'baz') + foo.bar + foo.baz end assert_passed(test_result) end @@ -125,10 +119,8 @@ def test_should_build_string_named_mock_incorporating_two_expectations_the_first def test_should_build_symbol_named_mock_incorporating_two_expectations_the_first_of_which_is_not_satisifed test_result = run_as_test do - DeprecationDisabler.disable_deprecations do - foo = mock(:foo, :bar => 'bar', :baz => 'baz') - foo.baz - end + foo = mock(:foo, :bar => 'bar', :baz => 'baz') + foo.baz end assert_failed(test_result) end @@ -143,10 +135,8 @@ def test_should_build_string_named_mock_incorporating_two_expectations_the_secon def test_should_build_symbol_named_mock_incorporating_two_expectations_the_second_of_which_is_not_satisifed test_result = run_as_test do - DeprecationDisabler.disable_deprecations do - foo = mock(:foo, :bar => 'bar', :baz => 'baz') - foo.bar - end + foo = mock(:foo, :bar => 'bar', :baz => 'baz') + foo.bar end assert_failed(test_result) end diff --git a/test/unit/expectation_test.rb b/test/unit/expectation_test.rb index 5d79709e2..035a06b86 100644 --- a/test/unit/expectation_test.rb +++ b/test/unit/expectation_test.rb @@ -117,14 +117,14 @@ def test_should_yield_no_parameters end def test_yield_should_fail_when_the_caller_does_not_provide_a_block_and_behaviour_from_v1_9_not_retained - Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => false) do - assert_raises(LocalJumpError) { invoke(new_expectation.yields(:foo)) } - end + assert_raises(LocalJumpError) { invoke(new_expectation.yields(:foo)) } end def test_yields_should_display_warning_when_caller_does_not_provide_block - DeprecationDisabler.disable_deprecations do - invoke(new_expectation.yields(:foo, 1, [2, 3])) + Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do + DeprecationDisabler.disable_deprecations do + invoke(new_expectation.yields(:foo, 1, [2, 3])) + end end assert message = Deprecation.messages.last assert message.include?('Stubbed method was instructed to yield (:foo, 1, [2, 3])') @@ -133,8 +133,10 @@ def test_yields_should_display_warning_when_caller_does_not_provide_block end def test_multiple_yields_should_display_warning_when_caller_does_not_provide_block - DeprecationDisabler.disable_deprecations do - invoke(new_expectation.multiple_yields(:foo, 1, [2, 3])) + Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => true) do + DeprecationDisabler.disable_deprecations do + invoke(new_expectation.multiple_yields(:foo, 1, [2, 3])) + end end assert message = Deprecation.messages.last assert message.include?('Stubbed method was instructed to yield (2, 3)')