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

Commit adf4b19

Browse files
committed
Add NonZero assertion function
1 parent 474d585 commit adf4b19

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

f/obj.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,13 @@ func (x FluentObj[T]) Zero() FailureMessage {
6060
}
6161
return FailureMessage("not a zero value (-want +got):\n" + diff)
6262
}
63+
64+
// NonZero tests if the object is a non-zero value.
65+
func (x FluentObj[T]) NonZero() FailureMessage {
66+
var want T
67+
ok := cmp.Equal(want, x.Got)
68+
if !ok {
69+
return ""
70+
}
71+
return FailureMessage("a zero value")
72+
}

f/obj_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ func TestObj(t *testing.T) {
7171
})
7272
})
7373

74+
t.Run("NonZero", func(t *testing.T) {
75+
t.Run("Passed", func(t *testing.T) {
76+
got := A{Str: "string"}
77+
msg := f.Obj(got).NonZero()
78+
assertPassed(t, msg)
79+
})
80+
t.Run("Failed", func(t *testing.T) {
81+
got := A{}
82+
msg := f.Obj(got).NonZero()
83+
assertFailed(t, msg, "a zero value")
84+
})
85+
t.Run("nil", func(t *testing.T) {
86+
var got *A
87+
msg := f.Obj(got).NonZero()
88+
assertFailed(t, msg, "a zero value")
89+
})
90+
})
91+
7492
t.Run("Check", func(t *testing.T) {
7593
t.Run("Passed", func(t *testing.T) {
7694
fn := func(x A) string {

0 commit comments

Comments
 (0)