Nil check in Implements assertion

pull/410/merge
Adam Medzinski 2017-06-20 12:59:03 +02:00 committed by Ernesto Jiménez
parent b3dfaa9086
commit 42baa3e5a4
2 changed files with 6 additions and 2 deletions

View File

@ -280,15 +280,16 @@ func labeledOutput(content ...labeledContent) string {
//
// assert.Implements(t, (*MyInterface)(nil), new(MyObject))
func Implements(t TestingT, interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) bool {
interfaceType := reflect.TypeOf(interfaceObject).Elem()
if object == nil {
return Fail(t, fmt.Sprintf("Cannot check if nil implements %v", interfaceType), msgAndArgs...)
}
if !reflect.TypeOf(object).Implements(interfaceType) {
return Fail(t, fmt.Sprintf("%T must implement %v", object, interfaceType), msgAndArgs...)
}
return true
}
// IsType asserts that the specified objects are of the same type.

View File

@ -155,6 +155,9 @@ func TestImplements(t *testing.T) {
if Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) {
t.Error("Implements method should return false: AssertionTesterNonConformingObject does not implements AssertionTesterInterface")
}
if Implements(mockT, (*AssertionTesterInterface)(nil), nil) {
t.Error("Implements method should return false: nil does not implement AssertionTesterInterface")
}
}