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
Copy file name to clipboardExpand all lines: docs/index.md
+33-4Lines changed: 33 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2786,7 +2786,14 @@ Due to the global nature of these methods, keep in mind that signaling processes
2786
2786
2787
2787
### Testing type `struct`
2788
2788
2789
-
`gstruct` provides the `FieldsMatcher` through the `MatchAllFields` and `MatchFields` functions for applying a separate matcher to each field of a struct:
2789
+
`gstruct` provides the `FieldsMatcher` through the `MatchAllFields` and `MatchFields` functions for applying a separate matcher to each field of a struct.
2790
+
2791
+
To match a subset or superset of a struct, you should use the `MatchFields` function with the `IgnoreExtras`, `IgnoreUnexportedExtras` and `IgnoreMissing` options.
2792
+
The options can be combined with the binary or, for instance : `IgnoreMissing|IgnoreExtras`.
2793
+
2794
+
#### Match all fields
2795
+
2796
+
`MatchAllFields` requires that every field is matched, and each matcher is mapped to a field. This is useful for test maintainability, as it ensures that all fields are tested, and will fail in the future if you add or remove a field and forget to update the test, e.g.
`MatchAllFields` requires that every field is matched, and each matcher is mapped to a field. To match a subset or superset of a struct, you should use the `MatchFields` function with the `IgnoreExtras` and `IgnoreMissing` options. `IgnoreExtras` will ignore fields that don't map to a matcher, e.g.
2811
+
#### Ignore extra fields
2812
+
2813
+
`IgnoreExtras` will ignore fields that don't map to a matcher, e.g.
Using IgnoreExtras will ignore any new field that you will add to the struct in the future, you might want to consider using `gstruct.Ignore()` instead if you want to ignore only specific fields.
2824
+
2825
+
#### Ignore unexported extra fields
2826
+
2827
+
`IgnoreUnexportedExtras` will ignore fields that don't map to a matcher, but only if they are unexported e.g.
// But does not ignore "C" in the matcher, because it is exported.
2835
+
}))
2836
+
```
2837
+
2838
+
This is useful because gstruct uses the `reflect` package to access the fields of a struct, and it will not be able to access unexported fields.
2839
+
This is a compromise between using MatchAllFields and MatchFields with IgnoreExtras, as it allows you to ignore unexported fields without having to specify them in the matcher.
2840
+
If you prefer to list the unexported fields you want to ignore, you can use `gstruct.Ignore()` instead, the matcher will make sure to not use reflect on those fields.
2841
+
2842
+
2843
+
#### Ignore missing fields
2844
+
2814
2845
`IgnoreMissing` will ignore matchers that don't map to a field, e.g.
The options can be combined with the binary or: `IgnoreMissing|IgnoreExtras`.
2826
-
2827
2856
### Testing type slice
2828
2857
2829
2858
`gstruct` provides the `ElementsMatcher` through the `MatchAllElements` and `MatchElements` function for applying a separate matcher to each element, identified by an `Identifier` function:
0 commit comments