@@ -50,21 +50,25 @@ defmodule ExUnit.Assertions do
50
50
51
51
"""
52
52
defmacro assert ( expected ) do
53
- translate_assertion ( expected , fn ->
54
- quote do
55
- value = unquote ( expected )
56
-
57
- unless value do
58
- raise ExUnit.ExpectationError ,
59
- expr: unquote ( Macro . to_string ( expected ) ) ,
60
- assertion: "be" ,
61
- expected: "true" ,
62
- actual: inspect ( value )
53
+ case translate_assertion ( expected ) do
54
+ nil ->
55
+ # Default message in case no transform was performed
56
+ quote do
57
+ value = unquote ( expected )
58
+
59
+ unless value do
60
+ raise ExUnit.ExpectationError ,
61
+ expr: unquote ( Macro . to_string ( expected ) ) ,
62
+ assertion: "be" ,
63
+ expected: "true" ,
64
+ actual: inspect ( value )
65
+ end
66
+
67
+ value
63
68
end
64
69
65
- value
66
- end
67
- end )
70
+ value -> value
71
+ end
68
72
end
69
73
70
74
@ doc """
@@ -79,28 +83,32 @@ defmodule ExUnit.Assertions do
79
83
80
84
"""
81
85
defmacro refute ( expected ) do
82
- contents = translate_assertion ( { :! , [ ] , [ expected ] } , fn ->
83
- quote do
84
- value = unquote ( expected )
85
-
86
- if value do
87
- raise ExUnit.ExpectationError ,
88
- expr: unquote ( Macro . to_string ( expected ) ) ,
89
- assertion: "be" ,
90
- expected: "false" ,
91
- actual: inspect ( value )
86
+ contents = case translate_assertion ( { :! , [ ] , [ expected ] } ) do
87
+ nil ->
88
+ # Default message in case no transform was performed
89
+ quote do
90
+ value = unquote ( expected )
91
+
92
+ if value do
93
+ raise ExUnit.ExpectationError ,
94
+ expr: unquote ( Macro . to_string ( expected ) ) ,
95
+ assertion: "be" ,
96
+ expected: "false" ,
97
+ actual: inspect ( value )
98
+ end
99
+
100
+ true
92
101
end
93
102
94
- true
95
- end
96
- end )
103
+ value -> value
104
+ end
97
105
98
106
{ :! , [ ] , [ contents ] }
99
107
end
100
108
101
109
## START HELPERS
102
110
103
- defp translate_assertion ( { := , _ , [ left , right ] } , _else ) do
111
+ defp translate_assertion ( { := , _ , [ left , right ] } ) do
104
112
quote do
105
113
right = unquote ( right )
106
114
case right do
@@ -115,43 +123,43 @@ defmodule ExUnit.Assertions do
115
123
end
116
124
end
117
125
118
- defp translate_assertion ( { :== , _ , [ left , right ] } , _else ) do
126
+ defp translate_assertion ( { :== , _ , [ left , right ] } ) do
119
127
assert_operator :== , left , right , "be equal to (==)"
120
128
end
121
129
122
- defp translate_assertion ( { :< , _ , [ left , right ] } , _else ) do
130
+ defp translate_assertion ( { :< , _ , [ left , right ] } ) do
123
131
assert_operator :< , left , right , "be less than"
124
132
end
125
133
126
- defp translate_assertion ( { :> , _ , [ left , right ] } , _else ) do
134
+ defp translate_assertion ( { :> , _ , [ left , right ] } ) do
127
135
assert_operator :> , left , right , "be more than"
128
136
end
129
137
130
- defp translate_assertion ( { :<= , _ , [ left , right ] } , _else ) do
138
+ defp translate_assertion ( { :<= , _ , [ left , right ] } ) do
131
139
assert_operator :<= , left , right , "be less than or equal to"
132
140
end
133
141
134
- defp translate_assertion ( { :>= , _ , [ left , right ] } , _else ) do
142
+ defp translate_assertion ( { :>= , _ , [ left , right ] } ) do
135
143
assert_operator :>= , left , right , "be more than or equal to"
136
144
end
137
145
138
- defp translate_assertion ( { :=== , _ , [ left , right ] } , _else ) do
146
+ defp translate_assertion ( { :=== , _ , [ left , right ] } ) do
139
147
assert_operator :=== , left , right , "be equal to (===)"
140
148
end
141
149
142
- defp translate_assertion ( { :!== , _ , [ left , right ] } , _else ) do
150
+ defp translate_assertion ( { :!== , _ , [ left , right ] } ) do
143
151
assert_operator :!== , left , right , "be not equal to (!==)"
144
152
end
145
153
146
- defp translate_assertion ( { :!= , _ , [ left , right ] } , _else ) do
154
+ defp translate_assertion ( { :!= , _ , [ left , right ] } ) do
147
155
assert_operator :!= , left , right , "be not equal to (!=)"
148
156
end
149
157
150
- defp translate_assertion ( { :=~ , _ , [ left , right ] } , _else ) do
158
+ defp translate_assertion ( { :=~ , _ , [ left , right ] } ) do
151
159
assert_operator :=~ , left , right , "match (=~)"
152
160
end
153
161
154
- defp translate_assertion ( { :in , _ , [ left , right ] } , _else ) do
162
+ defp translate_assertion ( { :in , _ , [ left , right ] } ) do
155
163
quote do
156
164
left = unquote ( left )
157
165
right = unquote ( right )
@@ -161,7 +169,7 @@ defmodule ExUnit.Assertions do
161
169
162
170
## Negative versions
163
171
164
- defp translate_assertion ( { :! , _ , [ { := , _ , [ left , right ] } ] } , _else ) do
172
+ defp translate_assertion ( { :! , _ , [ { := , _ , [ left , right ] } ] } ) do
165
173
quote do
166
174
right = unquote ( right )
167
175
case right do
@@ -177,15 +185,15 @@ defmodule ExUnit.Assertions do
177
185
end
178
186
end
179
187
180
- defp translate_assertion ( { :! , _ , [ { :=~ , _ , [ left , right ] } ] } , _else ) do
188
+ defp translate_assertion ( { :! , _ , [ { :=~ , _ , [ left , right ] } ] } ) do
181
189
quote do
182
190
left = unquote ( left )
183
191
right = unquote ( right )
184
192
assert ! ( left =~ right ) , left , right , assertion: "match (=~)" , negation: true
185
193
end
186
194
end
187
195
188
- defp translate_assertion ( { negation , _ , [ { :in , _ , [ left , right ] } ] } , _else ) when negation in [ :! , :not ] do
196
+ defp translate_assertion ( { negation , _ , [ { :in , _ , [ left , right ] } ] } ) when negation in [ :! , :not ] do
189
197
quote do
190
198
left = unquote ( left )
191
199
right = unquote ( right )
@@ -195,8 +203,8 @@ defmodule ExUnit.Assertions do
195
203
196
204
## Fallback
197
205
198
- defp translate_assertion ( _expected , fallback ) do
199
- fallback . ( )
206
+ defp translate_assertion ( _expected ) do
207
+ nil
200
208
end
201
209
202
210
defp assert_operator ( operator , expected , actual , text ) do
0 commit comments