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]
matches_any_value_when_list_is_empty() -> Result<()>20 fn matches_any_value_when_list_is_empty() -> Result<()> {
21     verify_that!((), all!())
22 }
23 
24 #[test]
matches_value_with_single_matching_component() -> Result<()>25 fn matches_value_with_single_matching_component() -> Result<()> {
26     verify_that!(123, all!(eq(123)))
27 }
28 
29 #[test]
does_not_match_value_with_single_non_matching_component() -> Result<()>30 fn does_not_match_value_with_single_non_matching_component() -> Result<()> {
31     verify_that!(123, not(all!(eq(456))))
32 }
33 
34 #[test]
matches_value_with_two_matching_components() -> Result<()>35 fn matches_value_with_two_matching_components() -> Result<()> {
36     verify_that!("A string", all!(starts_with("A"), ends_with("string")))
37 }
38 
39 #[test]
does_not_match_value_with_one_non_matching_component_among_two_components() -> Result<()>40 fn does_not_match_value_with_one_non_matching_component_among_two_components() -> Result<()> {
41     verify_that!(123, not(all!(eq(123), eq(456))))
42 }
43 
44 #[test]
supports_trailing_comma() -> Result<()>45 fn supports_trailing_comma() -> Result<()> {
46     verify_that!(
47         "An important string",
48         all!(starts_with("An"), contains_substring("important"), ends_with("string"),)
49     )
50 }
51 
52 #[test]
admits_matchers_without_static_lifetime() -> Result<()>53 fn admits_matchers_without_static_lifetime() -> Result<()> {
54     #[derive(Debug, PartialEq)]
55     struct AStruct(i32);
56     let expected_value = AStruct(123);
57     verify_that!(AStruct(123), all![eq_deref_of(&expected_value)])
58 }
59 
60 #[test]
mismatch_description_two_failed_matchers() -> Result<()>61 fn mismatch_description_two_failed_matchers() -> Result<()> {
62     verify_that!(
63         all!(starts_with("One"), starts_with("Two")).explain_match("Three"),
64         displays_as(eq("* which does not start with \"One\"\n* which does not start with \"Two\""))
65     )
66 }
67 
68 #[test]
mismatch_description_empty_matcher() -> Result<()>69 fn mismatch_description_empty_matcher() -> Result<()> {
70     verify_that!(all!().explain_match("Three"), displays_as(eq("which is anything")))
71 }
72 
73 #[test]
all_multiple_failed_assertions() -> Result<()>74 fn all_multiple_failed_assertions() -> Result<()> {
75     let result = verify_that!(4, all![eq(1), eq(2), eq(3)]);
76     verify_that!(
77         result,
78         err(displays_as(contains_substring(indoc!(
79             "
80             Value of: 4
81             Expected: has all the following properties:
82               * is equal to 1
83               * is equal to 2
84               * is equal to 3
85             Actual: 4,
86               * which isn't equal to 1
87               * which isn't equal to 2
88               * which isn't equal to 3"
89         ))))
90     )
91 }
92 
93 #[test]
formats_error_message_correctly_when_all_is_inside_some() -> Result<()>94 fn formats_error_message_correctly_when_all_is_inside_some() -> Result<()> {
95     let value = Some(4);
96     let result = verify_that!(value, some(all![eq(1), eq(2), eq(3)]));
97     verify_that!(
98         result,
99         err(displays_as(contains_substring(indoc!(
100             "
101             Value of: value
102             Expected: has a value which has all the following properties:
103               * is equal to 1
104               * is equal to 2
105               * is equal to 3
106             Actual: Some(4),
107               which has a value
108                 * which isn't equal to 1
109                 * which isn't equal to 2
110                 * which isn't equal to 3"
111         ))))
112     )
113 }
114 
115 #[test]
formats_error_message_correctly_when_all_is_inside_ok() -> Result<()>116 fn formats_error_message_correctly_when_all_is_inside_ok() -> Result<()> {
117     let value: std::result::Result<i32, std::io::Error> = Ok(4);
118     let result = verify_that!(value, ok(all![eq(1), eq(2), eq(3)]));
119     verify_that!(
120         result,
121         err(displays_as(contains_substring(indoc!(
122             "
123             Value of: value
124             Expected: is a success containing a value, which has all the following properties:
125               * is equal to 1
126               * is equal to 2
127               * is equal to 3
128             Actual: Ok(4),
129               which is a success
130                 * which isn't equal to 1
131                 * which isn't equal to 2
132                 * which isn't equal to 3"
133         ))))
134     )
135 }
136 
137 #[test]
formats_error_message_correctly_when_all_is_inside_err() -> Result<()>138 fn formats_error_message_correctly_when_all_is_inside_err() -> Result<()> {
139     let value: std::result::Result<(), &'static str> = Err("An error");
140     let result = verify_that!(value, err(all![starts_with("Not"), ends_with("problem")]));
141     verify_that!(
142         result,
143         err(displays_as(contains_substring(indoc!(
144             r#"
145             Value of: value
146             Expected: is an error which has all the following properties:
147               * starts with prefix "Not"
148               * ends with suffix "problem"
149             Actual: Err("An error"),
150               which is an error
151                 * which does not start with "Not"
152                 * which does not end with "problem""#
153         ))))
154     )
155 }
156