initializable_test.rb 3.6 KB
Newer Older
1 2 3 4 5 6
require 'abstract_unit'
require 'rails/initializable'

module InitializableTests

  class Foo
7
    include Rails::Initializable
8 9 10 11 12

    class << self
      attr_accessor :foo, :bar
    end

13
    initializer :omg do
14 15 16 17 18 19
      @foo ||= 0
      @foo += 1
    end
  end

  class Bar < Foo
20
    initializer :bar do
21 22 23 24 25
      @bar ||= 0
      @bar += 1
    end
  end

26
  module Word
27
    include Rails::Initializable
28

29
    initializer :word do
30 31 32 33
      $word = "bird"
    end
  end

Y
Yehuda Katz + Carl Lerche 已提交
34
  class Parent
35
    include Rails::Initializable
Y
Yehuda Katz + Carl Lerche 已提交
36

37
    initializer :one do
Y
Yehuda Katz + Carl Lerche 已提交
38 39 40
      $arr << 1
    end

41
    initializer :two do
Y
Yehuda Katz + Carl Lerche 已提交
42 43 44 45 46
      $arr << 2
    end
  end

  class Child < Parent
47
    include Rails::Initializable
Y
Yehuda Katz + Carl Lerche 已提交
48

49
    initializer :three, :before => :one do
Y
Yehuda Katz + Carl Lerche 已提交
50 51 52
      $arr << 3
    end

53
    initializer :four, :after => :one do
Y
Yehuda Katz + Carl Lerche 已提交
54 55 56 57 58
      $arr << 4
    end
  end

  class Parent
59
    initializer :five, :before => :one do
Y
Yehuda Katz + Carl Lerche 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
      $arr << 5
    end
  end

  class Instance
    include Rails::Initializable

    initializer :one do
      $arr << 1
    end

    initializer :two do
      $arr << 2
    end

75
    initializer :three do
Y
Yehuda Katz + Carl Lerche 已提交
76 77 78
      $arr << 3
    end

79
    initializer :four do
Y
Yehuda Katz + Carl Lerche 已提交
80 81 82 83
      $arr << 4
    end
  end

84 85 86 87 88 89 90 91 92 93 94 95 96
  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
Y
Yehuda Katz + Carl Lerche 已提交
97 98 99 100
        $arr << 3
      end

      initializer :terminate, :after => :first do
101 102 103 104 105
        $arr << two
      end

      def two
        2
106 107 108 109 110 111 112 113 114 115
      end
    end

    include Rails::Initializable

    initializer :first do
      $arr << 1
    end

    initializer :last do
Y
Yehuda Katz + Carl Lerche 已提交
116
      $arr << 4
117 118 119
    end

    def self.initializers
120
      super + MoreInitializers.new.initializers
121 122 123
    end
  end

124 125 126 127
  class Basic < ActiveSupport::TestCase
    include ActiveSupport::Testing::Isolation

    test "initializers run" do
Y
Yehuda Katz 已提交
128
      Foo.run_initializers
129 130 131 132
      assert_equal 1, Foo.foo
    end

    test "initializers are inherited" do
Y
Yehuda Katz 已提交
133
      Bar.run_initializers
134 135 136 137
      assert_equal [1, 1], [Bar.foo, Bar.bar]
    end

    test "initializers only get run once" do
Y
Yehuda Katz 已提交
138 139
      Foo.run_initializers
      Foo.run_initializers
140 141 142 143
      assert_equal 1, Foo.foo
    end

    test "running initializers on children does not effect the parent" do
Y
Yehuda Katz 已提交
144
      Bar.run_initializers
145 146 147 148
      assert_nil Foo.foo
      assert_nil Foo.bar
    end

149
    test "initializing with modules" do
Y
Yehuda Katz 已提交
150
      Word.run_initializers
151 152
      assert_equal "bird", $word
    end
153 154 155 156 157 158 159 160 161 162

    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
163
  end
Y
Yehuda Katz + Carl Lerche 已提交
164 165 166 167

  class BeforeAfter < ActiveSupport::TestCase
    test "running on parent" do
      $arr = []
Y
Yehuda Katz 已提交
168
      Parent.run_initializers
Y
Yehuda Katz + Carl Lerche 已提交
169 170 171 172 173
      assert_equal [5, 1, 2], $arr
    end

    test "running on child" do
      $arr = []
Y
Yehuda Katz 已提交
174
      Child.run_initializers
Y
Yehuda Katz + Carl Lerche 已提交
175 176 177 178 179 180 181 182
      assert_equal [5, 3, 1, 4, 2], $arr
    end
  end

  class InstanceTest < ActiveSupport::TestCase
    test "running locals" do
      $arr = []
      instance = Instance.new
Y
Yehuda Katz 已提交
183
      instance.run_initializers
184
      assert_equal [1, 2, 3, 4], $arr
Y
Yehuda Katz + Carl Lerche 已提交
185 186
    end
  end
187 188 189 190 191 192 193 194 195 196 197 198 199

  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
Y
Yehuda Katz + Carl Lerche 已提交
200
      assert_equal [1, 2, 3, 4], $arr
201 202
    end
  end
203
end