aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/new_initializer_test.rb
blob: 8d9ef7bee3deff9095e4957ebe9c06b0762d6586 (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
require 'abstract_unit'
require 'active_support/ruby/shim'
require 'initializer'

class InitializerRunnerTest < ActiveSupport::TestCase

  def setup
    @runner = Rails::Initializer::Runner.new
  end

  test "A new runner can be created" do
    assert @runner
  end

  test "The initializers actually get run when the runner is run" do
    state = nil

    @runner.add :foo do
      run { state = true }
    end

    @runner.run
    assert state
  end

  test "By default, initializers get run in the order that they are added" do
    state = []

    @runner.add :first do
      run { state << :first }
    end

    @runner.add :second do
      run { state << :second }
    end

    @runner.run
    assert_equal [:first, :second], state
  end

  test "Raises an exception if :before or :after are specified, but don't exist" do
    assert_raise(Rails::Initializer::Error) do
      @runner.add(:fail, :before => :whale) { 1 }
    end

    assert_raise(Rails::Initializer::Error) do
      @runner.add(:fail, :after => :whale) { 1 }
    end
  end

  test "When adding an initializer, specifying :after allows you to move an initializer after another" do
    state = []

    @runner.add :first do
      run { state << :first }
    end

    @runner.add :second do
      run { state << :second }
    end

    @runner.add :third, :after => :first do
      run { state << :third }
    end

    @runner.run
    assert_equal [:first, :third, :second], state
  end

  test "An initializer can be deleted" do
    state = []

    @runner.add :first do
      run { state << :first }
    end

    @runner.add :second do
      run { state << :second }
    end

    @runner.delete(:second)

    @runner.run
    assert_equal [:first], state
  end

  test "A runner can be initialized with an existing runner, which it copies" do
    state = []

    @runner.add :first do
      run { state << :first }
    end

    @runner.add :second do
      run { state << :second }
    end

    Rails::Initializer::Runner.new(@runner).run
    assert_equal [:first, :second], state
  end

  test "A child runner can be still be modified without modifying the parent" do
    state = []

    @runner.add :first do
      run { state << :first }
    end

    @runner.add :second do
      run { state << :second }
    end

    new_runner = Rails::Initializer::Runner.new(@runner)
    new_runner.add :trois do
      run { state << :trois }
    end
    new_runner.delete(:second)

    new_runner.run
    assert_equal [:first, :trois], state
    state.clear
    @runner.run
    assert_equal [:first, :second], state
  end

  test "A child runner that is modified does not modify any other children of the same parent" do
    state = []

    @runner.add :first do
      run { state << :first }
    end

    @runner.add :second do
      run { state << :second }
    end

    child_one = Rails::Initializer::Runner.new(@runner)
    child_two = Rails::Initializer::Runner.new(@runner)

    child_one.delete(:second)
    child_two.run

    assert_equal [:first, :second], state
  end

  test "It does not run the initializer block immediately" do
    state = []
    @runner.add :first do
      state << :first
    end

    assert_equal [], state
  end

  test "It runs the block when the runner is run" do
    state = []
    @runner.add :first do
      state << :first
    end

    @runner.run
    assert_equal [:first], state
  end

end