Skip to content

Commit 4739499

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

8 files changed

+248
-2
lines changed

googletest/src/assertions.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,84 @@ macro_rules! expect_ne {
843843
};
844844
}
845845

846+
/// Checks whether the first argument is less than second argument.
847+
///
848+
/// Evaluates to `Result::Ok(())` if the first argument is less than the second
849+
/// and `Result::Err(TestAssertionFailure)` if it is greater or equal. The
850+
/// caller must then decide how to handle the `Err` variant. It has a few
851+
/// options:
852+
/// * Abort the current function with the `?` operator. This requires that the
853+
/// function return a suitable `Result`.
854+
/// * Log the test failure and continue by calling the method
855+
/// `and_log_failure`.
856+
///
857+
/// Of course, one can also use all other standard methods on `Result`.
858+
///
859+
/// **Invoking this macro by itself does not cause a test failure to be recorded
860+
/// or output.** The resulting `Result` must be handled as described above to
861+
/// cause the test to be recorded as a failure.
862+
///
863+
/// Example:
864+
/// ```ignore
865+
/// use googletest::prelude::*;
866+
///
867+
/// #[test]
868+
/// fn should_fail() -> Result<()> {
869+
/// verify_lt!(2, 1)
870+
/// }
871+
#[macro_export]
872+
macro_rules! verify_lt {
873+
($actual:expr, $expected:expr $(,)?) => {
874+
verify_that!($actual, $crate::matchers::lt($expected))
875+
};
876+
}
877+
878+
/// Marks test as failed and continues execution if the first argument is
879+
/// greater or equal to second argument.
880+
///
881+
/// This is a **not-fatal** failure. The test continues execution even after the
882+
/// macro execution.
883+
///
884+
/// This can only be invoked inside tests with the
885+
/// [`googletest::test`][crate::test] attribute. The failure must be generated
886+
/// in the same thread as that running the thread itself.
887+
///
888+
/// Example:
889+
/// ```ignore
890+
/// use googletest::prelude::*;
891+
///
892+
/// #[googletest::test]
893+
/// fn should_fail() {
894+
/// expect_lt!(2, 1);
895+
/// println!("This will print!");
896+
/// }
897+
/// ```
898+
///
899+
/// One may include formatted arguments in the failure message:
900+
///```ignore
901+
/// use googletest::prelude::*;
902+
///
903+
/// #[googletest::test]
904+
/// fn should_fail() {
905+
/// let argument = "argument"
906+
/// expect_lt!(1, 1, "custom failure message: {argument}");
907+
/// println!("This will print!");
908+
/// }
909+
/// ```
910+
#[macro_export]
911+
macro_rules! expect_lt {
912+
($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {
913+
use $crate::GoogleTestSupport;
914+
verify_lt!($actual, $expected)
915+
.with_failure_message(|| format!($($format_args),*))
916+
.and_log_failure();
917+
};
918+
($actual:expr, $expected:expr $(,)?) => {
919+
use $crate::GoogleTestSupport;
920+
verify_lt!($actual, $expected).and_log_failure();
921+
};
922+
}
923+
846924
/// Matches the given value against the given matcher, panicking if it does not
847925
/// match.
848926
///

googletest/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ 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_ne, expect_pred, expect_that, expect_true, fail, succeed, verify_eq,
55-
verify_false, verify_ne, verify_pred, verify_that, verify_true,
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,
5656
};
5757
}
5858

integration_tests/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,21 @@ name = "expect_ne_supports_custom_message"
265265
path = "src/expect_ne_supports_custom_message.rs"
266266
test = false
267267

268+
[[bin]]
269+
name = "verify_lt_when_not_less_returns_error"
270+
path = "src/verify_lt_when_not_less_returns_error.rs"
271+
test = false
272+
273+
[[bin]]
274+
name = "expect_lt_when_not_less_marks_failed"
275+
path = "src/expect_lt_when_not_less_marks_failed.rs"
276+
test = false
277+
278+
[[bin]]
279+
name = "expect_lt_supports_custom_message"
280+
path = "src/expect_lt_supports_custom_message.rs"
281+
test = false
282+
268283
[[bin]]
269284
name = "failure_due_to_returned_error"
270285
path = "src/failure_due_to_returned_error.rs"
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+
#[googletest::test]
22+
fn should_fail() {
23+
let arg = "argument";
24+
expect_lt!(1, 1, "Failure message with argument: {arg}");
25+
println!("This will print");
26+
}
27+
}
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_lt!(1, 1);
24+
println!("This will print");
25+
}
26+
}

integration_tests/src/integration_tests.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,78 @@ mod tests {
13421342
verify_that!(output, contains_regex("This will print"))
13431343
}
13441344

1345+
#[test]
1346+
fn verify_lt_should_pass() -> Result<()> {
1347+
let value = 2;
1348+
verify_lt!(value, 3)
1349+
}
1350+
1351+
#[test]
1352+
fn verify_lt_supports_trailing_comma() -> Result<()> {
1353+
let value = 2;
1354+
verify_lt!(value, 3,)
1355+
}
1356+
1357+
#[test]
1358+
fn verify_lt_when_not_less_returns_error() -> Result<()> {
1359+
let output =
1360+
run_external_process_in_tests_directory("verify_lt_when_not_less_returns_error")?;
1361+
1362+
verify_that!(
1363+
output,
1364+
contains_regex(indoc! {"
1365+
Expected: is less than 1
1366+
Actual: 2,
1367+
which is greater than or equal to 1
1368+
at .*verify_lt_when_not_less_returns_error.rs:[0-9]+:[0-9]
1369+
"})
1370+
)
1371+
}
1372+
1373+
#[googletest::test]
1374+
fn expect_lt_should_pass() {
1375+
expect_lt!(1, 2);
1376+
}
1377+
1378+
#[googletest::test]
1379+
fn expect_lt_supports_trailing_comma() {
1380+
expect_lt!(1, 2,);
1381+
}
1382+
1383+
#[googletest::test]
1384+
fn expect_lt_when_not_less_marks_failed() -> Result<()> {
1385+
let output =
1386+
run_external_process_in_tests_directory("expect_lt_when_not_less_marks_failed")?;
1387+
1388+
expect_that!(
1389+
output,
1390+
contains_regex(indoc! {"
1391+
Expected: is less than 1
1392+
Actual: 1,
1393+
which is greater than or equal to 1
1394+
at .*expect_lt_when_not_less_marks_failed.rs:[0-9]+:[0-9]
1395+
"})
1396+
);
1397+
verify_that!(output, contains_regex("This will print"))
1398+
}
1399+
1400+
#[googletest::test]
1401+
fn expect_lt_supports_custom_message() -> Result<()> {
1402+
let output = run_external_process_in_tests_directory("expect_lt_supports_custom_message")?;
1403+
1404+
expect_that!(
1405+
output,
1406+
contains_regex(indoc! {"
1407+
Expected: is less than 1
1408+
Actual: 1,
1409+
which is greater than or equal to 1
1410+
Failure message with argument: argument
1411+
at .*expect_lt_supports_custom_message.rs:[0-9]+:[0-9]
1412+
"})
1413+
);
1414+
verify_that!(output, contains_regex("This will print"))
1415+
}
1416+
13451417
#[test]
13461418
fn test_using_normal_test_attribute_macro_formats_failure_message_correctly() -> Result<()> {
13471419
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_lt!(2, 1)
24+
}
25+
}

run_integration_tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ INTEGRATION_TEST_BINARIES=(
7171
"verify_ne_when_equal_returns_error"
7272
"expect_ne_when_equal_marks_failed"
7373
"expect_ne_supports_custom_message"
74+
"verify_lt_when_not_less_returns_error"
75+
"expect_lt_when_not_less_marks_failed"
76+
"expect_lt_supports_custom_message"
7477
"fatal_and_non_fatal_failure"
7578
"first_failure_aborts"
7679
"google_test_with_rstest"

0 commit comments

Comments
 (0)