-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoception.go
45 lines (38 loc) · 853 Bytes
/
goception.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package goception
import (
"reflect"
"testing"
)
type Suite interface {
Before(t *testing.T)
After(t *testing.T)
}
type test struct {
method string
f func(*testing.T)
}
func RunSuites(t *testing.T, suites ...Suite) {
for _, suite := range suites {
suiteType := reflect.TypeOf(suite)
tests := make([]test, 0)
for i := 0; i < suiteType.NumMethod(); i++ {
method := suiteType.Method(i)
if isTestMethod(method) {
tests = append(tests, test{
method: method.Name,
f: func(t *testing.T) {
suite.Before(t)
method.Func.Call([]reflect.Value{reflect.ValueOf(suite), reflect.ValueOf(t)})
suite.After(t)
},
})
}
}
for _, tst := range tests {
t.Run(tst.method, tst.f)
}
}
}
func isTestMethod(method reflect.Method) bool {
return len(method.Name) > 4 && method.Name[:4] == "Test"
}