• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, MatcherBase, MatcherResult},
18 };
19 use std::fmt::Debug;
20 
21 /// Matches an empty container.
22 ///
23 /// `T` can be any container that implements `IntoIterator`. For instance, `T`
24 /// can be the reference of a common container like `&Vec` and
25 /// [`&HashSet`][std::collections::HashSet].
26 ///
27 /// ```
28 /// # use googletest::prelude::*;
29 /// # use std::collections::HashSet;
30 /// # fn should_pass() -> Result<()> {
31 /// let value: Vec<i32> = vec![];
32 /// verify_that!(value, empty())?;
33 /// let value: HashSet<i32> = HashSet::new();
34 /// verify_that!(value, empty())?;
35 /// let value: &[u32] = &[];
36 /// verify_that!(value, empty())?;
37 /// #     Ok(())
38 /// # }
39 /// # should_pass().unwrap();
40 /// ```
empty() -> EmptyMatcher41 pub fn empty() -> EmptyMatcher {
42     EmptyMatcher
43 }
44 
45 #[derive(MatcherBase)]
46 pub struct EmptyMatcher;
47 
48 impl<T: Debug + Copy> Matcher<T> for EmptyMatcher
49 where
50     T: IntoIterator,
51 {
matches(&self, actual: T) -> MatcherResult52     fn matches(&self, actual: T) -> MatcherResult {
53         actual.into_iter().next().is_none().into()
54     }
55 
describe(&self, matcher_result: MatcherResult) -> Description56     fn describe(&self, matcher_result: MatcherResult) -> Description {
57         if matcher_result.into() { "is empty" } else { "isn't empty" }.into()
58     }
59 }
60 
61 #[cfg(test)]
62 mod tests {
63     use crate::prelude::*;
64     use std::collections::HashSet;
65 
66     #[test]
empty_matcher_match_empty_vec() -> Result<()>67     fn empty_matcher_match_empty_vec() -> Result<()> {
68         let value: Vec<i32> = vec![];
69         verify_that!(value, empty())
70     }
71 
72     #[test]
empty_matcher_does_not_match_empty_vec() -> Result<()>73     fn empty_matcher_does_not_match_empty_vec() -> Result<()> {
74         let value = vec![1, 2, 3];
75         verify_that!(value, not(empty()))
76     }
77 
78     #[test]
empty_matcher_matches_empty_slice() -> Result<()>79     fn empty_matcher_matches_empty_slice() -> Result<()> {
80         let value: &[i32] = &[];
81         verify_that!(value, empty())
82     }
83 
84     #[test]
empty_matcher_matches_empty_hash_set() -> Result<()>85     fn empty_matcher_matches_empty_hash_set() -> Result<()> {
86         let value: HashSet<i32> = HashSet::new();
87         verify_that!(value, empty())
88     }
89 }
90