aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/deprecation_test.rb
blob: c691b5bde0f905d3845eda3befc2ec8ebb9d6c53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/active_support/deprecation'

# Stub out the warnings to allow assertions
module ActiveSupport
  module Deprecation
    class << self
      def issue_warning(message)
        @@warning = message
      end
      def last_warning
        @@warning
      end
    end
  end
end

class DeprecationTestingClass

  def partiallly_deprecated(foo = nil)
    if foo.nil?
      ActiveSupport::Deprecation.issue_warning("calling partially_deprecated with foo=nil is now deprecated")
    end
  end
  
  def not_deprecated
    2
  end
  
  def deprecated_no_args
    1
  end
  deprecate :deprecated_no_args
  
  def deprecated_one_arg(a)
    a
  end
  deprecate :deprecated_one_arg
  
  def deprecated_multiple_args(a,b,c)
    [a,b,c]
  end
  deprecate :deprecated_multiple_args
  
end


class DeprecationTest < Test::Unit::TestCase
  def setup
    @dtc = DeprecationTestingClass.new
    ActiveSupport::Deprecation.issue_warning(nil) # reset
  end
  
  def test_partial_deprecation
    @dtc.partiallly_deprecated
    assert_warning_matches /foo=nil/
  end
  
  def test_raises_nothing
    assert_equal 2, @dtc.not_deprecated
  end
  
  def test_deprecating_class_method
    assert_equal 1, @dtc.deprecated_no_args
    assert_deprecation_warning
    assert_warning_matches /DeprecationTestingClass#deprecated_no_args/
  end
  
  def test_deprecating_class_method_with_argument
    assert_equal 1, @dtc.deprecated_one_arg(1)
  end
  
  def test_deprecating_class_method_with_argument
    assert_equal [1,2,3], @dtc.deprecated_multiple_args(1,2,3)
  end
  
  private
  def assert_warning_matches(rx)
    assert ActiveSupport::Deprecation.last_warning =~ rx, "The deprecation warning did not match #{rx}"
  end
  
  def assert_deprecation_warning
    assert_not_nil ActiveSupport::Deprecation.last_warning, "No Deprecation warnings were issued"
  end
end