2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/deprecation"
|
2015-10-13 14:10:14 -04:00
|
|
|
|
|
|
|
class MethodWrappersTest < ActiveSupport::TestCase
|
|
|
|
def setup
|
|
|
|
@klass = Class.new do
|
|
|
|
def new_method; "abc" end
|
|
|
|
alias_method :old_method, :new_method
|
2017-12-13 14:46:42 -05:00
|
|
|
|
|
|
|
protected
|
|
|
|
def new_protected_method; "abc" end
|
|
|
|
alias_method :old_protected_method, :new_protected_method
|
|
|
|
|
|
|
|
private
|
|
|
|
def new_private_method; "abc" end
|
|
|
|
alias_method :old_private_method, :new_private_method
|
2015-10-13 14:10:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-13 21:49:54 -05:00
|
|
|
def test_deprecate_methods_without_alternate_method
|
|
|
|
warning = /old_method is deprecated and will be removed from Rails \d.\d./
|
|
|
|
ActiveSupport::Deprecation.deprecate_methods(@klass, :old_method)
|
|
|
|
|
|
|
|
assert_deprecated(warning) { assert_equal "abc", @klass.new.old_method }
|
|
|
|
end
|
|
|
|
|
2015-10-13 14:10:14 -04:00
|
|
|
def test_deprecate_methods_warning_default
|
|
|
|
warning = /old_method is deprecated and will be removed from Rails \d.\d \(use new_method instead\)/
|
2016-08-06 13:38:33 -04:00
|
|
|
ActiveSupport::Deprecation.deprecate_methods(@klass, old_method: :new_method)
|
2015-10-13 14:10:14 -04:00
|
|
|
|
|
|
|
assert_deprecated(warning) { assert_equal "abc", @klass.new.old_method }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_deprecate_methods_warning_with_optional_deprecator
|
|
|
|
warning = /old_method is deprecated and will be removed from MyGem next-release \(use new_method instead\)/
|
|
|
|
deprecator = ActiveSupport::Deprecation.new("next-release", "MyGem")
|
2016-08-06 13:38:33 -04:00
|
|
|
ActiveSupport::Deprecation.deprecate_methods(@klass, old_method: :new_method, deprecator: deprecator)
|
2015-10-13 14:10:14 -04:00
|
|
|
|
|
|
|
assert_deprecated(warning, deprecator) { assert_equal "abc", @klass.new.old_method }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_deprecate_methods_warning_when_deprecated_with_custom_deprecator
|
|
|
|
warning = /old_method is deprecated and will be removed from MyGem next-release \(use new_method instead\)/
|
|
|
|
deprecator = ActiveSupport::Deprecation.new("next-release", "MyGem")
|
2016-08-06 13:38:33 -04:00
|
|
|
deprecator.deprecate_methods(@klass, old_method: :new_method)
|
2015-10-13 14:10:14 -04:00
|
|
|
|
|
|
|
assert_deprecated(warning, deprecator) { assert_equal "abc", @klass.new.old_method }
|
|
|
|
end
|
2017-12-13 14:46:42 -05:00
|
|
|
|
|
|
|
def test_deprecate_methods_protected_method
|
|
|
|
ActiveSupport::Deprecation.deprecate_methods(@klass, old_protected_method: :new_protected_method)
|
|
|
|
|
|
|
|
assert(@klass.protected_method_defined?(:old_protected_method))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_deprecate_methods_private_method
|
|
|
|
ActiveSupport::Deprecation.deprecate_methods(@klass, old_private_method: :new_private_method)
|
|
|
|
|
|
|
|
assert(@klass.private_method_defined?(:old_private_method))
|
|
|
|
end
|
2018-07-10 12:03:54 -04:00
|
|
|
|
|
|
|
def test_deprecate_class_method
|
|
|
|
mod = Module.new do
|
|
|
|
extend self
|
|
|
|
|
|
|
|
def old_method
|
|
|
|
"abc"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ActiveSupport::Deprecation.deprecate_methods(mod, old_method: :new_method)
|
|
|
|
|
|
|
|
warning = /old_method is deprecated and will be removed from Rails \d.\d \(use new_method instead\)/
|
|
|
|
assert_deprecated(warning) { assert_equal "abc", mod.old_method }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_deprecate_method_when_class_extends_module
|
|
|
|
mod = Module.new do
|
|
|
|
def old_method
|
|
|
|
"abc"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@klass.extend mod
|
|
|
|
ActiveSupport::Deprecation.deprecate_methods(mod, old_method: :new_method)
|
|
|
|
|
|
|
|
warning = /old_method is deprecated and will be removed from Rails \d.\d \(use new_method instead\)/
|
|
|
|
assert_deprecated(warning) { assert_equal "abc", @klass.old_method }
|
|
|
|
end
|
2015-10-13 14:10:14 -04:00
|
|
|
end
|