Skip to content

feat: added IsError test #11

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions assert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package assert

import (
"errors"
"fmt"
"path"
"reflect"
Expand Down Expand Up @@ -166,6 +167,23 @@ func NotEqualSkip(t testing.TB, skip int, val1, val2 interface{}) {
t.FailNow()
}
}
// IsErrorSkip validates that any error in err's chain matches target
// and throws an error with line number
// but the skip variable tells IsErrorSkip how far back on the stack to report the error.
// This is a building block to creating your own more complex validation functions.
func IsErrorSkip(t testing.TB, skip int, err, target error) {
if !errors.Is(err, target) {
_, file, line, _ := runtime.Caller(skip)
fmt.Printf("%s:%d %v does not is %v\n", path.Base(file), line, err, target)
t.FailNow()
}
}

// IsError validates that any error in err's chain matches target
// and throws an error with line number
func IsError(t testing.TB, err, target error) {
IsErrorSkip(t, 2, err, target)
}

// PanicMatches validates that the panic output of running fn matches the supplied string
func PanicMatches(t testing.TB, fn func(), matches string) {
Expand Down
7 changes: 7 additions & 0 deletions assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package assert
import (
"errors"
"testing"
"fmt"
)

// NOTES:
Expand Down Expand Up @@ -90,3 +91,9 @@ func TestEquals(t *testing.T) {
Equal(t, iface, 1)
NotEqual(t, iface, iface2)
}

func TestIsError(t *testing.T) {
err1 := errors.New("unknown error")
errWrapped := fmt.Errorf("another error: %w", err1)
IsError(t, errWrapped, err1)
}