Skip to content

Commit 815e9f1

Browse files
hovinenbcopybara-github
authored andcommitted
Introduce a macro expect_pred.
This expands to verify_pred!(...).and_log_failure() and is analogous to GoogleTest's EXPECT_PRED* family of macros. It serves the same function as the previously introduced expect_that macro and completes this API surface. PiperOrigin-RevId: 503417665
1 parent 7bbf606 commit 815e9f1

File tree

6 files changed

+150
-2
lines changed

6 files changed

+150
-2
lines changed

googletest/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ name = "expect_that_failure"
6161
path = "integration_tests/expect_that_failure.rs"
6262
test = false
6363

64+
[[bin]]
65+
name = "expect_pred_failure"
66+
path = "integration_tests/expect_pred_failure.rs"
67+
test = false
68+
6469
[[bin]]
6570
name = "failure_due_to_fail_macro"
6671
path = "integration_tests/failure_due_to_fail_macro.rs"
@@ -101,6 +106,11 @@ name = "simple_assertion_failure_with_assert_that"
101106
path = "integration_tests/simple_assertion_failure_with_assert_that.rs"
102107
test = false
103108

109+
[[bin]]
110+
name = "two_expect_pred_failures"
111+
path = "integration_tests/two_expect_pred_failures.rs"
112+
test = false
113+
104114
[[bin]]
105115
name = "two_expect_that_failures"
106116
path = "integration_tests/two_expect_that_failures.rs"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2022 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::{expect_pred, google_test, Result};
20+
21+
#[google_test]
22+
fn verify_predicate_with_failure() -> Result<()> {
23+
let a = 1;
24+
let b = 2;
25+
expect_pred!(eq_predicate(a, b));
26+
Ok(())
27+
}
28+
29+
fn eq_predicate(a: i32, b: i32) -> bool {
30+
a == b
31+
}
32+
}

googletest/integration_tests/integration_tests.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ mod tests {
1919
#[cfg(not(google3))]
2020
use googletest::{all, matchers};
2121
use googletest::{
22-
assert_that, expect_that, google_test, verify_pred, verify_that, GoogleTestSupport,
23-
MapErrorToTestFailure, Result,
22+
assert_that, expect_pred, expect_that, google_test, verify_pred, verify_that,
23+
GoogleTestSupport, MapErrorToTestFailure, Result,
2424
};
2525
#[cfg(google3)]
2626
use matchers::all;
@@ -249,6 +249,12 @@ Actual: 2, which isn't equal to 3
249249
a == b
250250
}
251251

252+
#[google_test]
253+
fn should_verify_predicate_with_success_using_expect_pred() -> Result<()> {
254+
expect_pred!(eq_predicate(1, 1));
255+
Ok(())
256+
}
257+
252258
#[google_test]
253259
fn verify_pred_should_fail_test_on_failure() -> Result<()> {
254260
let status =
@@ -281,6 +287,13 @@ eq_predicate(a, b) was false with
281287
verify_that!(status.success(), eq(false))
282288
}
283289

290+
#[google_test]
291+
fn expect_pred_should_fail_test_on_failure() -> Result<()> {
292+
let status = run_external_process("expect_pred_failure").status().err_to_test_failure()?;
293+
294+
verify_that!(status.success(), eq(false))
295+
}
296+
284297
#[google_test]
285298
fn assert_pred_should_output_correct_failure_message() -> Result<()> {
286299
let output = run_external_process_in_tests_directory("assert_predicate_with_failure")?;
@@ -297,6 +310,38 @@ eq_predicate(a, b) was false with
297310
)
298311
}
299312

313+
#[google_test]
314+
fn expect_pred_should_output_correct_failure_message() -> Result<()> {
315+
let output = run_external_process_in_tests_directory("expect_pred_failure")?;
316+
317+
verify_that!(
318+
output,
319+
contains_substring(
320+
"\
321+
eq_predicate(a, b) was false with
322+
a = 1,
323+
b = 2
324+
"
325+
)
326+
)
327+
}
328+
329+
#[google_test]
330+
fn expect_pred_should_output_failure_message_for_second_failure() -> Result<()> {
331+
let output = run_external_process_in_tests_directory("two_expect_pred_failures")?;
332+
333+
verify_that!(
334+
output,
335+
contains_substring(
336+
"\
337+
eq_predicate(a, b) was false with
338+
a = 3,
339+
b = 4
340+
"
341+
)
342+
)
343+
}
344+
300345
#[google_test]
301346
fn should_verify_predicate_in_a_submodule() -> Result<()> {
302347
verify_pred!(submodule::eq_predicate_in_submodule(1, 1))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2022 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::{expect_pred, google_test, Result};
20+
21+
#[google_test]
22+
fn verify_predicate_with_failure() -> Result<()> {
23+
let a = 1;
24+
let b = 2;
25+
expect_pred!(eq_predicate(a, b));
26+
let a = 3;
27+
let b = 4;
28+
expect_pred!(eq_predicate(a, b));
29+
Ok(())
30+
}
31+
32+
fn eq_predicate(a: i32, b: i32) -> bool {
33+
a == b
34+
}
35+
}

googletest/src/assertions.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,30 @@ macro_rules! expect_that {
263263
}};
264264
}
265265

266+
/// Asserts that the given predicate applied to the given arguments returns
267+
/// true, failing the test but continuing execution if not.
268+
///
269+
/// This is a *non-fatal* predicate assertion: the test
270+
/// continues execution in the event of assertion failure.
271+
///
272+
/// This can only be invoked inside tests with the
273+
/// [`google_test`][crate::google_test] attribute. The assertion must occur in
274+
/// the same thread as that running the test itself.
275+
///
276+
/// Invoking this macro is equivalent to using
277+
/// [`and_log_failure`](crate::GoogleTestSupport::and_log_failure) as follows:
278+
///
279+
/// ```rust
280+
/// verify_pred!(predicate(...)).and_log_failure()
281+
/// ```
282+
#[macro_export]
283+
macro_rules! expect_pred {
284+
($($content:tt)*) => {{
285+
use $crate::GoogleTestSupport;
286+
$crate::verify_pred!($($content)*).and_log_failure();
287+
}};
288+
}
289+
266290
/// Functions for use only by the procedural macros in this module.
267291
///
268292
/// **For internal use only. API stablility is not guaranteed!**

run_integration_tests.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ INTEGRATION_TEST_BINARIES=(
2727
"assert_predicate_with_failure"
2828
"assertion_failure_in_subroutine"
2929
"custom_error_message"
30+
"expect_pred_failure"
3031
"expect_that_failure"
3132
"failure_due_to_fail_macro"
3233
"failure_due_to_fail_macro_with_empty_message"
@@ -36,6 +37,7 @@ INTEGRATION_TEST_BINARIES=(
3637
"non_fatal_failure_in_subroutine"
3738
"simple_assertion_failure"
3839
"simple_assertion_failure_with_assert_that"
40+
"two_expect_pred_failures"
3941
"two_expect_that_failures"
4042
"two_non_fatal_failures"
4143
"verify_predicate_with_failure"

0 commit comments

Comments
 (0)