Skip to content

Commit 9fc264e

Browse files
authored
assert: add IsNotType (#1730)
Add the `IsNotType` assertion, which is the inverse of the existing `IsType` assertion. It allows users to assert that an object is not of a specific type. Additionally, minor documentation improvements were made. ## Summary This PR adds a new assertion function, `IsNotType`, to the `testify/assert` package. It complements the existing `IsType` function by providing a way to assert that an object is not of a specific type. ## Changes * Added the `IsNotType` function to the `assert` package. * Wrote unit tests for `IsNotType` to ensure correctness. * Updated documentation to include examples and usage for `IsNotType`. ## Motivation The `testify/assert` package already provides an `IsType` function to check if an object is of a specific type. However, there was no built-in way to assert that an object is **not** of a specific type. This PR addresses that gap by introducing the `IsNotType` function, improving the library's completeness and usability. ## Example usage ```go assert.IsNotType(t, &MyStruct{}, actualObject) ``` ## Related issues _N/A_
1 parent 2035e7d commit 9fc264e

File tree

7 files changed

+136
-5
lines changed

7 files changed

+136
-5
lines changed

assert/assertion_format.go

Lines changed: 12 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: 24 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: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -454,17 +454,34 @@ func NotImplements(t TestingT, interfaceObject interface{}, object interface{},
454454
return true
455455
}
456456

457+
func isType(expectedType, object interface{}) bool {
458+
return ObjectsAreEqual(reflect.TypeOf(object), reflect.TypeOf(expectedType))
459+
}
460+
457461
// IsType asserts that the specified objects are of the same type.
458-
func IsType(t TestingT, expectedType interface{}, object interface{}, msgAndArgs ...interface{}) bool {
462+
//
463+
// assert.IsType(t, &MyStruct{}, &MyStruct{})
464+
func IsType(t TestingT, expectedType, object interface{}, msgAndArgs ...interface{}) bool {
465+
if isType(expectedType, object) {
466+
return true
467+
}
459468
if h, ok := t.(tHelper); ok {
460469
h.Helper()
461470
}
471+
return Fail(t, fmt.Sprintf("Object expected to be of type %T, but was %T", expectedType, object), msgAndArgs...)
472+
}
462473

463-
if !ObjectsAreEqual(reflect.TypeOf(object), reflect.TypeOf(expectedType)) {
464-
return Fail(t, fmt.Sprintf("Object expected to be of type %T, but was %T", expectedType, object), msgAndArgs...)
474+
// IsNotType asserts that the specified objects are not of the same type.
475+
//
476+
// assert.IsNotType(t, &NotMyStruct{}, &MyStruct{})
477+
func IsNotType(t TestingT, theType, object interface{}, msgAndArgs ...interface{}) bool {
478+
if !isType(theType, object) {
479+
return true
465480
}
466-
467-
return true
481+
if h, ok := t.(tHelper); ok {
482+
h.Helper()
483+
}
484+
return Fail(t, fmt.Sprintf("Object type expected to be different than %T", theType), msgAndArgs...)
468485
}
469486

470487
// Equal asserts that two objects are equal.

assert/assertions_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,18 @@ func TestIsType(t *testing.T) {
556556
}
557557
}
558558

559+
func TestNotIsType(t *testing.T) {
560+
561+
mockT := new(testing.T)
562+
563+
if !IsNotType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject)) {
564+
t.Error("NotIsType should return true: AssertionTesterConformingObject is not the same type as AssertionTesterNonConformingObject")
565+
}
566+
if IsNotType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) {
567+
t.Error("NotIsType should return false: AssertionTesterConformingObject is the same type as AssertionTesterConformingObject")
568+
}
569+
}
570+
559571
func TestEqual(t *testing.T) {
560572
type myType string
561573

require/forward_requirements_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ func TestImplementsWrapper(t *testing.T) {
1919
}
2020
}
2121

22+
func TestIsNotTypeWrapper(t *testing.T) {
23+
require := New(t)
24+
require.IsNotType(new(AssertionTesterNonConformingObject), new(AssertionTesterConformingObject))
25+
26+
mockT := new(MockT)
27+
mockRequire := New(mockT)
28+
mockRequire.IsNotType(new(AssertionTesterConformingObject), new(AssertionTesterConformingObject))
29+
if !mockT.Failed {
30+
t.Error("Check should fail")
31+
}
32+
}
33+
2234
func TestIsTypeWrapper(t *testing.T) {
2335
require := New(t)
2436
require.IsType(new(AssertionTesterConformingObject), new(AssertionTesterConformingObject))

require/require.go

Lines changed: 30 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: 24 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)