Skip to content

Commit

Permalink
Add assert.Len, assert.NotNil, assert.Empty
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Jul 24, 2022
1 parent e35d2aa commit 78a940a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
34 changes: 34 additions & 0 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,37 @@ func Nil(t TestingT, object any, msgAndArgs ...any) bool {
}
return testifyAssert.Nil(t, object, msgAndArgs...)
}

// NotNil asserts that the specified object is not nil.
//
// assert.NotNil(t, "hello")
//
func NotNil(t TestingT, object any, msgAndArgs ...any) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return testifyAssert.NotNil(t, object, msgAndArgs...)
}

// Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either
// a slice or a channel with len == 0.
//
// assert.Empty(t, "")
//
func Empty(t TestingT, object any, msgAndArgs ...any) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return testifyAssert.Empty(t, object, msgAndArgs...)
}

// Len asserts that the specified object has specific length.
// Len also fails if the object has a type that len() not accept.
//
// assert.Len(t, mySlice, 3)
func Len(t TestingT, object any, length int, msgAndArgs ...any) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return testifyAssert.Len(t, object, length, msgAndArgs...)
}
36 changes: 36 additions & 0 deletions assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,40 @@ func Test_AssertNil(t *testing.T) {
testAssert(t, false, func(t *testing.T) bool {
return Nil(t, []any{"hello"})
})

testAssert(t, true, func(t *testing.T) bool {
return NotNil(t, []any{"hello"})
})

testAssert(t, false, func(t *testing.T) bool {
return NotNil(t, nil)
})

testAssert(t, true, func(t *testing.T) bool {
return Empty(t, nil)
})

testAssert(t, true, func(t *testing.T) bool {
return Empty(t, "")
})

testAssert(t, true, func(t *testing.T) bool {
return Empty(t, []any{})
})

testAssert(t, false, func(t *testing.T) bool {
return Empty(t, []any{"a"})
})

testAssert(t, false, func(t *testing.T) bool {
return Empty(t, "1")
})

testAssert(t, true, func(t *testing.T) bool {
return Len(t, []any{"a", "b"}, 2)
})

testAssert(t, false, func(t *testing.T) bool {
return Len(t, []any{"a", "b"}, 3)
})
}

0 comments on commit 78a940a

Please sign in to comment.