-
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 all commits
c73c6a4
626d9c2
7eedfa1
8adec64
48fffa8
194f059
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -486,6 +486,54 @@ 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() | ||
| } | ||
|
|
||
| if expectedKind == reflect.Invalid { | ||
| return Fail(t, "reflect.Invalid must not be used as expected kind", msgAndArgs...) | ||
| } | ||
|
|
||
| if object == nil { | ||
| return Fail(t, "Object must not be nil", msgAndArgs...) | ||
| } | ||
|
|
||
| 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.
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. Hi @dolmen |
||
| if unexpectedKind == reflect.Invalid { | ||
| return Fail(t, "reflect.Invalid must not be used as unexpected kind", msgAndArgs...) | ||
| } | ||
|
|
||
| if object == nil { | ||
| return Fail(t, "Object must not be nil", msgAndArgs...) | ||
| } | ||
|
|
||
| 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) | ||
|
|
||
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