gotestyourself / gotest.tools

A collection of packages to augment the go testing package and support common patterns.
https://gotest.tools/v3
Other
513 stars 50 forks source link

Add complete property assertion #239

Closed dnephin closed 1 year ago

dnephin commented 2 years ago

This PR adds a new function for property testing of operations that need to include all the fields on a struct. Common examples of functions that could be tested with Complete include:

In all of these cases, adding a new field to the struct will break the operation unless the function is updated to include the new field. This addition can be easy to miss. If the function has a test that uses Complete, any contributor will get a clear test failure that they've forgotten to consider the new field in some operation.

This implementation uses generics, which makes it a bit easier to validate the inputs. I had a previous version that used reflection to validate the inputs, but it was a lot more involved. Since it uses generics we won't be able to include this package in the main Go module until we drop support for Go 1.17. For now the new package is added under x/generics which is a separate Go module. The package path should change whenever we include it in the main module, so that there are never multiple packages that provide the same package path.

dnephin commented 1 year ago

There are a few permutations of nested structs and pointers that aren't fully tested yet, and maps are not supported, but this is going into a new experimental module, so it can change at any time. I'm going to merge this as-is to try it out in a few places.