From ed7dcb17f41c826f410476bae05e1ce78bc2c669 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Mon, 12 Sep 2022 22:56:59 -0500 Subject: [PATCH] Use explicit deprecator in AS::Deprecation tests Follow-up to #46000. This changes more deprecation tests to use an explicit deprecator, as part of an initiative to move away from the top-level `ActiveSupport::Deprecation` API, based on the discussion in #44772. --- activesupport/test/deprecation_test.rb | 115 ++++++++++++------------- 1 file changed, 55 insertions(+), 60 deletions(-) diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb index d817f7b105..a264446943 100644 --- a/activesupport/test/deprecation_test.rb +++ b/activesupport/test/deprecation_test.rb @@ -7,31 +7,11 @@ require "active_support/testing/stream" class Deprecatee - def initialize - @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request) - @_request = "there we go" - end - def request; @_request end - def old_request; @request end + attr_accessor :fubar, :foo_bar - def not() 2 end - def none() 1 end + def zero() 0 end def one(a) a end def multi(a, b, c) [a, b, c] end - deprecate :none, :one, :multi - - def a; end - def b; end - def c; end - def d; end - def e; end - deprecate :a, :b, c: :e, d: "you now need to do something extra for this one" - - def f=(v); end - deprecate :f= - - deprecate :g - def g(h) h end module B C = 1 @@ -62,7 +42,6 @@ class DeprecationTest < ActiveSupport::TestCase def setup @original_configuration = get_configuration(ActiveSupport::Deprecation) @deprecator = ActiveSupport::Deprecation - @dtc = Deprecatee.new end def teardown @@ -82,34 +61,41 @@ def teardown end test "Module::deprecate" do - assert_deprecated(/none is deprecated/) do - assert_equal 1, @dtc.none + klass = Class.new(Deprecatee) + klass.deprecate :zero, :one, :multi, deprecator: @deprecator + + assert_deprecated(/zero is deprecated/, @deprecator) do + assert_equal 0, klass.new.zero end - assert_deprecated(/one is deprecated/) do - assert_equal 1, @dtc.one(1) + assert_deprecated(/one is deprecated/, @deprecator) do + assert_equal 1, klass.new.one(1) end - assert_deprecated(/multi is deprecated/) do - assert_equal [1, 2, 3], @dtc.multi(1, 2, 3) + assert_deprecated(/multi is deprecated/, @deprecator) do + assert_equal [1, 2, 3], klass.new.multi(1, 2, 3) end end test "Module::deprecate does not expand Hash positional argument" do + klass = Class.new(Deprecatee) + klass.deprecate :one, :one!, deprecator: @deprecator + klass.alias_method :one!, :one + hash = { k: 1 } - assert_deprecated(/one is deprecated/) do - assert_same hash, @dtc.one(hash) + assert_deprecated(/one is deprecated/, @deprecator) do + assert_same hash, klass.new.one(hash) end - assert_deprecated(/g is deprecated/) do - assert_same hash, @dtc.g(hash) + assert_deprecated(/one! is deprecated/, @deprecator) do + assert_same hash, klass.new.one!(hash) end end test "DeprecatedObjectProxy" do - deprecated_object = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(Object.new, ":bomb:") - assert_deprecated(/:bomb:/) { deprecated_object.to_s } + deprecated_object = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(Object.new, ":bomb:", @deprecator) + assert_deprecated(/:bomb:/, @deprecator) { deprecated_object.to_s } end test "nil behavior is ignored" do @@ -245,14 +231,24 @@ def call(message, callstack, horizon, gem_name) end test "DeprecatedInstanceVariableProxy" do - assert_not_deprecated { @dtc.request.size } + instance = Deprecatee.new + instance.fubar = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(instance, :foo_bar, "@fubar", @deprecator) + instance.foo_bar = "foo bar!" - assert_deprecated("@request.size") { assert_equal @dtc.request.size, @dtc.old_request.size } - assert_deprecated("@request.to_s") { assert_equal @dtc.request.to_s, @dtc.old_request.to_s } + fubar_size = assert_deprecated("@fubar.size", @deprecator) { instance.fubar.size } + assert_equal instance.foo_bar.size, fubar_size + + fubar_s = assert_deprecated("@fubar.to_s", @deprecator) { instance.fubar.to_s } + assert_equal instance.foo_bar.to_s, fubar_s end test "DeprecatedInstanceVariableProxy does not warn on inspect" do - assert_not_deprecated { assert_equal @dtc.request.inspect, @dtc.old_request.inspect } + instance = Deprecatee.new + instance.fubar = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(instance, :foo_bar, "@fubar", @deprecator) + instance.foo_bar = "foo bar!" + + fubar_inspected = assert_not_deprecated(@deprecator) { instance.fubar.inspect } + assert_equal instance.foo_bar.inspect, fubar_inspected end test "DeprecatedConstantProxy" do @@ -359,17 +355,25 @@ def call(message, callstack, horizon, gem_name) end test "Module::deprecate with method name only" do - assert_deprecated { @dtc.a } - assert_deprecated { @dtc.b } - assert_deprecated { @dtc.f = :foo } + klass = Class.new(Deprecatee) + klass.deprecate :fubar, :fubar=, deprecator: @deprecator + + assert_deprecated(/./, @deprecator) { klass.new.fubar } + assert_deprecated(/./, @deprecator) { klass.new.fubar = :foo } end test "Module::deprecate with alternative method" do - assert_deprecated(/use e instead/) { @dtc.c } + klass = Class.new(Deprecatee) + klass.deprecate fubar: :foo_bar, deprecator: @deprecator + + assert_deprecated(/use foo_bar instead/, @deprecator) { klass.new.fubar } end test "Module::deprecate with message" do - assert_deprecated(/you now need to do something extra for this one/) { @dtc.d } + klass = Class.new(Deprecatee) + klass.deprecate fubar: "this is the old way", deprecator: @deprecator + + assert_deprecated(/this is the old way/, @deprecator) { klass.new.fubar } end test "delegating to ActiveSupport::Deprecation" do @@ -439,21 +443,6 @@ def method assert_match "foo", deprecator.messages.last end - test "DeprecatedInstanceVariableProxy with explicit deprecator" do - deprecator = deprecator_with_messages - - klass = Class.new() do - def initialize(deprecator) - @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator) - @_request = :a_request - end - def request; @_request end - def old_request; @request end - end - - assert_difference("deprecator.messages.size") { klass.new(deprecator).old_request.to_s } - end - test "default deprecation_horizon is greater than the current Rails version" do assert_operator ActiveSupport::Deprecation.new.deprecation_horizon, :>, ActiveSupport::VERSION::STRING end @@ -475,7 +464,13 @@ def old_request; @request end end test "Module::deprecate can be called before the target method is defined" do - assert_deprecated(/g is deprecated/) { @dtc.g(1) } + klass = Class.new(Deprecatee) + klass.deprecate :multi!, deprecator: @deprecator + klass.alias_method :multi!, :multi + + assert_deprecated(/multi! is deprecated/, @deprecator) do + assert_equal [1, 2, 3], klass.new.multi!(1, 2, 3) + end end test "warn with empty callstack" do