Skip to content

Commit 9f3e413

Browse files
Merge pull request #182 from google:adjust-property-matcher-docs
PiperOrigin-RevId: 528740813
2 parents 4c385c9 + 88cb3e0 commit 9f3e413

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

googletest/src/matchers/matches_pattern.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
/// # .unwrap();
4545
/// ```
4646
///
47-
/// It is not required to include all named fields in the specification:
47+
/// It is not required to include all named fields in the specification. Omitted
48+
/// fields have no effect on the output of the matcher.
4849
///
4950
/// ```
5051
/// # use googletest::{matchers::starts_with, matches_pattern, verify_that};
@@ -136,6 +137,11 @@
136137
/// # .unwrap();
137138
/// ```
138139
///
140+
/// **Important**: The method should be pure function with a deterministic
141+
/// output and no side effects. In particular, in the event of an assertion
142+
/// failure, it will be invoked a second time, with the assertion failure output
143+
/// reflecting the *second* invocation.
144+
///
139145
/// These may also include extra parameters you pass in:
140146
///
141147
/// ```
@@ -176,9 +182,6 @@
176182
/// # .unwrap();
177183
/// ```
178184
///
179-
/// > Note: At the moment, this does not work properly with methods returning
180-
/// > string references or slices.
181-
///
182185
/// One can also match tuple structs with up to 10 fields. In this case, all
183186
/// fields must have matchers:
184187
///
@@ -217,14 +220,30 @@
217220
/// # should_fail().unwrap_err();
218221
/// ```
219222
///
220-
/// It is perfectly okay to omit fields from a pattern with named fields. The
221-
/// values of omitted fields then have no effect on the output of the matcher.
222-
///
223223
/// This macro does not support plain (non-struct) tuples. Use the macro
224224
/// [`tuple`] for that purpose.
225225
///
226226
/// Trailing commas are allowed (but not required) in both ordinary and tuple
227227
/// structs.
228+
///
229+
/// Unfortunately, this matcher does *not* work with methods returning string slices:
230+
///
231+
/// ```compile_fail
232+
/// # use googletest::{matchers::eq, property, verify_that};
233+
/// # #[derive(Debug)]
234+
/// pub struct MyStruct {
235+
/// a_string: String,
236+
/// }
237+
/// impl MyStruct {
238+
/// pub fn get_a_string(&self) -> &str { &self.a_string }
239+
/// }
240+
///
241+
/// let value = MyStruct { a_string: "A string".into() };
242+
/// verify_that!(value, matches_pattern!( MyStruct {
243+
/// get_a_string(): eq("A string"), // Does not compile
244+
/// }))
245+
/// # .unwrap();
246+
/// ```
228247
#[macro_export]
229248
macro_rules! matches_pattern {
230249
($($t:tt)*) => { $crate::matches_pattern_internal!($($t)*) }

googletest/src/matchers/property_matcher.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ use googletest::*;
4242
/// # .unwrap();
4343
/// ```
4444
///
45+
/// **Important**: The method should be pure function with a deterministic
46+
/// output and no side effects. In particular, in the event of an assertion
47+
/// failure, it will be invoked a second time, with the assertion failure output
48+
/// reflecting the *second* invocation.
49+
///
4550
/// If the method returns a *reference*, then it must be preceded by the keyword
4651
/// `ref`:
4752
///
@@ -60,9 +65,6 @@ use googletest::*;
6065
/// # .unwrap();
6166
/// ```
6267
///
63-
/// > Note: At the moment, this does not work properly with methods returning
64-
/// > string references or slices.
65-
///
6668
/// The method may also take additional arguments:
6769
///
6870
/// ```
@@ -80,10 +82,22 @@ use googletest::*;
8082
/// # .unwrap();
8183
/// ```
8284
///
83-
/// > **Note**: The method should be pure function with a deterministic output
84-
/// > and no side effects. In particular, in the event of an assertion failure,
85-
/// > it will be invoked a second time, with the assertion failure output
86-
/// > reflecting the *second* invocation.
85+
/// Unfortunately, this matcher does *not* work with methods returning string slices:
86+
///
87+
/// ```compile_fail
88+
/// # use googletest::{matchers::eq, property, verify_that};
89+
/// #[derive(Debug)]
90+
/// pub struct MyStruct {
91+
/// a_string: String,
92+
/// }
93+
/// impl MyStruct {
94+
/// pub fn get_a_string(&self) -> &str { &self.a_string }
95+
/// }
96+
///
97+
/// let value = MyStruct { a_string: "A string".into() };
98+
/// verify_that!(value, property!(ref MyStruct.get_a_string(), eq("A string"))) // Does not compile
99+
/// # .unwrap();
100+
/// ```
87101
///
88102
/// This macro is analogous to [`field`][crate::field], except that it extracts
89103
/// the datum to be matched from the given object by invoking a method rather

googletest/tests/property_matcher_test.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ fn matches_struct_with_matching_string_reference_property() -> Result<()> {
9595
verify_that!(value, property!(ref StructWithString.get_property_ref(), eq("Something")))
9696
}
9797

98+
#[google_test]
99+
fn matches_struct_with_matching_slice_property() -> Result<()> {
100+
#[derive(Debug)]
101+
struct StructWithVec {
102+
property: Vec<u32>,
103+
}
104+
impl StructWithVec {
105+
fn get_property_ref(&self) -> &[u32] {
106+
&self.property
107+
}
108+
}
109+
let value = StructWithVec { property: vec![1, 2, 3] };
110+
verify_that!(value, property!(ref StructWithVec.get_property_ref(), eq([1, 2, 3])))
111+
}
112+
98113
#[google_test]
99114
fn matches_struct_with_matching_property_ref_with_parameters() -> Result<()> {
100115
let value = SomeStruct { a_property: 10 };

0 commit comments

Comments
 (0)