-
Notifications
You must be signed in to change notification settings - Fork 1.7k
assert: add Kind and NotKind #1803
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 5 commits
3a702f0
0e1ca6f
0ea708d
f7231cc
8b7c2e0
efa51f8
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 |
|---|---|---|
|
|
@@ -487,6 +487,36 @@ func IsNotType(t TestingT, theType, object interface{}, msgAndArgs ...interface{ | |
| return Fail(t, fmt.Sprintf("Object type expected to be different than %T", theType), msgAndArgs...) | ||
| } | ||
|
|
||
| // Kind asserts that the given object's kind matches the expected kind. | ||
| // | ||
| // assert.Kind(t, reflect.String, "Hello World") | ||
| func Kind(t TestingT, expectedKind reflect.Kind, object interface{}, msgAndArgs ...interface{}) bool { | ||
| if h, ok := t.(tHelper); ok { | ||
| h.Helper() | ||
| } | ||
|
|
||
| objectKind := reflect.TypeOf(object).Kind() | ||
|
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.
Author
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. Thanks for catching this. |
||
| if objectKind == expectedKind { | ||
| return true | ||
| } | ||
| return Fail(t, fmt.Sprintf("Object expected to be of kind %v, but was %v", expectedKind, objectKind), msgAndArgs...) | ||
| } | ||
|
|
||
| // NotKind asserts that the given object's kind does not match the unexpected kind. | ||
| // | ||
| // assert.NotKind(t, reflect.Int, "Hello World") | ||
| func NotKind(t TestingT, unexpectedKind reflect.Kind, 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. Same issues as in
Author
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. Updated, thank you. |
||
| objectKind := reflect.TypeOf(object).Kind() | ||
| if objectKind != unexpectedKind { | ||
| return true | ||
| } | ||
| return Fail(t, fmt.Sprintf("Object expected NOT to be of kind %v, but was %v", unexpectedKind, objectKind), msgAndArgs...) | ||
| } | ||
|
|
||
| // Equal asserts that two objects are equal. | ||
| // | ||
| // assert.Equal(t, 123, 123) | ||
|
|
||
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 case where
expectedKind == reflect.Invalidmust be rejected (and added to test cases).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.
Thanks for catching this.
I've updated to handle this