• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 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 googletest::prelude::*;
16 use indoc::indoc;
17 
18 #[test]
elements_are_matches_vector() -> Result<()>19 fn elements_are_matches_vector() -> Result<()> {
20     let value = vec![1, 2, 3];
21     verify_that!(value, elements_are![eq(&1), eq(&2), eq(&3)])
22 }
23 
24 #[test]
elements_are_matches_slice() -> Result<()>25 fn elements_are_matches_slice() -> Result<()> {
26     let value = vec![1, 2, 3];
27     let slice = value.as_slice();
28     verify_that!(slice, elements_are![eq(&1), eq(&2), eq(&3)])
29 }
30 
31 #[test]
elements_are_matches_array() -> Result<()>32 fn elements_are_matches_array() -> Result<()> {
33     verify_that!([1, 2, 3], elements_are![eq(1), eq(2), eq(3)])
34 }
35 
36 #[test]
elements_are_supports_trailing_comma() -> Result<()>37 fn elements_are_supports_trailing_comma() -> Result<()> {
38     let value = vec![1, 2, 3];
39     verify_that!(value, elements_are![eq(&1), eq(&2), eq(&3),])
40 }
41 
42 #[test]
elements_are_returns_no_match_when_expected_and_actual_sizes_differ() -> Result<()>43 fn elements_are_returns_no_match_when_expected_and_actual_sizes_differ() -> Result<()> {
44     let value = vec![1, 2];
45     verify_that!(value, not(elements_are![eq(&1), eq(&2), eq(&3)]))
46 }
47 
48 #[test]
elements_are_admits_matchers_without_static_lifetime() -> Result<()>49 fn elements_are_admits_matchers_without_static_lifetime() -> Result<()> {
50     #[derive(Debug, PartialEq)]
51     struct AStruct(i32);
52     let expected_value = AStruct(123);
53     verify_that!(vec![AStruct(123)], elements_are![eq(&expected_value)])
54 }
55 
56 #[test]
elements_are_matches_iterator_returning_by_value() -> Result<()>57 fn elements_are_matches_iterator_returning_by_value() -> Result<()> {
58     #[derive(Debug, Copy, Clone)]
59     struct Countdown(i32);
60     impl Iterator for Countdown {
61         type Item = i32;
62 
63         fn next(&mut self) -> Option<Self::Item> {
64             match self.0 {
65                 0 => None,
66                 x => {
67                     self.0 -= 1;
68                     Some(x)
69                 }
70             }
71         }
72     }
73     verify_that!(Countdown(3), elements_are![eq(3), eq(2), eq(1)])
74 }
75 
76 #[test]
elements_are_produces_correct_failure_message() -> Result<()>77 fn elements_are_produces_correct_failure_message() -> Result<()> {
78     let result = verify_that!(vec![1, 4, 3], elements_are![eq(&1), eq(&2), eq(&3)]);
79     verify_that!(
80         result,
81         err(displays_as(contains_substring(indoc!(
82             "
83                 Value of: vec![1, 4, 3]
84                 Expected: has elements:
85                   0. is equal to 1
86                   1. is equal to 2
87                   2. is equal to 3
88                 Actual: [1, 4, 3],
89                   where element #1 is 4, which isn't equal to 2"
90         ))))
91     )
92 }
93 
94 #[test]
elements_are_produces_correct_failure_message_nested() -> Result<()>95 fn elements_are_produces_correct_failure_message_nested() -> Result<()> {
96     let result = verify_that!(
97         vec![vec![0, 1], vec![1, 2]],
98         elements_are![elements_are![eq(&1), eq(&2)], elements_are![eq(&2), eq(&3)]]
99     );
100     verify_that!(
101         result,
102         err(displays_as(contains_substring(indoc!(
103             "
104                 Expected: has elements:
105                   0. has elements:
106                        0. is equal to 1
107                        1. is equal to 2
108                   1. has elements:
109                        0. is equal to 2
110                        1. is equal to 3
111                 Actual: [[0, 1], [1, 2]],
112                   where:
113                     * element #0 is [0, 1], where:
114                         * element #0 is 0, which isn't equal to 1
115                         * element #1 is 1, which isn't equal to 2
116                     * element #1 is [1, 2], where:
117                         * element #0 is 1, which isn't equal to 2
118                         * element #1 is 2, which isn't equal to 3"
119         ))))
120     )
121 }
122 
123 #[test]
elements_are_explain_match_wrong_size() -> Result<()>124 fn elements_are_explain_match_wrong_size() -> Result<()> {
125     let matcher = elements_are![eq(&1)];
126     verify_that!(matcher.explain_match(&vec![1, 2]), displays_as(eq("whose size is 2")))
127 }
128 
create_matcher<'a>() -> impl Matcher<&'a Vec<i32>>129 fn create_matcher<'a>() -> impl Matcher<&'a Vec<i32>> {
130     elements_are![eq(&1)]
131 }
132 
133 #[test]
elements_are_works_when_matcher_is_created_in_subroutine() -> Result<()>134 fn elements_are_works_when_matcher_is_created_in_subroutine() -> Result<()> {
135     verify_that!(vec![1], create_matcher())
136 }
137 
138 #[test]
elements_are_implicitly_called() -> Result<()>139 fn elements_are_implicitly_called() -> Result<()> {
140     verify_that!(vec![1, 2, 3], [eq(&1), eq(&2), eq(&3)])
141 }
142 
143 #[test]
elements_are_with_auto_eq() -> Result<()>144 fn elements_are_with_auto_eq() -> Result<()> {
145     verify_that!(vec![1, 2, 3], [&1, &2, lt(&43)])
146 }
147