Skip to content

Commit 78a940a

Browse files
committed
Add assert.Len, assert.NotNil, assert.Empty
1 parent e35d2aa commit 78a940a

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

assert.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,37 @@ func Nil(t TestingT, object any, msgAndArgs ...any) bool {
232232
}
233233
return testifyAssert.Nil(t, object, msgAndArgs...)
234234
}
235+
236+
// NotNil asserts that the specified object is not nil.
237+
//
238+
// assert.NotNil(t, "hello")
239+
//
240+
func NotNil(t TestingT, object any, msgAndArgs ...any) bool {
241+
if h, ok := t.(tHelper); ok {
242+
h.Helper()
243+
}
244+
return testifyAssert.NotNil(t, object, msgAndArgs...)
245+
}
246+
247+
// Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either
248+
// a slice or a channel with len == 0.
249+
//
250+
// assert.Empty(t, "")
251+
//
252+
func Empty(t TestingT, object any, msgAndArgs ...any) bool {
253+
if h, ok := t.(tHelper); ok {
254+
h.Helper()
255+
}
256+
return testifyAssert.Empty(t, object, msgAndArgs...)
257+
}
258+
259+
// Len asserts that the specified object has specific length.
260+
// Len also fails if the object has a type that len() not accept.
261+
//
262+
// assert.Len(t, mySlice, 3)
263+
func Len(t TestingT, object any, length int, msgAndArgs ...any) bool {
264+
if h, ok := t.(tHelper); ok {
265+
h.Helper()
266+
}
267+
return testifyAssert.Len(t, object, length, msgAndArgs...)
268+
}

assert_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,40 @@ func Test_AssertNil(t *testing.T) {
175175
testAssert(t, false, func(t *testing.T) bool {
176176
return Nil(t, []any{"hello"})
177177
})
178+
179+
testAssert(t, true, func(t *testing.T) bool {
180+
return NotNil(t, []any{"hello"})
181+
})
182+
183+
testAssert(t, false, func(t *testing.T) bool {
184+
return NotNil(t, nil)
185+
})
186+
187+
testAssert(t, true, func(t *testing.T) bool {
188+
return Empty(t, nil)
189+
})
190+
191+
testAssert(t, true, func(t *testing.T) bool {
192+
return Empty(t, "")
193+
})
194+
195+
testAssert(t, true, func(t *testing.T) bool {
196+
return Empty(t, []any{})
197+
})
198+
199+
testAssert(t, false, func(t *testing.T) bool {
200+
return Empty(t, []any{"a"})
201+
})
202+
203+
testAssert(t, false, func(t *testing.T) bool {
204+
return Empty(t, "1")
205+
})
206+
207+
testAssert(t, true, func(t *testing.T) bool {
208+
return Len(t, []any{"a", "b"}, 2)
209+
})
210+
211+
testAssert(t, false, func(t *testing.T) bool {
212+
return Len(t, []any{"a", "b"}, 3)
213+
})
178214
}

0 commit comments

Comments
 (0)