Proposal to introduce an assertion that checks that no (exported) fields are empty in a struct.
Changes
A function NoFieldIsEmpty is introduced into into the assert 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.
Tests for the newly introduced 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:
// Tests that all fields in entity can be stored and loaded
func TestPersistence(t *testing.T) {
entity := Entity{
// Filled with data
}
s := NewStore()
err := s.Store(entity)
require.NoError(t, err)
result, err := s.Load(entity.ID)
require.NoError(t, err)
assert.Equal(t, entity, result, "result should match the entity stored")
}
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
require.NoFieldIsEmpty(t, entity)
or as check at the end of test to ensure all fields where populated
assert.NoFieldIsEmpty(t, result)
I have found this assertion useful for when using tests that require populated structs including:
Testing the storing and loading of structs into database or persistence storage.
Testing the population of structs e.g. writing fakers.
Testing the marshalling/formatting of structs and unmarshalling/parsing data to structs.
Related issues
None.
Additional comments
This form of assertion does not appear to be wide spread from what I have seen and thus maybe is of low impact - however, maybe that is just due to a dislike of reflection 😅.
Although I find such an assertion useful there are times when I want all bar some fields populated and it might be better to have a function with the signature
This assertion doesn't solve the issue of nested structs are populated too (although it does help). I avoided approaching addressing this because it was unclear of how to express nested field names and handle arrays of structs. Also, having some feedback on the the simple case seemed to be a good starting point.
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