From 7b738055774d5c09e40d39d62930fe938e9f673f Mon Sep 17 00:00:00 2001 From: Jacalz Date: Sat, 6 Sep 2025 12:23:05 +0200 Subject: [PATCH 1/3] Switch all interface{} to any --- README.md | 32 ++++++++++++++++---------------- assert.go | 4 ++-- assert_test.go | 4 ++-- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 9ffe5ee..ae3ec3d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # A simple assertion library using Go generics -[![PkgGoDev](https://pkg.go.dev/badge/github.com/alecthomas/assert/v2)](https://pkg.go.dev/github.com/alecthomas/assert/v2) [![CI](https://github.com/alecthomas/assert/actions/workflows/ci.yml/badge.svg)](https://github.com/alecthomas/assert/actions/workflows/ci.yml) +[![PkgGoDev](https://pkg.go.dev/badge/github.com/alecthomas/assert/v2)](https://pkg.go.dev/github.com/alecthomas/assert/v2) [![CI](https://github.com/alecthomas/assert/actions/workflows/ci.yml/badge.svg)](https://github.com/alecthomas/assert/actions/workflows/ci.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/alecthomas/assert/v2)](https://goreportcard.com/report/github.com/alecthomas/assert/v2) [![Slack chat](https://img.shields.io/static/v1?logo=slack&style=flat&label=slack&color=green&message=gophers)](https://gophers.slack.com/messages/CN9DS8YF3) @@ -35,55 +35,55 @@ format error messages using the `fmt` package. // Equal asserts that "expected" and "actual" are equal using google/go-cmp. // // If they are not, a diff of the Go representation of the values will be displayed. -func Equal[T comparable](t testing.TB, expected, actual T, msgAndArgs ...interface{}) +func Equal[T comparable](t testing.TB, expected, actual T, msgAndArgs ...any) // NotEqual asserts that "expected" is not equal to "actual" using google/go-cmp. // // If they are equal the expected value will be displayed. -func NotEqual[T comparable](t testing.TB, expected, actual T, msgAndArgs ...interface{}) +func NotEqual[T comparable](t testing.TB, expected, actual T, msgAndArgs ...any) // Zero asserts that a value is its zero value. -func Zero[T comparable](t testing.TB, value T, msgAndArgs ...interface{}) +func Zero[T comparable](t testing.TB, value T, msgAndArgs ...any) // NotZero asserts that a value is not its zero value. -func NotZero[T comparable](t testing.TB, value T, msgAndArgs ...interface{}) +func NotZero[T comparable](t testing.TB, value T, msgAndArgs ...any) // Contains asserts that "haystack" contains "needle". -func Contains(t testing.TB, haystack string, needle string, msgAndArgs ...interface{}) +func Contains(t testing.TB, haystack string, needle string, msgAndArgs ...any) // NotContains asserts that "haystack" does not contain "needle". -func NotContains(t testing.TB, haystack string, needle string, msgAndArgs ...interface{}) +func NotContains(t testing.TB, haystack string, needle string, msgAndArgs ...any) // EqualError asserts that either an error is non-nil and that its message is what is expected, // or that error is nil if the expected message is empty. -func EqualError(t testing.TB, err error, errString string, msgAndArgs...interface{}) +func EqualError(t testing.TB, err error, errString string, msgAndArgs ...any) // Error asserts that an error is not nil. -func Error(t testing.TB, err error, msgAndArgs ...interface{}) +func Error(t testing.TB, err error, msgAndArgs ...any) // NoError asserts that an error is nil. -func NoError(t testing.TB, err error, msgAndArgs ...interface{}) +func NoError(t testing.TB, err error, msgAndArgs ...any) // IsError asserts than any error in "err"'s tree matches "target". -func IsError(t testing.TB, err, target error, msgAndArgs ...interface{}) +func IsError(t testing.TB, err, target error, msgAndArgs ...any) // NotIsError asserts than no error in "err"'s tree matches "target". -func NotIsError(t testing.TB, err, target error, msgAndArgs ...interface{}) +func NotIsError(t testing.TB, err, target error, msgAndArgs ...any) // Panics asserts that the given function panics. -func Panics(t testing.TB, fn func(), msgAndArgs ...interface{}) +func Panics(t testing.TB, fn func(), msgAndArgs ...any) // NotPanics asserts that the given function does not panic. -func NotPanics(t testing.TB, fn func(), msgAndArgs ...interface{}) +func NotPanics(t testing.TB, fn func(), msgAndArgs ...any) // Compare two values for equality and return true or false. func Compare[T any](t testing.TB, x, y T) bool // True asserts that an expression is true. -func True(t testing.TB, ok bool, msgAndArgs ...interface{}) +func True(t testing.TB, ok bool, msgAndArgs ...any) // False asserts that an expression is false. -func False(t testing.TB, ok bool, msgAndArgs ...interface{}) +func False(t testing.TB, ok bool, msgAndArgs ...any) ``` ## Evaluation process diff --git a/assert.go b/assert.go index 261beaf..f8b74df 100644 --- a/assert.go +++ b/assert.go @@ -125,7 +125,7 @@ func NotContains(t testing.TB, haystack string, needle string, msgAndArgs ...any } // SliceContains asserts that "haystack" contains "needle". -func SliceContains[T any](t testing.TB, haystack []T, needle T, msgAndArgs ...interface{}) { +func SliceContains[T any](t testing.TB, haystack []T, needle T, msgAndArgs ...any) { t.Helper() for _, item := range haystack { if objectsAreEqual(item, needle) { @@ -140,7 +140,7 @@ func SliceContains[T any](t testing.TB, haystack []T, needle T, msgAndArgs ...in } // NotSliceContains asserts that "haystack" does not contain "needle". -func NotSliceContains[T any](t testing.TB, haystack []T, needle T, msgAndArgs ...interface{}) { +func NotSliceContains[T any](t testing.TB, haystack []T, needle T, msgAndArgs ...any) { t.Helper() for _, item := range haystack { if objectsAreEqual(item, needle) { diff --git a/assert_test.go b/assert_test.go index 4a01f1b..227f6b3 100644 --- a/assert_test.go +++ b/assert_test.go @@ -217,11 +217,11 @@ type testTester struct { failed string } -func (t *testTester) Fatalf(message string, args ...interface{}) { +func (t *testTester) Fatalf(message string, args ...any) { t.failed = fmt.Sprintf(message, args...) } -func (t *testTester) Fatal(args ...interface{}) { +func (t *testTester) Fatal(args ...any) { t.failed = fmt.Sprint(args...) } From 3c6ec7acaa67b5d41fc629d03ea7136b4c4b6f9d Mon Sep 17 00:00:00 2001 From: Jacalz Date: Sat, 6 Sep 2025 12:27:17 +0200 Subject: [PATCH 2/3] Clean up some val.Kind() calls --- assert.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/assert.go b/assert.go index f8b74df..ab2f7ea 100644 --- a/assert.go +++ b/assert.go @@ -159,8 +159,11 @@ func Zero[T any](t testing.TB, value T, msgAndArgs ...any) { return } val := reflect.ValueOf(value) - if (val.Kind() == reflect.Slice || val.Kind() == reflect.Map || val.Kind() == reflect.Array) && val.Len() == 0 { - return + switch val.Kind() { + case reflect.Slice, reflect.Map, reflect.Array: + if val.Len() == 0 { + return + } } t.Helper() msg := formatMsgAndArgs("Expected a zero value but got:", msgAndArgs...) @@ -172,8 +175,11 @@ func NotZero[T any](t testing.TB, value T, msgAndArgs ...any) { var zero T if !objectsAreEqual(value, zero) { val := reflect.ValueOf(value) - if !((val.Kind() == reflect.Slice || val.Kind() == reflect.Map || val.Kind() == reflect.Array) && val.Len() == 0) { - return + switch val.Kind() { + case reflect.Slice, reflect.Map, reflect.Array: + if val.Len() == 0 { + return + } } } t.Helper() From b6260690329438fbda560f95751aa924c48fc5cf Mon Sep 17 00:00:00 2001 From: Jacalz Date: Tue, 30 Sep 2025 08:48:47 +0200 Subject: [PATCH 3/3] Fix tests and linting --- assert.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/assert.go b/assert.go index ab2f7ea..0b481f4 100644 --- a/assert.go +++ b/assert.go @@ -164,6 +164,7 @@ func Zero[T any](t testing.TB, value T, msgAndArgs ...any) { if val.Len() == 0 { return } + default: } t.Helper() msg := formatMsgAndArgs("Expected a zero value but got:", msgAndArgs...) @@ -177,9 +178,11 @@ func NotZero[T any](t testing.TB, value T, msgAndArgs ...any) { val := reflect.ValueOf(value) switch val.Kind() { case reflect.Slice, reflect.Map, reflect.Array: - if val.Len() == 0 { + if val.Len() > 0 { return } + default: + return } } t.Helper()