@@ -168,24 +168,23 @@ macro_rules! verify_that {
168168/// # */
169169/// fn test() -> Result<()> {
170170/// 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))?;
174173/// Ok(())
175174/// }
176175/// # verify_that!(
177176/// # 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")))
179178/// # ).unwrap();
180179/// ```
181180///
182181/// This results in the following message:
183182///
184183/// ```text
185- /// equals_modulo(a, b, n ) was false with
184+ /// equals_modulo(a, b(b(2)), 2 + 3 ) was false with
186185/// a = 1,
187- /// b = 7,
188- /// n = 5
186+ /// b(v) = 7,
187+ /// 2 + 3 = 5
189188/// ```
190189///
191190/// The function passed to this macro must return `bool`. Each of the arguments
@@ -205,20 +204,30 @@ macro_rules! verify_that {
205204/// ```
206205///
207206/// **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:
214212///
215213/// ```ignore
216214/// let output = generate_random_number(); // Assigned outside of verify_pred.
217215/// verify_pred!(is_sufficiently_random(output))?;
218216/// ```
219217#[ macro_export]
220218macro_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) ,* $( , ) ?) ) => {
222231 if !$( $predicate) * ( $( $arg) ,* ) {
223232 $crate:: assertions:: internal:: report_failed_predicate(
224233 concat!( stringify!( $( $predicate) * ) , stringify!( ( $( $arg) ,* ) ) ) ,
@@ -229,12 +238,12 @@ macro_rules! verify_pred {
229238 }
230239 } ;
231240
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) * )
234243 } ;
235244
236245 ( $first: tt $( $rest: tt) * ) => {
237- $crate:: verify_pred!( [ $first] $( $rest) * )
246+ $crate:: verify_pred!( @internal [ $first] $( $rest) * )
238247 } ;
239248}
240249
0 commit comments