Skip to content

Commit

Permalink
Merge pull request #1385 from hslatman/not-implements
Browse files Browse the repository at this point in the history
Add `NotImplements` and variants
  • Loading branch information
brackendawson committed Feb 28, 2024
2 parents 9f97d67 + 4e56e1e commit 5b6926d
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 @@ -412,6 +412,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 @@ -485,6 +485,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 5b6926d

Please sign in to comment.