Skip to content

Commit d72eb79

Browse files
marcianxcopybara-github
authored andcommitted
Similar to native match pattern for braced structs, make matches_pattern enforce exhaustive field checks for braced structs by default, to be suppressed by explicit .. at the end of the pattern.
This change is not backward-compatible since: * It enforces field exhaustiveness by default in the absence of a trailing `..` in the pattern. * Things that previously fell back to `match` pattern matching now use `match_pattern!`'s specialized matching, which requires the matched object to implement `Debug`. Toward #447 PiperOrigin-RevId: 700843379
1 parent 16e975d commit d72eb79

File tree

3 files changed

+204
-55
lines changed

3 files changed

+204
-55
lines changed

googletest/src/matchers/matches_pattern.rs

+35-3
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@
4444
/// # .unwrap();
4545
/// ```
4646
///
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.
47+
/// If any fields are provided in the pattern, then all fields must be
48+
/// specified, or the pattern must end with `..`, just like regular match
49+
/// patterns. Omitted fields have no effect on the output of the matcher.
4950
///
5051
/// ```
5152
/// # use googletest::prelude::*;
@@ -61,7 +62,7 @@
6162
/// # };
6263
/// verify_that!(my_struct, matches_pattern!(MyStruct {
6364
/// a_field: starts_with("Something"),
64-
/// // another_field is missing, so it may be anything.
65+
/// .. // another_field is missing, so it may be anything.
6566
/// }))
6667
/// # .unwrap();
6768
/// ```
@@ -367,3 +368,34 @@ pub mod internal {
367368
}
368369
}
369370
}
371+
372+
mod compile_fail_tests {
373+
/// ```compile_fail
374+
/// use ::googletest::prelude::*;
375+
/// #[derive(Debug)]
376+
/// struct Foo { a: u32, b: u32 }
377+
/// let actual = Foo { a: 1, b: 2 };
378+
/// verify_that!(actual, matches_pattern!(Foo { a: eq(&1), .., }));
379+
/// ```
380+
fn _dot_dot_supported_only_at_end_of_struct_pattern() {}
381+
382+
/// ```compile_fail
383+
/// use ::googletest::prelude::*;
384+
/// #[derive(Debug)]
385+
/// struct Foo { a: u32, b: u32 }
386+
/// let actual = Foo { a: 1, b: 2 };
387+
/// verify_that!(actual, matches_pattern!(Foo { a: eq(&1) }));
388+
/// ```
389+
fn _unexhaustive_struct_field_check_requires_dot_dot() {}
390+
391+
/// ```compile_fail
392+
/// use ::googletest::prelude::*;
393+
/// #[derive(Debug)]
394+
/// struct Foo {
395+
/// Bar { a: u32, b: u32 }
396+
/// }
397+
/// let actual = Foo::Bar { a: 1, b: 2 };
398+
/// verify_that!(actual, matches_pattern!(Foo::Bar { a: eq(&1) }));
399+
/// ```
400+
fn _unexhaustive_enum_struct_field_check_requires_dot_dot() {}
401+
}

0 commit comments

Comments
 (0)