Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ErrorAssertionFunc creators #1556

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions assert/assertions.go
Expand Up @@ -48,6 +48,17 @@ type ErrorAssertionFunc func(TestingT, error, ...interface{}) bool
// Comparison is a custom function that returns true on success and false on failure
type Comparison func() (success bool)

// ErrorIsFor returns an [ErrorAssertionFunc] which tests if the error wraps target.
func ErrorIsFor(target error) ErrorAssertionFunc {
return func(t TestingT, err error, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}

return ErrorIs(t, err, target, msgAndArgs...)
}
}

/*
Helper functions
*/
Expand Down
22 changes: 20 additions & 2 deletions assert/assertions_test.go
Expand Up @@ -2750,7 +2750,13 @@ func ExampleErrorAssertionFunc() {
t := &testing.T{} // provided by test

dumbParseNum := func(input string, v interface{}) error {
return json.Unmarshal([]byte(input), v)

err := json.Unmarshal([]byte(input), v)
if err != nil {
return testingError{"could not Unmarshal " + input}
}

return nil
}

tests := []struct {
Expand All @@ -2760,8 +2766,8 @@ func ExampleErrorAssertionFunc() {
}{
{"1.2 is number", "1.2", NoError},
{"1.2.3 not number", "1.2.3", Error},
{"true is not number", "true", Error},
{"3 is number", "3", NoError},
{"3% is not a valid number", "3%", ErrorIsFor(testingError{"could not Unmarshal 3%"})},
}

for _, tt := range tests {
Expand All @@ -2772,14 +2778,26 @@ func ExampleErrorAssertionFunc() {
}
}

type testingError struct {
extraInfo string
}

func (t testingError) Error() string {
return t.extraInfo
}

func TestErrorAssertionFunc(t *testing.T) {
var testError = errors.New("test error")
tests := []struct {
name string
err error
assertion ErrorAssertionFunc
}{
{"noError", nil, NoError},
{"error", errors.New("whoops"), Error},
{"errorIs", testError, ErrorIsFor(testError)},
{"wrappedErrorIs", fmt.Errorf("This wrapped error: %w", testError),
ErrorIsFor(testError)},
}

for _, tt := range tests {
Expand Down
11 changes: 11 additions & 0 deletions require/requirements.go
Expand Up @@ -26,4 +26,15 @@ type BoolAssertionFunc func(TestingT, bool, ...interface{})
// for table driven tests.
type ErrorAssertionFunc func(TestingT, error, ...interface{})

// ErrorIsFunc returns an [ErrorAssertionFunc] which tests if the error wraps target.
func ErrorIsFor(expectedError error) ErrorAssertionFunc {
return func(t TestingT, err error, msgsAndArgs ...interface{}) {
if h, ok := t.(tHelper); ok {
h.Helper()
}

ErrorIs(t, err, expectedError, msgsAndArgs...)
}
}

//go:generate sh -c "cd ../_codegen && go build && cd - && ../_codegen/_codegen -output-package=require -template=require.go.tmpl -include-format-funcs"
5 changes: 5 additions & 0 deletions require/requirements_test.go
Expand Up @@ -3,6 +3,7 @@ package require
import (
"encoding/json"
"errors"
"fmt"
"testing"
"time"
)
Expand Down Expand Up @@ -666,13 +667,17 @@ func ExampleErrorAssertionFunc() {
}

func TestErrorAssertionFunc(t *testing.T) {
var testError = errors.New("test error")
tests := []struct {
name string
err error
assertion ErrorAssertionFunc
}{
{"noError", nil, NoError},
{"error", errors.New("whoops"), Error},
{"errorIs", testError, ErrorIsFor(testError)},
JERHAV marked this conversation as resolved.
Show resolved Hide resolved
{"wrappedErrorIs", fmt.Errorf("This wrapped error: %w", testError),
ErrorIsFor(testError)},
}

for _, tt := range tests {
Expand Down