Skip to main content

test_that/
compat.rs

1// Copyright 2022 Google LLC
2// Copyright 2026 Bradford Hovinen <bradford@hovinen.me>
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Aliases to ease porting from [GoogleTest Rust](https://docs.rs/googletest).
17
18use crate::OrFailExt;
19
20/// Alias for [`contains_exactly!`] with [`in_order()`].
21///
22/// This exists to ease porting from [googletest].
23///
24/// [`contains_exactly!`]: crate::matchers::containers::contains_exactly
25/// [`in_order()`]: crate::matchers::containers::ContainerContainsUnorderedMatcher::in_order
26/// [googletest]: https://docs.rs/googletest
27#[cfg_attr(
28    feature = "googletest-migrate",
29    deprecated(note = "Use contains_exactly![...].in_order() instead")
30)]
31#[macro_export]
32#[doc(hidden)]
33macro_rules! __elements_are {
34    ($($content:tt)*) => {{
35        $crate::matchers::containers::contains_exactly![$($content)*].in_order()
36    }}
37}
38
39/// Alias for [`contains_exactly!`].
40///
41/// This exists to ease porting from [googletest].
42///
43/// [`contains_exactly!`]: crate::matchers::containers::contains_exactly
44/// [googletest]: https://docs.rs/googletest
45#[cfg_attr(feature = "googletest-migrate", deprecated(note = "Use contains_exactly![...] instead"))]
46#[macro_export]
47#[doc(hidden)]
48macro_rules! __unordered_elements_are {
49    ($(,)?) => {{
50        $crate::matchers::__internal::ContainerContainsUnorderedMatcher::new(
51            [],
52            $crate::matchers::__internal::Requirements::PerfectMatch
53        )
54    }};
55
56    ($(($key_matcher:expr, $value_matcher:expr)),* $(,)?) => {{
57        $crate::matchers::__internal::MapContainsMatcher::new(
58            [$(($crate::__alloc::boxed::Box::new($key_matcher), $crate::__alloc::boxed::Box::new($value_matcher))),*],
59            $crate::matchers::__internal::Requirements::PerfectMatch
60        )
61    }};
62
63    ($($matcher:expr),* $(,)?) => {{
64        $crate::matchers::__internal::ContainerContainsUnorderedMatcher::new(
65            [$($crate::__alloc::boxed::Box::new($matcher)),*],
66            $crate::matchers::__internal::Requirements::PerfectMatch
67        )
68    }};
69}
70
71/// Alias for [OrFailExt].
72#[cfg_attr(feature = "googletest-migrate", deprecated(note = "Use OrFailExt instead"))]
73pub trait IntoTestResult<T> {
74    /// Alias for [`or_fail`][crate::OrFailExt::or_fail].
75    #[cfg_attr(feature = "googletest-migrate", deprecated(note = "Use or_fail() instead"))]
76    fn into_test_result(self) -> crate::TestResult<T>;
77}
78
79#[allow(deprecated)]
80impl<S: OrFailExt<T>, T> IntoTestResult<T> for S {
81    fn into_test_result(self) -> crate::TestResult<T> {
82        self.or_fail()
83    }
84}
85
86#[cfg(test)]
87#[allow(deprecated)]
88mod tests {
89    use crate::prelude::*;
90    use std::collections::HashMap;
91
92    #[test]
93    fn elements_are_maps_to_contains_exactly_in_order() -> TestResult<()> {
94        verify_that!(vec![1, 2, 3], elements_are![eq(1), eq(2), eq(3)])?;
95        verify_that!(vec![1, 2, 3], not(elements_are![eq(3), eq(2), eq(1)]))
96    }
97
98    #[test]
99    fn unordered_elements_are_maps_to_contains_exactly() -> TestResult<()> {
100        verify_that!(vec![1, 2, 3], unordered_elements_are![eq(1), eq(2), eq(3)])?;
101        verify_that!(vec![1, 2, 3], unordered_elements_are![eq(3), eq(2), eq(1)])
102    }
103
104    #[test]
105    fn unordered_elements_are_uses_map_matcher_when_matching_pairs() -> TestResult<()> {
106        verify_that!(HashMap::from([(1, 1)]), unordered_elements_are![(eq(1), eq(1))])
107    }
108
109    #[test]
110    #[cfg(feature = "anyhow")]
111    fn into_test_result_works() -> TestResult<()> {
112        let _ = Err::<(), anyhow::Error>(anyhow::anyhow!("Expected failure")).into_test_result();
113        Ok(())
114    }
115}