Merge pull request #46019 from jonathanhefner/deprecation-tests-use-explicit-deprecator-pt2

Use explicit deprecator in `AS::Deprecation` tests
This commit is contained in:
Jonathan Hefner 2022-09-12 23:51:35 -05:00 committed by GitHub
commit f0ba13a2b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,31 +7,11 @@
require "active_support/testing/stream" require "active_support/testing/stream"
class Deprecatee class Deprecatee
def initialize attr_accessor :fubar, :foo_bar
@request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request)
@_request = "there we go"
end
def request; @_request end
def old_request; @request end
def not() 2 end def zero() 0 end
def none() 1 end
def one(a) a end def one(a) a end
def multi(a, b, c) [a, b, c] 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 module B
C = 1 C = 1
@ -62,7 +42,6 @@ class DeprecationTest < ActiveSupport::TestCase
def setup def setup
@original_configuration = get_configuration(ActiveSupport::Deprecation) @original_configuration = get_configuration(ActiveSupport::Deprecation)
@deprecator = ActiveSupport::Deprecation @deprecator = ActiveSupport::Deprecation
@dtc = Deprecatee.new
end end
def teardown def teardown
@ -82,34 +61,41 @@ def teardown
end end
test "Module::deprecate" do test "Module::deprecate" do
assert_deprecated(/none is deprecated/) do klass = Class.new(Deprecatee)
assert_equal 1, @dtc.none klass.deprecate :zero, :one, :multi, deprecator: @deprecator
assert_deprecated(/zero is deprecated/, @deprecator) do
assert_equal 0, klass.new.zero
end end
assert_deprecated(/one is deprecated/) do assert_deprecated(/one is deprecated/, @deprecator) do
assert_equal 1, @dtc.one(1) assert_equal 1, klass.new.one(1)
end end
assert_deprecated(/multi is deprecated/) do assert_deprecated(/multi is deprecated/, @deprecator) do
assert_equal [1, 2, 3], @dtc.multi(1, 2, 3) assert_equal [1, 2, 3], klass.new.multi(1, 2, 3)
end end
end end
test "Module::deprecate does not expand Hash positional argument" do 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 } hash = { k: 1 }
assert_deprecated(/one is deprecated/) do assert_deprecated(/one is deprecated/, @deprecator) do
assert_same hash, @dtc.one(hash) assert_same hash, klass.new.one(hash)
end end
assert_deprecated(/g is deprecated/) do assert_deprecated(/one! is deprecated/, @deprecator) do
assert_same hash, @dtc.g(hash) assert_same hash, klass.new.one!(hash)
end end
end end
test "DeprecatedObjectProxy" do test "DeprecatedObjectProxy" do
deprecated_object = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(Object.new, ":bomb:") deprecated_object = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(Object.new, ":bomb:", @deprecator)
assert_deprecated(/:bomb:/) { deprecated_object.to_s } assert_deprecated(/:bomb:/, @deprecator) { deprecated_object.to_s }
end end
test "nil behavior is ignored" do test "nil behavior is ignored" do
@ -245,14 +231,24 @@ def call(message, callstack, horizon, gem_name)
end end
test "DeprecatedInstanceVariableProxy" do 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 } fubar_size = assert_deprecated("@fubar.size", @deprecator) { instance.fubar.size }
assert_deprecated("@request.to_s") { assert_equal @dtc.request.to_s, @dtc.old_request.to_s } 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 end
test "DeprecatedInstanceVariableProxy does not warn on inspect" do 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 end
test "DeprecatedConstantProxy" do test "DeprecatedConstantProxy" do
@ -359,17 +355,25 @@ def call(message, callstack, horizon, gem_name)
end end
test "Module::deprecate with method name only" do test "Module::deprecate with method name only" do
assert_deprecated { @dtc.a } klass = Class.new(Deprecatee)
assert_deprecated { @dtc.b } klass.deprecate :fubar, :fubar=, deprecator: @deprecator
assert_deprecated { @dtc.f = :foo }
assert_deprecated(/./, @deprecator) { klass.new.fubar }
assert_deprecated(/./, @deprecator) { klass.new.fubar = :foo }
end end
test "Module::deprecate with alternative method" do 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 end
test "Module::deprecate with message" do 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 end
test "delegating to ActiveSupport::Deprecation" do test "delegating to ActiveSupport::Deprecation" do
@ -439,21 +443,6 @@ def method
assert_match "foo", deprecator.messages.last assert_match "foo", deprecator.messages.last
end 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 test "default deprecation_horizon is greater than the current Rails version" do
assert_operator ActiveSupport::Deprecation.new.deprecation_horizon, :>, ActiveSupport::VERSION::STRING assert_operator ActiveSupport::Deprecation.new.deprecation_horizon, :>, ActiveSupport::VERSION::STRING
end end
@ -475,7 +464,13 @@ def old_request; @request end
end end
test "Module::deprecate can be called before the target method is defined" do 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 end
test "warn with empty callstack" do test "warn with empty callstack" do