// Package testing provides a testing object T // with functions for classical unit testing. package testing import ( "regexp" "testing" ) type Map map[string]interface{} type T struct { Base *testing.T } func MakeT(tbase *testing.T) *T { return &T{ Base: tbase, } } func SetUp(tbase *testing.T) *T { t := MakeT(tbase) return t } // testing methods func (t *T) Run(name string, f func(*T)) bool { return t.Base.Run(name, func(_ *testing.T) { f(t) }) } func (t *T) LogErr(txt string, fields Map) { t.Base.Helper() t.Base.Errorf("%v, %+v", txt, fields) } func (t *T) AssertEqual(have interface{}, want interface{}) { t.Base.Helper() if have != want { t.LogErr("AssertEqual", Map{"have": have, "want": want}) } } func (t *T) AssertMatch(have string, want string) { t.Base.Helper() match, _ := regexp.MatchString(Normalize(want), Normalize(have)) if !match { t.LogErr("AssertMatch", Map{"have": have, "want": want}) } } // testing functions: generic functions may not be methods func AssertOneOf[V comparable](t *T, have V, want []V) { t.Base.Helper() for _, w := range want { if have == w { return } } t.LogErr("AssertOneOf", Map{"have": have, "want": want}) } // helpers func Normalize(s string) string { re, _ := regexp.Compile(`\s*`) return re.ReplaceAllString(s, "") }