Skip to content

Commit

Permalink
chore: use testing.TB instead of *testing.T (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
katcipis committed Oct 7, 2022
1 parent 600dcdc commit 8ce6c95
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: "1.17"
go-version: "1.18"

- name: Lint
run: make lint
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ bench/memory/%:
go test -bench=. -benchmem -memprofile="profilling/${*}-memory.p" "./${*}"

lint:
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.43.0 run ./...
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.49.0 run ./...
19 changes: 12 additions & 7 deletions assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package assert

import (
"fmt"
"strings"
"testing"
)

// Assert is a custom assert helper.
type Assert struct {
t *testing.T
t testing.TB
details []interface{}
failfunc FailureReport
}
Expand All @@ -34,7 +35,7 @@ const detailSeparator = ": "
// ...
// The code above fails with message below:
// wanted[test] but got[tesd].Name mismatch: comparing objects Value1 and Value2
func New(t *testing.T, fail FailureReport, details ...interface{}) *Assert {
func New(t testing.TB, fail FailureReport, details ...interface{}) *Assert {
return &Assert{
t: t,
failfunc: fail,
Expand Down Expand Up @@ -79,10 +80,14 @@ func errordetails(details ...interface{}) string {
}

func errctx(context []interface{}, details ...interface{}) string {
errstr := errordetails(details...)
if len(errstr) > 0 {
errstr += detailSeparator
msgs := []string{}
detailsMsg := errordetails(details...)
if detailsMsg != "" {
msgs = append(msgs, detailsMsg)
}
errstr += errordetails(context...)
return errstr
ctxMsg := errordetails(context...)
if ctxMsg != "" {
msgs = append(msgs, ctxMsg)
}
return strings.Join(msgs, detailSeparator)
}
58 changes: 58 additions & 0 deletions assert/assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,61 @@ func TestPartial(t *testing.T) {
})
}
}

func TestAssertErrMessages(t *testing.T) {
t.Run("error: no details", func(t *testing.T) {
a := assert.New(t, func(a *assert.Assert, got string) {
want := "expected error, got nil"
assert.EqualStrings(t, want, got)
})
a.Error(nil)
})

t.Run("error: constructor msg and details msg with fmt", func(t *testing.T) {
a := assert.New(t, func(a *assert.Assert, got string) {
want := "expected error, got nil: func fmt 777: constructor fmt 666"
assert.EqualStrings(t, want, got)
}, "constructor fmt %d", 666)
a.Error(nil, "func fmt %d", 777)
})

t.Run("error: constructor msg and details msg no fmt", func(t *testing.T) {
a := assert.New(t, func(a *assert.Assert, got string) {
want := "expected error, got nil: func msg: constructor msg"
assert.EqualStrings(t, want, got)
}, "constructor msg")
a.Error(nil, "func msg")
})

t.Run("error: constructor msg with fmt", func(t *testing.T) {
a := assert.New(t, func(a *assert.Assert, got string) {
want := "expected error, got nil: constructor fmt 666"
assert.EqualStrings(t, want, got)
}, "constructor fmt %d", 666)
a.Error(nil)
})

t.Run("error: constructor msg no fmt", func(t *testing.T) {
a := assert.New(t, func(a *assert.Assert, got string) {
want := "expected error, got nil: constructor msg"
assert.EqualStrings(t, want, got)
}, "constructor msg")
a.Error(nil)
})

t.Run("error: detail msg with fmt", func(t *testing.T) {
a := assert.New(t, func(a *assert.Assert, got string) {
want := "expected error, got nil: func fmt 666"
assert.EqualStrings(t, want, got)
})
a.Error(nil, "func fmt %d", 666)
})

t.Run("error: detail msg no fmt", func(t *testing.T) {
a := assert.New(t, func(a *assert.Assert, got string) {
want := "expected error, got nil: func msg"
assert.EqualStrings(t, want, got)
})
a.Error(nil, "func msg")
})
}
12 changes: 6 additions & 6 deletions assert/equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (assert *Assert) IsFalse(b bool, details ...interface{}) {

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

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

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

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

// EqualComplexes compares the two complex numbers for equality.
// If they are not equal then the Fatal function is called with details.
func EqualComplexes(t *testing.T, want, got complex128, details ...interface{}) {
func EqualComplexes(t testing.TB, want, got complex128, details ...interface{}) {
t.Helper()
assert := New(t, Fatal)
assert.EqualComplexes(want, got, details...)
Expand Down Expand Up @@ -138,7 +138,7 @@ func (assert *Assert) EqualErrs(want error, got error, details ...interface{}) {
// EqualErrs compares if two errors have the same error description (by calling .Error()).
// If they are not equal then the Fatal() function is called with details.
// Both errors can't be nil.
func EqualErrs(t *testing.T, want, got error, details ...interface{}) {
func EqualErrs(t testing.TB, want, got error, details ...interface{}) {
t.Helper()
assert := New(t, Fatal)
assert.EqualErrs(want, got, details...)
Expand Down
6 changes: 3 additions & 3 deletions assert/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (assert *Assert) NoError(err error, details ...interface{}) {

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

// Error will call Fatal with the given details if the error is nil.
func Error(t *testing.T, err error, details ...interface{}) {
func Error(t testing.TB, err error, details ...interface{}) {
t.Helper()
assert := New(t, Fatal)
assert.Error(err, details...)
Expand All @@ -50,7 +50,7 @@ func (assert *Assert) IsError(got, want error, details ...interface{}) {
// IsError will assert if the given error matches the want error.
// It uses the errors.Is() function to check if the error wraps the wanted error.
// It calls the Fatal() function if errors.Is() returns false.
func IsError(t *testing.T, got, want error, details ...interface{}) {
func IsError(t testing.TB, got, want error, details ...interface{}) {
t.Helper()
assert := New(t, Fatal)
assert.IsError(got, want, details...)
Expand Down
2 changes: 1 addition & 1 deletion assert/partial.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (assert *Assert) partialStruct(obj interface{}, target interface{}, details
}
}

func Partial(t *testing.T, obj interface{}, target interface{}, details ...interface{}) {
func Partial(t testing.TB, obj interface{}, target interface{}, details ...interface{}) {
t.Helper()
assert := New(t, Fatal, details...)
assert.Partial(obj, target)
Expand Down
4 changes: 2 additions & 2 deletions assert/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ func (assert *Assert) StringMatch(pattern string, str string, details ...interfa

// StringContains asserts that string s contains the subst string and calls
// the Fatal() function with details otherwise.
func StringContains(t *testing.T, s, substr string, details ...interface{}) {
func StringContains(t testing.TB, s, substr string, details ...interface{}) {
assert := New(t, Fatal)
assert.StringContains(s, substr, details...)
}

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

0 comments on commit 8ce6c95

Please sign in to comment.