Skip to content

Commit cf01f7d

Browse files
satylogincopybara-github
authored andcommitted
PUBLIC: expect not equal family of assertions.
Rust flavours for:[] * `verify_ne`: verify if actual is not equal to expected and return `Result`. * `assert_ne`: exists in rust: https://doc.rust-lang.org/std/macro.assert_ne.html. * `expect_ne`: **logs failure** and **continue execution** if actual is equal to expected. PiperOrigin-RevId: 644683062
1 parent 585d48c commit cf01f7d

File tree

8 files changed

+246
-2
lines changed

8 files changed

+246
-2
lines changed

googletest/src/assertions.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,84 @@ macro_rules! expect_eq {
765765
};
766766
}
767767

768+
/// Checks whether the second argument is not equal to the first argument.
769+
///
770+
/// Evaluates to `Result::Ok(())` if they are not equal and
771+
/// `Result::Err(TestAssertionFailure)` if they are equal. The caller must then
772+
/// decide how to handle the `Err` variant. It has a few options:
773+
/// * Abort the current function with the `?` operator. This requires that the
774+
/// function return a suitable `Result`.
775+
/// * Log the test failure and continue by calling the method
776+
/// `and_log_failure`.
777+
///
778+
/// Of course, one can also use all other standard methods on `Result`.
779+
///
780+
/// **Invoking this macro by itself does not cause a test failure to be recorded
781+
/// or output.** The resulting `Result` must be handled as described above to
782+
/// cause the test to be recorded as a failure.
783+
///
784+
/// Example:
785+
/// ```ignore
786+
/// use googletest::prelude::*;
787+
///
788+
/// #[test]
789+
/// fn should_fail() -> Result<()> {
790+
/// verify_ne!(1, 1)
791+
/// }
792+
/// ```
793+
#[macro_export]
794+
macro_rules! verify_ne {
795+
($actual:expr, $expected:expr $(,)?) => {
796+
verify_that!($actual, $crate::matchers::not($crate::matchers::eq($expected)))
797+
};
798+
}
799+
800+
/// Marks test as failed and continues execution if the second argument is
801+
/// equal to first argument.
802+
///
803+
/// This is a **not-fatal** failure. The test continues execution even after the
804+
/// macro execution.
805+
///
806+
/// This can only be invoked inside tests with the
807+
/// [`googletest::test`][crate::test] attribute. The failure must be generated
808+
/// in the same thread as that running the thread itself.
809+
///
810+
/// Example:
811+
/// ```ignore
812+
/// use googletest::prelude::*;
813+
///
814+
/// #[googletest::test]
815+
/// fn should_fail() {
816+
/// expect_ne!(1, 1);
817+
/// println!("This will print!");
818+
/// }
819+
/// ```
820+
///
821+
/// One may include formatted arguments in the failure message:
822+
///```ignore
823+
/// use googletest::prelude::*;
824+
///
825+
/// #[googletest::test]
826+
/// fn should_fail() {
827+
/// let argument = "argument"
828+
/// expect_ne!(1, 1, "custom failure message: {argument}");
829+
/// println!("This will print!");
830+
/// }
831+
/// ```
832+
#[macro_export]
833+
macro_rules! expect_ne {
834+
($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {
835+
use $crate::GoogleTestSupport;
836+
verify_ne!($actual, $expected)
837+
.with_failure_message(|| format!($($format_args),*))
838+
.and_log_failure();
839+
};
840+
($actual:expr, $expected:expr $(,)?) => {
841+
use $crate::GoogleTestSupport;
842+
verify_ne!($actual, $expected).and_log_failure();
843+
};
844+
}
845+
768846
/// Matches the given value against the given matcher, panicking if it does not
769847
/// match.
770848
///

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_pred, expect_that, expect_true, fail, succeed, verify_eq,
55-
verify_false, verify_pred, verify_that, verify_true,
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,
5656
};
5757
}
5858

integration_tests/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,21 @@ name = "expect_eq_with_unordered_elements_supports_custom_message"
250250
path = "src/expect_eq_with_unordered_elements_supports_custom_message.rs"
251251
test = false
252252

253+
[[bin]]
254+
name = "verify_ne_when_equal_returns_error"
255+
path = "src/verify_ne_when_equal_returns_error.rs"
256+
test = false
257+
258+
[[bin]]
259+
name = "expect_ne_when_equal_marks_failed"
260+
path = "src/expect_ne_when_equal_marks_failed.rs"
261+
test = false
262+
263+
[[bin]]
264+
name = "expect_ne_supports_custom_message"
265+
path = "src/expect_ne_supports_custom_message.rs"
266+
test = false
267+
253268
[[bin]]
254269
name = "failure_due_to_returned_error"
255270
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 argument = "argument";
24+
expect_ne!(1, 1, "Failure message with argument: {argument}");
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_ne!(1, 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
@@ -1272,6 +1272,76 @@ mod tests {
12721272
verify_that!(output, contains_regex("This will print"))
12731273
}
12741274

1275+
#[test]
1276+
fn verify_ne_should_pass() -> Result<()> {
1277+
let value = 2;
1278+
verify_ne!(value, 3)
1279+
}
1280+
1281+
#[test]
1282+
fn verify_ne_supports_trailing_comma() -> Result<()> {
1283+
let value = 2;
1284+
verify_ne!(value, 3,)
1285+
}
1286+
1287+
#[test]
1288+
fn verify_ne_when_equal_returns_error() -> Result<()> {
1289+
let output = run_external_process_in_tests_directory("verify_ne_when_equal_returns_error")?;
1290+
1291+
verify_that!(
1292+
output,
1293+
contains_regex(indoc! {"
1294+
Expected: isn't equal to 1
1295+
Actual: 1,
1296+
which is equal to 1
1297+
at .*verify_ne_when_equal_returns_error.rs:[0-9]+:[0-9]
1298+
"})
1299+
)
1300+
}
1301+
1302+
#[googletest::test]
1303+
fn expect_ne_should_pass() {
1304+
expect_ne!(1, 2);
1305+
}
1306+
1307+
#[googletest::test]
1308+
fn expect_ne_supports_trailing_comma() {
1309+
expect_ne!(1, 2,);
1310+
}
1311+
1312+
#[googletest::test]
1313+
fn expect_ne_when_equal_marks_failed() -> Result<()> {
1314+
let output = run_external_process_in_tests_directory("expect_ne_when_equal_marks_failed")?;
1315+
1316+
expect_that!(
1317+
output,
1318+
contains_regex(indoc! {"
1319+
Expected: isn't equal to 1
1320+
Actual: 1,
1321+
which is equal to 1
1322+
at .*expect_ne_when_equal_marks_failed.rs:[0-9]+:[0-9]
1323+
"})
1324+
);
1325+
verify_that!(output, contains_regex("This will print"))
1326+
}
1327+
1328+
#[googletest::test]
1329+
fn expect_ne_supports_custom_message() -> Result<()> {
1330+
let output = run_external_process_in_tests_directory("expect_ne_supports_custom_message")?;
1331+
1332+
expect_that!(
1333+
output,
1334+
contains_regex(indoc! {"
1335+
Expected: isn't equal to 1
1336+
Actual: 1,
1337+
which is equal to 1
1338+
Failure message with argument: argument
1339+
at .*expect_ne_supports_custom_message.rs:[0-9]+:[0-9]
1340+
"})
1341+
);
1342+
verify_that!(output, contains_regex("This will print"))
1343+
}
1344+
12751345
#[test]
12761346
fn test_using_normal_test_attribute_macro_formats_failure_message_correctly() -> Result<()> {
12771347
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_ne!(1, 1)
24+
}
25+
}

run_integration_tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ INTEGRATION_TEST_BINARIES=(
6868
"expect_eq_with_ordered_elements_supports_custom_message"
6969
"expect_eq_with_unordered_elements_when_not_equal_returns_error"
7070
"expect_eq_with_unordered_elements_supports_custom_message"
71+
"verify_ne_when_equal_returns_error"
72+
"expect_ne_when_equal_marks_failed"
73+
"expect_ne_supports_custom_message"
7174
"fatal_and_non_fatal_failure"
7275
"first_failure_aborts"
7376
"google_test_with_rstest"

0 commit comments

Comments
 (0)