Skip to content

Commit f3796bb

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 a1e9bac commit f3796bb

File tree

3 files changed

+212
-59
lines changed

3 files changed

+212
-59
lines changed

googletest/src/matchers/matches_pattern.rs

+37-3
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@
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.
50+
/// The `..` is unnecessary when no fields are provided and only method
51+
/// values are checked.
4952
///
5053
/// ```
5154
/// # use googletest::prelude::*;
@@ -61,7 +64,7 @@
6164
/// # };
6265
/// verify_that!(my_struct, matches_pattern!(MyStruct {
6366
/// a_field: starts_with("Something"),
64-
/// // another_field is missing, so it may be anything.
67+
/// .. // another_field is missing, so it may be anything.
6568
/// }))
6669
/// # .unwrap();
6770
/// ```
@@ -367,3 +370,34 @@ pub mod internal {
367370
}
368371
}
369372
}
373+
374+
mod compile_fail_tests {
375+
/// ```compile_fail
376+
/// use ::googletest::prelude::*;
377+
/// #[derive(Debug)]
378+
/// struct Foo { a: u32, b: u32 }
379+
/// let actual = Foo { a: 1, b: 2 };
380+
/// verify_that!(actual, matches_pattern!(Foo { a: eq(&1), .., }));
381+
/// ```
382+
fn _dot_dot_supported_only_at_end_of_struct_pattern() {}
383+
384+
/// ```compile_fail
385+
/// use ::googletest::prelude::*;
386+
/// #[derive(Debug)]
387+
/// struct Foo { a: u32, b: u32 }
388+
/// let actual = Foo { a: 1, b: 2 };
389+
/// verify_that!(actual, matches_pattern!(Foo { a: eq(&1) }));
390+
/// ```
391+
fn _unexhaustive_struct_field_check_requires_dot_dot() {}
392+
393+
/// ```compile_fail
394+
/// use ::googletest::prelude::*;
395+
/// #[derive(Debug)]
396+
/// enum Foo {
397+
/// Bar { a: u32, b: u32 }
398+
/// }
399+
/// let actual = Foo::Bar { a: 1, b: 2 };
400+
/// verify_that!(actual, matches_pattern!(Foo::Bar { a: eq(&1) }));
401+
/// ```
402+
fn _unexhaustive_enum_struct_field_check_requires_dot_dot() {}
403+
}

0 commit comments

Comments
 (0)