Skip to content

Instantly share code, notes, and snippets.

@tmichel
Created September 26, 2025 07:37
Show Gist options
  • Save tmichel/7aba759c8d2ce6af2b69d26590353579 to your computer and use it in GitHub Desktop.
Save tmichel/7aba759c8d2ce6af2b69d26590353579 to your computer and use it in GitHub Desktop.
package assert
import (
"errors"
"reflect"
"regexp"
"testing"
)
func Equal[T any](t *testing.T, got, want T) {
t.Helper()
if !isEqual(got, want) {
t.Errorf("got: %v; want: %v", got, want)
}
}
func NotEqual[T any](t *testing.T, got, want T) {
t.Helper()
if isEqual(got, want) {
t.Errorf("got: %v; expected values to be different", got)
}
}
func True(t *testing.T, got bool) {
t.Helper()
if !got {
t.Errorf("got: false; want: true")
}
}
func False(t *testing.T, got bool) {
t.Helper()
if got {
t.Errorf("got: true; want: false")
}
}
func Nil(t *testing.T, got any) {
t.Helper()
if !isNil(got) {
t.Errorf("got: %v; want: nil", got)
}
}
func NotNil(t *testing.T, got any) {
t.Helper()
if isNil(got) {
t.Errorf("got: nil; want: non-nil")
}
}
func ErrorIs(t *testing.T, got, want error) {
t.Helper()
if !errors.Is(got, want) {
t.Errorf("got: %v; want: %v", got, want)
}
}
func ErrorAs(t *testing.T, got error, target any) {
t.Helper()
if got == nil {
t.Errorf("got: nil; want assignable to: %T", target)
return
}
if !errors.As(got, target) {
t.Errorf("got: %v; want assignable to: %T", got, target)
}
}
func MatchesRegexp(t *testing.T, got, pattern string) {
t.Helper()
matched, err := regexp.MatchString(pattern, got)
if err != nil {
t.Fatalf("unable to parse regexp pattern %s: %s", pattern, err.Error())
return
}
if !matched {
t.Errorf("got: %q; want to match %q", got, pattern)
}
}
func isEqual[T any](got, want T) bool {
if isNil(got) && isNil(want) {
return true
}
if equalable, ok := any(got).(interface{ Equal(T) bool }); ok {
return equalable.Equal(want)
}
return reflect.DeepEqual(got, want)
}
func isNil(v any) bool {
if v == nil {
return true
}
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice, reflect.UnsafePointer:
return rv.IsNil()
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment