aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/initializable_test.rb
blob: a1306adb602f439ac2f6691c170ec1fcefb767db (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
require 'abstract_unit'
require 'rails/initializable'

module InitializableTests

  class Foo
    extend Rails::Initializable

    class << self
      attr_accessor :foo, :bar
    end

    initializer :omg do
      @foo ||= 0
      @foo += 1
    end
  end

  class Bar < Foo
    initializer :bar do
      @bar ||= 0
      @bar += 1
    end
  end

  module Word
    extend Rails::Initializable

    initializer :word do
      $word = "bird"
    end
  end

  class Parent
    extend Rails::Initializable

    initializer :one do
      $arr << 1
    end

    initializer :two do
      $arr << 2
    end
  end

  class Child < Parent
    extend Rails::Initializable

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

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

  class Parent
    initializer :five, :before => :one 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 Basic < ActiveSupport::TestCase
    include ActiveSupport::Testing::Isolation

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

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

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

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

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

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

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

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

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