1 // Copyright 2022 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 use crate::{
16 description::Description,
17 matcher::{Matcher, MatcherResult},
18 };
19 use std::{fmt::Debug, marker::PhantomData};
20
21 /// Matches anything. This matcher always succeeds.
22 ///
23 /// This is useful to check if `actual` matches the specific structure (like
24 /// `Some(...)`) but without caring about the internal value.
25 ///
26 /// ```
27 /// # use googletest::prelude::*;
28 /// # fn should_pass() -> Result<()> {
29 /// let option = Some("Some value");
30 /// verify_that!(option, some(anything()))?;
31 /// # Ok(())
32 /// # }
33 /// # should_pass().unwrap();
34 /// ```
anything<T: Debug + ?Sized>() -> impl Matcher<ActualT = T>35 pub fn anything<T: Debug + ?Sized>() -> impl Matcher<ActualT = T> {
36 Anything::<T>(Default::default())
37 }
38
39 struct Anything<T: ?Sized>(PhantomData<T>);
40
41 impl<T: Debug + ?Sized> Matcher for Anything<T> {
42 type ActualT = T;
43
matches(&self, _: &T) -> MatcherResult44 fn matches(&self, _: &T) -> MatcherResult {
45 MatcherResult::Match
46 }
47
describe(&self, matcher_result: MatcherResult) -> Description48 fn describe(&self, matcher_result: MatcherResult) -> Description {
49 match matcher_result {
50 MatcherResult::Match => "is anything".into(),
51 MatcherResult::NoMatch => "never matches".into(),
52 }
53 }
54 }
55
56 #[cfg(test)]
57 mod tests {
58 use super::anything;
59 use crate::prelude::*;
60
61 #[test]
anything_matches_i32() -> Result<()>62 fn anything_matches_i32() -> Result<()> {
63 let value = 32;
64 verify_that!(value, anything())?;
65 Ok(())
66 }
67
68 #[test]
anything_matches_str() -> Result<()>69 fn anything_matches_str() -> Result<()> {
70 let value = "32";
71 verify_that!(value, anything())?;
72 Ok(())
73 }
74
75 #[test]
anything_matches_option() -> Result<()>76 fn anything_matches_option() -> Result<()> {
77 let value = Some(32);
78 verify_that!(value, some(anything()))?;
79 Ok(())
80 }
81 }
82