From 71993c6f9770b1350aa41fe8c68f1dd2c7800403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Nie=C5=82acny?= Date: Thu, 13 Sep 2012 08:38:34 +0200 Subject: Change ActiveSupport::Deprecation to class. ActiveSupport::Deprecation is now a class rather than a module. You can get instance of ActiveSupport::Deprecation calling #instance method. ActiveSupport::Deprecation.instance But when you need to get new object od ActiveSupport::Deprecation you need to just call #new. @instance = ActiveSupport::Deprecation.new Since you can create a new object, you can change the version and the name of the library where the deprecator concerned. ActiveSupport::Deprecation.new('2.0', 'MyGem') If you need use another deprecator instance you can select it in the options of deprecate method. deprecate :method, :deprecator => deprecator_instance Documentation has been updated. --- activesupport/test/deprecation_test.rb | 134 +++++++++++++++++++-------------- 1 file changed, 78 insertions(+), 56 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb index 308c7c3fe7..c081103cc7 100644 --- a/activesupport/test/deprecation_test.rb +++ b/activesupport/test/deprecation_test.rb @@ -104,6 +104,17 @@ class DeprecationTest < ActiveSupport::TestCase assert_match(/call stack!/, content) end + def test_default_stderr_behavior_with_warn_method + ActiveSupport::Deprecation.behavior = :stderr + + content = capture(:stderr) { + ActiveSupport::Deprecation.warn('Instance error!', ['instance call stack!']) + } + + assert_match(/Instance error!/, content) + assert_match(/instance call stack!/, content) + end + def test_default_silence_behavior ActiveSupport::Deprecation.behavior = :silence behavior = ActiveSupport::Deprecation.behavior.first @@ -187,39 +198,57 @@ class DeprecationTest < ActiveSupport::TestCase assert_deprecated(/you now need to do something extra for this one/) { @dtc.d } end - def test_deprecation_in_other_module_does_not_interfere + def test_deprecation_in_other_object messages = [] - m = Module.new - m.extend ActiveSupport::Deprecation - m.behavior = Proc.new{|message, callstack| messages << message} - assert_not_deprecated do # not globally - assert_difference("messages.size") do # but locally - m.warn("warning") - end + klass = Class.new do + delegate :warn, :behavior=, to: ActiveSupport::Deprecation + end + + o = klass.new + o.behavior = Proc.new { |message, callstack| messages << message } + assert_difference("messages.size") do + o.warn("warning") end end - def test_deprecated_method_with_deprecator_implemented + def test_deprecated_method_with_custom_method_warning deprecator = deprecator_with_messages - def deprecator.deprecated_method_warning(method, *params) - "deprecator.deprecated_method_warning.#{method}" + + class << deprecator + private + def deprecated_method_warning(method, message) + "deprecator.deprecated_method_warning.#{method}" + end end - deprecatee = Class.new() do + deprecatee = Class.new do def method end - deprecate :method - define_method(:deprecator){ deprecator } + deprecate :method, deprecator: deprecator end deprecatee.new.method assert deprecator.messages.first.match("DEPRECATION WARNING: deprecator.deprecated_method_warning.method") end + def test_deprecate_with_custom_deprecator + custom_deprecator = mock('Deprecator') do + expects(:deprecation_warning) + end + + klass = Class.new do + def method + end + deprecate :method, deprecator: custom_deprecator + end + + klass.new.method + end + def test_deprecated_constant_with_deprecator_given deprecator = deprecator_with_messages - klass = Class.new() + klass = Class.new klass.const_set(:OLD, ActiveSupport::Deprecation::DeprecatedConstantProxy.new('klass::OLD', 'Object', deprecator) ) assert_difference("deprecator.messages.size") do klass::OLD.to_s @@ -230,23 +259,21 @@ class DeprecationTest < ActiveSupport::TestCase deprecator = deprecator_with_messages klass = Class.new() do - def initialize - @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request) + 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 - define_method(:deprecator) { deprecator } end - assert_difference("deprecator.messages.size") { klass.new.old_request.to_s } - + assert_difference("deprecator.messages.size") { klass.new(deprecator).old_request.to_s } end def test_deprecated_instance_variable_with_given_deprecator deprecator = deprecator_with_messages - klass = Class.new() do + klass = Class.new do define_method(:initialize) do @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator) @_request = :a_request @@ -258,23 +285,23 @@ class DeprecationTest < ActiveSupport::TestCase assert_difference("deprecator.messages.size") { klass.new.old_request.to_s } end - def test_included_deprecation_module - klass = Class.new() do + def test_delegate_deprecator_instance + klass = Class.new do attr_reader :last_message - include ActiveSupport::Deprecation + delegate :warn, :behavior=, to: ActiveSupport::Deprecation + + def initialize + self.behavior = [Proc.new { |message| @last_message = message }] + end + def deprecated_method warn(deprecated_method_warning(:deprecated_method, "You are calling deprecated method")) end private - - def deprecated_method_warning(method_name, message = nil) - message || "#{method_name} is deprecated and will be removed from This Library" - end - - def behavior - @behavior ||= [Proc.new { |message| @last_message = message }] - end + def deprecated_method_warning(method_name, message = nil) + message || "#{method_name} is deprecated and will be removed from This Library" + end end object = klass.new @@ -282,35 +309,30 @@ class DeprecationTest < ActiveSupport::TestCase assert_match(/You are calling deprecated method/, object.last_message) end - unless defined?(::MiniTest) - def test_assertion_failed_error_doesnt_spout_deprecation_warnings - error_class = Class.new(StandardError) do - def message - ActiveSupport::Deprecation.warn 'warning in error message' - super - end - end - - raise error_class.new('hmm') + def test_default_gem_name + deprecator = ActiveSupport::Deprecation.new - rescue => e - error = Test::Unit::Error.new('testing ur doodz', e) - assert_not_deprecated { error.message } - assert_nil @last_message + deprecator.send(:deprecated_method_warning, :deprecated_method, "You are calling deprecated method").tap do |message| + assert_match(/is deprecated and will be removed from Rails/, message) end end + def test_custom_gem_name + deprecator = ActiveSupport::Deprecation.new('2.0', 'Custom') - private - - - def deprecator_with_messages - deprecator = Object.new - deprecator.extend(ActiveSupport::Deprecation) - deprecator.behavior = Proc.new{|message, callstack| deprecator.messages << message} - def deprecator.messages - @messages ||= [] + deprecator.send(:deprecated_method_warning, :deprecated_method, "You are calling deprecated method").tap do |message| + assert_match(/is deprecated and will be removed from Custom/, message) end - deprecator end + + private + def deprecator_with_messages + klass = Class.new(ActiveSupport::Deprecation) + deprecator = klass.new + deprecator.behavior = Proc.new{|message, callstack| deprecator.messages << message} + def deprecator.messages + @messages ||= [] + end + deprecator + end end -- cgit v1.2.3