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::matcher::{Matcher, MatcherResult};
16 use googletest::prelude::*;
17 use indoc::indoc;
18 
19 #[test]
empty_matcher_matches_empty_tuple() -> Result<()>20 fn empty_matcher_matches_empty_tuple() -> Result<()> {
21     verify_that!((), ())
22 }
23 
24 #[test]
singleton_matcher_matches_matching_singleton_tuple() -> Result<()>25 fn singleton_matcher_matches_matching_singleton_tuple() -> Result<()> {
26     verify_that!((123,), (eq(123),))
27 }
28 
29 #[test]
singleton_matcher_does_not_match_non_matching_singleton_tuple() -> Result<()>30 fn singleton_matcher_does_not_match_non_matching_singleton_tuple() -> Result<()> {
31     verify_that!((123,), not((eq(456),)))
32 }
33 
34 #[test]
pair_matcher_matches_matching_pair_tuple() -> Result<()>35 fn pair_matcher_matches_matching_pair_tuple() -> Result<()> {
36     verify_that!((123, 456), (eq(123), eq(456)))
37 }
38 
39 #[test]
pair_matcher_matches_matching_pair_tuple_with_different_types() -> Result<()>40 fn pair_matcher_matches_matching_pair_tuple_with_different_types() -> Result<()> {
41     verify_that!((123, "A string"), (eq(123), eq("A string")))
42 }
43 
44 #[test]
pair_matcher_with_trailing_comma_matches_matching_pair_tuple() -> Result<()>45 fn pair_matcher_with_trailing_comma_matches_matching_pair_tuple() -> Result<()> {
46     verify_that!((123, 456), (eq(123), eq(456),))
47 }
48 
49 #[test]
tuple_matcher_matches_matching_3_tuple() -> Result<()>50 fn tuple_matcher_matches_matching_3_tuple() -> Result<()> {
51     verify_that!((1, 2, 3), (eq(1), eq(2), eq(3)))
52 }
53 
54 #[test]
tuple_matcher_matches_matching_4_tuple() -> Result<()>55 fn tuple_matcher_matches_matching_4_tuple() -> Result<()> {
56     verify_that!((1, 2, 3, 4), (eq(1), eq(2), eq(3), eq(4)))
57 }
58 
59 #[test]
tuple_matcher_matches_matching_5_tuple() -> Result<()>60 fn tuple_matcher_matches_matching_5_tuple() -> Result<()> {
61     verify_that!((1, 2, 3, 4, 5), (eq(1), eq(2), eq(3), eq(4), eq(5)))
62 }
63 
64 #[test]
tuple_matcher_matches_matching_6_tuple() -> Result<()>65 fn tuple_matcher_matches_matching_6_tuple() -> Result<()> {
66     verify_that!((1, 2, 3, 4, 5, 6), (eq(1), eq(2), eq(3), eq(4), eq(5), eq(6)))
67 }
68 
69 #[test]
tuple_matcher_matches_matching_7_tuple() -> Result<()>70 fn tuple_matcher_matches_matching_7_tuple() -> Result<()> {
71     verify_that!((1, 2, 3, 4, 5, 6, 7), (eq(1), eq(2), eq(3), eq(4), eq(5), eq(6), eq(7)))
72 }
73 
74 #[test]
tuple_matcher_matches_matching_8_tuple() -> Result<()>75 fn tuple_matcher_matches_matching_8_tuple() -> Result<()> {
76     verify_that!((1, 2, 3, 4, 5, 6, 7, 8), (eq(1), eq(2), eq(3), eq(4), eq(5), eq(6), eq(7), eq(8)))
77 }
78 
79 #[test]
tuple_matcher_matches_matching_9_tuple() -> Result<()>80 fn tuple_matcher_matches_matching_9_tuple() -> Result<()> {
81     verify_that!(
82         (1, 2, 3, 4, 5, 6, 7, 8, 9),
83         (eq(1), eq(2), eq(3), eq(4), eq(5), eq(6), eq(7), eq(8), eq(9))
84     )
85 }
86 
87 #[test]
tuple_matcher_matches_matching_10_tuple() -> Result<()>88 fn tuple_matcher_matches_matching_10_tuple() -> Result<()> {
89     verify_that!(
90         (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
91         (eq(1), eq(2), eq(3), eq(4), eq(5), eq(6), eq(7), eq(8), eq(9), eq(10))
92     )
93 }
94 
95 #[test]
tuple_matcher_matches_matching_11_tuple() -> Result<()>96 fn tuple_matcher_matches_matching_11_tuple() -> Result<()> {
97     verify_that!(
98         (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
99         (eq(1), eq(2), eq(3), eq(4), eq(5), eq(6), eq(7), eq(8), eq(9), eq(10), eq(11))
100     )
101 }
102 
103 #[test]
tuple_matcher_matches_matching_12_tuple() -> Result<()>104 fn tuple_matcher_matches_matching_12_tuple() -> Result<()> {
105     verify_that!(
106         (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
107         (eq(1), eq(2), eq(3), eq(4), eq(5), eq(6), eq(7), eq(8), eq(9), eq(10), eq(11), eq(12))
108     )
109 }
110 
111 #[test]
tuple_matcher_with_trailing_comma_matches_matching_3_tuple() -> Result<()>112 fn tuple_matcher_with_trailing_comma_matches_matching_3_tuple() -> Result<()> {
113     verify_that!((1, 2, 3), (eq(1), eq(2), eq(3),))
114 }
115 
116 #[test]
tuple_matcher_with_trailing_comma_matches_matching_4_tuple() -> Result<()>117 fn tuple_matcher_with_trailing_comma_matches_matching_4_tuple() -> Result<()> {
118     verify_that!((1, 2, 3, 4), (eq(1), eq(2), eq(3), eq(4),))
119 }
120 
121 #[test]
tuple_matcher_with_trailing_comma_matches_matching_5_tuple() -> Result<()>122 fn tuple_matcher_with_trailing_comma_matches_matching_5_tuple() -> Result<()> {
123     verify_that!((1, 2, 3, 4, 5), (eq(1), eq(2), eq(3), eq(4), eq(5),))
124 }
125 
126 #[test]
tuple_matcher_with_trailing_comma_matches_matching_6_tuple() -> Result<()>127 fn tuple_matcher_with_trailing_comma_matches_matching_6_tuple() -> Result<()> {
128     verify_that!((1, 2, 3, 4, 5, 6), (eq(1), eq(2), eq(3), eq(4), eq(5), eq(6),))
129 }
130 
131 #[test]
tuple_matcher_with_trailing_comma_matches_matching_7_tuple() -> Result<()>132 fn tuple_matcher_with_trailing_comma_matches_matching_7_tuple() -> Result<()> {
133     verify_that!((1, 2, 3, 4, 5, 6, 7), (eq(1), eq(2), eq(3), eq(4), eq(5), eq(6), eq(7),))
134 }
135 
136 #[test]
tuple_matcher_with_trailing_comma_matches_matching_8_tuple() -> Result<()>137 fn tuple_matcher_with_trailing_comma_matches_matching_8_tuple() -> Result<()> {
138     verify_that!(
139         (1, 2, 3, 4, 5, 6, 7, 8),
140         (eq(1), eq(2), eq(3), eq(4), eq(5), eq(6), eq(7), eq(8),)
141     )
142 }
143 
144 #[test]
tuple_matcher_with_trailing_comma_matches_matching_9_tuple() -> Result<()>145 fn tuple_matcher_with_trailing_comma_matches_matching_9_tuple() -> Result<()> {
146     verify_that!(
147         (1, 2, 3, 4, 5, 6, 7, 8, 9),
148         (eq(1), eq(2), eq(3), eq(4), eq(5), eq(6), eq(7), eq(8), eq(9),)
149     )
150 }
151 
152 #[test]
tuple_matcher_with_trailing_comma_matches_matching_10_tuple() -> Result<()>153 fn tuple_matcher_with_trailing_comma_matches_matching_10_tuple() -> Result<()> {
154     verify_that!(
155         (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
156         (eq(1), eq(2), eq(3), eq(4), eq(5), eq(6), eq(7), eq(8), eq(9), eq(10),)
157     )
158 }
159 
160 #[test]
tuple_matcher_with_trailing_comma_matches_matching_11_tuple() -> Result<()>161 fn tuple_matcher_with_trailing_comma_matches_matching_11_tuple() -> Result<()> {
162     verify_that!(
163         (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
164         (eq(1), eq(2), eq(3), eq(4), eq(5), eq(6), eq(7), eq(8), eq(9), eq(10), eq(11),)
165     )
166 }
167 
168 #[test]
tuple_matcher_with_trailing_comma_matches_matching_12_tuple() -> Result<()>169 fn tuple_matcher_with_trailing_comma_matches_matching_12_tuple() -> Result<()> {
170     verify_that!(
171         (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
172         (eq(1), eq(2), eq(3), eq(4), eq(5), eq(6), eq(7), eq(8), eq(9), eq(10), eq(11), eq(12),)
173     )
174 }
175 
176 #[test]
tuple_matcher_1_has_correct_description_for_match() -> Result<()>177 fn tuple_matcher_1_has_correct_description_for_match() -> Result<()> {
178     verify_that!(
179         (eq::<i32, _>(1),).describe(MatcherResult::Match),
180         displays_as(eq(indoc!(
181             "
182             is a tuple whose values respectively match:
183               is equal to 1"
184         )))
185     )
186 }
187 
188 #[test]
tuple_matcher_1_has_correct_description_for_mismatch() -> Result<()>189 fn tuple_matcher_1_has_correct_description_for_mismatch() -> Result<()> {
190     verify_that!(
191         (eq::<i32, _>(1),).describe(MatcherResult::NoMatch),
192         displays_as(eq(indoc!(
193             "
194             is a tuple whose values do not respectively match:
195               is equal to 1"
196         )))
197     )
198 }
199 
200 #[test]
tuple_matcher_2_has_correct_description_for_match() -> Result<()>201 fn tuple_matcher_2_has_correct_description_for_match() -> Result<()> {
202     verify_that!(
203         (eq::<i32, _>(1), eq::<i32, _>(2)).describe(MatcherResult::Match),
204         displays_as(eq(indoc!(
205             "
206             is a tuple whose values respectively match:
207               is equal to 1
208               is equal to 2"
209         )))
210     )
211 }
212 
213 #[test]
tuple_matcher_2_has_correct_description_for_mismatch() -> Result<()>214 fn tuple_matcher_2_has_correct_description_for_mismatch() -> Result<()> {
215     verify_that!(
216         (eq::<i32, _>(1), eq::<i32, _>(2)).describe(MatcherResult::NoMatch),
217         displays_as(eq(indoc!(
218             "
219             is a tuple whose values do not respectively match:
220               is equal to 1
221               is equal to 2"
222         )))
223     )
224 }
225 
226 #[test]
describe_match_shows_which_tuple_element_did_not_match() -> Result<()>227 fn describe_match_shows_which_tuple_element_did_not_match() -> Result<()> {
228     verify_that!(
229         (eq(1), eq(2)).explain_match(&(1, 3)),
230         displays_as(eq(indoc!(
231             "
232             which
233               is a tuple whose values do not respectively match:
234                 is equal to 1
235                 is equal to 2
236             Element #1 is 3,
237               which isn't equal to 2"
238         )))
239     )
240 }
241 
242 #[test]
describe_match_shows_which_two_tuple_elements_did_not_match() -> Result<()>243 fn describe_match_shows_which_two_tuple_elements_did_not_match() -> Result<()> {
244     verify_that!(
245         (eq(1), eq(2)).explain_match(&(2, 3)),
246         displays_as(eq(indoc!(
247             "
248             which
249               is a tuple whose values do not respectively match:
250                 is equal to 1
251                 is equal to 2
252             Element #0 is 2,
253               which isn't equal to 1
254             Element #1 is 3,
255               which isn't equal to 2"
256         )))
257     )
258 }
259