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]
pointwise_matches_single_element() -> Result<()>19 fn pointwise_matches_single_element() -> Result<()> {
20     let value = vec![1];
21     verify_that!(value, pointwise!(lt, vec![2]))
22 }
23 
24 #[test]
pointwise_matches_two_elements() -> Result<()>25 fn pointwise_matches_two_elements() -> Result<()> {
26     let value = vec![1, 2];
27     verify_that!(value, pointwise!(lt, vec![2, 3]))
28 }
29 
30 #[test]
pointwise_matches_two_elements_with_array() -> Result<()>31 fn pointwise_matches_two_elements_with_array() -> Result<()> {
32     let value = vec![1, 2];
33     verify_that!(value, pointwise!(lt, [2, 3]))
34 }
35 
36 #[test]
pointwise_matches_two_element_slice() -> Result<()>37 fn pointwise_matches_two_element_slice() -> Result<()> {
38     let value = vec![1, 2];
39     let slice = value.as_slice();
40     verify_that!(*slice, pointwise!(lt, [2, 3]))
41 }
42 
43 #[test]
pointwise_does_not_match_value_of_wrong_length() -> Result<()>44 fn pointwise_does_not_match_value_of_wrong_length() -> Result<()> {
45     let value = vec![1];
46     verify_that!(value, not(pointwise!(lt, vec![2, 3])))
47 }
48 
49 #[test]
pointwise_does_not_match_value_not_matching_in_first_position() -> Result<()>50 fn pointwise_does_not_match_value_not_matching_in_first_position() -> Result<()> {
51     let value = vec![1, 2];
52     verify_that!(value, not(pointwise!(lt, vec![1, 3])))
53 }
54 
55 #[test]
pointwise_does_not_match_value_not_matching_in_second_position() -> Result<()>56 fn pointwise_does_not_match_value_not_matching_in_second_position() -> Result<()> {
57     let value = vec![1, 2];
58     verify_that!(value, not(pointwise!(lt, vec![2, 2])))
59 }
60 
61 #[test]
pointwise_allows_qualified_matcher_name() -> Result<()>62 fn pointwise_allows_qualified_matcher_name() -> Result<()> {
63     mod submodule {
64         pub(super) use super::lt;
65     }
66     let value = vec![1];
67     verify_that!(value, pointwise!(submodule::lt, vec![2]))
68 }
69 
70 #[test]
pointwise_returns_mismatch_when_actual_value_has_wrong_length() -> Result<()>71 fn pointwise_returns_mismatch_when_actual_value_has_wrong_length() -> Result<()> {
72     let result = verify_that!(vec![1, 2, 3], pointwise!(eq, vec![1, 2]));
73 
74     verify_that!(
75         result,
76         err(displays_as(contains_substring(indoc!(
77             "
78             Value of: vec![1, 2, 3]
79             Expected: has elements satisfying respectively:
80               0. is equal to 1
81               1. is equal to 2
82             Actual: [1, 2, 3],
83               which has size 3 (expected 2)
84             "
85         ))))
86     )
87 }
88 
89 #[test]
pointwise_returns_mismatch_when_actual_value_does_not_match_on_first_item() -> Result<()>90 fn pointwise_returns_mismatch_when_actual_value_does_not_match_on_first_item() -> Result<()> {
91     let result = verify_that!(vec![1, 2, 3], pointwise!(eq, vec![2, 2, 3]));
92 
93     verify_that!(
94         result,
95         err(displays_as(contains_substring(indoc!(
96             "
97             Value of: vec![1, 2, 3]
98             Expected: has elements satisfying respectively:
99               0. is equal to 2
100               1. is equal to 2
101               2. is equal to 3
102             Actual: [1, 2, 3],
103               where element #0 is 1, which isn't equal to 2
104             "
105         ))))
106     )
107 }
108 
109 #[test]
pointwise_returns_mismatch_when_actual_value_does_not_match_on_second_item() -> Result<()>110 fn pointwise_returns_mismatch_when_actual_value_does_not_match_on_second_item() -> Result<()> {
111     let result = verify_that!(vec![1, 2, 3], pointwise!(eq, vec![1, 3, 3]));
112 
113     verify_that!(
114         result,
115         err(displays_as(contains_substring(indoc!(
116             "
117             Value of: vec![1, 2, 3]
118             Expected: has elements satisfying respectively:
119               0. is equal to 1
120               1. is equal to 3
121               2. is equal to 3
122             Actual: [1, 2, 3],
123               where element #1 is 2, which isn't equal to 3
124             "
125         ))))
126     )
127 }
128 
129 #[test]
pointwise_returns_mismatch_when_actual_value_does_not_match_on_first_and_second_items() -> Result<()>130 fn pointwise_returns_mismatch_when_actual_value_does_not_match_on_first_and_second_items()
131 -> Result<()> {
132     let result = verify_that!(vec![1, 2, 3], pointwise!(eq, vec![2, 3, 3]));
133 
134     verify_that!(
135         result,
136         err(displays_as(contains_substring(indoc!(
137             "
138             Value of: vec![1, 2, 3]
139             Expected: has elements satisfying respectively:
140               0. is equal to 2
141               1. is equal to 3
142               2. is equal to 3
143             Actual: [1, 2, 3],
144               where:
145                 * element #0 is 1, which isn't equal to 2
146                 * element #1 is 2, which isn't equal to 3"
147         ))))
148     )
149 }
150 
151 #[test]
pointwise_matches_single_element_with_lambda_expression_with_extra_value() -> Result<()>152 fn pointwise_matches_single_element_with_lambda_expression_with_extra_value() -> Result<()> {
153     let value = vec![1.00001f32];
154     verify_that!(value, pointwise!(|v| near(v, 0.0001), vec![1.0]))
155 }
156 
157 #[test]
pointwise_matches_single_element_with_two_containers() -> Result<()>158 fn pointwise_matches_single_element_with_two_containers() -> Result<()> {
159     let value = vec![1.00001f32];
160     verify_that!(value, pointwise!(near, vec![1.0], vec![0.0001]))
161 }
162 
163 #[test]
pointwise_matches_single_element_with_three_containers() -> Result<()>164 fn pointwise_matches_single_element_with_three_containers() -> Result<()> {
165     let value = vec![1.00001f32];
166     verify_that!(
167         value,
168         pointwise!(|v, t, u| near(v, t * u), vec![1.0f32], vec![0.0001f32], vec![0.5f32])
169     )
170 }
171