Skip to content

Commit bbae567

Browse files
Googlercopybara-github
Googler
authored andcommitted
Add ability to pass failure message and formatting args to assert_pred, expect_pred macros and add integration tests to verify the failure message.
Also, the matcher syntax of `assert_pred` and `expect_pred` macros is changed to align with `verify_pred`, which is called internally. The previous matcher syntax was overly broad and incorrectly captured additional formatting arguments PiperOrigin-RevId: 736811118
1 parent 2986294 commit bbae567

6 files changed

+123
-6
lines changed

googletest/src/assertions.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,8 +1412,21 @@ pub use assert_that;
14121412
/// operator.
14131413
#[macro_export]
14141414
macro_rules! assert_pred {
1415-
($($content:tt)*) => {
1416-
match $crate::verify_pred!($($content)*) {
1415+
($content:expr $(,)?) => {
1416+
match $crate::verify_pred!($content) {
1417+
Ok(_) => {}
1418+
Err(e) => {
1419+
// The extra newline before the assertion failure message makes the failure a
1420+
// bit easier to read when there's some generic boilerplate from the panic.
1421+
panic!("\n{}", e);
1422+
}
1423+
}
1424+
};
1425+
1426+
// w/ format args
1427+
($content:expr $(,)?, $($format_args:expr),* $(,)?) => {
1428+
match $crate::verify_pred!($content)
1429+
.with_failure_message(|| format!($($format_args),*)) {
14171430
Ok(_) => {}
14181431
Err(e) => {
14191432
// The extra newline before the assertion failure message makes the failure a
@@ -1525,9 +1538,15 @@ pub use expect_that;
15251538
/// ```
15261539
#[macro_export]
15271540
macro_rules! expect_pred {
1528-
($($content:tt)*) => {{
1529-
$crate::GoogleTestSupport::and_log_failure($crate::verify_pred!($($content)*));
1541+
($content:expr $(,)?) => {{
1542+
$crate::GoogleTestSupport::and_log_failure($crate::verify_pred!($content));
15301543
}};
1544+
// w/ format args
1545+
($content:expr $(,)?, $($format_args:expr),* $(,)?) => {
1546+
$crate::GoogleTestSupport::and_log_failure($crate::verify_pred!($content)
1547+
.with_failure_message(|| format!($($format_args),*))
1548+
)
1549+
};
15311550
}
15321551
pub use expect_pred;
15331552

integration_tests/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,14 @@ test = false
439439
[[bin]]
440440
name = "expect_true_macro_on_false_condition_with_format_args"
441441
path = "src/expect_true_macro_on_false_condition_with_format_args.rs"
442+
test = false
443+
444+
[[bin]]
445+
name = "assert_pred_macro_on_assertion_failure_with_format_args"
446+
path = "src/assert_pred_macro_on_assertion_failure_with_format_args.rs"
447+
test = false
448+
449+
[[bin]]
450+
name = "expect_pred_macro_on_assertion_failure_with_format_args"
451+
path = "src/expect_pred_macro_on_assertion_failure_with_format_args.rs"
442452
test = false
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
fn main() {}
16+
17+
#[cfg(test)]
18+
mod tests {
19+
use googletest::prelude::*;
20+
21+
#[gtest]
22+
fn should_fail() {
23+
let a = 1;
24+
let b = 2;
25+
let extra_information = "extra information";
26+
27+
assert_pred!(eq_predicate(a, b), "assertion failed: {extra_information}")
28+
}
29+
30+
fn eq_predicate(a: i32, b: i32) -> bool {
31+
a == b
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
fn main() {}
16+
17+
#[cfg(test)]
18+
mod tests {
19+
use googletest::prelude::*;
20+
21+
#[gtest]
22+
fn should_fail() {
23+
let a = 1;
24+
let b = 2;
25+
let extra_information = "extra information";
26+
27+
expect_pred!(eq_predicate(a, b), "assertion failed: {extra_information}")
28+
}
29+
30+
fn eq_predicate(a: i32, b: i32) -> bool {
31+
a == b
32+
}
33+
}

integration_tests/src/integration_tests.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,7 +2032,7 @@ mod tests {
20322032
"expect_false_macro_on_true_condition_with_format_args",
20332033
)?;
20342034

2035-
verify_that!(output, contains_substring(indoc! {"expected false: extra information"}))
2035+
verify_that!(output, contains_substring("expected false: extra information"))
20362036
}
20372037

20382038
#[gtest]
@@ -2042,7 +2042,27 @@ mod tests {
20422042
"expect_true_macro_on_false_condition_with_format_args",
20432043
)?;
20442044

2045-
verify_that!(output, contains_substring(indoc! {"expected true: extra information"}))
2045+
verify_that!(output, contains_substring("expected true: extra information"))
2046+
}
2047+
2048+
#[gtest]
2049+
fn assert_pred_macro_on_assertion_failure_with_format_args_logs_error_when_handled(
2050+
) -> Result<()> {
2051+
let output = run_external_process_in_tests_directory(
2052+
"assert_pred_macro_on_assertion_failure_with_format_args",
2053+
)?;
2054+
2055+
verify_that!(output, contains_substring("assertion failed: extra information"))
2056+
}
2057+
2058+
#[gtest]
2059+
fn expect_pred_macro_on_assertion_failure_with_format_args_logs_error_when_handled(
2060+
) -> Result<()> {
2061+
let output = run_external_process_in_tests_directory(
2062+
"expect_pred_macro_on_assertion_failure_with_format_args",
2063+
)?;
2064+
2065+
verify_that!(output, contains_substring("assertion failed: extra information"))
20462066
}
20472067

20482068
fn run_external_process_in_tests_directory_with_args(

run_integration_tests.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ INTEGRATION_TEST_BINARIES=(
103103
"expect_panic_with_expected"
104104
"expect_false_macro_on_true_condition_with_format_args"
105105
"expect_true_macro_on_false_condition_with_format_args"
106+
"assert_pred_macro_on_assertion_failure_with_format_args"
107+
"expect_pred_macro_on_assertion_failure_with_format_args"
106108
)
107109

108110
cargo build

0 commit comments

Comments
 (0)