Skip to content

Commit db9e9e6

Browse files
satylogincopybara-github
authored andcommitted
PUBLIC: expect less or equal family of assertions.
Rust flavours for:[] * `verify_lt`: verify if `actual` is **less than or equal to** `expected` and return `Result`. * `assert_lt`: exists in rust: https://docs.rs/all_asserts/latest/all_asserts/macro.assert_le.html * `expect_lt`: **logs failure** and **continue execution** if `actual` is **greater** than `expected`. PiperOrigin-RevId: 647676022
1 parent 4739499 commit db9e9e6

File tree

8 files changed

+255
-12
lines changed

8 files changed

+255
-12
lines changed

googletest/src/assertions.rs

Lines changed: 89 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ macro_rules! succeed {
334334
///
335335
/// This can only be invoked inside tests with the
336336
/// [`googletest::test`][crate::test] attribute. The failure must be generated
337-
/// in the same thread as that running the thread itself.
337+
/// in the same thread as that running the test itself.
338338
///
339339
/// ```ignore
340340
/// use googletest::prelude::*;
@@ -377,7 +377,7 @@ macro_rules! add_failure {
377377
///
378378
/// This can only be invoked inside tests with the
379379
/// [`googletest::test`][crate::test] attribute. The failure must be generated
380-
/// in the same thread as that running the thread itself.
380+
/// in the same thread as that running the test itself.
381381
///
382382
/// ```ignore
383383
/// use googletest::prelude::*;
@@ -456,7 +456,7 @@ macro_rules! verify_true {
456456
///
457457
/// This can only be invoked inside tests with the
458458
/// [`googletest::test`][crate::test] attribute. The failure must be generated
459-
/// in the same thread as that running the thread itself.
459+
/// in the same thread as that running the test itself.
460460
///
461461
/// Example:
462462
/// ```ignore
@@ -482,7 +482,7 @@ macro_rules! assert_true {
482482
///
483483
/// This can only be invoked inside tests with the
484484
/// [`googletest::test`][crate::test] attribute. The failure must be generated
485-
/// in the same thread as that running the thread itself.
485+
/// in the same thread as that running the test itself.
486486
///
487487
/// Example:
488488
/// ```ignore
@@ -541,7 +541,7 @@ macro_rules! verify_false {
541541
///
542542
/// This can only be invoked inside tests with the
543543
/// [`googletest::test`][crate::test] attribute. The failure must be generated
544-
/// in the same thread as that running the thread itself.
544+
/// in the same thread as that running the test itself.
545545
///
546546
/// Example:
547547
/// ```ignore
@@ -567,7 +567,7 @@ macro_rules! assert_false {
567567
///
568568
/// This can only be invoked inside tests with the
569569
/// [`googletest::test`][crate::test] attribute. The failure must be generated
570-
/// in the same thread as that running the thread itself.
570+
/// in the same thread as that running the test itself.
571571
///
572572
/// Example:
573573
/// ```ignore
@@ -637,7 +637,7 @@ macro_rules! verify_eq {
637637
///
638638
/// This can only be invoked inside tests with the
639639
/// [`googletest::test`][crate::test] attribute. The failure must be generated
640-
/// in the same thread as that running the thread itself.
640+
/// in the same thread as that running the test itself.
641641
///
642642
/// Example:
643643
/// ```ignore
@@ -701,7 +701,7 @@ macro_rules! assert_eq {
701701
///
702702
/// This can only be invoked inside tests with the
703703
/// [`googletest::test`][crate::test] attribute. The failure must be generated
704-
/// in the same thread as that running the thread itself.
704+
/// in the same thread as that running the test itself.
705705
///
706706
/// Example:
707707
/// ```ignore
@@ -805,7 +805,7 @@ macro_rules! verify_ne {
805805
///
806806
/// This can only be invoked inside tests with the
807807
/// [`googletest::test`][crate::test] attribute. The failure must be generated
808-
/// in the same thread as that running the thread itself.
808+
/// in the same thread as that running the test itself.
809809
///
810810
/// Example:
811811
/// ```ignore
@@ -883,7 +883,7 @@ macro_rules! verify_lt {
883883
///
884884
/// This can only be invoked inside tests with the
885885
/// [`googletest::test`][crate::test] attribute. The failure must be generated
886-
/// in the same thread as that running the thread itself.
886+
/// in the same thread as that running the test itself.
887887
///
888888
/// Example:
889889
/// ```ignore
@@ -921,6 +921,85 @@ macro_rules! expect_lt {
921921
};
922922
}
923923

924+
/// Checks whether the first argument is less than or equal to the second
925+
/// argument.
926+
///
927+
/// Evaluates to `Result::Ok(())` if the first argument is less than or equal to
928+
/// the second and `Result::Err(TestAssertionFailure)` if it is greater. The
929+
/// caller must then decide how to handle the `Err` variant. It has a few
930+
/// options:
931+
/// * Abort the current function with the `?` operator. This requires that the
932+
/// function return a suitable `Result`.
933+
/// * Log the test failure and continue by calling the method
934+
/// `and_log_failure`.
935+
///
936+
/// Of course, one can also use all other standard methods on `Result`.
937+
///
938+
/// **Invoking this macro by itself does not cause a test failure to be recorded
939+
/// or output.** The resulting `Result` must be handled as described above to
940+
/// cause the test to be recorded as a failure.
941+
///
942+
/// Example:
943+
/// ```ignore
944+
/// use googletest::prelude::*;
945+
///
946+
/// #[test]
947+
/// fn should_fail() -> Result<()> {
948+
/// verify_le!(2, 1)
949+
/// }
950+
#[macro_export]
951+
macro_rules! verify_le {
952+
($actual:expr, $expected:expr $(,)?) => {
953+
verify_that!($actual, $crate::matchers::le($expected))
954+
};
955+
}
956+
957+
/// Marks test as failed and continues execution if the first argument is
958+
/// greater than the second argument.
959+
///
960+
/// This is a **not-fatal** failure. The test continues execution even after the
961+
/// macro execution.
962+
///
963+
/// This can only be invoked inside tests with the
964+
/// [`googletest::test`][crate::test] attribute. The failure must be generated
965+
/// in the same thread as that running the test itself.
966+
///
967+
/// Example:
968+
/// ```ignore
969+
/// use googletest::prelude::*;
970+
///
971+
/// #[googletest::test]
972+
/// fn should_fail() {
973+
/// expect_le!(2, 1);
974+
/// println!("This will print!");
975+
/// }
976+
/// ```
977+
///
978+
/// One may include formatted arguments in the failure message:
979+
///```ignore
980+
/// use googletest::prelude::*;
981+
///
982+
/// #[googletest::test]
983+
/// fn should_fail() {
984+
/// let argument = "argument"
985+
/// expect_le!(2, 1, "custom failure message: {argument}");
986+
/// println!("This will print!");
987+
/// }
988+
/// ```
989+
#[macro_export]
990+
macro_rules! expect_le {
991+
($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {
992+
use $crate::GoogleTestSupport;
993+
verify_le!($actual, $expected)
994+
.with_failure_message(|| format!($($format_args),*))
995+
.and_log_failure();
996+
};
997+
($actual:expr, $expected:expr $(,)?) => {
998+
use $crate::GoogleTestSupport;
999+
verify_le!($actual, $expected).and_log_failure();
1000+
};
1001+
}
1002+
9241003
/// Matches the given value against the given matcher, panicking if it does not
9251004
/// match.
9261005
///

googletest/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +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_lt, expect_ne, expect_pred, expect_that, expect_true, fail, succeed,
55-
verify_eq, verify_false, verify_lt, verify_ne, verify_pred, verify_that, verify_true,
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,
5657
};
5758
}
5859

integration_tests/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,21 @@ name = "failure_due_to_returned_error"
285285
path = "src/failure_due_to_returned_error.rs"
286286
test = false
287287

288+
[[bin]]
289+
name = "verify_le_when_greater_returns_error"
290+
path = "src/verify_le_when_greater_returns_error.rs"
291+
test = false
292+
293+
[[bin]]
294+
name = "expect_le_when_greater_marks_failed"
295+
path = "src/expect_le_when_greater_marks_failed.rs"
296+
test = false
297+
298+
[[bin]]
299+
name = "expect_le_supports_custom_message"
300+
path = "src/expect_le_supports_custom_message.rs"
301+
test = false
302+
288303
[[bin]]
289304
name = "failure_due_to_returned_error_with_line_numbers"
290305
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_le!(2, 1, "Failure message with argument: {arg}");
22+
println!("This will print");
23+
}
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
#[googletest::test]
22+
fn should_fail() {
23+
expect_le!(2, 1);
24+
println!("This will print");
25+
}
26+
}

integration_tests/src/integration_tests.rs

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

1417+
#[test]
1418+
fn verify_le_should_pass() -> Result<()> {
1419+
verify_le!(1, 1)
1420+
}
1421+
1422+
#[test]
1423+
fn verify_le_supports_trailing_comma() -> Result<()> {
1424+
verify_le!(1, 2,)
1425+
}
1426+
1427+
#[test]
1428+
fn verify_le_when_greater_returns_error() -> Result<()> {
1429+
let output =
1430+
run_external_process_in_tests_directory("verify_le_when_greater_returns_error")?;
1431+
1432+
verify_that!(
1433+
output,
1434+
contains_regex(indoc! {"
1435+
Expected: is less than or equal to 1
1436+
Actual: 2,
1437+
which is greater than 1
1438+
at .*verify_le_when_greater_returns_error.rs:[0-9]+:[0-9]
1439+
"})
1440+
)
1441+
}
1442+
1443+
#[googletest::test]
1444+
fn expect_le_should_pass() {
1445+
expect_le!(1, 1);
1446+
}
1447+
1448+
#[googletest::test]
1449+
fn expect_le_supports_trailing_comma() {
1450+
expect_le!(1, 2,);
1451+
}
1452+
1453+
#[googletest::test]
1454+
fn expect_le_when_greater_marks_failed() -> Result<()> {
1455+
let output =
1456+
run_external_process_in_tests_directory("expect_le_when_greater_marks_failed")?;
1457+
1458+
expect_that!(
1459+
output,
1460+
contains_regex(indoc! {"
1461+
Expected: is less than or equal to 1
1462+
Actual: 2,
1463+
which is greater than 1
1464+
at .*expect_le_when_greater_marks_failed.rs:[0-9]+:[0-9]
1465+
"})
1466+
);
1467+
verify_that!(output, contains_regex("This will print"))
1468+
}
1469+
1470+
#[googletest::test]
1471+
fn expect_le_supports_custom_message() -> Result<()> {
1472+
let output = run_external_process_in_tests_directory("expect_le_supports_custom_message")?;
1473+
1474+
expect_that!(
1475+
output,
1476+
contains_regex(indoc! {"
1477+
Expected: is less than or equal to 1
1478+
Actual: 2,
1479+
which is greater than 1
1480+
Failure message with argument: argument
1481+
at .*expect_le_supports_custom_message.rs:[0-9]+:[0-9]
1482+
"})
1483+
);
1484+
verify_that!(output, contains_regex("This will print"))
1485+
}
1486+
14171487
#[test]
14181488
fn test_using_normal_test_attribute_macro_formats_failure_message_correctly() -> Result<()> {
14191489
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_le!(2, 1)
24+
}
25+
}

run_integration_tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ INTEGRATION_TEST_BINARIES=(
7474
"verify_lt_when_not_less_returns_error"
7575
"expect_lt_when_not_less_marks_failed"
7676
"expect_lt_supports_custom_message"
77+
"verify_le_when_greater_returns_error"
78+
"expect_le_when_greater_marks_failed"
79+
"expect_le_supports_custom_message"
7780
"fatal_and_non_fatal_failure"
7881
"first_failure_aborts"
7982
"google_test_with_rstest"

0 commit comments

Comments
 (0)