Skip to content

Instantly share code, notes, and snippets.

@SemanticallyNull
Created February 17, 2022 12:32
Show Gist options
  • Save SemanticallyNull/9d706fea155f0247836cf313509bf5fb to your computer and use it in GitHub Desktop.
Save SemanticallyNull/9d706fea155f0247836cf313509bf5fb to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"reflect"
"strings"
"testing"
)
type TestCases struct{}
func (TestCases) Test_Foo(t *testing.T) {
assertEqual(t, 1, "Foo")
}
func (TestCases) Test_Bar(t *testing.T) {
assertEqual(t, "Bar", "Bar")
}
func (t *TestCases) All() (it []testing.InternalTest) {
caseT := reflect.TypeOf(t)
for i := 0; i < caseT.NumMethod(); i++ {
method := caseT.Method(i)
if strings.HasPrefix(method.Name, "Test_") {
it = append(it, testing.InternalTest{
Name: method.Name,
F: func(t *testing.T) {
method.Func.Call([]reflect.Value{
reflect.ValueOf(&TestCases{}),
reflect.ValueOf(t),
})
},
})
}
}
return it
}
func assertEqual(t *testing.T, expected, actual interface{}) {
if expected != actual {
t.Log(fmt.Sprintf("expected %v but got %v\n", expected, actual))
t.Fail()
}
}
func main() {
t := TestCases{}
testing.Main(runAll,
t.All(),
[]testing.InternalBenchmark{},
[]testing.InternalExample{})
}
func runAll(pat, str string) (bool, error) {
return true, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment