Skip to content

Commit 4159298

Browse files
satylogincopybara-github
authored andcommitted
expect greater family of assertions.
* `verify_gt`: verify if `actual` is **greater than** `expected` and return `Result`. * `assert_gt`: exists in rust: https://docs.rs/all_asserts/latest/all_asserts/macro.assert_gt.html * `expect_gt`: **logs failure** and **continue execution** if `actual` is **greater than** `expected`. PiperOrigin-RevId: 649062547
1 parent db9e9e6 commit 4159298

8 files changed

+241
-3
lines changed

googletest/src/assertions.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,84 @@ macro_rules! expect_le {
10001000
};
10011001
}
10021002

1003+
/// Checks whether the first argument is greater than the second argument.
1004+
///
1005+
/// Evaluates to `Result::Ok(())` if the first argument is greater than
1006+
/// the second and `Result::Err(TestAssertionFailure)` if it is not. The
1007+
/// caller must then decide how to handle the `Err` variant. It has a few
1008+
/// options:
1009+
/// * Abort the current function with the `?` operator. This requires that the
1010+
/// function return a suitable `Result`.
1011+
/// * Log the test failure and continue by calling the method
1012+
/// `and_log_failure`.
1013+
///
1014+
/// Of course, one can also use all other standard methods on `Result`.
1015+
///
1016+
/// **Invoking this macro by itself does not cause a test failure to be recorded
1017+
/// or output.** The resulting `Result` must be handled as described above to
1018+
/// cause the test to be recorded as a failure.
1019+
///
1020+
/// Example:
1021+
/// ```ignore
1022+
/// use googletest::prelude::*;
1023+
///
1024+
/// #[test]
1025+
/// fn should_fail() -> Result<()> {
1026+
/// verify_gt!(1, 2)
1027+
/// }
1028+
#[macro_export]
1029+
macro_rules! verify_gt {
1030+
($actual:expr, $expected:expr $(,)?) => {
1031+
verify_that!($actual, $crate::matchers::gt($expected))
1032+
};
1033+
}
1034+
1035+
/// Marks test as failed and continues execution if the first argument is
1036+
/// not greater than the second argument.
1037+
///
1038+
/// This is a **not-fatal** failure. The test continues execution even after the
1039+
/// macro execution.
1040+
///
1041+
/// This can only be invoked inside tests with the
1042+
/// [`googletest::test`][crate::test] attribute. The failure must be generated
1043+
/// in the same thread as that running the test itself.
1044+
///
1045+
/// Example:
1046+
/// ```ignore
1047+
/// use googletest::prelude::*;
1048+
///
1049+
/// #[googletest::test]
1050+
/// fn should_fail() {
1051+
/// expect_gt!(1, 2);
1052+
/// println!("This will print!");
1053+
/// }
1054+
/// ```
1055+
///
1056+
/// One may include formatted arguments in the failure message:
1057+
///```ignore
1058+
/// use googletest::prelude::*;
1059+
///
1060+
/// #[googletest::test]
1061+
/// fn should_fail() {
1062+
/// let argument = "argument"
1063+
/// expect_gt!(1, 2, "custom failure message: {argument}");
1064+
/// println!("This will print!");
1065+
/// }
1066+
/// ```
1067+
#[macro_export]
1068+
macro_rules! expect_gt {
1069+
($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {
1070+
use $crate::GoogleTestSupport;
1071+
verify_gt!($actual, $expected)
1072+
.with_failure_message(|| format!($($format_args),*))
1073+
.and_log_failure();
1074+
};
1075+
($actual:expr, $expected:expr $(,)?) => {
1076+
use $crate::GoogleTestSupport;
1077+
verify_gt!($actual, $expected).and_log_failure();
1078+
};
1079+
}
1080+
10031081
/// Matches the given value against the given matcher, panicking if it does not
10041082
/// match.
10051083
///

googletest/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ pub mod prelude {
5151
// Assert macros
5252
pub use super::{
5353
add_failure, add_failure_at, assert_false, assert_that, assert_true, expect_eq,
54-
expect_false, expect_le, expect_lt, expect_ne, expect_pred, expect_that, expect_true, fail,
55-
succeed, verify_eq, verify_false, verify_le, verify_lt, verify_ne, verify_pred,
56-
verify_that, verify_true,
54+
expect_false, expect_gt, expect_le, expect_lt, expect_ne, expect_pred, expect_that,
55+
expect_true, fail, succeed, verify_eq, verify_false, verify_gt, verify_le, verify_lt,
56+
verify_ne, verify_pred, verify_that, verify_true,
5757
};
5858
}
5959

integration_tests/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,21 @@ name = "expect_le_supports_custom_message"
300300
path = "src/expect_le_supports_custom_message.rs"
301301
test = false
302302

303+
[[bin]]
304+
name = "verify_gt_when_not_greater_returns_error"
305+
path = "src/verify_gt_when_not_greater_returns_error.rs"
306+
test = false
307+
308+
[[bin]]
309+
name = "expect_gt_when_not_greater_marks_failed"
310+
path = "src/expect_gt_when_not_greater_marks_failed.rs"
311+
test = false
312+
313+
[[bin]]
314+
name = "expect_gt_supports_custom_message"
315+
path = "src/expect_gt_supports_custom_message.rs"
316+
test = false
317+
303318
[[bin]]
304319
name = "failure_due_to_returned_error_with_line_numbers"
305320
path = "src/failure_due_to_returned_error_with_line_numbers.rs"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
fn main() {}
15+
#[cfg(test)]
16+
mod tests {
17+
use googletest::prelude::*;
18+
#[googletest::test]
19+
fn should_fail() {
20+
let arg = "argument";
21+
expect_gt!(1, 2, "Failure message with argument: {arg}");
22+
println!("This will print");
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
fn main() {}
15+
#[cfg(test)]
16+
mod tests {
17+
use googletest::prelude::*;
18+
#[googletest::test]
19+
fn should_fail() {
20+
expect_gt!(1, 2);
21+
println!("This will print");
22+
}
23+
}

integration_tests/src/integration_tests.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,76 @@ mod tests {
14841484
verify_that!(output, contains_regex("This will print"))
14851485
}
14861486

1487+
#[test]
1488+
fn verify_gt_should_pass() -> Result<()> {
1489+
verify_gt!(2, 1)
1490+
}
1491+
1492+
#[test]
1493+
fn verify_gt_supports_trailing_comma() -> Result<()> {
1494+
verify_gt!(2, 1,)
1495+
}
1496+
1497+
#[test]
1498+
fn verify_gt_when_not_greater_returns_error() -> Result<()> {
1499+
let output =
1500+
run_external_process_in_tests_directory("verify_gt_when_not_greater_returns_error")?;
1501+
1502+
verify_that!(
1503+
output,
1504+
contains_regex(indoc! {"
1505+
Expected: is greater than 2
1506+
Actual: 1,
1507+
which is less than or equal to 2
1508+
at .*verify_gt_when_not_greater_returns_error.rs:[0-9]+:[0-9]
1509+
"})
1510+
)
1511+
}
1512+
1513+
#[googletest::test]
1514+
fn expect_gt_should_pass() {
1515+
expect_gt!(2, 1);
1516+
}
1517+
1518+
#[googletest::test]
1519+
fn expect_gt_supports_trailing_comma() {
1520+
expect_gt!(2, 1,);
1521+
}
1522+
1523+
#[googletest::test]
1524+
fn expect_gt_when_not_greater_marks_failed() -> Result<()> {
1525+
let output =
1526+
run_external_process_in_tests_directory("expect_gt_when_not_greater_marks_failed")?;
1527+
1528+
expect_that!(
1529+
output,
1530+
contains_regex(indoc! {"
1531+
Expected: is greater than 2
1532+
Actual: 1,
1533+
which is less than or equal to 2
1534+
at .*expect_gt_when_not_greater_marks_failed.rs:[0-9]+:[0-9]
1535+
"})
1536+
);
1537+
verify_that!(output, contains_regex("This will print"))
1538+
}
1539+
1540+
#[googletest::test]
1541+
fn expect_gt_supports_custom_message() -> Result<()> {
1542+
let output = run_external_process_in_tests_directory("expect_gt_supports_custom_message")?;
1543+
1544+
expect_that!(
1545+
output,
1546+
contains_regex(indoc! {"
1547+
Expected: is greater than 2
1548+
Actual: 1,
1549+
which is less than or equal to 2
1550+
Failure message with argument: argument
1551+
at .*expect_gt_supports_custom_message.rs:[0-9]+:[0-9]
1552+
"})
1553+
);
1554+
verify_that!(output, contains_regex("This will print"))
1555+
}
1556+
14871557
#[test]
14881558
fn test_using_normal_test_attribute_macro_formats_failure_message_correctly() -> Result<()> {
14891559
let result = should_display_error_correctly_without_google_test_macro();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
#[test]
22+
fn should_fail() -> Result<()> {
23+
verify_gt!(1, 2)
24+
}
25+
}

run_integration_tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ INTEGRATION_TEST_BINARIES=(
7777
"verify_le_when_greater_returns_error"
7878
"expect_le_when_greater_marks_failed"
7979
"expect_le_supports_custom_message"
80+
"verify_gt_when_not_greater_returns_error"
81+
"expect_gt_when_not_greater_marks_failed"
82+
"expect_gt_supports_custom_message"
8083
"fatal_and_non_fatal_failure"
8184
"first_failure_aborts"
8285
"google_test_with_rstest"

0 commit comments

Comments
 (0)