Skip to content

Commit 330ff30

Browse files
Merge pull request #334 from google:add-message-parameter-to-macros
PiperOrigin-RevId: 589108054
2 parents 617b25d + 82df24c commit 330ff30

File tree

3 files changed

+159
-1
lines changed

3 files changed

+159
-1
lines changed

googletest/src/assertions.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,42 @@ macro_rules! fail {
309309
/// Matches the given value against the given matcher, panicking if it does not
310310
/// match.
311311
///
312+
/// ```should_panic
313+
/// # use googletest::prelude::*;
314+
/// # fn should_fail() {
315+
/// let value = 2;
316+
/// assert_that!(value, eq(3)); // Fails and panics.
317+
/// # }
318+
/// # should_fail();
319+
/// ```
320+
///
312321
/// This is analogous to assertions in most Rust test libraries, where a failed
313322
/// assertion causes a panic.
314323
///
324+
/// One may optionally add arguments which will be formatted and appended to a
325+
/// failure message. For example:
326+
///
327+
/// ```should_panic
328+
/// # use googletest::prelude::*;
329+
/// # fn should_fail() {
330+
/// let value = 2;
331+
/// let extra_information = "Some additional information";
332+
/// assert_that!(value, eq(3), "Test failed. Extra information: {extra_information}.");
333+
/// # }
334+
/// # should_fail();
335+
/// ```
336+
///
337+
/// This is output as follows:
338+
///
339+
/// ```text
340+
/// Value of: value
341+
/// Expected: is equal to 3
342+
/// Actual: 2,
343+
/// which isn't equal to 3
344+
/// at ...
345+
/// Test failed. Extra information: Some additional information.
346+
/// ```
347+
///
315348
/// **Note for users of [GoogleTest for C++](http://google.github.io/googletest/):**
316349
/// This differs from the `ASSERT_THAT` macro in that it panics rather
317350
/// than triggering an early return from the invoking function. To get behaviour
@@ -328,6 +361,19 @@ macro_rules! assert_that {
328361
}
329362
}
330363
};
364+
365+
($actual:expr, $expected:expr, $($format_args:expr),* $(,)?) => {
366+
match $crate::verify_that!($actual, $expected)
367+
.with_failure_message(|| format!($($format_args),*))
368+
{
369+
Ok(_) => {}
370+
Err(e) => {
371+
// The extra newline before the assertion failure message makes the failure a
372+
// bit easier to read when there's some generic boilerplate from the panic.
373+
panic!("\n{}", e);
374+
}
375+
}
376+
};
331377
}
332378

333379
/// Asserts that the given predicate applied to the given arguments returns
@@ -368,12 +414,44 @@ macro_rules! assert_pred {
368414
/// ```ignore
369415
/// verify_that!(actual, expected).and_log_failure()
370416
/// ```
417+
///
418+
/// One may optionally add arguments which will be formatted and appended to a
419+
/// failure message. For example:
420+
///
421+
/// ```
422+
/// # use googletest::prelude::*;
423+
/// # fn should_fail() -> std::result::Result<(), googletest::internal::test_outcome::TestFailure> {
424+
/// # googletest::internal::test_outcome::TestOutcome::init_current_test_outcome();
425+
/// let value = 2;
426+
/// let extra_information = "Some additional information";
427+
/// expect_that!(value, eq(3), "Test failed. Extra information: {extra_information}.");
428+
/// # googletest::internal::test_outcome::TestOutcome::close_current_test_outcome::<&str>(Ok(()))
429+
/// # }
430+
/// # should_fail().unwrap_err();
431+
/// ```
432+
///
433+
/// This is output as follows:
434+
///
435+
/// ```text
436+
/// Value of: value
437+
/// Expected: is equal to 3
438+
/// Actual: 2,
439+
/// which isn't equal to 3
440+
/// at ...
441+
/// Test failed. Extra information: Some additional information.
442+
/// ```
371443
#[macro_export]
372444
macro_rules! expect_that {
373445
($actual:expr, $expected:expr) => {{
374446
use $crate::GoogleTestSupport;
375447
$crate::verify_that!($actual, $expected).and_log_failure();
376448
}};
449+
450+
($actual:expr, $expected:expr, $($format_args:expr),* $(,)?) => {
451+
$crate::verify_that!($actual, $expected)
452+
.with_failure_message(|| format!($($format_args),*))
453+
.and_log_failure()
454+
};
377455
}
378456

379457
/// Asserts that the given predicate applied to the given arguments returns

integration_tests/src/custom_error_message.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,60 @@ mod tests {
3636
verify_that!(value, eq(3))
3737
.with_failure_message(|| "A custom error message from a closure".to_string())
3838
}
39+
40+
#[test]
41+
fn should_include_failure_message_in_third_parameter_to_assert_that() {
42+
let value = 2;
43+
assert_that!(value, eq(3), "assert_that: A custom error message for value {value}");
44+
}
45+
46+
#[test]
47+
fn should_include_failure_message_in_third_parameter_with_format_arguments_to_assert_that() {
48+
let value = 2;
49+
assert_that!(
50+
value,
51+
eq(3),
52+
"assert_that: A custom error message for incremented value {}",
53+
value + 1
54+
);
55+
}
56+
57+
#[test]
58+
fn should_accept_trailing_comma_after_format_arguments_in_assert_that() {
59+
let value = 2;
60+
assert_that!(
61+
value,
62+
eq(3),
63+
"assert_that: A custom error message for twice incremented value {}",
64+
value + 2,
65+
);
66+
}
67+
68+
#[googletest::test]
69+
fn should_include_failure_message_in_third_parameter_to_expect_that() {
70+
let value = 2;
71+
expect_that!(value, eq(3), "expect_that: A custom error message for value {value}");
72+
}
73+
74+
#[googletest::test]
75+
fn should_include_failure_message_in_third_parameter_with_format_arguments_to_expect_that() {
76+
let value = 2;
77+
expect_that!(
78+
value,
79+
eq(3),
80+
"expect_that: A custom error message for incremented value {}",
81+
value + 1
82+
);
83+
}
84+
85+
#[googletest::test]
86+
fn should_accept_trailing_comma_after_format_arguments_in_expect_that() {
87+
let value = 2;
88+
expect_that!(
89+
value,
90+
eq(3),
91+
"expect_that: A custom error message for twice incremented value {}",
92+
value + 2,
93+
);
94+
}
3995
}

integration_tests/src/integration_tests.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,31 @@ mod tests {
291291

292292
verify_that!(output, contains_substring("A custom error message"))?;
293293
verify_that!(output, contains_substring("A custom error message in a String"))?;
294-
verify_that!(output, contains_substring("A custom error message from a closure"))
294+
verify_that!(output, contains_substring("A custom error message from a closure"))?;
295+
verify_that!(
296+
output,
297+
contains_substring("assert_that: A custom error message for value 2")
298+
)?;
299+
verify_that!(
300+
output,
301+
contains_substring("assert_that: A custom error message for incremented value 3")
302+
)?;
303+
verify_that!(
304+
output,
305+
contains_substring("assert_that: A custom error message for twice incremented value 4")
306+
)?;
307+
verify_that!(
308+
output,
309+
contains_substring("expect_that: A custom error message for value 2")
310+
)?;
311+
verify_that!(
312+
output,
313+
contains_substring("expect_that: A custom error message for incremented value 3")
314+
)?;
315+
verify_that!(
316+
output,
317+
contains_substring("expect_that: A custom error message for twice incremented value 4")
318+
)
295319
}
296320

297321
#[test]

0 commit comments

Comments
 (0)