@@ -183,14 +183,10 @@ macro_rules! verify_that {
183183/// ```text
184184/// equals_modulo(a, b(b(2)), 2 + 3) was false with
185185/// a = 1,
186- /// b(v ) = 7,
187- /// 2 + 3 = 5
186+ /// b(b(2) ) = 7,
187+ /// 2 + 3 = 5,
188188/// ```
189189///
190- /// The function passed to this macro must return `bool`. Each of the arguments
191- /// must evaluate to a type implementing [`std::fmt::Debug`]. The debug output
192- /// is used to construct the failure message.
193- ///
194190/// The predicate can also be a method on a struct, e.g.:
195191///
196192/// ```ignore
@@ -204,46 +200,12 @@ macro_rules! verify_that {
204200/// ```
205201///
206202/// **Warning:** This macro assumes that the arguments passed to the predicate
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:
212- ///
213- /// ```ignore
214- /// let output = generate_random_number(); // Assigned outside of verify_pred.
215- /// verify_pred!(is_sufficiently_random(output))?;
216- /// ```
203+ /// cause no mutations, or else the output message of a test failure will
204+ /// deviate from the values actually passed to the predicate.
217205#[ macro_export]
218206macro_rules! verify_pred {
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) ,* $( , ) ?) ) => {
231- if !$( $predicate) * ( $( $arg) ,* ) {
232- $crate:: assertions:: internal:: report_failed_predicate(
233- concat!( stringify!( $( $predicate) * ) , stringify!( ( $( $arg) ,* ) ) ) ,
234- vec![ $( ( format!( concat!( stringify!( $arg) , " = {:?}" ) , $arg) ) ) ,* ] ,
235- )
236- } else {
237- Ok ( ( ) )
238- }
239- } ;
240-
241- ( @internal [ $( $predicate: tt) +] $first: tt $( $rest: tt) * ) => {
242- $crate:: verify_pred!( @internal [ $( $predicate) * $first] $( $rest) * )
243- } ;
244-
245- ( $first: tt $( $rest: tt) * ) => {
246- $crate:: verify_pred!( @internal [ $first] $( $rest) * )
207+ ( $expr: expr $( , ) ?) => {
208+ $crate:: assertions:: internal:: __googletest_macro_verify_pred!( $expr)
247209 } ;
248210}
249211
@@ -1414,6 +1376,8 @@ pub mod internal {
14141376 } ;
14151377 use std:: fmt:: Debug ;
14161378
1379+ pub use :: googletest_macro:: __googletest_macro_verify_pred;
1380+
14171381 /// Extension trait to perform autoref through method lookup in the
14181382 /// assertion macros. With this trait, the subject can be either a value
14191383 /// or a reference. For example, this trait makes the following code
@@ -1453,25 +1417,6 @@ pub mod internal {
14531417
14541418 impl < T : Copy + Debug > Subject for T { }
14551419
1456- /// Constructs a `Result::Err(TestAssertionFailure)` for a predicate failure
1457- /// as produced by the macro [`crate::verify_pred`].
1458- ///
1459- /// This intended only for use by the macro [`crate::verify_pred`].
1460- ///
1461- /// **For internal use only. API stablility is not guaranteed!**
1462- #[ track_caller]
1463- #[ must_use = "The assertion result must be evaluated to affect the test result." ]
1464- pub fn report_failed_predicate (
1465- actual_expr : & ' static str ,
1466- formatted_arguments : Vec < String > ,
1467- ) -> Result < ( ) , TestAssertionFailure > {
1468- Err ( TestAssertionFailure :: create ( format ! (
1469- "{} was false with\n {}" ,
1470- actual_expr,
1471- formatted_arguments. join( ",\n " )
1472- ) ) )
1473- }
1474-
14751420 /// Creates a failure at specified location.
14761421 ///
14771422 /// **For internal use only. API stability is not guaranteed!**
@@ -1480,4 +1425,45 @@ pub mod internal {
14801425 pub fn create_fail_result ( message : String ) -> crate :: Result < ( ) > {
14811426 Err ( crate :: internal:: test_outcome:: TestAssertionFailure :: create ( message) )
14821427 }
1428+
1429+ /// Trait that defaults to not rendering values. Used for autoref
1430+ /// specialization to conditionally render only values that implement
1431+ /// `Debug`. See also [`FormatDebug`].
1432+ pub trait FormatNonDebug {
1433+ fn __googletest_format_as_line ( & self , output : & mut String , expr_label : & str ) {
1434+ use :: std:: fmt:: Write as _;
1435+ write ! ( output, "\n {} does not implement Debug," , expr_label)
1436+ . expect ( "Formatting to String should never fail" ) ;
1437+ }
1438+ }
1439+
1440+ impl < T > FormatNonDebug for & T { }
1441+
1442+ /// Trait to render values that implement `Debug` to a format string. Used
1443+ /// for autoref specialization to conditionally render only values that
1444+ /// implement `Debug`. See also [`FormatNonDebug`].
1445+ pub trait FormatDebug {
1446+ fn __googletest_format_as_line ( & self , output : & mut String , expr_label : & str ) ;
1447+ }
1448+
1449+ impl < T : Debug > FormatDebug for T {
1450+ #[ track_caller]
1451+ fn __googletest_format_as_line ( & self , output : & mut String , expr_label : & str ) {
1452+ use :: std:: fmt:: Write as _;
1453+ write ! ( output, "\n {} = {:?}," , expr_label, self )
1454+ . expect ( "Formatting to String should never fail" ) ;
1455+ }
1456+ }
1457+
1458+ #[ macro_export]
1459+ macro_rules! __googletest__format_as_line(
1460+ ( $output_str: expr, $expr_str: expr, $value: expr $( , ) ?) => {
1461+ {
1462+ use $crate:: assertions:: internal:: FormatNonDebug as _;
1463+ use $crate:: assertions:: internal:: FormatDebug as _;
1464+ ( & $value) . __googletest_format_as_line( $output_str, $expr_str)
1465+ }
1466+ }
1467+ ) ;
1468+ pub use __googletest__format_as_line;
14831469}
0 commit comments