diff --git a/check/check.cmd.go b/check/check.cmd.go index 738b9b9..8202c67 100644 --- a/check/check.cmd.go +++ b/check/check.cmd.go @@ -120,6 +120,18 @@ var funcs = []Func{ Check: "checkNotNil(v)", Doc: "Check that v is not nil. This is a strict equality check.", }, + { + Name: "Zero", + Args: "v any", + Check: "checkZero(v)", + Doc: "Check that v is the zero value for its type.", + }, + { + Name: "NotZero", + Args: "v any", + Check: "checkNotZero(v)", + Doc: "Check that v is not the zero value for its type.", + }, { Name: "ErrIs", Args: "err, target error", diff --git a/check/check.go b/check/check.go index 61a67ca..1acba58 100644 --- a/check/check.go +++ b/check/check.go @@ -133,6 +133,24 @@ func checkNotNil(v any) (string, bool) { return "Expected something, got nil", false } +func checkZero(v any) (string, bool) { + rv := reflect.ValueOf(v) + if !rv.IsValid() || rv.IsZero() { + return "", true + } + + return equalMsg(v, reflect.Zero(rv.Type())), false +} + +func checkNotZero(v any) (string, bool) { + rv := reflect.ValueOf(v) + if rv.IsValid() && !rv.IsZero() { + return "", true + } + + return "Expected something, got {}", false +} + func checkErrIs(err, target error) (string, bool) { if errors.Is(err, target) { return "", true diff --git a/check/check.out.go b/check/check.out.go index 1ae2e96..2fd0bdd 100644 --- a/check/check.out.go +++ b/check/check.out.go @@ -234,6 +234,82 @@ func MustNotNilf(t Fatal, v any, format string, args ...any) { } } +// Check that v is the zero value for its type. +func Zero(t Error, v any) bool { + if msg, ok := checkZero(v); !ok { + t.Helper() + t.Error("\n" + msg) + return false + } + + return true +} + +// Check that v is the zero value for its type. +func Zerof(t Error, v any, format string, args ...any) bool { + if msg, ok := checkZero(v); !ok { + t.Helper() + t.Error(fmt.Sprintf(format, args...) + "\n" + msg) + return false + } + + return true +} + +// Check that v is the zero value for its type. +func MustZero(t Fatal, v any) { + if msg, ok := checkZero(v); !ok { + t.Helper() + t.Fatal("\n" + msg) + } +} + +// Check that v is the zero value for its type. +func MustZerof(t Fatal, v any, format string, args ...any) { + if msg, ok := checkZero(v); !ok { + t.Helper() + t.Fatal(fmt.Sprintf(format, args...) + "\n" + msg) + } +} + +// Check that v is not the zero value for its type. +func NotZero(t Error, v any) bool { + if msg, ok := checkNotZero(v); !ok { + t.Helper() + t.Error("\n" + msg) + return false + } + + return true +} + +// Check that v is not the zero value for its type. +func NotZerof(t Error, v any, format string, args ...any) bool { + if msg, ok := checkNotZero(v); !ok { + t.Helper() + t.Error(fmt.Sprintf(format, args...) + "\n" + msg) + return false + } + + return true +} + +// Check that v is not the zero value for its type. +func MustNotZero(t Fatal, v any) { + if msg, ok := checkNotZero(v); !ok { + t.Helper() + t.Fatal("\n" + msg) + } +} + +// Check that v is not the zero value for its type. +func MustNotZerof(t Fatal, v any, format string, args ...any) { + if msg, ok := checkNotZero(v); !ok { + t.Helper() + t.Fatal(fmt.Sprintf(format, args...) + "\n" + msg) + } +} + // Check that [errors.Is] returns true. func ErrIs(t Error, err, target error) bool { if msg, ok := checkErrIs(err, target); !ok { diff --git a/check/check_test.go b/check/check_test.go index aa4d2d4..48ba13a 100644 --- a/check/check_test.go +++ b/check/check_test.go @@ -60,6 +60,22 @@ func TestCheckNotNil(t *testing.T) { testCheck(checkNotNil(nil))(t, false) } +func TestCheckZero(t *testing.T) { + testCheck(checkZero(nil))(t, true) + testCheck(checkZero(0))(t, true) + testCheck(checkZero((*int)(nil)))(t, true) + testCheck(checkZero(1))(t, false) + testCheck(checkZero(new(int)))(t, false) +} + +func TestCheckNotZero(t *testing.T) { + testCheck(checkNotZero(1))(t, true) + testCheck(checkNotZero(new(int)))(t, true) + testCheck(checkNotZero(nil))(t, false) + testCheck(checkNotZero(0))(t, false) + testCheck(checkNotZero((*int)(nil)))(t, false) +} + func TestCheckErrIs(t *testing.T) { err := &os.PathError{ Op: "test",