Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 1727b3a

Browse files
committed
Add Nil and NotNil assertions
1 parent 976213b commit 1727b3a

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The main additions are the new assertions for
1919
### Added
2020

2121
- Add `True`, `False`, assertion functions.
22+
- Add `Nil`, `NotNil`, assertion functions.
2223
- Add `NoError`, `IsError` assertion functions.
2324
- Add `Panics`, `NoPanic` assertion functions.
2425
- Add `Eventually`, `EventuallyChan` assertion functions.

nil.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package verify
2+
3+
import "fmt"
4+
5+
// Nil tests if provided interface value is nil.
6+
// Use it only for interfaces.
7+
// For structs and pointers use Obj(got).Zero().
8+
func Nil(v interface{}) FailureMessage {
9+
if v == nil {
10+
return ""
11+
}
12+
return FailureMessage(fmt.Sprintf("value is not nil\ngot: %+v", v))
13+
}
14+
15+
// NotNil tests if provided interface is not nil.
16+
// Use it only for interfaces.
17+
// For structs and pointers use Obj(got).NonZero().
18+
func NotNil(v any) FailureMessage {
19+
if v != nil {
20+
return ""
21+
}
22+
return "value is <nil>"
23+
}

nil_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package verify_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/fluentassert/verify"
7+
)
8+
9+
func TestNil(t *testing.T) {
10+
t.Run("Passed", func(t *testing.T) {
11+
var err error
12+
msg := verify.Nil(err)
13+
assertPassed(t, msg)
14+
})
15+
t.Run("Failed", func(t *testing.T) {
16+
msg := verify.Nil(0)
17+
assertFailed(t, msg, "value is not nil")
18+
})
19+
}
20+
21+
func TestNotNil(t *testing.T) {
22+
t.Run("Passed", func(t *testing.T) {
23+
msg := verify.NotNil(0)
24+
assertPassed(t, msg)
25+
})
26+
t.Run("Failed", func(t *testing.T) {
27+
var err error
28+
msg := verify.NotNil(err)
29+
assertFailed(t, msg, "value is <nil>")
30+
})
31+
}

0 commit comments

Comments
 (0)