Skip to content

Commit 5b6926d

Browse files
Merge pull request #1385 from hslatman/not-implements
Add `NotImplements` and variants
2 parents 9f97d67 + 4e56e1e commit 5b6926d

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed

assert/assertion_format.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assert/assertion_forward.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assert/assertions.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,25 @@ func Implements(t TestingT, interfaceObject interface{}, object interface{}, msg
412412
return true
413413
}
414414

415+
// NotImplements asserts that an object does not implement the specified interface.
416+
//
417+
// assert.NotImplements(t, (*MyInterface)(nil), new(MyObject))
418+
func NotImplements(t TestingT, interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) bool {
419+
if h, ok := t.(tHelper); ok {
420+
h.Helper()
421+
}
422+
interfaceType := reflect.TypeOf(interfaceObject).Elem()
423+
424+
if object == nil {
425+
return Fail(t, fmt.Sprintf("Cannot check if nil does not implement %v", interfaceType), msgAndArgs...)
426+
}
427+
if reflect.TypeOf(object).Implements(interfaceType) {
428+
return Fail(t, fmt.Sprintf("%T implements %v", object, interfaceType), msgAndArgs...)
429+
}
430+
431+
return true
432+
}
433+
415434
// IsType asserts that the specified objects are of the same type.
416435
func IsType(t TestingT, expectedType interface{}, object interface{}, msgAndArgs ...interface{}) bool {
417436
if h, ok := t.(tHelper); ok {

assert/assertions_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,22 @@ func TestImplements(t *testing.T) {
485485

486486
}
487487

488+
func TestNotImplements(t *testing.T) {
489+
490+
mockT := new(testing.T)
491+
492+
if !NotImplements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) {
493+
t.Error("NotImplements method should return true: AssertionTesterNonConformingObject does not implement AssertionTesterInterface")
494+
}
495+
if NotImplements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) {
496+
t.Error("NotImplements method should return false: AssertionTesterConformingObject implements AssertionTesterInterface")
497+
}
498+
if NotImplements(mockT, (*AssertionTesterInterface)(nil), nil) {
499+
t.Error("NotImplements method should return false: nil can't be checked to be implementing AssertionTesterInterface or not")
500+
}
501+
502+
}
503+
488504
func TestIsType(t *testing.T) {
489505

490506
mockT := new(testing.T)

require/require.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

require/require_forward.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy