1
- defmodule ExUnit.NoValueSupplied do
2
- def no_value , do: { :__no__ , :__meaningful__ , :__value__ }
3
- end
4
-
5
- defexception ExUnit.AssertionError , [
6
- left: ExUnit.NoValueSupplied . no_value ,
7
- right: ExUnit.NoValueSupplied . no_value ,
8
- value: ExUnit.NoValueSupplied . no_value ,
9
- message: ExUnit.NoValueSupplied . no_value ,
10
- operator: ExUnit.NoValueSupplied . no_value ,
11
- expr: "missing failing expression" ] do
1
+ defexception ExUnit.AssertionError ,
2
+ left: :ex_unit_no_meaningful_value ,
3
+ right: :ex_unit_no_meaningful_value ,
4
+ message: :ex_unit_no_meaningful_value ,
5
+ expr: "missing failing expression" do
12
6
7
+ @ doc """
8
+ Indicates no meaningful value for a field.
9
+ """
10
+ def no_value do
11
+ :ex_unit_no_meaningful_value
12
+ end
13
13
end
14
14
15
15
defmodule ExUnit.Assertions do
@@ -56,12 +56,12 @@ defmodule ExUnit.Assertions do
56
56
57
57
will fail with the message:
58
58
59
- Comparison (using >) failed in:
59
+ Assertion with > failed
60
60
code: 1+2+3+4 > 15
61
61
lhs: 10
62
62
rhs: 15
63
63
"""
64
- defmacro assert ( assertion = { := , _ , [ left , right ] } ) do
64
+ defmacro assert ( { := , _ , [ left , right ] } = assertion ) do
65
65
code = Macro . to_string ( assertion )
66
66
{ :case , meta , args } =
67
67
quote do
@@ -70,29 +70,27 @@ defmodule ExUnit.Assertions do
70
70
right
71
71
_ ->
72
72
raise ExUnit.AssertionError ,
73
- right: Macro . to_string ( right ) ,
73
+ right: right ,
74
74
expr: unquote ( code ) ,
75
75
message: "match (=) failed"
76
76
end
77
77
end
78
78
79
79
quote do
80
80
right = unquote ( right )
81
- unquote ( { :case , [ { :export_head , true } | meta ] , args } )
81
+ unquote ( { :case , [ { :export_head , true } | meta ] , args } )
82
82
end
83
83
end
84
84
85
-
86
85
defmacro assert ( assertion ) do
87
86
case translate_assertion ( assertion ) do
88
87
nil ->
89
- # Default message in case no transform was performed
90
88
quote do
91
89
value = unquote ( assertion )
92
90
93
91
unless value do
94
92
raise ExUnit.AssertionError ,
95
- expr: unquote ( Macro . to_string ( assertion ) ) ,
93
+ expr: unquote ( Macro . to_string ( assertion ) ) ,
96
94
message: "#{ inspect value } is not truthy"
97
95
end
98
96
@@ -113,15 +111,14 @@ defmodule ExUnit.Assertions do
113
111
refute age < 0
114
112
115
113
"""
116
-
117
- defmacro refute ( assertion = { := , _ , [ left , right ] } ) do
114
+ defmacro refute ( { := , _ , [ left , right ] } = assertion ) do
118
115
code = Macro . to_string ( assertion )
119
116
{ :case , meta , args } =
120
117
quote do
121
118
case right do
122
119
unquote ( left ) ->
123
120
raise ExUnit.AssertionError ,
124
- right: Macro . to_string ( right ) ,
121
+ right: right ,
125
122
expr: unquote ( code ) ,
126
123
message: "match (=) succeeded, but should have failed"
127
124
_ ->
@@ -131,21 +128,19 @@ defmodule ExUnit.Assertions do
131
128
132
129
quote do
133
130
right = unquote ( right )
134
- unquote ( { :case , [ { :export_head , true } | meta ] , args } )
131
+ unquote ( { :case , [ { :export_head , true } | meta ] , args } )
135
132
end
136
133
end
137
134
138
-
139
135
defmacro refute ( assertion ) do
140
136
case translate_assertion ( { :! , [ ] , [ assertion ] } ) do
141
137
nil ->
142
- # Default message in case no transform was performed
143
138
quote do
144
139
value = unquote ( assertion )
145
140
146
141
if value do
147
142
raise ExUnit.AssertionError ,
148
- expr: unquote ( Macro . to_string ( assertion ) ) ,
143
+ expr: unquote ( Macro . to_string ( assertion ) ) ,
149
144
message: "#{ inspect value } should be false or nil"
150
145
end
151
146
@@ -159,86 +154,38 @@ defmodule ExUnit.Assertions do
159
154
160
155
## START HELPERS
161
156
157
+ @ operator [ :== , :< , :> , :<= , :>= , :=== , :=~ , :!== , :!= , :in ]
162
158
163
-
164
- defp translate_assertion ( expr = { operator , _ , [ left , right ] } )
165
- when operator in [ :== , :< , :> , :<= , :>= , :=== , :=~ , :!== , :!= ] do
166
- assert_operator operator , left , right , expr
167
- end
168
-
169
-
170
- defp translate_assertion ( expr = { :in , _ , [ left , right ] } ) do
171
- code = Macro . to_string ( expr )
159
+ defp translate_assertion ( { operator , _ , [ left , right ] } = expr ) when operator in @ operator do
160
+ expr = Macro . to_string ( expr )
172
161
quote do
173
162
left = unquote ( left )
174
163
right = unquote ( right )
175
- assert_internal Enum . member? ( right , left ) ,
176
- left: left , right: right , expr: unquote ( code )
164
+ assert unquote ( operator ) ( left , right ) ,
165
+ left: left ,
166
+ right: right ,
167
+ expr: unquote ( expr ) ,
168
+ message: unquote ( "Assertion with #{ operator } failed" )
177
169
end
178
170
end
179
171
180
- ## Negative versions
181
-
182
- defp translate_assertion ( { :! , _ , [ { := , _ , [ left , right ] } ] } ) do
183
- quote do
184
- right = unquote ( right )
185
- case right do
186
- unquote ( left ) ->
187
- raise ExUnit.AssertionError ,
188
- expected: inspect ( right ) ,
189
- actual: unquote ( Macro . to_string ( left ) ) ,
190
- assertion: "match pattern (=)" ,
191
- negation: true
192
- _ ->
193
- nil
194
- end
195
- end
196
- end
197
-
198
- defp translate_assertion ( expr = { negation , _ , [ { :in , _ , [ left , right ] } ] } )
199
- when negation in [ :! , :not ] do
200
- code = Macro . to_string ( expr )
172
+ defp translate_assertion ( { :! , [ ] , [ { operator , _ , [ left , right ] } = expr ] } ) when operator in @ operator do
173
+ expr = Macro . to_string ( expr )
201
174
quote do
202
175
left = unquote ( left )
203
176
right = unquote ( right )
204
- assert_internal ! Enum . member? ( right , left ) ,
205
- left: left , right: right , expr: unquote ( code )
177
+ assert not ( unquote ( operator ) ( left , right ) ) ,
178
+ left: left ,
179
+ right: right ,
180
+ expr: unquote ( expr ) ,
181
+ message: unquote ( "Refute with #{ operator } failed" )
206
182
end
207
183
end
208
184
209
- defp translate_assertion ( expr = { :! , [ ] , [ { operator , _ , [ left , right ] } ] } )
210
- when operator in [ :== , :< , :> , :<= , :>= , :=== , :=~ , :!== , :!= ] do
211
- refute_operator operator , left , right , expr
212
- end
213
-
214
- ## Fallback
215
-
216
185
defp translate_assertion ( _expected ) do
217
186
nil
218
187
end
219
188
220
- defp assert_operator ( operator , left , right , expr ) do
221
- expr = Macro . to_string ( expr )
222
- quote location: :keep do
223
- left = unquote ( left )
224
- right = unquote ( right )
225
- assert_internal unquote ( operator ) ( left , right ) , left: left , right: right , expr: unquote ( expr ) , operator: to_string ( unquote ( operator ) )
226
- end
227
- end
228
-
229
- defp refute_operator ( operator , left , right , expr ) do
230
- expr = Macro . to_string ( expr )
231
- quote location: :keep do
232
- left = unquote ( left )
233
- right = unquote ( right )
234
- assert_internal not ( unquote ( operator ) ( left , right ) ) , left: left , right: right , expr: unquote ( expr ) , operator: to_string ( unquote ( operator ) )
235
- end
236
- end
237
-
238
- def assert_internal ( successful , opts ) do
239
- unless successful , do: raise ( ExUnit.AssertionError , opts )
240
- true
241
- end
242
189
## END HELPERS
243
190
244
191
@ doc """
@@ -250,7 +197,12 @@ defmodule ExUnit.Assertions do
250
197
251
198
"""
252
199
def assert ( value , message ) when is_binary ( message ) do
253
- assert_internal ( value , message: message , expr: ExUnit.NoValueSupplied . no_value )
200
+ assert ( value , message: message , expr: ExUnit.AssertionError . no_value )
201
+ end
202
+
203
+ def assert ( value , opts ) when is_list ( opts ) do
204
+ unless value , do: raise ( ExUnit.AssertionError , opts )
205
+ true
254
206
end
255
207
256
208
@ doc """
@@ -267,16 +219,8 @@ defmodule ExUnit.Assertions do
267
219
268
220
"""
269
221
def assert ( value , left , right , message ) when is_binary ( message ) do
270
- assert_internal ( value , left: left , right: right ,
271
- message: message , expr: ExUnit.NoValueSupplied . no_value )
272
- end
273
-
274
- def assert ( value , expected , actual , opts ) do
275
- unless value do
276
- raise ExUnit.AssertionError ,
277
- Keyword . merge ( [ expected: inspect ( expected ) , actual: inspect ( actual ) ] , opts )
278
- end
279
- true
222
+ assert ( value , left: left , right: right ,
223
+ message: message , expr: ExUnit.AssertionError . no_value )
280
224
end
281
225
282
226
@ doc """
@@ -363,7 +307,7 @@ defmodule ExUnit.Assertions do
363
307
end
364
308
365
309
msg = "Wrong message for #{ inspect exception } . Expected #{ inspect message } , got #{ inspect error . message } "
366
- assert_internal is_match , message: msg , expr: ExUnit.NoValueSupplied . no_value
310
+ assert is_match , message: msg , expr: ExUnit.AssertionError . no_value
367
311
368
312
error
369
313
end
@@ -388,7 +332,7 @@ defmodule ExUnit.Assertions do
388
332
error ->
389
333
name = error . __record__ ( :name )
390
334
391
- if name in [ ExUnit.AssertionError , ExUnit.AssertionError ] do
335
+ if name in [ ExUnit.AssertionError ] do
392
336
raise ( error )
393
337
else
394
338
flunk "Expected exception '#{ inspect exception } ' but got #{ inspect name } (#{ error . message } )"
@@ -459,7 +403,7 @@ defmodule ExUnit.Assertions do
459
403
unquote ( expr )
460
404
flunk "Expected to catch #{ unquote ( kind ) } , got nothing"
461
405
rescue
462
- e in [ ExUnit.AssertionError , ExUnit.AssertionError ] -> raise ( e )
406
+ e in [ ExUnit.AssertionError ] -> raise ( e )
463
407
catch
464
408
unquote ( kind ) , what_we_got -> what_we_got
465
409
end
@@ -576,6 +520,6 @@ defmodule ExUnit.Assertions do
576
520
@ spec flunk :: no_return
577
521
@ spec flunk ( String . t ) :: no_return
578
522
def flunk ( message \\ "Flunked!" ) do
579
- assert_internal false , message: message , expr: ExUnit.NoValueSupplied . no_value
523
+ assert false , message: message , expr: ExUnit.AssertionError . no_value
580
524
end
581
525
end
0 commit comments