assert: introducing no field is empty assertion function #1591
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Proposal to introduce an assertion that checks that no (exported) fields are empty in a struct.
Changes
NoFieldIsEmpty
is introduced into into theassert
package. This function fails the test and returns false if any of the fields in the inputed struct (or reference to struct) are empty, aligning with pre-existing definition of empty.NoFieldIsEmpty
function were added.go generate ./...
was run.Motivation
I found myself repeatedly rewriting similar code in tests across different repositories in an effort to ensure that tests do not become out-of-date and no longer align with their original intention.
An example of this is writing tests that check that all fields in a struct can be stored and then loaded. Consider the test:
This test is claiming to check that all fields can be stored and loaded but it not enforcing it. If the entity was not correctly populated initially or if new fields where added to the Entity and the test was not updated then the test would not be align with its stated purpose.
In this particular case the assertion could be used as a pre-condition to ensure we always start with all fields containing data
or as check at the end of test to ensure all fields where populated
I have found this assertion useful for when using tests that require populated structs including:
Related issues
None.
Additional comments