File: test_case.go

package info (click to toggle)
golang-github-smartystreets-gunit 1.2.0%2Bgit20180314.6f0d627-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 200 kB
  • sloc: makefile: 5
file content (93 lines) | stat: -rw-r--r-- 2,315 bytes parent folder | download | duplicates (2)
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
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, parallel bool, positions scan.TestCasePositions) *testCase {
	return &testCase{
		parallel:    parallel,
		methodIndex: methodIndex,
		description: method.name,
		skipped:     method.isSkippedTest,
		long:        method.isLongTest,
		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) {
	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) {
	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)
	}
}