Skip to content

Commit afff491

Browse files
authored
Remove old interface{} uses and clean up val.Kind() (#24)
Just some small fixes to clean up and modernise the code.
1 parent 96885de commit afff491

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# A simple assertion library using Go generics
22

3-
[![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)
3+
[![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)
44
[![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)
55

66

@@ -35,55 +35,55 @@ format error messages using the `fmt` package.
3535
// Equal asserts that "expected" and "actual" are equal using google/go-cmp.
3636
//
3737
// If they are not, a diff of the Go representation of the values will be displayed.
38-
func Equal[T comparable](t testing.TB, expected, actual T, msgAndArgs ...interface{})
38+
func Equal[T comparable](t testing.TB, expected, actual T, msgAndArgs ...any)
3939

4040
// NotEqual asserts that "expected" is not equal to "actual" using google/go-cmp.
4141
//
4242
// If they are equal the expected value will be displayed.
43-
func NotEqual[T comparable](t testing.TB, expected, actual T, msgAndArgs ...interface{})
43+
func NotEqual[T comparable](t testing.TB, expected, actual T, msgAndArgs ...any)
4444

4545
// Zero asserts that a value is its zero value.
46-
func Zero[T comparable](t testing.TB, value T, msgAndArgs ...interface{})
46+
func Zero[T comparable](t testing.TB, value T, msgAndArgs ...any)
4747

4848
// NotZero asserts that a value is not its zero value.
49-
func NotZero[T comparable](t testing.TB, value T, msgAndArgs ...interface{})
49+
func NotZero[T comparable](t testing.TB, value T, msgAndArgs ...any)
5050

5151
// Contains asserts that "haystack" contains "needle".
52-
func Contains(t testing.TB, haystack string, needle string, msgAndArgs ...interface{})
52+
func Contains(t testing.TB, haystack string, needle string, msgAndArgs ...any)
5353

5454
// NotContains asserts that "haystack" does not contain "needle".
55-
func NotContains(t testing.TB, haystack string, needle string, msgAndArgs ...interface{})
55+
func NotContains(t testing.TB, haystack string, needle string, msgAndArgs ...any)
5656

5757
// EqualError asserts that either an error is non-nil and that its message is what is expected,
5858
// or that error is nil if the expected message is empty.
59-
func EqualError(t testing.TB, err error, errString string, msgAndArgs...interface{})
59+
func EqualError(t testing.TB, err error, errString string, msgAndArgs ...any)
6060

6161
// Error asserts that an error is not nil.
62-
func Error(t testing.TB, err error, msgAndArgs ...interface{})
62+
func Error(t testing.TB, err error, msgAndArgs ...any)
6363

6464
// NoError asserts that an error is nil.
65-
func NoError(t testing.TB, err error, msgAndArgs ...interface{})
65+
func NoError(t testing.TB, err error, msgAndArgs ...any)
6666

6767
// IsError asserts than any error in "err"'s tree matches "target".
68-
func IsError(t testing.TB, err, target error, msgAndArgs ...interface{})
68+
func IsError(t testing.TB, err, target error, msgAndArgs ...any)
6969

7070
// NotIsError asserts than no error in "err"'s tree matches "target".
71-
func NotIsError(t testing.TB, err, target error, msgAndArgs ...interface{})
71+
func NotIsError(t testing.TB, err, target error, msgAndArgs ...any)
7272

7373
// Panics asserts that the given function panics.
74-
func Panics(t testing.TB, fn func(), msgAndArgs ...interface{})
74+
func Panics(t testing.TB, fn func(), msgAndArgs ...any)
7575

7676
// NotPanics asserts that the given function does not panic.
77-
func NotPanics(t testing.TB, fn func(), msgAndArgs ...interface{})
77+
func NotPanics(t testing.TB, fn func(), msgAndArgs ...any)
7878

7979
// Compare two values for equality and return true or false.
8080
func Compare[T any](t testing.TB, x, y T) bool
8181

8282
// True asserts that an expression is true.
83-
func True(t testing.TB, ok bool, msgAndArgs ...interface{})
83+
func True(t testing.TB, ok bool, msgAndArgs ...any)
8484

8585
// False asserts that an expression is false.
86-
func False(t testing.TB, ok bool, msgAndArgs ...interface{})
86+
func False(t testing.TB, ok bool, msgAndArgs ...any)
8787
```
8888

8989
## Evaluation process

assert.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func NotContains(t testing.TB, haystack string, needle string, msgAndArgs ...any
125125
}
126126

127127
// SliceContains asserts that "haystack" contains "needle".
128-
func SliceContains[T any](t testing.TB, haystack []T, needle T, msgAndArgs ...interface{}) {
128+
func SliceContains[T any](t testing.TB, haystack []T, needle T, msgAndArgs ...any) {
129129
t.Helper()
130130
for _, item := range haystack {
131131
if objectsAreEqual(item, needle) {
@@ -140,7 +140,7 @@ func SliceContains[T any](t testing.TB, haystack []T, needle T, msgAndArgs ...in
140140
}
141141

142142
// NotSliceContains asserts that "haystack" does not contain "needle".
143-
func NotSliceContains[T any](t testing.TB, haystack []T, needle T, msgAndArgs ...interface{}) {
143+
func NotSliceContains[T any](t testing.TB, haystack []T, needle T, msgAndArgs ...any) {
144144
t.Helper()
145145
for _, item := range haystack {
146146
if objectsAreEqual(item, needle) {
@@ -159,8 +159,12 @@ func Zero[T any](t testing.TB, value T, msgAndArgs ...any) {
159159
return
160160
}
161161
val := reflect.ValueOf(value)
162-
if (val.Kind() == reflect.Slice || val.Kind() == reflect.Map || val.Kind() == reflect.Array) && val.Len() == 0 {
163-
return
162+
switch val.Kind() {
163+
case reflect.Slice, reflect.Map, reflect.Array:
164+
if val.Len() == 0 {
165+
return
166+
}
167+
default:
164168
}
165169
t.Helper()
166170
msg := formatMsgAndArgs("Expected a zero value but got:", msgAndArgs...)
@@ -172,7 +176,12 @@ func NotZero[T any](t testing.TB, value T, msgAndArgs ...any) {
172176
var zero T
173177
if !objectsAreEqual(value, zero) {
174178
val := reflect.ValueOf(value)
175-
if !((val.Kind() == reflect.Slice || val.Kind() == reflect.Map || val.Kind() == reflect.Array) && val.Len() == 0) {
179+
switch val.Kind() {
180+
case reflect.Slice, reflect.Map, reflect.Array:
181+
if val.Len() > 0 {
182+
return
183+
}
184+
default:
176185
return
177186
}
178187
}

assert_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,11 @@ type testTester struct {
217217
failed string
218218
}
219219

220-
func (t *testTester) Fatalf(message string, args ...interface{}) {
220+
func (t *testTester) Fatalf(message string, args ...any) {
221221
t.failed = fmt.Sprintf(message, args...)
222222
}
223223

224-
func (t *testTester) Fatal(args ...interface{}) {
224+
func (t *testTester) Fatal(args ...any) {
225225
t.failed = fmt.Sprint(args...)
226226
}
227227

0 commit comments

Comments
 (0)