1 require 'abstract_unit'
5 @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request)
6 @_request = 'there we go'
8 def request; @_request end
9 def old_request; @request end
11 def partially(foo = nil)
12 ActiveSupport::Deprecation.warn('calling with foo=nil is out', caller) if foo.nil?
18 def multi(a,b,c) [a,b,c] end
19 deprecate :none, :one, :multi
26 deprecate :a, :b, :c => :e, :d => "you now need to do something extra for this one"
34 A = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Deprecatee::A', 'Deprecatee::B::C')
38 class DeprecationTest < ActiveSupport::TestCase
40 # Track the last warning.
41 @old_behavior = ActiveSupport::Deprecation.behavior
43 ActiveSupport::Deprecation.behavior = Proc.new { |message| @last_message = message }
49 ActiveSupport::Deprecation.behavior = @old_behavior
52 def test_inline_deprecation_warning
53 assert_deprecated(/foo=nil/) do
59 assert_not_deprecated do
60 assert_equal 2, @dtc.not
64 def test_deprecate_class_method
65 assert_deprecated(/none is deprecated.*test_deprecate_class_method at/) do
66 assert_equal 1, @dtc.none
69 assert_deprecated(/one is deprecated/) do
70 assert_equal 1, @dtc.one(1)
73 assert_deprecated(/multi is deprecated/) do
74 assert_equal [1,2,3], @dtc.multi(1,2,3)
78 def test_nil_behavior_is_ignored
79 ActiveSupport::Deprecation.behavior = nil
80 assert_deprecated(/foo=nil/) { @dtc.partially }
83 def test_deprecated_instance_variable_proxy
84 assert_not_deprecated { @dtc.request.size }
86 assert_deprecated('@request.size') { assert_equal @dtc.request.size, @dtc.old_request.size }
87 assert_deprecated('@request.to_s') { assert_equal @dtc.request.to_s, @dtc.old_request.to_s }
90 def test_deprecated_instance_variable_proxy_shouldnt_warn_on_inspect
91 assert_not_deprecated { assert_equal @dtc.request.inspect, @dtc.old_request.inspect }
94 def test_deprecated_constant_proxy
95 assert_not_deprecated { Deprecatee::B::C }
96 assert_deprecated('Deprecatee::A') { assert_equal Deprecatee::B::C, Deprecatee::A }
99 def test_assert_deprecation_without_match
105 def test_assert_deprecated_matches_any_warning
106 assert_deprecated 'abc' do
107 ActiveSupport::Deprecation.warn 'abc'
108 ActiveSupport::Deprecation.warn 'def'
110 rescue Test::Unit::AssertionFailedError
111 flunk 'assert_deprecated should match any warning in block, not just the last one'
114 def test_assert_not_deprecated_returns_result_of_block
115 assert_equal 123, assert_not_deprecated { 123 }
118 def test_assert_deprecated_returns_result_of_block
119 result = assert_deprecated('abc') do
120 ActiveSupport::Deprecation.warn 'abc'
123 assert_equal 123, result
127 ActiveSupport::Deprecation.silence do
128 assert_not_deprecated { @dtc.partially }
131 ActiveSupport::Deprecation.silenced = true
132 assert_not_deprecated { @dtc.partially }
133 ActiveSupport::Deprecation.silenced = false
136 def test_deprecation_without_explanation
137 assert_deprecated { @dtc.a }
138 assert_deprecated { @dtc.b }
139 assert_deprecated { @dtc.f = :foo }
142 def test_deprecation_with_alternate_method
143 assert_deprecated(/use e instead/) { @dtc.c }
146 def test_deprecation_with_explicit_message
147 assert_deprecated(/you now need to do something extra for this one/) { @dtc.d }
150 unless defined?(::MiniTest)
151 def test_assertion_failed_error_doesnt_spout_deprecation_warnings
152 error_class = Class.new(StandardError) do
154 ActiveSupport::Deprecation.warn 'warning in error message'
159 raise error_class.new('hmm')
162 error = Test::Unit::Error.new('testing ur doodz', e)
163 assert_not_deprecated { error.message }
164 assert_nil @last_message