Skip to content

Commit

Permalink
refactor: Rename inner function truthy -> isZero
Browse files Browse the repository at this point in the history
  • Loading branch information
raeperd committed Nov 8, 2024
1 parent 2c5d4b9 commit e79d9c5
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,30 @@ func AllEqual[T comparable](t testing.TB, want, got []T) {
// Zero calls t.Fatalf if value != the zero value for T.
func Zero[T any](t testing.TB, value T) {
t.Helper()
if truthy(value) {
if !isZero(value) {
t.Fatalf("got: %v", value)
}
}

// NotZero calls t.Fatalf if value == the zero value for T.
func NotZero[T any](t testing.TB, value T) {
t.Helper()
if !truthy(value) {
if isZero(value) {
t.Fatalf("got: %v", value)
}
}

func truthy[T any](v T) bool {
func isZero[T any](v T) bool {
switch m := any(v).(type) {
case interface{ IsZero() bool }:
return !m.IsZero()
return m.IsZero()
}
return reflectValue(&v)
}

func reflectValue(vp any) bool {
switch rv := reflect.ValueOf(vp).Elem(); rv.Kind() {
switch rv := reflect.ValueOf(&v).Elem(); rv.Kind() {
case reflect.Map, reflect.Slice:
return rv.Len() != 0
return rv.Len() == 0
default:
return !rv.IsZero()
return rv.IsZero()
}
}

Expand Down

0 comments on commit e79d9c5

Please sign in to comment.