Skip to content

Commit

Permalink
Add NotImplements and variants
Browse files Browse the repository at this point in the history
  • Loading branch information
hslatman authored and dolmen committed Oct 16, 2023
1 parent 4ae48e9 commit 4e56e1e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
10 changes: 10 additions & 0 deletions assert/assertion_format.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions assert/assertion_forward.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions assert/assertions.go
Expand Up @@ -392,6 +392,25 @@ func Implements(t TestingT, interfaceObject interface{}, object interface{}, msg
return true
}

// NotImplements asserts that an object does not implement the specified interface.
//
// assert.NotImplements(t, (*MyInterface)(nil), new(MyObject))
func NotImplements(t TestingT, interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
interfaceType := reflect.TypeOf(interfaceObject).Elem()

if object == nil {
return Fail(t, fmt.Sprintf("Cannot check if nil does not implement %v", interfaceType), msgAndArgs...)
}
if reflect.TypeOf(object).Implements(interfaceType) {
return Fail(t, fmt.Sprintf("%T implements %v", object, interfaceType), msgAndArgs...)
}

return true
}

// IsType asserts that the specified objects are of the same type.
func IsType(t TestingT, expectedType interface{}, object interface{}, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
Expand Down
16 changes: 16 additions & 0 deletions assert/assertions_test.go
Expand Up @@ -449,6 +449,22 @@ func TestImplements(t *testing.T) {

}

func TestNotImplements(t *testing.T) {

mockT := new(testing.T)

if !NotImplements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) {
t.Error("NotImplements method should return true: AssertionTesterNonConformingObject does not implement AssertionTesterInterface")
}
if NotImplements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) {
t.Error("NotImplements method should return false: AssertionTesterConformingObject implements AssertionTesterInterface")
}
if NotImplements(mockT, (*AssertionTesterInterface)(nil), nil) {
t.Error("NotImplements method should return false: nil can't be checked to be implementing AssertionTesterInterface or not")
}

}

func TestIsType(t *testing.T) {

mockT := new(testing.T)
Expand Down
26 changes: 26 additions & 0 deletions require/require.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions require/require_forward.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4e56e1e

Please sign in to comment.