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
126
7+ @ doc """
8+ Indicates no meaningful value for a field.
9+ """
10+ def no_value do
11+ :ex_unit_no_meaningful_value
12+ end
1313end
1414
1515defmodule ExUnit.Assertions do
@@ -56,12 +56,12 @@ defmodule ExUnit.Assertions do
5656
5757 will fail with the message:
5858
59- Comparison (using >) failed in:
59+ Assertion with > failed
6060 code: 1+2+3+4 > 15
6161 lhs: 10
6262 rhs: 15
6363 """
64- defmacro assert ( assertion = { := , _ , [ left , right ] } ) do
64+ defmacro assert ( { := , _ , [ left , right ] } = assertion ) do
6565 code = Macro . to_string ( assertion )
6666 { :case , meta , args } =
6767 quote do
@@ -70,29 +70,27 @@ defmodule ExUnit.Assertions do
7070 right
7171 _ ->
7272 raise ExUnit.AssertionError ,
73- right: Macro . to_string ( right ) ,
73+ right: right ,
7474 expr: unquote ( code ) ,
7575 message: "match (=) failed"
7676 end
7777 end
7878
7979 quote do
8080 right = unquote ( right )
81- unquote ( { :case , [ { :export_head , true } | meta ] , args } )
81+ unquote ( { :case , [ { :export_head , true } | meta ] , args } )
8282 end
8383 end
8484
85-
8685 defmacro assert ( assertion ) do
8786 case translate_assertion ( assertion ) do
8887 nil ->
89- # Default message in case no transform was performed
9088 quote do
9189 value = unquote ( assertion )
9290
9391 unless value do
9492 raise ExUnit.AssertionError ,
95- expr: unquote ( Macro . to_string ( assertion ) ) ,
93+ expr: unquote ( Macro . to_string ( assertion ) ) ,
9694 message: "#{ inspect value } is not truthy"
9795 end
9896
@@ -113,15 +111,14 @@ defmodule ExUnit.Assertions do
113111 refute age < 0
114112
115113 """
116-
117- defmacro refute ( assertion = { := , _ , [ left , right ] } ) do
114+ defmacro refute ( { := , _ , [ left , right ] } = assertion ) do
118115 code = Macro . to_string ( assertion )
119116 { :case , meta , args } =
120117 quote do
121118 case right do
122119 unquote ( left ) ->
123120 raise ExUnit.AssertionError ,
124- right: Macro . to_string ( right ) ,
121+ right: right ,
125122 expr: unquote ( code ) ,
126123 message: "match (=) succeeded, but should have failed"
127124 _ ->
@@ -131,21 +128,19 @@ defmodule ExUnit.Assertions do
131128
132129 quote do
133130 right = unquote ( right )
134- unquote ( { :case , [ { :export_head , true } | meta ] , args } )
131+ unquote ( { :case , [ { :export_head , true } | meta ] , args } )
135132 end
136133 end
137134
138-
139135 defmacro refute ( assertion ) do
140136 case translate_assertion ( { :! , [ ] , [ assertion ] } ) do
141137 nil ->
142- # Default message in case no transform was performed
143138 quote do
144139 value = unquote ( assertion )
145140
146141 if value do
147142 raise ExUnit.AssertionError ,
148- expr: unquote ( Macro . to_string ( assertion ) ) ,
143+ expr: unquote ( Macro . to_string ( assertion ) ) ,
149144 message: "#{ inspect value } should be false or nil"
150145 end
151146
@@ -159,86 +154,38 @@ defmodule ExUnit.Assertions do
159154
160155 ## START HELPERS
161156
157+ @ operator [ :== , :< , :> , :<= , :>= , :=== , :=~ , :!== , :!= , :in ]
162158
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 )
172161 quote do
173162 left = unquote ( left )
174163 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" )
177169 end
178170 end
179171
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 )
201174 quote do
202175 left = unquote ( left )
203176 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" )
206182 end
207183 end
208184
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-
216185 defp translate_assertion ( _expected ) do
217186 nil
218187 end
219188
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
242189 ## END HELPERS
243190
244191 @ doc """
@@ -250,7 +197,12 @@ defmodule ExUnit.Assertions do
250197
251198 """
252199 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
254206 end
255207
256208 @ doc """
@@ -267,16 +219,8 @@ defmodule ExUnit.Assertions do
267219
268220 """
269221 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 )
280224 end
281225
282226 @ doc """
@@ -363,7 +307,7 @@ defmodule ExUnit.Assertions do
363307 end
364308
365309 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
367311
368312 error
369313 end
@@ -388,7 +332,7 @@ defmodule ExUnit.Assertions do
388332 error ->
389333 name = error . __record__ ( :name )
390334
391- if name in [ ExUnit.AssertionError , ExUnit.AssertionError ] do
335+ if name in [ ExUnit.AssertionError ] do
392336 raise ( error )
393337 else
394338 flunk "Expected exception '#{ inspect exception } ' but got #{ inspect name } (#{ error . message } )"
@@ -459,7 +403,7 @@ defmodule ExUnit.Assertions do
459403 unquote ( expr )
460404 flunk "Expected to catch #{ unquote ( kind ) } , got nothing"
461405 rescue
462- e in [ ExUnit.AssertionError , ExUnit.AssertionError ] -> raise ( e )
406+ e in [ ExUnit.AssertionError ] -> raise ( e )
463407 catch
464408 unquote ( kind ) , what_we_got -> what_we_got
465409 end
@@ -576,6 +520,6 @@ defmodule ExUnit.Assertions do
576520 @ spec flunk :: no_return
577521 @ spec flunk ( String . t ) :: no_return
578522 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
580524 end
581525end
0 commit comments