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;
16 use googletest::prelude::*;
17 use indoc::indoc;
18
19 #[test]
elements_are_matches_vector() -> Result<()>20 fn elements_are_matches_vector() -> Result<()> {
21 let value = vec![1, 2, 3];
22 verify_that!(value, elements_are![eq(1), eq(2), eq(3)])
23 }
24
25 #[test]
elements_are_matches_slice() -> Result<()>26 fn elements_are_matches_slice() -> Result<()> {
27 let value = vec![1, 2, 3];
28 let slice = value.as_slice();
29 verify_that!(*slice, elements_are![eq(1), eq(2), eq(3)])
30 }
31
32 #[test]
elements_are_matches_array() -> Result<()>33 fn elements_are_matches_array() -> Result<()> {
34 verify_that!([1, 2, 3], elements_are![eq(1), eq(2), eq(3)])
35 }
36
37 #[test]
elements_are_supports_trailing_comma() -> Result<()>38 fn elements_are_supports_trailing_comma() -> Result<()> {
39 let value = vec![1, 2, 3];
40 verify_that!(value, elements_are![eq(1), eq(2), eq(3),])
41 }
42
43 #[test]
elements_are_returns_no_match_when_expected_and_actual_sizes_differ() -> Result<()>44 fn elements_are_returns_no_match_when_expected_and_actual_sizes_differ() -> Result<()> {
45 let value = vec![1, 2];
46 verify_that!(value, not(elements_are![eq(1), eq(2), eq(3)]))
47 }
48
49 #[test]
elements_are_admits_matchers_without_static_lifetime() -> Result<()>50 fn elements_are_admits_matchers_without_static_lifetime() -> Result<()> {
51 #[derive(Debug, PartialEq)]
52 struct AStruct(i32);
53 let expected_value = AStruct(123);
54 verify_that!(vec![AStruct(123)], elements_are![eq_deref_of(&expected_value)])
55 }
56
57 #[test]
elements_are_produces_correct_failure_message() -> Result<()>58 fn elements_are_produces_correct_failure_message() -> Result<()> {
59 let result = verify_that!(vec![1, 4, 3], elements_are![eq(1), eq(2), eq(3)]);
60 verify_that!(
61 result,
62 err(displays_as(contains_substring(indoc!(
63 "
64 Value of: vec![1, 4, 3]
65 Expected: has elements:
66 0. is equal to 1
67 1. is equal to 2
68 2. is equal to 3
69 Actual: [1, 4, 3],
70 where element #1 is 4, which isn't equal to 2"
71 ))))
72 )
73 }
74
75 #[test]
elements_are_produces_correct_failure_message_nested() -> Result<()>76 fn elements_are_produces_correct_failure_message_nested() -> Result<()> {
77 let result = verify_that!(
78 vec![vec![0, 1], vec![1, 2]],
79 elements_are![elements_are![eq(1), eq(2)], elements_are![eq(2), eq(3)]]
80 );
81 verify_that!(
82 result,
83 err(displays_as(contains_substring(indoc!(
84 "
85 Expected: has elements:
86 0. has elements:
87 0. is equal to 1
88 1. is equal to 2
89 1. has elements:
90 0. is equal to 2
91 1. is equal to 3
92 Actual: [[0, 1], [1, 2]],
93 where:
94 * element #0 is [0, 1], where:
95 * element #0 is 0, which isn't equal to 1
96 * element #1 is 1, which isn't equal to 2
97 * element #1 is [1, 2], where:
98 * element #0 is 1, which isn't equal to 2
99 * element #1 is 2, which isn't equal to 3"
100 ))))
101 )
102 }
103
104 #[test]
elements_are_explain_match_wrong_size() -> Result<()>105 fn elements_are_explain_match_wrong_size() -> Result<()> {
106 verify_that!(
107 elements_are![eq(1)].explain_match(&vec![1, 2]),
108 displays_as(eq("whose size is 2"))
109 )
110 }
111
create_matcher() -> impl Matcher<ActualT = Vec<i32>>112 fn create_matcher() -> impl Matcher<ActualT = Vec<i32>> {
113 elements_are![eq(1)]
114 }
115
116 #[test]
elements_are_works_when_matcher_is_created_in_subroutine() -> Result<()>117 fn elements_are_works_when_matcher_is_created_in_subroutine() -> Result<()> {
118 verify_that!(vec![1], create_matcher())
119 }
120
121 #[test]
elements_are_implicitly_called() -> Result<()>122 fn elements_are_implicitly_called() -> Result<()> {
123 verify_that!(vec![1, 2, 3], [eq(1), eq(2), eq(3)])
124 }
125