From 2c690a0f5b36896da9b003d4e24159a27ebd7f71 Mon Sep 17 00:00:00 2001 From: Robert Pankowecki Date: Mon, 25 Jul 2011 21:05:06 +0200 Subject: extend ActiveSupport::Deprecation with self, allow other objects to extend/include it also. test local deprecation deprecator object Test ActiveSupport::Deprecation when included --- activesupport/test/deprecation_test.rb | 127 +++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) (limited to 'activesupport/test/deprecation_test.rb') diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb index e21f3efe36..308c7c3fe7 100644 --- a/activesupport/test/deprecation_test.rb +++ b/activesupport/test/deprecation_test.rb @@ -186,4 +186,131 @@ class DeprecationTest < ActiveSupport::TestCase def test_deprecation_with_explicit_message assert_deprecated(/you now need to do something extra for this one/) { @dtc.d } end + + def test_deprecation_in_other_module_does_not_interfere + 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 + end + end + + def test_deprecated_method_with_deprecator_implemented + deprecator = deprecator_with_messages + def deprecator.deprecated_method_warning(method, *params) + "deprecator.deprecated_method_warning.#{method}" + end + + deprecatee = Class.new() do + def method + end + deprecate :method + define_method(:deprecator){ deprecator } + end + + deprecatee.new.method + assert deprecator.messages.first.match("DEPRECATION WARNING: deprecator.deprecated_method_warning.method") + end + + def test_deprecated_constant_with_deprecator_given + deprecator = deprecator_with_messages + 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 + end + end + + def test_deprecated_instance_variable_with_instance_deprecator + deprecator = deprecator_with_messages + + klass = Class.new() do + def initialize + @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request) + @_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 } + + end + + def test_deprecated_instance_variable_with_given_deprecator + deprecator = deprecator_with_messages + + klass = Class.new() do + define_method(:initialize) do + @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.old_request.to_s } + end + + def test_included_deprecation_module + klass = Class.new() do + attr_reader :last_message + include ActiveSupport::Deprecation + 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 + end + + object = klass.new + object.deprecated_method + 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') + + rescue => e + error = Test::Unit::Error.new('testing ur doodz', e) + assert_not_deprecated { error.message } + assert_nil @last_message + end + end + + + 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 ||= [] + end + deprecator + end end -- cgit v1.2.3