-
Notifications
You must be signed in to change notification settings - Fork 11
/
fixture_runner.go
92 lines (79 loc) · 2.1 KB
/
fixture_runner.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package gunit
import (
"reflect"
"testing"
"github.com/smarty/gunit/scan"
)
func newFixtureRunner(
fixture any,
outerT *testing.T,
config configuration,
positions scan.TestCasePositions,
) *fixtureRunner {
if config.ParallelFixture() {
outerT.Parallel()
}
return &fixtureRunner{
config: config,
setup: -1,
teardown: -1,
outerT: outerT,
fixtureType: reflect.ValueOf(fixture).Type(),
positions: positions,
}
}
type fixtureRunner struct {
outerT *testing.T
fixtureType reflect.Type
config configuration
setup int
teardown int
focus []*testCase
tests []*testCase
positions scan.TestCasePositions
}
func (this *fixtureRunner) ScanFixtureForTestCases() {
for methodIndex := 0; methodIndex < this.fixtureType.NumMethod(); methodIndex++ {
methodName := this.fixtureType.Method(methodIndex).Name
this.scanFixtureMethod(methodIndex, this.newFixtureMethodInfo(methodName))
}
}
func (this *fixtureRunner) scanFixtureMethod(methodIndex int, method fixtureMethodInfo) {
switch {
case method.isSetup:
this.setup = methodIndex
case method.isTeardown:
this.teardown = methodIndex
case method.isFocusTest:
this.focus = append(this.focus, this.buildTestCase(methodIndex, method))
case method.isTest:
this.tests = append(this.tests, this.buildTestCase(methodIndex, method))
}
}
func (this *fixtureRunner) buildTestCase(methodIndex int, method fixtureMethodInfo) *testCase {
return newTestCase(methodIndex, method, this.config, this.positions)
}
func (this *fixtureRunner) RunTestCases() {
this.outerT.Helper()
if len(this.focus) > 0 {
this.tests = append(this.focus, skipped(this.tests)...)
}
if len(this.tests) > 0 {
this.runTestCases(this.tests)
} else {
this.outerT.Skipf("Fixture (%v) has no test cases.", this.fixtureType)
}
}
func (this *fixtureRunner) runTestCases(cases []*testCase) {
this.outerT.Helper()
for _, test := range cases {
test.Prepare(this.setup, this.teardown, this.fixtureType)
test.Run(this.outerT)
}
}
func skipped(cases []*testCase) []*testCase {
for _, test := range cases {
test.skipped = true
}
return cases
}