| 1 |
require 'abstract_unit' |
|---|
| 2 |
|
|---|
| 3 |
class Deprecatee |
|---|
| 4 |
def initialize |
|---|
| 5 |
@request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request) |
|---|
| 6 |
@_request = 'there we go' |
|---|
| 7 |
end |
|---|
| 8 |
def request; @_request end |
|---|
| 9 |
def old_request; @request end |
|---|
| 10 |
|
|---|
| 11 |
def partially(foo = nil) |
|---|
| 12 |
ActiveSupport::Deprecation.warn('calling with foo=nil is out', caller) if foo.nil? |
|---|
| 13 |
end |
|---|
| 14 |
|
|---|
| 15 |
def not() 2 end |
|---|
| 16 |
def none() 1 end |
|---|
| 17 |
def one(a) a end |
|---|
| 18 |
def multi(a,b,c) [a,b,c] end |
|---|
| 19 |
deprecate :none, :one, :multi |
|---|
| 20 |
|
|---|
| 21 |
def a; end |
|---|
| 22 |
def b; end |
|---|
| 23 |
def c; end |
|---|
| 24 |
def d; end |
|---|
| 25 |
def e; end |
|---|
| 26 |
deprecate :a, :b, :c => :e, :d => "you now need to do something extra for this one" |
|---|
| 27 |
end |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
class DeprecationTest < Test::Unit::TestCase |
|---|
| 31 |
def setup |
|---|
| 32 |
|
|---|
| 33 |
@old_behavior = ActiveSupport::Deprecation.behavior |
|---|
| 34 |
@last_message = nil |
|---|
| 35 |
ActiveSupport::Deprecation.behavior = Proc.new { |message| @last_message = message } |
|---|
| 36 |
|
|---|
| 37 |
@dtc = Deprecatee.new |
|---|
| 38 |
end |
|---|
| 39 |
|
|---|
| 40 |
def teardown |
|---|
| 41 |
ActiveSupport::Deprecation.behavior = @old_behavior |
|---|
| 42 |
end |
|---|
| 43 |
|
|---|
| 44 |
def test_inline_deprecation_warning |
|---|
| 45 |
assert_deprecated(/foo=nil/) do |
|---|
| 46 |
@dtc.partially |
|---|
| 47 |
end |
|---|
| 48 |
end |
|---|
| 49 |
|
|---|
| 50 |
def test_undeprecated |
|---|
| 51 |
assert_not_deprecated do |
|---|
| 52 |
assert_equal 2, @dtc.not |
|---|
| 53 |
end |
|---|
| 54 |
end |
|---|
| 55 |
|
|---|
| 56 |
def test_deprecate_class_method |
|---|
| 57 |
assert_deprecated(/none is deprecated.*test_deprecate_class_method at/) do |
|---|
| 58 |
assert_equal 1, @dtc.none |
|---|
| 59 |
end |
|---|
| 60 |
|
|---|
| 61 |
assert_deprecated(/one is deprecated/) do |
|---|
| 62 |
assert_equal 1, @dtc.one(1) |
|---|
| 63 |
end |
|---|
| 64 |
|
|---|
| 65 |
assert_deprecated(/multi is deprecated/) do |
|---|
| 66 |
assert_equal [1,2,3], @dtc.multi(1,2,3) |
|---|
| 67 |
end |
|---|
| 68 |
end |
|---|
| 69 |
|
|---|
| 70 |
def test_nil_behavior_is_ignored |
|---|
| 71 |
ActiveSupport::Deprecation.behavior = nil |
|---|
| 72 |
assert_deprecated(/foo=nil/) { @dtc.partially } |
|---|
| 73 |
end |
|---|
| 74 |
|
|---|
| 75 |
def test_deprecated_instance_variable_proxy |
|---|
| 76 |
assert_not_deprecated { @dtc.request.size } |
|---|
| 77 |
|
|---|
| 78 |
assert_deprecated('@request.size') { assert_equal @dtc.request.size, @dtc.old_request.size } |
|---|
| 79 |
assert_deprecated('@request.to_s') { assert_equal @dtc.request.to_s, @dtc.old_request.to_s } |
|---|
| 80 |
end |
|---|
| 81 |
|
|---|
| 82 |
def test_deprecated_instance_variable_proxy_shouldnt_warn_on_inspect |
|---|
| 83 |
assert_not_deprecated { assert_equal @dtc.request.inspect, @dtc.old_request.inspect } |
|---|
| 84 |
end |
|---|
| 85 |
|
|---|
| 86 |
def test_assert_deprecation_without_match |
|---|
| 87 |
assert_deprecated do |
|---|
| 88 |
@dtc.partially |
|---|
| 89 |
end |
|---|
| 90 |
end |
|---|
| 91 |
|
|---|
| 92 |
def test_assert_deprecated_matches_any_warning |
|---|
| 93 |
assert_deprecated 'abc' do |
|---|
| 94 |
ActiveSupport::Deprecation.warn 'abc' |
|---|
| 95 |
ActiveSupport::Deprecation.warn 'def' |
|---|
| 96 |
end |
|---|
| 97 |
rescue Test::Unit::AssertionFailedError |
|---|
| 98 |
flunk 'assert_deprecated should match any warning in block, not just the last one' |
|---|
| 99 |
end |
|---|
| 100 |
|
|---|
| 101 |
def test_assert_not_deprecated_returns_result_of_block |
|---|
| 102 |
assert_equal 123, assert_not_deprecated { 123 } |
|---|
| 103 |
end |
|---|
| 104 |
|
|---|
| 105 |
def test_assert_deprecated_returns_result_of_block |
|---|
| 106 |
result = assert_deprecated('abc') do |
|---|
| 107 |
ActiveSupport::Deprecation.warn 'abc' |
|---|
| 108 |
123 |
|---|
| 109 |
end |
|---|
| 110 |
assert_equal 123, result |
|---|
| 111 |
end |
|---|
| 112 |
|
|---|
| 113 |
def test_silence |
|---|
| 114 |
ActiveSupport::Deprecation.silence do |
|---|
| 115 |
assert_not_deprecated { @dtc.partially } |
|---|
| 116 |
end |
|---|
| 117 |
|
|---|
| 118 |
ActiveSupport::Deprecation.silenced = true |
|---|
| 119 |
assert_not_deprecated { @dtc.partially } |
|---|
| 120 |
ActiveSupport::Deprecation.silenced = false |
|---|
| 121 |
end |
|---|
| 122 |
|
|---|
| 123 |
def test_deprecation_without_explanation |
|---|
| 124 |
assert_deprecated { @dtc.a } |
|---|
| 125 |
assert_deprecated { @dtc.b } |
|---|
| 126 |
end |
|---|
| 127 |
|
|---|
| 128 |
def test_deprecation_with_alternate_method |
|---|
| 129 |
assert_deprecated(/use e instead/) { @dtc.c } |
|---|
| 130 |
end |
|---|
| 131 |
|
|---|
| 132 |
def test_deprecation_with_explicit_message |
|---|
| 133 |
assert_deprecated(/you now need to do something extra for this one/) { @dtc.d } |
|---|
| 134 |
end |
|---|
| 135 |
|
|---|
| 136 |
def test_assertion_failed_error_doesnt_spout_deprecation_warnings |
|---|
| 137 |
error_class = Class.new(StandardError) do |
|---|
| 138 |
def message |
|---|
| 139 |
ActiveSupport::Deprecation.warn 'warning in error message' |
|---|
| 140 |
super |
|---|
| 141 |
end |
|---|
| 142 |
end |
|---|
| 143 |
|
|---|
| 144 |
raise error_class.new('hmm') |
|---|
| 145 |
|
|---|
| 146 |
rescue => e |
|---|
| 147 |
error = Test::Unit::Error.new('testing ur doodz', e) |
|---|
| 148 |
assert_not_deprecated { error.message } |
|---|
| 149 |
assert_nil @last_message |
|---|
| 150 |
end |
|---|
| 151 |
end |
|---|