/
test_case.go
97 lines (83 loc) · 2.37 KB
/
test_case.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
93
94
95
96
97
package gunit
import (
"reflect"
"testing"
"github.com/smartystreets/gunit/scan"
)
type testCase struct {
methodIndex int
description string
skipped bool
long bool
parallel bool
setup int
teardown int
innerFixture *Fixture
outerFixtureType reflect.Type
outerFixture reflect.Value
positions scan.TestCasePositions
}
func newTestCase(methodIndex int, method fixtureMethodInfo, config configuration, positions scan.TestCasePositions) *testCase {
return &testCase{
parallel: config.ParallelTestCases(),
methodIndex: methodIndex,
description: method.name,
skipped: method.isSkippedTest || config.SkippedTestCases,
long: method.isLongTest || config.LongRunningTestCases,
positions: positions,
}
}
func (this *testCase) Prepare(setup, teardown int, outerFixtureType reflect.Type) {
this.setup = setup
this.teardown = teardown
this.outerFixtureType = outerFixtureType
}
func (this *testCase) Run(t *testing.T) {
t.Helper()
if this.skipped {
t.Run(this.description, this.skip)
} else if this.long && testing.Short() {
t.Run(this.description, this.skipLong)
} else {
t.Run(this.description, this.run)
}
}
func (this *testCase) skip(innerT *testing.T) {
innerT.Skip("\n" + this.positions[innerT.Name()])
}
func (this *testCase) skipLong(innerT *testing.T) {
innerT.Skipf("Skipped long-running test:\n" + this.positions[innerT.Name()])
}
func (this *testCase) run(innerT *testing.T) {
innerT.Helper()
if this.parallel {
innerT.Parallel()
}
this.initializeFixture(innerT)
defer this.innerFixture.finalize()
this.runWithSetupAndTeardown()
}
func (this *testCase) initializeFixture(innerT *testing.T) {
innerT.Log("Test definition:\n" + this.positions[innerT.Name()])
this.innerFixture = newFixture(innerT, testing.Verbose())
this.outerFixture = reflect.New(this.outerFixtureType.Elem())
this.outerFixture.Elem().FieldByName("Fixture").Set(reflect.ValueOf(this.innerFixture))
}
func (this *testCase) runWithSetupAndTeardown() {
this.runSetup()
defer this.runTeardown()
this.runTest()
}
func (this *testCase) runSetup() {
if this.setup >= 0 {
this.outerFixture.Method(this.setup).Call(nil)
}
}
func (this *testCase) runTest() {
this.outerFixture.Method(this.methodIndex).Call(nil)
}
func (this *testCase) runTeardown() {
if this.teardown >= 0 {
this.outerFixture.Method(this.teardown).Call(nil)
}
}