Skip to content

Commit 2986294

Browse files
Googlercopybara-github
authored andcommitted
Add ability to pass failure message and formatting args to expect_true and expect_false macros.
Also, added integration tests to verify failure messages. PiperOrigin-RevId: 736809149
1 parent f5a975e commit 2986294

File tree

6 files changed

+96
-0
lines changed

6 files changed

+96
-0
lines changed

googletest/src/assertions.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,11 @@ macro_rules! expect_true {
489489
($condition:expr) => {{
490490
$crate::GoogleTestSupport::and_log_failure($crate::verify_true!($condition))
491491
}};
492+
($condition:expr, $($format_args:expr),* $(,)?) => {{
493+
$crate::GoogleTestSupport::and_log_failure($crate::verify_true!($condition)
494+
.with_failure_message(|| format!($($format_args),*))
495+
);
496+
}};
492497
}
493498
pub use expect_true;
494499

@@ -550,6 +555,11 @@ macro_rules! expect_false {
550555
($condition:expr) => {{
551556
$crate::GoogleTestSupport::and_log_failure(($crate::verify_false!($condition)))
552557
}};
558+
($condition:expr, $($format_args:expr),* $(,)?) => {{
559+
$crate::GoogleTestSupport::and_log_failure($crate::verify_false!($condition)
560+
.with_failure_message(|| format!($($format_args),*))
561+
);
562+
}};
553563
}
554564
pub use expect_false;
555565

integration_tests/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,13 @@ test = false
430430
name = "abbreviated_stringify_macro"
431431
path = "src/abbreviated_stringify_macro.rs"
432432
test = false
433+
434+
[[bin]]
435+
name = "expect_false_macro_on_true_condition_with_format_args"
436+
path = "src/expect_false_macro_on_true_condition_with_format_args.rs"
437+
test = false
438+
439+
[[bin]]
440+
name = "expect_true_macro_on_false_condition_with_format_args"
441+
path = "src/expect_true_macro_on_false_condition_with_format_args.rs"
442+
test = false
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 extra_information = "extra information";
24+
25+
expect_false!(1 == 1, "expected false: {extra_information}")
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 extra_information = "extra information";
24+
25+
expect_true!(1 == 2, "expected true: {extra_information}")
26+
}
27+
}

integration_tests/src/integration_tests.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,26 @@ mod tests {
20252025
)
20262026
}
20272027

2028+
#[gtest]
2029+
fn expect_false_macro_on_true_condition_with_format_args_logs_error_when_handled() -> Result<()>
2030+
{
2031+
let output = run_external_process_in_tests_directory(
2032+
"expect_false_macro_on_true_condition_with_format_args",
2033+
)?;
2034+
2035+
verify_that!(output, contains_substring(indoc! {"expected false: extra information"}))
2036+
}
2037+
2038+
#[gtest]
2039+
fn expect_true_macro_on_false_condition_with_format_args_logs_error_when_handled() -> Result<()>
2040+
{
2041+
let output = run_external_process_in_tests_directory(
2042+
"expect_true_macro_on_false_condition_with_format_args",
2043+
)?;
2044+
2045+
verify_that!(output, contains_substring(indoc! {"expected true: extra information"}))
2046+
}
2047+
20282048
fn run_external_process_in_tests_directory_with_args(
20292049
name: &'static str,
20302050
args: &[&'static str],

run_integration_tests.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ INTEGRATION_TEST_BINARIES=(
101101
"macro_hygiene"
102102
"expect_panic"
103103
"expect_panic_with_expected"
104+
"expect_false_macro_on_true_condition_with_format_args"
105+
"expect_true_macro_on_false_condition_with_format_args"
104106
)
105107

106108
cargo build

0 commit comments

Comments
 (0)