You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Include the type and variant being matched in the description of the matcher created by matches_pattern!.
Previously, the description would defer immediately to the inner matcher of the respective field or property. This was problematic when matching enum variants, since the variant itself would be left out. For example, the following code:
```
enum AnEnum {
A(u32),
B(u32),
}
let value = AnEnum::B(123);
verify_that!(value, matches_pattern!(AnEnum::A(eq(123)))
```
would yield the following assertion failure message:
```
Value of: value
Expected: has field `0`, which is equal to 123
Actual: AnEnum::B(123),
which does not have field `0`
```
The failure message does not give any indication that the expected variant is `MyEnum::A` and is therefore misleading.
This change introduces a new matcher `is` which does nothing but defer to an inner matcher and add a given string to the description. With this new matcher, `matches_pattern!` now outputs the expected type or enum variant in the description:
```
Value of: value
Expected: is AnEnum :: A which has field `0`, which is equal to 123
Actual: AnEnum::B(123),
which does not have field `0`
```
The match explanation is admittedly still rather confusing in this case, but fixing that is outside the scope of this change.
For consistency, this change also implements the new behaviour with properties as well as fields, even though properties cannot be used with enum variants.
This does not import the new matcher `is` into the list of matchers included in the prelude or the top-level list of matchers for the crate. The usefulness of `is` is fairly limited, so it does not seem worthwhile to include it. It must however have public visibility because it is used in a declarative macro.
PiperOrigin-RevId: 542850294
0 commit comments