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 
18 #[derive(Debug)]
19 struct IntField {
20     int: i32,
21 }
22 
23 #[test]
field_matches_integer_field() -> Result<()>24 fn field_matches_integer_field() -> Result<()> {
25     verify_that!(IntField { int: 32 }, field!(IntField.int, eq(32)))
26 }
27 
28 #[derive(Debug)]
29 struct StringField {
30     strink: String,
31 }
32 
33 #[test]
field_matches_string_field() -> Result<()>34 fn field_matches_string_field() -> Result<()> {
35     verify_that!(StringField { strink: "yes".to_string() }, field!(StringField.strink, eq("yes")))
36 }
37 
38 #[test]
field_error_message_shows_field_name_and_inner_matcher() -> Result<()>39 fn field_error_message_shows_field_name_and_inner_matcher() -> Result<()> {
40     let matcher = field!(IntField.int, eq(31));
41 
42     verify_that!(
43         matcher.describe(MatcherResult::Match),
44         displays_as(eq("has field `int`, which is equal to 31"))
45     )
46 }
47 
48 mod sub {
49     #[derive(Debug)]
50     pub struct SubStruct {
51         pub field: i32,
52     }
53 }
54 
55 #[test]
struct_in_other_module_matches() -> Result<()>56 fn struct_in_other_module_matches() -> Result<()> {
57     verify_that!(sub::SubStruct { field: 32 }, field!(sub::SubStruct.field, eq(32)))
58 }
59 
60 #[derive(Debug)]
61 struct Tuple(i32, String);
62 
63 #[test]
tuple_matches_with_index() -> Result<()>64 fn tuple_matches_with_index() -> Result<()> {
65     verify_that!(Tuple(32, "yes".to_string()), field!(Tuple.0, eq(32)))
66 }
67 
68 #[test]
matches_enum_value() -> Result<()>69 fn matches_enum_value() -> Result<()> {
70     #[derive(Debug)]
71     enum AnEnum {
72         AValue(u32),
73     }
74     let value = AnEnum::AValue(123);
75 
76     verify_that!(value, field!(AnEnum::AValue.0, eq(123)))
77 }
78 
79 #[test]
shows_correct_failure_message_for_wrong_struct_entry() -> Result<()>80 fn shows_correct_failure_message_for_wrong_struct_entry() -> Result<()> {
81     #[derive(Debug)]
82     struct AStruct {
83         a: Vec<u32>,
84     }
85     let value = AStruct { a: vec![1] };
86 
87     let result = verify_that!(value, field!(AStruct.a, container_eq([])));
88 
89     verify_that!(
90         result,
91         err(displays_as(contains_substring(
92             "which has field `a`, which contains the unexpected element 1"
93         )))
94     )
95 }
96 
97 #[test]
does_not_match_enum_value_with_wrong_enum_variant() -> Result<()>98 fn does_not_match_enum_value_with_wrong_enum_variant() -> Result<()> {
99     #[derive(Debug)]
100     enum AnEnum {
101         #[allow(dead_code)] // This variant is intentionally unused.
102         AValue(u32),
103         AnotherValue,
104     }
105     let value = AnEnum::AnotherValue;
106 
107     verify_that!(value, not(field!(AnEnum::AValue.0, eq(123))))
108 }
109 
110 #[test]
shows_correct_failure_message_for_wrong_enum_value() -> Result<()>111 fn shows_correct_failure_message_for_wrong_enum_value() -> Result<()> {
112     #[derive(Debug)]
113     enum AnEnum {
114         #[allow(dead_code)] // This variant is intentionally unused.
115         AValue {
116             a: u32,
117         },
118         AnotherValue,
119     }
120     let value = AnEnum::AnotherValue;
121 
122     let result = verify_that!(value, field!(AnEnum::AValue.a, eq(123)));
123 
124     verify_that!(
125         result,
126         err(displays_as(contains_substring("which has the wrong enum variant `AnotherValue`")))
127     )
128 }
129 
130 #[test]
shows_correct_failure_message_for_wrong_enum_value_with_tuple_field() -> Result<()>131 fn shows_correct_failure_message_for_wrong_enum_value_with_tuple_field() -> Result<()> {
132     #[derive(Debug)]
133     enum AnEnum {
134         #[allow(dead_code)] // This variant is intentionally unused.
135         AValue(u32),
136         AnotherValue(u32),
137     }
138     let value = AnEnum::AnotherValue(123);
139 
140     let result = verify_that!(value, field!(AnEnum::AValue.0, eq(123)));
141 
142     verify_that!(
143         result,
144         err(displays_as(contains_substring("which has the wrong enum variant `AnotherValue`")))
145     )
146 }
147 
148 #[test]
shows_correct_failure_message_for_wrong_enum_value_with_named_field() -> Result<()>149 fn shows_correct_failure_message_for_wrong_enum_value_with_named_field() -> Result<()> {
150     #[derive(Debug)]
151     enum AnEnum {
152         #[allow(dead_code)] // This variant is intentionally unused.
153         AValue(u32),
154         AnotherValue {
155             #[allow(unused)]
156             a: u32,
157         },
158     }
159     let value = AnEnum::AnotherValue { a: 123 };
160 
161     let result = verify_that!(value, field!(AnEnum::AValue.0, eq(123)));
162 
163     verify_that!(
164         result,
165         err(displays_as(contains_substring("which has the wrong enum variant `AnotherValue`")))
166     )
167 }
168 
169 #[test]
matches_struct_like_enum_value() -> Result<()>170 fn matches_struct_like_enum_value() -> Result<()> {
171     #[derive(Debug)]
172     enum AnEnum {
173         AValue { a_field: u32 },
174     }
175     let value = AnEnum::AValue { a_field: 123 };
176 
177     verify_that!(value, field!(AnEnum::AValue.a_field, eq(123)))
178 }
179