aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/initializable_test.rb
blob: e308cbcb0e4bf6f754a24ef9b3427a895575f805 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
require 'abstract_unit'
require 'rails/initializable'

module InitializableTests

  class Foo
    include Rails::Initializable

    class << self
      attr_accessor :foo, :bar
    end

    initializer :omg, :global => true do
      @foo ||= 0
      @foo += 1
    end
  end

  class Bar < Foo
    initializer :bar, :global => true do
      @bar ||= 0
      @bar += 1
    end
  end

  module Word
    include Rails::Initializable

    initializer :word, :global => true do
      $word = "bird"
    end
  end

  class Parent
    include Rails::Initializable

    initializer :one, :global => true do
      $arr << 1
    end

    initializer :two, :global => true do
      $arr << 2
    end
  end

  class Child < Parent
    include Rails::Initializable

    initializer :three, :before => :one, :global => true do
      $arr << 3
    end

    initializer :four, :after => :one, :global => true do
      $arr << 4
    end
  end

  class Parent
    initializer :five, :before => :one, :global => true do
      $arr << 5
    end
  end

  class Instance
    include Rails::Initializable

    initializer :one do
      $arr << 1
    end

    initializer :two do
      $arr << 2
    end

    initializer :three, :global => true do
      $arr << 3
    end

    initializer :four, :global => true do
      $arr << 4
    end
  end

  class WithArgs
    include Rails::Initializable

    initializer :foo do |arg|
      $with_arg = arg
    end
  end

  class OverriddenInitializer
    class MoreInitializers
      include Rails::Initializable

      initializer :startup, :before => :last do
        $arr << 3
      end

      initializer :terminate, :after => :first do
        $arr << two
      end

      def two
        2
      end
    end

    include Rails::Initializable

    initializer :first do
      $arr << 1
    end

    initializer :last do
      $arr << 4
    end

    def self.initializers
      super + MoreInitializers.new.initializers
    end
  end

  class Basic < ActiveSupport::TestCase
    include ActiveSupport::Testing::Isolation

    test "initializers run" do
      Foo.run_initializers
      assert_equal 1, Foo.foo
    end

    test "initializers are inherited" do
      Bar.run_initializers
      assert_equal [1, 1], [Bar.foo, Bar.bar]
    end

    test "initializers only get run once" do
      Foo.run_initializers
      Foo.run_initializers
      assert_equal 1, Foo.foo
    end

    test "running initializers on children does not effect the parent" do
      Bar.run_initializers
      assert_nil Foo.foo
      assert_nil Foo.bar
    end

    test "initializing with modules" do
      Word.run_initializers
      assert_equal "bird", $word
    end

    test "creating initializer without a block raises an error" do
      assert_raise(ArgumentError) do
        Class.new do
          include Rails::Initializable

          initializer :foo
        end
      end
    end
  end

  class BeforeAfter < ActiveSupport::TestCase
    test "running on parent" do
      $arr = []
      Parent.run_initializers
      assert_equal [5, 1, 2], $arr
    end

    test "running on child" do
      $arr = []
      Child.run_initializers
      assert_equal [5, 3, 1, 4, 2], $arr
    end
  end

  class InstanceTest < ActiveSupport::TestCase
    test "running locals" do
      $arr = []
      instance = Instance.new
      instance.run_initializers
      assert_equal [1, 2], $arr
    end

    test "running globals" do
      $arr = []
      Instance.run_initializers
      assert_equal [3, 4], $arr
    end
  end

  class WithArgsTest < ActiveSupport::TestCase
    test "running initializers with args" do
      $with_arg = nil
      WithArgs.new.run_initializers('foo')
      assert_equal 'foo', $with_arg
    end
  end

  class OverriddenInitializerTest < ActiveSupport::TestCase
    test "merges in the initializers from the parent in the right order" do
      $arr = []
      OverriddenInitializer.new.run_initializers
      assert_equal [1, 2, 3, 4], $arr
    end
  end
end