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
17 #[test]
all_matcher_works_as_inner_matcher() -> Result<()>18 fn all_matcher_works_as_inner_matcher() -> Result<()> {
19 let value = vec![1];
20 verify_that!(value, contains_each![all!(gt(&0), lt(&2))])
21 }
22
23 #[test]
matches_pattern_works_as_inner_matcher() -> Result<()>24 fn matches_pattern_works_as_inner_matcher() -> Result<()> {
25 #[derive(Debug)]
26 struct AStruct(i32);
27 verify_that!(vec![AStruct(123)], contains_each![matches_pattern!(&AStruct(eq(123)))])
28 }
29
30 #[test]
matches_pattern_works_with_property_as_inner_matcher() -> Result<()>31 fn matches_pattern_works_with_property_as_inner_matcher() -> Result<()> {
32 #[derive(Debug)]
33 struct AStruct(i32);
34 impl AStruct {
35 fn get_value(&self) -> i32 {
36 self.0
37 }
38 }
39 verify_that!(
40 vec![AStruct(123)],
41 contains_each![matches_pattern!(&AStruct {
42 get_value(): eq(123)
43 })]
44 )
45 }
46
47 #[test]
contains_each_works_as_inner_matcher() -> Result<()>48 fn contains_each_works_as_inner_matcher() -> Result<()> {
49 #[derive(Debug)]
50 struct AStruct(Vec<i32>);
51 verify_that!(AStruct(vec![123]), matches_pattern!(&AStruct(ref contains_each![eq(&123)])))
52 }
53
54 #[test]
pointwise_works_as_inner_matcher() -> Result<()>55 fn pointwise_works_as_inner_matcher() -> Result<()> {
56 #[derive(Debug)]
57 struct AStruct(Vec<i32>);
58 verify_that!(AStruct(vec![123]), matches_pattern!(&AStruct(ref pointwise!(eq, [&123]))))
59 }
60
61 #[test]
elements_are_works_as_inner_matcher() -> Result<()>62 fn elements_are_works_as_inner_matcher() -> Result<()> {
63 #[derive(Debug)]
64 struct AStruct(Vec<i32>);
65 verify_that!(AStruct(vec![123]), matches_pattern!(&AStruct(ref elements_are![eq(&123)])))
66 }
67
68 #[test]
tuple_works_as_inner_matcher() -> Result<()>69 fn tuple_works_as_inner_matcher() -> Result<()> {
70 verify_that!(vec![(123,)], elements_are![(eq(&123),)])
71 }
72
73 #[test]
matches_struct_with_method_returning_option_of_non_copy_value() -> Result<()>74 fn matches_struct_with_method_returning_option_of_non_copy_value() -> Result<()> {
75 #[derive(Debug)]
76 struct AnInnerStruct;
77
78 #[derive(Debug)]
79 struct AStruct;
80
81 impl AStruct {
82 fn get_value(&self) -> Option<AnInnerStruct> {
83 Some(AnInnerStruct)
84 }
85 }
86
87 verify_that!(
88 AStruct,
89 matches_pattern!(&AStruct {
90 get_value(): ref some(matches_pattern!(&AnInnerStruct))
91 })
92 )
93 }
94
95 #[test]
matches_struct_with_method_returning_option_of_non_copy_enum() -> Result<()>96 fn matches_struct_with_method_returning_option_of_non_copy_enum() -> Result<()> {
97 #[derive(Debug)]
98 enum AnInnerStruct {
99 ThisCase,
100 #[allow(unused)]
101 ThatCase,
102 }
103 #[derive(Debug)]
104 struct AStruct;
105 impl AStruct {
106 fn get_value(&self) -> Option<AnInnerStruct> {
107 Some(AnInnerStruct::ThisCase)
108 }
109 }
110
111 verify_that!(
112 AStruct,
113 matches_pattern!(&AStruct {
114 get_value(): ref some(matches_pattern!(&AnInnerStruct::ThisCase))
115 })
116 )
117 }
118
119 #[test]
matches_struct_with_method_returning_option_ref_binding_mode() -> Result<()>120 fn matches_struct_with_method_returning_option_ref_binding_mode() -> Result<()> {
121 #[derive(Debug)]
122 struct AnInnerStruct;
123 #[derive(Debug)]
124 struct AStruct;
125 impl AStruct {
126 fn get_value(&self) -> Option<AnInnerStruct> {
127 Some(AnInnerStruct)
128 }
129 }
130
131 verify_that!(
132 AStruct,
133 matches_pattern!(AStruct {
134 get_value(): some(matches_pattern!(AnInnerStruct))
135 })
136 )
137 }
138
139 #[test]
matches_struct_with_method_returning_option_enum_ref_binding_mode() -> Result<()>140 fn matches_struct_with_method_returning_option_enum_ref_binding_mode() -> Result<()> {
141 #[derive(Debug)]
142 enum AnInnerStruct {
143 ThisCase,
144 #[allow(unused)]
145 ThatCase,
146 }
147 #[derive(Debug)]
148 struct AStruct;
149 impl AStruct {
150 fn get_value(&self) -> Option<AnInnerStruct> {
151 Some(AnInnerStruct::ThisCase)
152 }
153 }
154
155 verify_that!(
156 AStruct,
157 matches_pattern!(AStruct {
158 get_value(): some(matches_pattern!(AnInnerStruct::ThisCase))
159 })
160 )
161 }
162
163 #[test]
matches_struct_with_property_against_predicate() -> Result<()>164 fn matches_struct_with_property_against_predicate() -> Result<()> {
165 #[derive(Debug)]
166 enum AnInnerStruct {
167 ThisCase,
168 #[allow(unused)]
169 ThatCase,
170 }
171
172 #[derive(Debug)]
173 struct AStruct;
174 impl AStruct {
175 fn get_value(&self) -> AnInnerStruct {
176 AnInnerStruct::ThisCase
177 }
178 }
179
180 verify_that!(
181 AStruct,
182 matches_pattern!(AStruct {
183 get_value(): predicate(|_: &_| true)
184 })
185 )
186 }
187