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

Commit

Permalink
Add Nil and NotNil assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed Oct 22, 2022
1 parent 976213b commit 1727b3a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The main additions are the new assertions for
### Added

- Add `True`, `False`, assertion functions.
- Add `Nil`, `NotNil`, assertion functions.
- Add `NoError`, `IsError` assertion functions.
- Add `Panics`, `NoPanic` assertion functions.
- Add `Eventually`, `EventuallyChan` assertion functions.
Expand Down
23 changes: 23 additions & 0 deletions nil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package verify

import "fmt"

// Nil tests if provided interface value is nil.
// Use it only for interfaces.
// For structs and pointers use Obj(got).Zero().
func Nil(v interface{}) FailureMessage {
if v == nil {
return ""
}
return FailureMessage(fmt.Sprintf("value is not nil\ngot: %+v", v))
}

// NotNil tests if provided interface is not nil.
// Use it only for interfaces.
// For structs and pointers use Obj(got).NonZero().
func NotNil(v any) FailureMessage {
if v != nil {
return ""
}
return "value is <nil>"
}
31 changes: 31 additions & 0 deletions nil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package verify_test

import (
"testing"

"github.com/fluentassert/verify"
)

func TestNil(t *testing.T) {
t.Run("Passed", func(t *testing.T) {
var err error
msg := verify.Nil(err)
assertPassed(t, msg)
})
t.Run("Failed", func(t *testing.T) {
msg := verify.Nil(0)
assertFailed(t, msg, "value is not nil")
})
}

func TestNotNil(t *testing.T) {
t.Run("Passed", func(t *testing.T) {
msg := verify.NotNil(0)
assertPassed(t, msg)
})
t.Run("Failed", func(t *testing.T) {
var err error
msg := verify.NotNil(err)
assertFailed(t, msg, "value is <nil>")
})
}

0 comments on commit 1727b3a

Please sign in to comment.