@@ -168,24 +168,23 @@ macro_rules! verify_that {
168
168
/// # */
169
169
/// fn test() -> Result<()> {
170
170
/// let a = 1;
171
- /// let b = 7;
172
- /// let n = 5;
173
- /// verify_pred!(equals_modulo(a, b, n))?;
171
+ /// fn b(_x: i32) -> i32 { 7 }
172
+ /// verify_pred!(equals_modulo(a, b(b(2)), 2 + 3))?;
174
173
/// Ok(())
175
174
/// }
176
175
/// # verify_that!(
177
176
/// # test(),
178
- /// # err(displays_as(contains_substring("equals_modulo(a, b, n ) was false with")))
177
+ /// # err(displays_as(contains_substring("equals_modulo(a, b(b(2)), 2 + 3 ) was false with")))
179
178
/// # ).unwrap();
180
179
/// ```
181
180
///
182
181
/// This results in the following message:
183
182
///
184
183
/// ```text
185
- /// equals_modulo(a, b, n ) was false with
184
+ /// equals_modulo(a, b(b(2)), 2 + 3 ) was false with
186
185
/// a = 1,
187
- /// b = 7,
188
- /// n = 5
186
+ /// b(v) = 7,
187
+ /// 2 + 3 = 5
189
188
/// ```
190
189
///
191
190
/// The function passed to this macro must return `bool`. Each of the arguments
@@ -205,20 +204,30 @@ macro_rules! verify_that {
205
204
/// ```
206
205
///
207
206
/// **Warning:** This macro assumes that the arguments passed to the predicate
208
- /// are either *variables* or *calls to pure functions*. If two subsequent
209
- /// invocations to any of the expresssions passed as arguments result in
210
- /// different values, then the output message of a test failure will deviate
211
- /// from the values actually passed to the predicate. For this reason, *always
212
- /// assign the outputs of non-pure functions to variables before using them in
213
- /// this macro. For example:
207
+ /// are pure so that two subsequent invocations to any of the expresssions
208
+ /// passed as arguments result in different values, then the output message of a
209
+ /// test failure will deviate from the values actually passed to the predicate.
210
+ /// For this reason, *always assign the outputs of non-pure functions to
211
+ /// variables before using them in this macro. For example:
214
212
///
215
213
/// ```ignore
216
214
/// let output = generate_random_number(); // Assigned outside of verify_pred.
217
215
/// verify_pred!(is_sufficiently_random(output))?;
218
216
/// ```
219
217
#[ macro_export]
220
218
macro_rules! verify_pred {
221
- ( [ $( $predicate: tt) * ] ( $( $arg: tt) ,* $( , ) ?) ) => {
219
+ ( @internal [ $( $predicate: tt) +] $( , ) ?) => {
220
+ if !$( $predicate) * {
221
+ $crate:: assertions:: internal:: report_failed_predicate(
222
+ stringify!( $( $predicate) * ) ,
223
+ vec![ ] ,
224
+ )
225
+ } else {
226
+ Ok ( ( ) )
227
+ }
228
+ } ;
229
+
230
+ ( @internal [ $( $predicate: tt) +] ( $( $arg: expr) ,* $( , ) ?) ) => {
222
231
if !$( $predicate) * ( $( $arg) ,* ) {
223
232
$crate:: assertions:: internal:: report_failed_predicate(
224
233
concat!( stringify!( $( $predicate) * ) , stringify!( ( $( $arg) ,* ) ) ) ,
@@ -229,12 +238,12 @@ macro_rules! verify_pred {
229
238
}
230
239
} ;
231
240
232
- ( [ $( $predicate: tt) * ] $first: tt $( $rest: tt) * ) => {
233
- $crate:: verify_pred!( [ $( $predicate) * $first] $( $rest) * )
241
+ ( @internal [ $( $predicate: tt) + ] $first: tt $( $rest: tt) * ) => {
242
+ $crate:: verify_pred!( @internal [ $( $predicate) * $first] $( $rest) * )
234
243
} ;
235
244
236
245
( $first: tt $( $rest: tt) * ) => {
237
- $crate:: verify_pred!( [ $first] $( $rest) * )
246
+ $crate:: verify_pred!( @internal [ $first] $( $rest) * )
238
247
} ;
239
248
}
240
249
0 commit comments