diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 9cc3afe..d346226 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -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 diff --git a/Makefile b/Makefile index e11de90..7f82e9c 100644 --- a/Makefile +++ b/Makefile @@ -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 ./... diff --git a/assert/assert.go b/assert/assert.go index 075554a..1b596bd 100644 --- a/assert/assert.go +++ b/assert/assert.go @@ -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 } @@ -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, @@ -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) } diff --git a/assert/assert_test.go b/assert/assert_test.go index 085717c..dda2ba1 100644 --- a/assert/assert_test.go +++ b/assert/assert_test.go @@ -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") + }) +} diff --git a/assert/equal.go b/assert/equal.go index bcdfa78..a1019ff 100644 --- a/assert/equal.go +++ b/assert/equal.go @@ -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...) } @@ -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...) @@ -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...) @@ -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) @@ -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...) @@ -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...) diff --git a/assert/error.go b/assert/error.go index b461581..f2ae891 100644 --- a/assert/error.go +++ b/assert/error.go @@ -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...) @@ -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...) @@ -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...) diff --git a/assert/partial.go b/assert/partial.go index 86bea67..c886f74 100644 --- a/assert/partial.go +++ b/assert/partial.go @@ -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) diff --git a/assert/string.go b/assert/string.go index cae0f6b..6c7d119 100644 --- a/assert/string.go +++ b/assert/string.go @@ -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...) }