1
1
Code . require_file "../test_helper.exs" , __DIR__
2
2
3
3
defmodule ExUnit.CallbacksTest do
4
- use ExUnit.Case , async: true
5
-
6
- setup_all do
7
- { :ok , [ context: :setup_all ] }
8
- end
9
-
10
- setup do
11
- { :ok , [ initial_setup: true ] }
12
- end
13
-
14
- setup context do
15
- assert context [ :initial_setup ]
16
- assert context [ :context ] == :setup_all
17
- { :ok , [ context: :setup ] }
18
- end
19
-
20
- setup context do
21
- if Process . get ( :ex_unit_callback ) do
22
- raise "ex_unit_callback was not cleaned"
23
- else
24
- Process . put ( :ex_unit_callback , context [ :test ] )
25
- end
26
- :ok
27
- end
28
-
29
- teardown context do
30
- assert context [ :context ] == :setup
31
- { :ok , context: :teardown }
32
- end
33
-
34
- teardown context do
35
- assert Process . get ( :ex_unit_callback ) == context [ :test ]
36
- Process . delete ( :ex_unit_callback )
37
- assert context [ :context ] == :teardown
38
- :ok
39
- end
40
-
41
- teardown_all context do
42
- assert context [ :context ] == :setup_all
43
- :ok
44
- end
4
+ use ExUnit.Case
45
5
46
6
import ExUnit.CaptureIO
47
7
48
- test "callbacks can run custom code" do
49
- assert Process . get ( :ex_unit_callback ) == :"test callbacks can run custom code"
50
- end
51
-
52
- test "receives context from callback" , context do
53
- assert context [ :context ] == :setup
54
- end
55
-
56
- test "doesn't choke on setup errors" do
57
- defmodule SetupTest do
8
+ test "callbacks run custom code with context" do
9
+ defmodule CallbacksTest do
58
10
use ExUnit.Case
59
11
60
- setup _ do
61
- :ok = error
12
+ setup_all do
13
+ { :ok , [ context: :setup_all ] }
62
14
end
63
15
64
- test "ok" do
65
- :ok
16
+ setup do
17
+ { :ok , [ initial_setup: true ] }
66
18
end
67
19
68
- defp error , do: :error
69
- end
70
-
71
- assert capture_io ( fn -> ExUnit . run end ) =~
72
- "** (MatchError) no match of right hand side value: :error"
73
- end
74
-
75
- test "doesn't choke on setup_all errors" do
76
- defmodule SetupAllTest do
77
- use ExUnit.Case , async: false
78
-
79
- setup_all _ do
80
- :ok = error
20
+ setup context do
21
+ assert context [ :initial_setup ]
22
+ assert context [ :context ] == :setup_all
23
+ { :ok , [ context: :setup ] }
81
24
end
82
25
83
- test "ok" do
84
- :ok
26
+ test "callbacks" , context do
27
+ assert context [ :context ] == :setup
85
28
end
86
-
87
- defp error , do: :error
88
29
end
89
30
90
- ExUnit . configure ( formatters: [ ExUnit.CLIFormatter ] )
91
-
92
31
assert capture_io ( fn -> ExUnit . run end ) =~
93
- "** (MatchError) no match of right hand side value: :error "
32
+ "1 tests, 0 failures "
94
33
end
95
34
96
- test "doesn't choke on teardown errors" do
97
- defmodule TeardownTest do
98
- use ExUnit.Case , async: false
35
+ test "doesn't choke on setup errors" do
36
+ defmodule SetupTest do
37
+ use ExUnit.Case
99
38
100
- teardown _ do
39
+ setup _ do
101
40
:ok = error
102
41
end
103
42
@@ -112,11 +51,11 @@ defmodule ExUnit.CallbacksTest do
112
51
"** (MatchError) no match of right hand side value: :error"
113
52
end
114
53
115
- test "doesn't choke on teardown_all errors" do
116
- defmodule TeardownAllTest do
54
+ test "doesn't choke on setup_all errors" do
55
+ defmodule SetupAllTest do
117
56
use ExUnit.Case , async: false
118
57
119
- teardown_all _ do
58
+ setup_all _ do
120
59
:ok = error
121
60
end
122
61
@@ -136,18 +75,13 @@ defmodule ExUnit.CallbacksTest do
136
75
use ExUnit.Case , async: false
137
76
138
77
test "ok" do
139
- on_exit fn ->
140
- :ok = error
141
- end
142
-
78
+ on_exit fn -> :ok = error end
143
79
:ok
144
80
end
145
81
146
82
defp error , do: :error
147
83
end
148
84
149
- ExUnit . configure ( formatters: [ ExUnit.CLIFormatter ] )
150
-
151
85
assert capture_io ( fn -> ExUnit . run end ) =~
152
86
"** (MatchError) no match of right hand side value: :error"
153
87
end
@@ -157,21 +91,22 @@ defmodule ExUnit.CallbacksTest do
157
91
use ExUnit.Case , async: false
158
92
159
93
test "ok" do
160
- on_exit fn ->
161
- Process . exit ( self ( ) , :kill )
162
- end
163
-
94
+ on_exit fn -> Process . exit ( self ( ) , :kill ) end
164
95
:ok
165
96
end
166
97
end
167
98
168
- ExUnit . configure ( formatters: [ ExUnit.CLIFormatter ] )
169
99
assert capture_io ( fn -> ExUnit . run end ) =~
170
100
">) killed"
171
101
end
172
102
103
+ defp no_formatters! do
104
+ ExUnit . configure ( formatters: [ ] )
105
+ on_exit fn -> ExUnit . configure ( formatters: [ ExUnit.CLIFormatter ] ) end
106
+ end
107
+
173
108
test "runs multiple on_exit exits and overrides by ref" do
174
- defmodule OnExitOverrideTest do
109
+ defmodule OnExitSuccessTest do
175
110
use ExUnit.Case , async: false
176
111
177
112
setup do
@@ -182,12 +117,16 @@ defmodule ExUnit.CallbacksTest do
182
117
on_exit { :overridden , 1 } , fn ->
183
118
IO . puts "on_exit 1 overridden -> not run"
184
119
end
120
+
121
+ :ok
185
122
end
186
123
187
124
setup_all do
188
125
on_exit fn ->
189
126
IO . puts "on_exit setup_all run"
190
127
end
128
+
129
+ :ok
191
130
end
192
131
193
132
test "ok" do
@@ -211,7 +150,7 @@ defmodule ExUnit.CallbacksTest do
211
150
end
212
151
end
213
152
214
- ExUnit . configure ( formatters: [ ExUnit.CLIFormatter ] )
153
+ no_formatters!
215
154
output = capture_io ( fn -> ExUnit . run end )
216
155
217
156
assert output =~ """
@@ -224,6 +163,45 @@ defmodule ExUnit.CallbacksTest do
224
163
225
164
refute output =~ "not run"
226
165
end
166
+
167
+ test "runs multiple on_exit on failure" do
168
+ defmodule OnExitFailureTest do
169
+ use ExUnit.Case , async: false
170
+
171
+ setup do
172
+ on_exit fn ->
173
+ IO . puts "on_exit setup run"
174
+ end
175
+
176
+ :ok
177
+ end
178
+
179
+ setup_all do
180
+ on_exit fn ->
181
+ IO . puts "on_exit setup_all run"
182
+ end
183
+
184
+ :ok
185
+ end
186
+
187
+ test "ok" do
188
+ on_exit fn ->
189
+ IO . puts "simple on_exit run"
190
+ end
191
+
192
+ flunk "oops"
193
+ end
194
+ end
195
+
196
+ no_formatters!
197
+ output = capture_io ( fn -> ExUnit . run end )
198
+
199
+ assert output =~ """
200
+ simple on_exit run
201
+ on_exit setup run
202
+ on_exit setup_all run
203
+ """
204
+ end
227
205
end
228
206
229
207
defmodule ExUnit.CallbacksNoTests do
@@ -236,12 +214,4 @@ defmodule ExUnit.CallbacksNoTests do
236
214
setup do
237
215
raise "Never run"
238
216
end
239
-
240
- teardown do
241
- raise "Never run"
242
- end
243
-
244
- teardown_all do
245
- raise "Never run"
246
- end
247
217
end
0 commit comments