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

Commit 8ce6c95

Browse files
authored
chore: use testing.TB instead of *testing.T (#22)
1 parent 600dcdc commit 8ce6c95

8 files changed

Lines changed: 84 additions & 21 deletions

File tree

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- name: Setup Go
2020
uses: actions/setup-go@v2
2121
with:
22-
go-version: "1.17"
22+
go-version: "1.18"
2323

2424
- name: Lint
2525
run: make lint

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ bench/memory/%:
2222
go test -bench=. -benchmem -memprofile="profilling/${*}-memory.p" "./${*}"
2323

2424
lint:
25-
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.43.0 run ./...
25+
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.49.0 run ./...

assert/assert.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package assert
22

33
import (
44
"fmt"
5+
"strings"
56
"testing"
67
)
78

89
// Assert is a custom assert helper.
910
type Assert struct {
10-
t *testing.T
11+
t testing.TB
1112
details []interface{}
1213
failfunc FailureReport
1314
}
@@ -34,7 +35,7 @@ const detailSeparator = ": "
3435
// ...
3536
// The code above fails with message below:
3637
// wanted[test] but got[tesd].Name mismatch: comparing objects Value1 and Value2
37-
func New(t *testing.T, fail FailureReport, details ...interface{}) *Assert {
38+
func New(t testing.TB, fail FailureReport, details ...interface{}) *Assert {
3839
return &Assert{
3940
t: t,
4041
failfunc: fail,
@@ -79,10 +80,14 @@ func errordetails(details ...interface{}) string {
7980
}
8081

8182
func errctx(context []interface{}, details ...interface{}) string {
82-
errstr := errordetails(details...)
83-
if len(errstr) > 0 {
84-
errstr += detailSeparator
83+
msgs := []string{}
84+
detailsMsg := errordetails(details...)
85+
if detailsMsg != "" {
86+
msgs = append(msgs, detailsMsg)
8587
}
86-
errstr += errordetails(context...)
87-
return errstr
88+
ctxMsg := errordetails(context...)
89+
if ctxMsg != "" {
90+
msgs = append(msgs, ctxMsg)
91+
}
92+
return strings.Join(msgs, detailSeparator)
8893
}

assert/assert_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,61 @@ func TestPartial(t *testing.T) {
354354
})
355355
}
356356
}
357+
358+
func TestAssertErrMessages(t *testing.T) {
359+
t.Run("error: no details", func(t *testing.T) {
360+
a := assert.New(t, func(a *assert.Assert, got string) {
361+
want := "expected error, got nil"
362+
assert.EqualStrings(t, want, got)
363+
})
364+
a.Error(nil)
365+
})
366+
367+
t.Run("error: constructor msg and details msg with fmt", func(t *testing.T) {
368+
a := assert.New(t, func(a *assert.Assert, got string) {
369+
want := "expected error, got nil: func fmt 777: constructor fmt 666"
370+
assert.EqualStrings(t, want, got)
371+
}, "constructor fmt %d", 666)
372+
a.Error(nil, "func fmt %d", 777)
373+
})
374+
375+
t.Run("error: constructor msg and details msg no fmt", func(t *testing.T) {
376+
a := assert.New(t, func(a *assert.Assert, got string) {
377+
want := "expected error, got nil: func msg: constructor msg"
378+
assert.EqualStrings(t, want, got)
379+
}, "constructor msg")
380+
a.Error(nil, "func msg")
381+
})
382+
383+
t.Run("error: constructor msg with fmt", func(t *testing.T) {
384+
a := assert.New(t, func(a *assert.Assert, got string) {
385+
want := "expected error, got nil: constructor fmt 666"
386+
assert.EqualStrings(t, want, got)
387+
}, "constructor fmt %d", 666)
388+
a.Error(nil)
389+
})
390+
391+
t.Run("error: constructor msg no fmt", func(t *testing.T) {
392+
a := assert.New(t, func(a *assert.Assert, got string) {
393+
want := "expected error, got nil: constructor msg"
394+
assert.EqualStrings(t, want, got)
395+
}, "constructor msg")
396+
a.Error(nil)
397+
})
398+
399+
t.Run("error: detail msg with fmt", func(t *testing.T) {
400+
a := assert.New(t, func(a *assert.Assert, got string) {
401+
want := "expected error, got nil: func fmt 666"
402+
assert.EqualStrings(t, want, got)
403+
})
404+
a.Error(nil, "func fmt %d", 666)
405+
})
406+
407+
t.Run("error: detail msg no fmt", func(t *testing.T) {
408+
a := assert.New(t, func(a *assert.Assert, got string) {
409+
want := "expected error, got nil: func msg"
410+
assert.EqualStrings(t, want, got)
411+
})
412+
a.Error(nil, "func msg")
413+
})
414+
}

assert/equal.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (assert *Assert) IsFalse(b bool, details ...interface{}) {
3131

3232
// IsTrue asserts that b is true.
3333
// If it's not then Fatal() is called with details.
34-
func IsTrue(t *testing.T, cond bool, details ...interface{}) {
34+
func IsTrue(t testing.TB, cond bool, details ...interface{}) {
3535
assert := New(t, Fatal)
3636
assert.IsTrue(cond, details...)
3737
}
@@ -47,7 +47,7 @@ func (assert *Assert) EqualStrings(want string, got string, details ...interface
4747

4848
// EqualStrings compares the two strings for equality.
4949
// If they are not equal then the Fatal() function is called with details.
50-
func EqualStrings(t *testing.T, want string, got string, details ...interface{}) {
50+
func EqualStrings(t testing.TB, want string, got string, details ...interface{}) {
5151
t.Helper()
5252
assert := New(t, Fatal)
5353
assert.EqualStrings(want, got, details...)
@@ -64,7 +64,7 @@ func (assert *Assert) EqualInts(want int, got int, details ...interface{}) {
6464

6565
// EqualInts compares the two ints for equality.
6666
// If they are not equal then the Fatal() function is called with details.
67-
func EqualInts(t *testing.T, want int, got int, details ...interface{}) {
67+
func EqualInts(t testing.TB, want int, got int, details ...interface{}) {
6868
t.Helper()
6969
assert := New(t, Fatal)
7070
assert.EqualInts(want, got, details...)
@@ -90,7 +90,7 @@ func (assert *Assert) EqualFloats(want float64, got float64, details ...interfac
9090

9191
// EqualFloats compares the two floats for equality.
9292
// If they are not equal then the Fatal() function is called with details.
93-
func EqualFloats(t *testing.T, want, got float64, details ...interface{}) {
93+
func EqualFloats(t testing.TB, want, got float64, details ...interface{}) {
9494
t.Helper()
9595
assert := New(t, Fatal)
9696
assert.EqualFloats(want, got)
@@ -107,7 +107,7 @@ func (assert *Assert) EqualComplexes(want, got complex128, details ...interface{
107107

108108
// EqualComplexes compares the two complex numbers for equality.
109109
// If they are not equal then the Fatal function is called with details.
110-
func EqualComplexes(t *testing.T, want, got complex128, details ...interface{}) {
110+
func EqualComplexes(t testing.TB, want, got complex128, details ...interface{}) {
111111
t.Helper()
112112
assert := New(t, Fatal)
113113
assert.EqualComplexes(want, got, details...)
@@ -138,7 +138,7 @@ func (assert *Assert) EqualErrs(want error, got error, details ...interface{}) {
138138
// EqualErrs compares if two errors have the same error description (by calling .Error()).
139139
// If they are not equal then the Fatal() function is called with details.
140140
// Both errors can't be nil.
141-
func EqualErrs(t *testing.T, want, got error, details ...interface{}) {
141+
func EqualErrs(t testing.TB, want, got error, details ...interface{}) {
142142
t.Helper()
143143
assert := New(t, Fatal)
144144
assert.EqualErrs(want, got, details...)

assert/error.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func (assert *Assert) NoError(err error, details ...interface{}) {
1616

1717
// NoError will assert that given error is nil.
1818
// If it's nil then the Fatal() function is called with details.
19-
func NoError(t *testing.T, err error, details ...interface{}) {
19+
func NoError(t testing.TB, err error, details ...interface{}) {
2020
t.Helper()
2121
assert := New(t, Fatal)
2222
assert.NoError(err, details...)
@@ -31,7 +31,7 @@ func (assert *Assert) Error(err error, details ...interface{}) {
3131
}
3232

3333
// Error will call Fatal with the given details if the error is nil.
34-
func Error(t *testing.T, err error, details ...interface{}) {
34+
func Error(t testing.TB, err error, details ...interface{}) {
3535
t.Helper()
3636
assert := New(t, Fatal)
3737
assert.Error(err, details...)
@@ -50,7 +50,7 @@ func (assert *Assert) IsError(got, want error, details ...interface{}) {
5050
// IsError will assert if the given error matches the want error.
5151
// It uses the errors.Is() function to check if the error wraps the wanted error.
5252
// It calls the Fatal() function if errors.Is() returns false.
53-
func IsError(t *testing.T, got, want error, details ...interface{}) {
53+
func IsError(t testing.TB, got, want error, details ...interface{}) {
5454
t.Helper()
5555
assert := New(t, Fatal)
5656
assert.IsError(got, want, details...)

assert/partial.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (assert *Assert) partialStruct(obj interface{}, target interface{}, details
141141
}
142142
}
143143

144-
func Partial(t *testing.T, obj interface{}, target interface{}, details ...interface{}) {
144+
func Partial(t testing.TB, obj interface{}, target interface{}, details ...interface{}) {
145145
t.Helper()
146146
assert := New(t, Fatal, details...)
147147
assert.Partial(obj, target)

assert/string.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ func (assert *Assert) StringMatch(pattern string, str string, details ...interfa
2424

2525
// StringContains asserts that string s contains the subst string and calls
2626
// the Fatal() function with details otherwise.
27-
func StringContains(t *testing.T, s, substr string, details ...interface{}) {
27+
func StringContains(t testing.TB, s, substr string, details ...interface{}) {
2828
assert := New(t, Fatal)
2929
assert.StringContains(s, substr, details...)
3030
}
3131

3232
// StringMatch asserts that string matches the regex pattern and calls
3333
// the Fatal() function with details otherwise.
34-
func StringMatch(t *testing.T, pattern string, str string, details ...interface{}) {
34+
func StringMatch(t testing.TB, pattern string, str string, details ...interface{}) {
3535
assert := New(t, Fatal)
3636
assert.StringMatch(pattern, str, details...)
3737
}

0 commit comments

Comments
 (0)