Skip to content

Commit 6f4f5ab

Browse files
satylogincopybara-github
authored andcommitted
expect greater equal family of assertions.
* `verify_ge`: verify if actual is greater than or equal to expected and return Result. * `assert_ge`: exists in rust: https://docs.rs/all_asserts/latest/all_asserts/macro.assert_ge.html * `expect_ge`: logs failure and continue execution if actual is less than expected. PiperOrigin-RevId: 650295574
1 parent cf8c1e9 commit 6f4f5ab

File tree

8 files changed

+241
-3
lines changed

8 files changed

+241
-3
lines changed

googletest/src/assertions.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,86 @@ macro_rules! expect_gt {
10781078
};
10791079
}
10801080

1081+
/// Checks whether the first argument is greater than or equal to the second
1082+
/// argument.
1083+
///
1084+
/// Evaluates to `Result::Ok(())` if the first argument is greater than or equal
1085+
/// to the second and `Result::Err(TestAssertionFailure)` if it is not. The
1086+
/// caller must then decide how to handle the `Err` variant. It has a few
1087+
/// options:
1088+
/// * Abort the current function with the `?` operator. This requires that the
1089+
/// function return a suitable `Result`.
1090+
/// * Log the test failure and continue by calling the method
1091+
/// `and_log_failure`.
1092+
///
1093+
/// Of course, one can also use all other standard methods on `Result`.
1094+
///
1095+
/// **Invoking this macro by itself does not cause a test failure to be recorded
1096+
/// or output.** The resulting `Result` must be handled as described above to
1097+
/// cause the test to be recorded as a failure.
1098+
///
1099+
/// Example:
1100+
/// ```ignore
1101+
/// use googletest::prelude::*;
1102+
///
1103+
/// #[test]
1104+
/// fn should_fail() -> Result<()> {
1105+
/// verify_ge!(1, 2)
1106+
/// }
1107+
/// ```
1108+
#[macro_export]
1109+
macro_rules! verify_ge {
1110+
($actual:expr, $expected:expr $(,)?) => {
1111+
verify_that!($actual, $crate::matchers::ge($expected))
1112+
};
1113+
}
1114+
1115+
/// Marks test as failed and continues execution if the first argument is
1116+
/// not greater than or equal to the second argument.
1117+
///
1118+
/// This is a **not-fatal** failure. The test continues execution even after the
1119+
/// macro execution.
1120+
///
1121+
/// This can only be invoked inside tests with the
1122+
/// [`googletest::test`][crate::test] attribute. The failure must be generated
1123+
/// in the same thread as that running the test itself.
1124+
///
1125+
/// Example:
1126+
/// ```ignore
1127+
/// use googletest::prelude::*;
1128+
///
1129+
/// #[googletest::test]
1130+
/// fn should_fail() {
1131+
/// expect_ge!(1, 2);
1132+
/// println!("This will print!");
1133+
/// }
1134+
/// ```
1135+
///
1136+
/// One may include formatted arguments in the failure message:
1137+
///```ignore
1138+
/// use googletest::prelude::*;
1139+
///
1140+
/// #[googletest::test]
1141+
/// fn should_fail() {
1142+
/// let argument = "argument"
1143+
/// expect_ge!(1, 2, "custom failure message: {argument}");
1144+
/// println!("This will print!");
1145+
/// }
1146+
/// ```
1147+
#[macro_export]
1148+
macro_rules! expect_ge {
1149+
($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {
1150+
use $crate::GoogleTestSupport;
1151+
verify_ge!($actual, $expected)
1152+
.with_failure_message(|| format!($($format_args),*))
1153+
.and_log_failure();
1154+
};
1155+
($actual:expr, $expected:expr $(,)?) => {
1156+
use $crate::GoogleTestSupport;
1157+
verify_ge!($actual, $expected).and_log_failure();
1158+
};
1159+
}
1160+
10811161
/// Matches the given value against the given matcher, panicking if it does not
10821162
/// match.
10831163
///

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_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,
54+
expect_false, expect_ge, expect_gt, expect_le, expect_lt, expect_ne, expect_pred,
55+
expect_that, expect_true, fail, succeed, verify_eq, verify_false, verify_ge, verify_gt,
56+
verify_le, verify_lt, 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
@@ -315,6 +315,21 @@ name = "expect_gt_supports_custom_message"
315315
path = "src/expect_gt_supports_custom_message.rs"
316316
test = false
317317

318+
[[bin]]
319+
name = "verify_ge_when_less_returns_error"
320+
path = "src/verify_ge_when_less_returns_error.rs"
321+
test = false
322+
323+
[[bin]]
324+
name = "expect_ge_when_less_marks_failed"
325+
path = "src/expect_ge_when_less_marks_failed.rs"
326+
test = false
327+
328+
[[bin]]
329+
name = "expect_ge_supports_custom_message"
330+
path = "src/expect_ge_supports_custom_message.rs"
331+
test = false
332+
318333
[[bin]]
319334
name = "failure_due_to_returned_error_with_line_numbers"
320335
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_ge!(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_ge!(1, 2);
21+
println!("This will print");
22+
}
23+
}

integration_tests/src/integration_tests.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,74 @@ mod tests {
15541554
verify_that!(output, contains_regex("This will print"))
15551555
}
15561556

1557+
#[test]
1558+
fn verify_ge_should_pass() -> Result<()> {
1559+
verify_ge!(1, 1)
1560+
}
1561+
1562+
#[test]
1563+
fn verify_ge_supports_trailing_comma() -> Result<()> {
1564+
verify_ge!(2, 1,)
1565+
}
1566+
1567+
#[test]
1568+
fn verify_ge_when_less_returns_error() -> Result<()> {
1569+
let output = run_external_process_in_tests_directory("verify_ge_when_less_returns_error")?;
1570+
1571+
verify_that!(
1572+
output,
1573+
contains_regex(indoc! {"
1574+
Expected: is greater than or equal to 2
1575+
Actual: 1,
1576+
which is less than 2
1577+
at .*verify_ge_when_less_returns_error.rs:[0-9]+:[0-9]
1578+
"})
1579+
)
1580+
}
1581+
1582+
#[googletest::test]
1583+
fn expect_ge_should_pass() {
1584+
expect_ge!(1, 1);
1585+
}
1586+
1587+
#[googletest::test]
1588+
fn expect_ge_supports_trailing_comma() {
1589+
expect_ge!(2, 1,);
1590+
}
1591+
1592+
#[googletest::test]
1593+
fn expect_ge_when_less_marks_failed() -> Result<()> {
1594+
let output = run_external_process_in_tests_directory("expect_ge_when_less_marks_failed")?;
1595+
1596+
expect_that!(
1597+
output,
1598+
contains_regex(indoc! {"
1599+
Expected: is greater than or equal to 2
1600+
Actual: 1,
1601+
which is less than 2
1602+
at .*expect_ge_when_less_marks_failed.rs:[0-9]+:[0-9]
1603+
"})
1604+
);
1605+
verify_that!(output, contains_regex("This will print"))
1606+
}
1607+
1608+
#[googletest::test]
1609+
fn expect_ge_supports_custom_message() -> Result<()> {
1610+
let output = run_external_process_in_tests_directory("expect_ge_supports_custom_message")?;
1611+
1612+
expect_that!(
1613+
output,
1614+
contains_regex(indoc! {"
1615+
Expected: is greater than or equal to 2
1616+
Actual: 1,
1617+
which is less than 2
1618+
Failure message with argument: argument
1619+
at .*expect_ge_supports_custom_message.rs:[0-9]+:[0-9]
1620+
"})
1621+
);
1622+
verify_that!(output, contains_regex("This will print"))
1623+
}
1624+
15571625
#[test]
15581626
fn test_using_normal_test_attribute_macro_formats_failure_message_correctly() -> Result<()> {
15591627
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_ge!(1, 2)
24+
}
25+
}

run_integration_tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ INTEGRATION_TEST_BINARIES=(
8080
"verify_gt_when_not_greater_returns_error"
8181
"expect_gt_when_not_greater_marks_failed"
8282
"expect_gt_supports_custom_message"
83+
"verify_ge_when_less_returns_error"
84+
"expect_ge_when_less_marks_failed"
85+
"expect_ge_supports_custom_message"
8386
"fatal_and_non_fatal_failure"
8487
"first_failure_aborts"
8588
"google_test_with_rstest"

0 commit comments

Comments
 (0)