Skip to content

Commit 177ab35

Browse files
satylogincopybara-github
authored andcommitted
PUBLIC: expect equal family of assertions.
Rust flavours for:[] * `verify_eq`: verify if `actual` is `expected` and returns `Result`. * `assert_eq`: **panics** if `actual` is not equal to `expected`. * `expect_eq`: **logs failure** and **continues execution** if `actual` is not equal to `expected`. PiperOrigin-RevId: 643330676
1 parent 5da0e04 commit 177ab35

20 files changed

+1069
-3
lines changed

googletest/src/assertions.rs

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,184 @@ macro_rules! expect_false {
587587
}};
588588
}
589589

590+
/// Checks whether the second argument is equal to the first argument.
591+
///
592+
/// Evaluates to `Result::Ok(())` if they are equal and
593+
/// `Result::Err(TestAssertionFailure)` if they are not. The caller must then
594+
/// decide how to handle the `Err` variant. It has a few options:
595+
/// * Abort the current function with the `?` operator. This requires that the
596+
/// function return a suitable `Result`.
597+
/// * Log the test failure and continue by calling the method
598+
/// `and_log_failure`.
599+
///
600+
/// Of course, one can also use all other standard methods on `Result`.
601+
///
602+
/// **Invoking this macro by itself does not cause a test failure to be recorded
603+
/// or output.** The resulting `Result` must be handled as described above to
604+
/// cause the test to be recorded as a failure.
605+
///
606+
/// Example:
607+
/// ```ignore
608+
/// use googletest::prelude::*;
609+
///
610+
/// #[test]
611+
/// fn should_fail() -> Result<()> {
612+
/// verify_eq!(2, 1)
613+
/// }
614+
/// ```
615+
///
616+
/// This macro has special support for matching against container. Namely:
617+
/// * `verify_eq!(actual, [e1, e2, ...])` is equivalent to
618+
/// `verify_that!(actual, elements_are![eq(e1), eq(e2), ...])`
619+
/// * `verify_eq!(actual, {e1, e2, ...})` is equivalent to
620+
/// `verify_that!(actual, unordered_elements_are![eq(e1), eq(e2), ...])`
621+
#[macro_export]
622+
macro_rules! verify_eq {
623+
($actual:expr, [$($expected:expr),+ $(,)?] $(,)?) => {
624+
verify_that!($actual, [$($crate::matchers::eq(&$expected)),*])
625+
};
626+
($actual:expr, {$($expected:expr),+ $(,)?} $(,)?) => {
627+
verify_that!($actual, {$($crate::matchers::eq(&$expected)),*})
628+
};
629+
($actual:expr, $expected:expr $(,)?) => {
630+
verify_that!($actual, $crate::matchers::eq($expected))
631+
};
632+
}
633+
634+
/// Panics if the second argument is not equal to first argument.
635+
///
636+
/// This is the same as calling `and_log_failure` on [`verify_eq`] macro Result.
637+
///
638+
/// This can only be invoked inside tests with the
639+
/// [`googletest::test`][crate::test] attribute. The failure must be generated
640+
/// in the same thread as that running the thread itself.
641+
///
642+
/// Example:
643+
/// ```ignore
644+
/// use googletest;
645+
///
646+
/// #[googletest::test]
647+
/// fn should_fail() {
648+
/// googletest::assert_eq!(2, 1);
649+
/// }
650+
/// ```
651+
///
652+
/// This macro has special support for matching against container. Namely:
653+
/// * `assert_eq!(actual, [e1, e2, ...])` for checking actual contains "e1, e2,
654+
/// ..." in order.
655+
/// * `assert_eq!(actual, {e1, e2, ...})` for checking actual contains "e1, e2,
656+
/// ..." in any order.
657+
///
658+
/// One may include formatted arguments in the failure message:
659+
///```ignore
660+
/// use googletest;
661+
///
662+
/// #[googletest::test]
663+
/// fn should_fail() {
664+
/// let argument = "argument"
665+
/// googletest::assert_eq!(2, 1, "custom failure message: {argument}");
666+
/// }
667+
/// ```
668+
#[macro_export]
669+
macro_rules! assert_eq {
670+
($actual:expr, [$($expected:expr),+ $(,)?] $(,)?) => {
671+
verify_eq!($actual, [$($expected),*]).and_log_failure();
672+
};
673+
($actual:expr, [$($expected:expr),+ $(,)?], $($format_args:expr),* $(,)?) => {
674+
verify_eq!($actual, [$($expected),*])
675+
.with_failure_message(|| format!($($format_args),*))
676+
.and_log_failure();
677+
};
678+
($actual:expr, {$($expected:expr),+ $(,)?} $(,)?) => {
679+
verify_eq!($actual, {$($expected),*}).and_log_failure();
680+
};
681+
($actual:expr, {$($expected:expr),+ $(,)?}, $($format_args:expr),* $(,)?) => {
682+
verify_eq!($actual, {$($expected),*})
683+
.with_failure_message(|| format!($($format_args),*))
684+
.and_log_failure();
685+
};
686+
($actual:expr, $expected:expr $(,)?) => {
687+
verify_eq!($actual, $expected).and_log_failure();
688+
};
689+
($actual:expr, $expected:expr, $($format_args:expr),* $(,)?) => {
690+
verify_eq!($actual, $expected)
691+
.with_failure_message(|| format!($($format_args),*))
692+
.and_log_failure();
693+
};
694+
}
695+
696+
/// Marks test as failed and continues execution if the second argument is not
697+
/// equal to first argument.
698+
///
699+
/// This is a **not-fatal** failure. The test continues execution even after the
700+
/// macro execution.
701+
///
702+
/// This can only be invoked inside tests with the
703+
/// [`googletest::test`][crate::test] attribute. The failure must be generated
704+
/// in the same thread as that running the thread itself.
705+
///
706+
/// Example:
707+
/// ```ignore
708+
/// use googletest::prelude::*;
709+
///
710+
/// #[googletest::test]
711+
/// fn should_fail() {
712+
/// expect_eq!(2, 1);
713+
/// println!("This will print!");
714+
/// }
715+
/// ```
716+
///
717+
/// This macro has special support for matching against container. Namely:
718+
/// * `expect_eq!(actual, [e1, e2, ...])` for checking actual contains "e1, e2,
719+
/// ..." in order.
720+
/// * `expect_eq!(actual, {e1, e2, ...})` for checking actual contains "e1, e2,
721+
/// ..." in any order.
722+
///
723+
/// One may include formatted arguments in the failure message:
724+
///```ignore
725+
/// use googletest::prelude::*;
726+
///
727+
/// #[googletest::test]
728+
/// fn should_fail() {
729+
/// let argument = "argument"
730+
/// expect_eq!(2, 1, "custom failure message: {argument}");
731+
/// println!("This will print!");
732+
/// }
733+
/// ```
734+
#[macro_export]
735+
macro_rules! expect_eq {
736+
($actual:expr, [$($expected:expr),+ $(,)?] $(,)?) => {
737+
use $crate::GoogleTestSupport;
738+
verify_eq!($actual, [$($expected),*]).and_log_failure();
739+
};
740+
($actual:expr, [$($expected:expr),+ $(,)?], $($format_args:expr),* $(,)?) => {
741+
use $crate::GoogleTestSupport;
742+
verify_eq!($actual, [$($expected),*])
743+
.with_failure_message(|| format!($($format_args),*))
744+
.and_log_failure();
745+
};
746+
($actual:expr, {$($expected:expr),+ $(,)?} $(,)?) => {
747+
use $crate::GoogleTestSupport;
748+
verify_eq!($actual, {$($expected),*}).and_log_failure();
749+
};
750+
($actual:expr, {$($expected:expr),+ $(,)?}, $($format_args:expr),* $(,)?) => {
751+
use $crate::GoogleTestSupport;
752+
verify_eq!($actual, {$($expected),*})
753+
.with_failure_message(|| format!($($format_args),*))
754+
.and_log_failure();
755+
};
756+
($actual:expr, $expected:expr $(,)?) => {
757+
use $crate::GoogleTestSupport;
758+
verify_eq!($actual, $expected).and_log_failure();
759+
};
760+
($actual:expr, $expected:expr, $($format_args:expr),* $(,)?) => {
761+
use $crate::GoogleTestSupport;
762+
verify_eq!($actual, $expected)
763+
.with_failure_message(|| format!($($format_args),*))
764+
.and_log_failure();
765+
};
766+
}
767+
590768
/// Matches the given value against the given matcher, panicking if it does not
591769
/// match.
592770
///

googletest/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ pub mod prelude {
5050
pub use super::Result;
5151
// Assert macros
5252
pub use super::{
53-
add_failure, add_failure_at, assert_false, assert_that, assert_true, expect_false,
54-
expect_pred, expect_that, expect_true, fail, succeed, verify_false, verify_pred,
55-
verify_that, verify_true,
53+
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,
5656
};
5757
}
5858

integration_tests/Cargo.toml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,81 @@ name = "expect_false_macro_on_true_condition_fails_test_and_continues"
175175
path = "src/expect_false_macro_on_true_condition_fails_test_and_continues.rs"
176176
test = false
177177

178+
[[bin]]
179+
name = "verify_eq_when_not_equal_returns_error"
180+
path = "src/verify_eq_when_not_equal_returns_error.rs"
181+
test = false
182+
183+
[[bin]]
184+
name = "verify_eq_with_ordered_elements_when_not_equal_returns_error"
185+
path = "src/verify_eq_with_ordered_elements_when_not_equal_returns_error.rs"
186+
test = false
187+
188+
[[bin]]
189+
name = "verify_eq_with_unordered_elements_when_not_equal_returns_error"
190+
path = "src/verify_eq_with_unordered_elements_when_not_equal_returns_error.rs"
191+
test = false
192+
193+
[[bin]]
194+
name = "assert_eq_when_not_equal_returns_error"
195+
path = "src/assert_eq_when_not_equal_returns_error.rs"
196+
test = false
197+
198+
[[bin]]
199+
name = "assert_eq_supports_custom_message"
200+
path = "src/assert_eq_supports_custom_message.rs"
201+
test = false
202+
203+
[[bin]]
204+
name = "assert_eq_with_ordered_elements_when_not_equal_returns_error"
205+
path = "src/assert_eq_with_ordered_elements_when_not_equal_returns_error.rs"
206+
test = false
207+
208+
[[bin]]
209+
name = "assert_eq_with_ordered_elements_supports_custom_message"
210+
path = "src/assert_eq_with_ordered_elements_supports_custom_message.rs"
211+
test = false
212+
213+
[[bin]]
214+
name = "assert_eq_with_unordered_elements_when_not_equal_returns_error"
215+
path = "src/assert_eq_with_unordered_elements_when_not_equal_returns_error.rs"
216+
test = false
217+
218+
[[bin]]
219+
name = "assert_eq_with_unordered_elements_supports_custom_message"
220+
path = "src/assert_eq_with_unordered_elements_supports_custom_message.rs"
221+
test = false
222+
223+
[[bin]]
224+
name = "expect_eq_when_not_equal_returns_error"
225+
path = "src/expect_eq_when_not_equal_returns_error.rs"
226+
test = false
227+
228+
[[bin]]
229+
name = "expect_eq_supports_custom_message"
230+
path = "src/expect_eq_supports_custom_message.rs"
231+
test = false
232+
233+
[[bin]]
234+
name = "expect_eq_with_ordered_elements_when_not_equal_returns_error"
235+
path = "src/expect_eq_with_ordered_elements_when_not_equal_returns_error.rs"
236+
test = false
237+
238+
[[bin]]
239+
name = "expect_eq_with_ordered_elements_supports_custom_message"
240+
path = "src/expect_eq_with_ordered_elements_supports_custom_message.rs"
241+
test = false
242+
243+
[[bin]]
244+
name = "expect_eq_with_unordered_elements_when_not_equal_returns_error"
245+
path = "src/expect_eq_with_unordered_elements_when_not_equal_returns_error.rs"
246+
test = false
247+
248+
[[bin]]
249+
name = "expect_eq_with_unordered_elements_supports_custom_message"
250+
path = "src/expect_eq_with_unordered_elements_supports_custom_message.rs"
251+
test = false
252+
178253
[[bin]]
179254
name = "failure_due_to_returned_error"
180255
path = "src/failure_due_to_returned_error.rs"
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+
let argument = "argument";
24+
googletest::assert_eq!(2, 1, "Failure message with argument: {argument}");
25+
}
26+
}
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+
#[googletest::test]
22+
fn should_fail() {
23+
googletest::assert_eq!(2, 1);
24+
}
25+
}
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+
let argument = "argument";
24+
googletest::assert_eq!(vec![1, 2], [1, 3], "Failure message with argument: {argument}");
25+
}
26+
}
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+
#[googletest::test]
22+
fn should_fail() {
23+
googletest::assert_eq!(vec![1, 2], [1, 3]);
24+
}
25+
}

0 commit comments

Comments
 (0)