Skip to content

Commit 6a2644c

Browse files
hovinenbcopybara-github
authored andcommitted
Support the property matcher macro in matches_pattern.
This allows the keys in a `matches_pattern!` call to include method calls rather than just fields: ``` verify_that!(value, matches_pattern!(MyStruct { my_method(): eq(100) }) ``` This is particularly useful when one wants to use `matches_pattern!` in a nested context, but one either cannot access the fields directly due to visibility or one does not want to access the data at that level of abstraction. This also supports passing arguments to the methods. Unfortunately, this does not work properly with slices and string references due to complicated issues with lifetimes. PiperOrigin-RevId: 521441047
1 parent 11a821b commit 6a2644c

File tree

3 files changed

+1108
-6
lines changed

3 files changed

+1108
-6
lines changed

googletest/src/matchers/matches_pattern.rs

Lines changed: 125 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,46 @@
7979
/// }))
8080
/// ```
8181
///
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+
///
82122
/// One can also match tuple structs with up to 10 fields. In this case, all
83123
/// fields must have matchers:
84124
///
@@ -128,6 +168,20 @@ macro_rules! matches_pattern_internal {
128168
all!(field!($($struct_name)*.$field_name, $matcher))
129169
};
130170

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+
131185
(
132186
[$($struct_name:tt)*],
133187
{ $field_name:ident : $matcher:expr, $($rest:tt)* }
@@ -139,6 +193,28 @@ macro_rules! matches_pattern_internal {
139193
)
140194
};
141195

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+
142218
(
143219
all!($($processed:tt)*),
144220
[$($struct_name:tt)*],
@@ -150,6 +226,28 @@ macro_rules! matches_pattern_internal {
150226
)
151227
};
152228

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+
153251
(
154252
all!($($processed:tt)*),
155253
[$($struct_name:tt)*],
@@ -168,11 +266,30 @@ macro_rules! matches_pattern_internal {
168266
(
169267
all!($($processed:tt)*),
170268
[$($struct_name:tt)*],
171-
{ $field_name:ident : $matcher:expr }
269+
{ $property_name:ident($($argument:expr),* $(,)?) : $matcher:expr, $($rest:tt)* }
172270
) => {
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)* }
176293
)
177294
};
178295

@@ -355,13 +472,16 @@ macro_rules! matches_pattern_internal {
355472
($first:tt $($rest:tt)*) => {{
356473
#[cfg(not(google3))]
357474
#[allow(unused)]
358-
use $crate::{all, field};
475+
use $crate::{all, field, property};
359476
#[cfg(google3)]
360477
#[allow(unused)]
361478
use all_matcher::all;
362479
#[cfg(google3)]
363480
#[allow(unused)]
364481
use field_matcher::field;
482+
#[cfg(google3)]
483+
#[allow(unused)]
484+
use property_matcher::property;
365485
$crate::matches_pattern_internal!([$first], $($rest)*)
366486
}};
367487
}

googletest/src/matchers/property_matcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ macro_rules! property_internal {
9595
}};
9696
}
9797

98-
/// Functions for use only by the declarative macros in this module.
98+
/// Items for use only by the declarative macros in this module.
9999
///
100100
/// **For internal use only. API stablility is not guaranteed!**
101101
#[doc(hidden)]

0 commit comments

Comments
 (0)