aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/reloadable_test.rb
blob: eca21295b4ea442a55badb85304cf0be9e7b31af (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
require File.dirname(__FILE__) + '/abstract_unit'

ActiveSupport::Deprecation.silence do
  module ReloadableTestSandbox
    class AReloadableClass
      include Reloadable
    end
    class AReloadableClassWithSubclasses
      include Reloadable
    end
    class AReloadableSubclass < AReloadableClassWithSubclasses
    end
    class ANonReloadableSubclass < AReloadableClassWithSubclasses
      def self.reloadable?
        false
      end
    end
    class AClassWhichDefinesItsOwnReloadable
      def self.reloadable?
        10
      end
      include Reloadable
    end

    class SubclassesReloadable
      include Reloadable::Subclasses
    end
    class ASubclassOfSubclassesReloadable < SubclassesReloadable
    end

    class AnOnlySubclassReloadableClassSubclassingAReloadableClass
      include Reloadable::Subclasses
    end

    class ASubclassofAOnlySubclassReloadableClassWhichWasSubclassingAReloadableClass < AnOnlySubclassReloadableClassSubclassingAReloadableClass
    end
  end
end

class ReloadableTest < Test::Unit::TestCase
  def test_classes_receive_reloadable
    assert ReloadableTestSandbox::AReloadableClass.respond_to?(:reloadable?)
  end
  def test_classes_inherit_reloadable
    assert ReloadableTestSandbox::AReloadableSubclass.respond_to?(:reloadable?)
  end
  def test_reloadable_is_not_overwritten_if_present
    assert_equal 10, ReloadableTestSandbox::AClassWhichDefinesItsOwnReloadable.reloadable?
  end

  def test_only_subclass_reloadable
    assert_deprecated_reloadable do
      assert !ReloadableTestSandbox::SubclassesReloadable.reloadable?
      assert ReloadableTestSandbox::ASubclassOfSubclassesReloadable.reloadable?
    end
  end

  def test_inside_hierarchy_only_subclass_reloadable
    assert_deprecated_reloadable do
      assert !ReloadableTestSandbox::AnOnlySubclassReloadableClassSubclassingAReloadableClass.reloadable?
      assert ReloadableTestSandbox::ASubclassofAOnlySubclassReloadableClassWhichWasSubclassingAReloadableClass.reloadable?
    end
  end

  def test_removable_classes
    reloadables = %w(
      AReloadableClass
      AReloadableClassWithSubclasses
      AReloadableSubclass
      AClassWhichDefinesItsOwnReloadable
      ASubclassOfSubclassesReloadable
    )
    non_reloadables = %w(
      ANonReloadableSubclass
      SubclassesReloadable
    )

    results = []
    assert_deprecated_reloadable { results = Reloadable.reloadable_classes }
    reloadables.each do |name|
      assert results.include?(ReloadableTestSandbox.const_get(name)), "Expected #{name} to be reloadable"
    end
    non_reloadables.each do |name|
      assert ! results.include?(ReloadableTestSandbox.const_get(name)), "Expected #{name} NOT to be reloadable"
    end
  end
  
  def test_including_reloadable_should_warn
    c = Class.new
    assert_deprecated_reloadable do
      c.send :include, Reloadable
    end
    
    assert_deprecated_reloadable { c.reloadable? }
  end
  
  def test_include_subclasses_should_warn
    c = Class.new
    result, deps = collect_deprecations do
      c.send :include, Reloadable::Subclasses
    end
    assert_equal 1, deps.size
    assert_match %r{Reloadable::Subclasses}, deps.first

    assert_deprecated_reloadable { c.reloadable? }
  end
  
  def test_include_deprecated_should_not_warn
    c = Class.new
    result, deps = collect_deprecations do
      c.send :include, Reloadable::Deprecated
    end
    assert_equal 0, deps.size
    
    assert c.respond_to?(:reloadable?)
    assert_deprecated_reloadable { c.reloadable? }
  end
  
  protected
    def assert_deprecated_reloadable(&block)
      assert_deprecated(/reloadable/, &block)
    end
end