-
Notifications
You must be signed in to change notification settings - Fork 1.7k
assert: add NoFieldIsZero #1591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2274,3 +2274,39 @@ func buildErrorChainString(err error, withType bool) string { | |
| } | ||
| return chain | ||
| } | ||
|
|
||
| // NoFieldIsZero asserts that object, which must be a struct or eventually | ||
| // reference to one, has no field with a value that is zero. | ||
| // | ||
| // The assertion is not recursive, meaning it only checks that the fields | ||
| // of the struct (embedded structs are considered fields) are not zero values. | ||
| // It does not check the fields of nested or embedded structs. | ||
| func NoFieldIsZero(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { | ||
| if h, ok := t.(tHelper); ok { | ||
| h.Helper() | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this block above at the top of the function. This is important for the case of pointer to struct to be reported at the right place. |
||
|
|
||
| if reflect.TypeOf(object).Kind() == reflect.Ptr { | ||
| return NoFieldIsZero(t, reflect.ValueOf(object).Elem().Interface(), msgAndArgs) | ||
| } | ||
|
|
||
| objectType := reflect.TypeOf(object) | ||
| if objectType.Kind() != reflect.Struct { | ||
| return Fail(t, "Input must be a struct or eventually reference one", msgAndArgs...) | ||
| } | ||
|
|
||
| var emptyFields []string | ||
| objectValue := reflect.ValueOf(object) | ||
| for i := 0; i < objectType.NumField(); i++ { | ||
| field := objectType.Field(i) | ||
| if objectValue.Field(i).IsZero() { | ||
| emptyFields = append(emptyFields, field.Name) | ||
| } | ||
| } | ||
|
|
||
| if len(emptyFields) > 0 { | ||
| return Fail(t, fmt.Sprintf("Object contained fields with zero values: %s", strings.Join(emptyFields, ", ")), msgAndArgs...) | ||
| } | ||
|
|
||
| return true | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change to use zero value rather than the existing but extremely weird testify "empty" definition is good and resolves my previous comments.
I think this should be recursive though, it doesn't solve the example use case well when it is not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you think it is an improvement I can change the implementation (my original implementations were).
I am slightly hesitant about it though due to naming and go conventions. The convention of the reflect package is the fields of a struct do not include fields of nested structs. Taking the recursive approach could lead to confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use case you provided as an example would fail as soon as any future developer nests a struct, it could fail to store/load and your NoFielIsZero asserion would not protect your test.
If following the conventions of reflect is a hard requirement, and there is no other use case for which this feature works well, then we should not add this feature.
I don't think testify is at all consistent with depth in its assertions. assert.Equal uses reflect.DeepEqual, but assert.ElementsMatch is shallow.