79
79
/// }))
80
80
/// ```
81
81
///
82
+ /// In addition to fields, one can match on the outputs of methods
83
+ /// ("properties"):
84
+ ///
85
+ /// ```
86
+ /// impl MyStruct {
87
+ /// fn get_a_field(&self) -> String {...}
88
+ /// }
89
+ ///
90
+ /// verify_that!(my_struct, matches_pattern!(MyStruct {
91
+ /// get_a_field(): starts_with("Something"),
92
+ /// }))
93
+ /// ```
94
+ ///
95
+ /// These may also include extra parameters you pass in:
96
+ ///
97
+ /// ```
98
+ /// impl MyStruct {
99
+ /// fn append_to_a_field(&self, suffix: &str) -> String {...}
100
+ /// }
101
+ ///
102
+ /// verify_that!(my_struct, matches_pattern!(MyStruct {
103
+ /// append_to_a_field("a suffix"): ends_with("a suffix"),
104
+ /// }))
105
+ /// ```
106
+ ///
107
+ /// If the method returns a reference, precede it with the keyword `ref`:
108
+ ///
109
+ /// ```
110
+ /// impl MyStruct {
111
+ /// fn get_a_field_ref(&self) -> &String {...}
112
+ /// }
113
+ ///
114
+ /// verify_that!(my_struct, matches_pattern!(MyStruct {
115
+ /// ref get_a_field_ref(): starts_with("Something"),
116
+ /// }))
117
+ /// ```
118
+ ///
119
+ /// > Note: At the moment, this does not work properly with methods returning
120
+ /// > string references or slices.
121
+ ///
82
122
/// One can also match tuple structs with up to 10 fields. In this case, all
83
123
/// fields must have matchers:
84
124
///
@@ -128,6 +168,20 @@ macro_rules! matches_pattern_internal {
128
168
all!( field!( $( $struct_name) * . $field_name, $matcher) )
129
169
} ;
130
170
171
+ (
172
+ [ $( $struct_name: tt) * ] ,
173
+ { $property_name: ident( $( $argument: expr) ,* $( , ) ?) : $matcher: expr $( , ) ? }
174
+ ) => {
175
+ all!( property!( $( $struct_name) * . $property_name( $( $argument) ,* ) , $matcher) )
176
+ } ;
177
+
178
+ (
179
+ [ $( $struct_name: tt) * ] ,
180
+ { ref $property_name: ident( $( $argument: expr) ,* $( , ) ?) : $matcher: expr $( , ) ? }
181
+ ) => {
182
+ all!( property!( ref $( $struct_name) * . $property_name( $( $argument) ,* ) , $matcher) )
183
+ } ;
184
+
131
185
(
132
186
[ $( $struct_name: tt) * ] ,
133
187
{ $field_name: ident : $matcher: expr, $( $rest: tt) * }
@@ -139,6 +193,28 @@ macro_rules! matches_pattern_internal {
139
193
)
140
194
} ;
141
195
196
+ (
197
+ [ $( $struct_name: tt) * ] ,
198
+ { $property_name: ident( $( $argument: expr) ,* $( , ) ?) : $matcher: expr, $( $rest: tt) * }
199
+ ) => {
200
+ $crate:: matches_pattern_internal!(
201
+ all!( property!( $( $struct_name) * . $property_name( $( $argument) ,* ) , $matcher) ) ,
202
+ [ $( $struct_name) * ] ,
203
+ { $( $rest) * }
204
+ )
205
+ } ;
206
+
207
+ (
208
+ [ $( $struct_name: tt) * ] ,
209
+ { ref $property_name: ident( $( $argument: expr) ,* $( , ) ?) : $matcher: expr, $( $rest: tt) * }
210
+ ) => {
211
+ $crate:: matches_pattern_internal!(
212
+ all!( property!( ref $( $struct_name) * . $property_name( $( $argument) ,* ) , $matcher) ) ,
213
+ [ $( $struct_name) * ] ,
214
+ { $( $rest) * }
215
+ )
216
+ } ;
217
+
142
218
(
143
219
all!( $( $processed: tt) * ) ,
144
220
[ $( $struct_name: tt) * ] ,
@@ -150,6 +226,28 @@ macro_rules! matches_pattern_internal {
150
226
)
151
227
} ;
152
228
229
+ (
230
+ all!( $( $processed: tt) * ) ,
231
+ [ $( $struct_name: tt) * ] ,
232
+ { $property_name: ident( $( $argument: expr) ,* $( , ) ?) : $matcher: expr $( , ) ? }
233
+ ) => {
234
+ all!(
235
+ $( $processed) * ,
236
+ property!( $( $struct_name) * . $property_name( $( $argument) ,* ) , $matcher)
237
+ )
238
+ } ;
239
+
240
+ (
241
+ all!( $( $processed: tt) * ) ,
242
+ [ $( $struct_name: tt) * ] ,
243
+ { ref $property_name: ident( $( $argument: expr) ,* $( , ) ?) : $matcher: expr $( , ) ? }
244
+ ) => {
245
+ all!(
246
+ $( $processed) * ,
247
+ property!( ref $( $struct_name) * . $property_name( $( $argument) ,* ) , $matcher)
248
+ )
249
+ } ;
250
+
153
251
(
154
252
all!( $( $processed: tt) * ) ,
155
253
[ $( $struct_name: tt) * ] ,
@@ -168,11 +266,30 @@ macro_rules! matches_pattern_internal {
168
266
(
169
267
all!( $( $processed: tt) * ) ,
170
268
[ $( $struct_name: tt) * ] ,
171
- { $field_name : ident : $matcher: expr }
269
+ { $property_name : ident( $ ( $argument : expr ) , * $ ( , ) ? ) : $matcher: expr, $ ( $rest : tt ) * }
172
270
) => {
173
- all!(
174
- $( $processed) * ,
175
- field!( $( $struct_name) * . $field_name, $matcher)
271
+ $crate:: matches_pattern_internal!(
272
+ all!(
273
+ $( $processed) * ,
274
+ property!( $( $struct_name) * . $property_name( $( $argument) ,* ) , $matcher)
275
+ ) ,
276
+ [ $( $struct_name) * ] ,
277
+ { $( $rest) * }
278
+ )
279
+ } ;
280
+
281
+ (
282
+ all!( $( $processed: tt) * ) ,
283
+ [ $( $struct_name: tt) * ] ,
284
+ { ref $property_name: ident( $( $argument: expr) ,* $( , ) ?) : $matcher: expr, $( $rest: tt) * }
285
+ ) => {
286
+ $crate:: matches_pattern_internal!(
287
+ all!(
288
+ $( $processed) * ,
289
+ property!( ref $( $struct_name) * . $property_name( $( $argument) ,* ) , $matcher)
290
+ ) ,
291
+ [ $( $struct_name) * ] ,
292
+ { $( $rest) * }
176
293
)
177
294
} ;
178
295
@@ -355,13 +472,16 @@ macro_rules! matches_pattern_internal {
355
472
( $first: tt $( $rest: tt) * ) => { {
356
473
#[ cfg( not( google3) ) ]
357
474
#[ allow( unused) ]
358
- use $crate:: { all, field} ;
475
+ use $crate:: { all, field, property } ;
359
476
#[ cfg( google3) ]
360
477
#[ allow( unused) ]
361
478
use all_matcher:: all;
362
479
#[ cfg( google3) ]
363
480
#[ allow( unused) ]
364
481
use field_matcher:: field;
482
+ #[ cfg( google3) ]
483
+ #[ allow( unused) ]
484
+ use property_matcher:: property;
365
485
$crate:: matches_pattern_internal!( [ $first] , $( $rest) * )
366
486
} } ;
367
487
}
0 commit comments