45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
// Package testutil provides lightweight test helpers to reduce boilerplate in
|
|
// table-driven tests. Import it from any _test.go file in this module.
|
|
//
|
|
// Every helper calls t.Helper() so failures are reported at the call site, not
|
|
// inside this package.
|
|
package testutil
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// NoError fails the test immediately if err is not nil.
|
|
func NoError(t *testing.T, err error) {
|
|
t.Helper()
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
// Error fails the test if err is nil.
|
|
func Error(t *testing.T, err error) {
|
|
t.Helper()
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
}
|
|
|
|
// ErrorContains fails the test if err is nil or its message does not contain substr.
|
|
func ErrorContains(t *testing.T, err error, substr string) {
|
|
t.Helper()
|
|
Error(t, err)
|
|
if !strings.Contains(err.Error(), substr) {
|
|
t.Errorf("error %q does not contain %q", err.Error(), substr)
|
|
}
|
|
}
|
|
|
|
// Equal fails the test if got != want.
|
|
func Equal[T comparable](t *testing.T, got, want T) {
|
|
t.Helper()
|
|
if got != want {
|
|
t.Errorf("got %v, want %v", got, want)
|
|
}
|
|
}
|