Skip to content

Commit 4cd2cae

Browse files
satylogincopybara-github
authored andcommitted
expect_near family of assertions.
PiperOrigin-RevId: 653242119
1 parent 2108291 commit 4cd2cae

8 files changed

+254
-3
lines changed

googletest/src/assertions.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,87 @@ macro_rules! expect_float_eq {
11411141
}};
11421142
}
11431143

1144+
/// Checks whether the float given by first argument is equal to second argument
1145+
/// with error tolerance of max_abs_error.
1146+
///
1147+
/// Evaluates to `Result::Ok(())` if the first argument is approximately equal
1148+
/// to the second and `Result::Err(TestAssertionFailure)` if it is not. The
1149+
/// caller must then decide how to handle the `Err` variant. It has a few
1150+
/// options:
1151+
/// * Abort the current function with the `?` operator. This requires that the
1152+
/// function return a suitable `Result`.
1153+
/// * Log the test failure and continue by calling the method
1154+
/// `and_log_failure`.
1155+
///
1156+
/// Of course, one can also use all other standard methods on `Result`.
1157+
///
1158+
/// **Invoking this macro by itself does not cause a test failure to be recorded
1159+
/// or output.** The resulting `Result` must be handled as described above to
1160+
/// cause the test to be recorded as a failure.
1161+
///
1162+
/// Example:
1163+
/// ```ignore
1164+
/// use googletest::prelude::*;
1165+
///
1166+
/// #[test]
1167+
/// fn should_fail() -> Result<()> {
1168+
/// verify_near!(1.12345, 1.12346, 1e-6)
1169+
/// }
1170+
/// ```
1171+
#[macro_export]
1172+
macro_rules! verify_near {
1173+
($actual:expr, $expected:expr, $max_abs_error:expr $(,)?) => {
1174+
verify_that!($actual, $crate::matchers::near($expected, $max_abs_error))
1175+
};
1176+
}
1177+
1178+
/// Marks the test as failed and continues execution if the float given by first
1179+
/// argument is not equal to second argument with error tolerance of
1180+
/// max_abs_error.
1181+
///
1182+
/// This is a **not-fatal** failure. The test continues execution even after the
1183+
/// macro execution.
1184+
///
1185+
/// This can only be invoked inside tests with the
1186+
/// [`googletest::test`][crate::test] attribute. The failure must be generated
1187+
/// in the same thread as that running the test itself.
1188+
///
1189+
/// Example:
1190+
/// ```ignore
1191+
/// use googletest::prelude::*;
1192+
///
1193+
/// #[googletest::test]
1194+
/// fn should_fail() {
1195+
/// expect_near!(1.12345, 1.12346, 1e-6);
1196+
/// println!("This will print!");
1197+
/// }
1198+
/// ```
1199+
///
1200+
/// One may include formatted arguments in the failure message:
1201+
///```ignore
1202+
/// use googletest::prelude::*;
1203+
///
1204+
/// #[googletest::test]
1205+
/// fn should_fail() {
1206+
/// let argument = "argument"
1207+
/// expect_near!(1.12345, 1.12346, 1e-6, "custom failure message: {argument}");
1208+
/// println!("This will print!");
1209+
/// }
1210+
/// ```
1211+
#[macro_export]
1212+
macro_rules! expect_near {
1213+
($actual:expr, $expected:expr, $max_abs_error:expr, $($format_args:expr),+ $(,)?) => {{
1214+
use $crate::GoogleTestSupport;
1215+
verify_near!($actual, $expected, $max_abs_error)
1216+
.with_failure_message(|| format!($($format_args),*))
1217+
.and_log_failure();
1218+
}};
1219+
($actual:expr, $expected:expr, $max_abs_error:expr $(,)?) => {{
1220+
use $crate::GoogleTestSupport;
1221+
verify_near!($actual, $expected, $max_abs_error).and_log_failure();
1222+
}};
1223+
}
1224+
11441225
/// Matches the given value against the given matcher, panicking if it does not
11451226
/// match.
11461227
///

googletest/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ pub mod prelude {
5151
// Assert macros
5252
pub use super::{
5353
add_failure, add_failure_at, assert_that, expect_eq, expect_false, expect_float_eq,
54-
expect_ge, expect_gt, expect_le, expect_lt, expect_ne, expect_pred, expect_that,
55-
expect_true, fail, succeed, verify_eq, verify_false, verify_float_eq, verify_ge, verify_gt,
56-
verify_le, verify_lt, verify_ne, verify_pred, verify_that, verify_true,
54+
expect_ge, expect_gt, expect_le, expect_lt, expect_ne, expect_near, expect_pred,
55+
expect_that, expect_true, fail, succeed, verify_eq, verify_false, verify_float_eq,
56+
verify_ge, verify_gt, verify_le, verify_lt, verify_ne, verify_near, verify_pred,
57+
verify_that, verify_true,
5758
};
5859
}
5960

integration_tests/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,21 @@ name = "expect_float_eq_supports_custom_message"
305305
path = "src/expect_float_eq_supports_custom_message.rs"
306306
test = false
307307

308+
[[bin]]
309+
name = "verify_near_when_not_near_returns_error"
310+
path = "src/verify_near_when_not_near_returns_error.rs"
311+
test = false
312+
313+
[[bin]]
314+
name = "expect_near_when_not_near_marks_failed"
315+
path = "src/expect_near_when_not_near_marks_failed.rs"
316+
test = false
317+
318+
[[bin]]
319+
name = "expect_near_supports_custom_message"
320+
path = "src/expect_near_supports_custom_message.rs"
321+
test = false
322+
308323
[[bin]]
309324
name = "failure_due_to_returned_error_with_line_numbers"
310325
path = "src/failure_due_to_returned_error_with_line_numbers.rs"
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+
fn main() {}
15+
#[cfg(test)]
16+
mod tests {
17+
use googletest::prelude::*;
18+
19+
#[googletest::test]
20+
fn should_fail() {
21+
let arg = "argument";
22+
expect_near!(1.12345, 1.12346, 1e-6, "Failure message with argument: {arg}");
23+
println!("This will print");
24+
}
25+
}
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+
19+
#[googletest::test]
20+
fn should_fail() {
21+
expect_near!(1.12345, 1.12346, 1e-6);
22+
println!("This will print");
23+
}
24+
}

integration_tests/src/integration_tests.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,83 @@ mod tests {
15421542
verify_that!(output, contains_regex("This will print"))
15431543
}
15441544

1545+
#[test]
1546+
fn verify_near_should_pass() -> Result<()> {
1547+
verify_near!(1.12345, 1.12346, 1e-5)
1548+
}
1549+
1550+
#[test]
1551+
fn verify_near_supports_trailing_comma() -> Result<()> {
1552+
verify_near!(1.12345, 1.12346, 1e-5,)
1553+
}
1554+
1555+
#[googletest::test]
1556+
fn verify_near_when_not_near_returns_error() -> Result<()> {
1557+
let output =
1558+
run_external_process_in_tests_directory("verify_near_when_not_near_returns_error")?;
1559+
1560+
verify_that!(
1561+
output,
1562+
contains_regex(indoc! {"
1563+
Expected: is within 1e-6 of 1.12346
1564+
Actual: 1.12345,
1565+
which isn't within 1e-6 of 1.12346
1566+
at .*verify_near_when_not_near_returns_error.rs:[0-9]+:[0-9]
1567+
"})
1568+
)
1569+
}
1570+
1571+
#[googletest::test]
1572+
fn expect_near_should_pass() {
1573+
expect_near!(1.12345, 1.12346, 1e-5);
1574+
}
1575+
1576+
#[googletest::test]
1577+
fn expect_near_should_allow_trailing_comma() {
1578+
expect_near!(1.12345, 1.12346, 1e-5,);
1579+
}
1580+
1581+
#[googletest::test]
1582+
fn expect_near_should_allow_multiple_execution() {
1583+
expect_near!(1.12345, 1.12346, 1e-5);
1584+
expect_near!(1.123456, 1.123457, 1e-6);
1585+
}
1586+
1587+
#[googletest::test]
1588+
fn expect_near_when_not_near_marks_failed() -> Result<()> {
1589+
let output =
1590+
run_external_process_in_tests_directory("expect_near_when_not_near_marks_failed")?;
1591+
1592+
expect_that!(
1593+
output,
1594+
contains_regex(indoc! {"
1595+
Expected: is within 1e-6 of 1.12346
1596+
Actual: 1.12345,
1597+
which isn't within 1e-6 of 1.12346
1598+
at .*expect_near_when_not_near_marks_failed.rs:[0-9]+:[0-9]
1599+
"})
1600+
);
1601+
verify_that!(output, contains_regex("This will print"))
1602+
}
1603+
1604+
#[googletest::test]
1605+
fn expect_near_supports_custom_message() -> Result<()> {
1606+
let output =
1607+
run_external_process_in_tests_directory("expect_near_supports_custom_message")?;
1608+
1609+
expect_that!(
1610+
output,
1611+
contains_regex(indoc! {"
1612+
Expected: is within 1e-6 of 1.12346
1613+
Actual: 1.12345,
1614+
which isn't within 1e-6 of 1.12346
1615+
Failure message with argument: argument
1616+
at .*expect_near_supports_custom_message.rs:[0-9]+:[0-9]
1617+
"})
1618+
);
1619+
verify_that!(output, contains_regex("This will print"))
1620+
}
1621+
15451622
#[test]
15461623
fn test_using_normal_test_attribute_macro_formats_failure_message_correctly() -> Result<()> {
15471624
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_near!(1.12345, 1.12346, 1e-6)
24+
}
25+
}

run_integration_tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ INTEGRATION_TEST_BINARIES=(
7878
"verify_float_eq_when_not_equal_returns_error"
7979
"expect_float_eq_when_not_equal_marks_failed"
8080
"expect_float_eq_supports_custom_message"
81+
"verify_near_when_not_near_returns_error"
82+
"expect_near_when_not_near_marks_failed"
83+
"expect_near_supports_custom_message"
8184
"fatal_and_non_fatal_failure"
8285
"first_failure_aborts"
8386
"google_test_with_rstest"

0 commit comments

Comments
 (0)