forked from onsi/ginkgo
/
ginkgo.go
177 lines (138 loc) · 5.24 KB
/
ginkgo.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package ginkgo
import (
"github.com/onsi/ginkgo/config"
"github.com/onsi/ginkgo/types"
"testing"
"time"
)
const GINKGO_VERSION = config.VERSION
const defaultTimeout = 1
var globalSuite *suite
func init() {
config.Flags("ginkgo", true)
globalSuite = newSuite()
}
type Reporter interface {
SpecSuiteWillBegin(config config.GinkgoConfigType, summary *types.SuiteSummary)
ExampleWillRun(exampleSummary *types.ExampleSummary)
ExampleDidComplete(exampleSummary *types.ExampleSummary)
SpecSuiteDidEnd(summary *types.SuiteSummary)
}
type Done chan<- interface{}
type Benchmarker interface {
Time(name string, body func(), info ...interface{}) (elapsedTime time.Duration)
RecordValue(name string, value float64, info ...interface{})
}
func RunSpecs(t *testing.T, description string) {
globalSuite.run(t, description, []Reporter{newDefaultReporter(config.DefaultReporterConfig)}, config.GinkgoConfig)
}
func RunSpecsWithDefaultAndCustomReporters(t *testing.T, description string, reporters []Reporter) {
reporters = append([]Reporter{newDefaultReporter(config.DefaultReporterConfig)}, reporters...)
globalSuite.run(t, description, reporters, config.GinkgoConfig)
}
func RunSpecsWithCustomReporters(t *testing.T, description string, reporters []Reporter) {
globalSuite.run(t, description, reporters, config.GinkgoConfig)
}
func Fail(message string, callerSkip ...int) {
skip := 0
if len(callerSkip) > 0 {
skip = callerSkip[0]
}
globalSuite.fail(message, skip)
}
//Describes
func Describe(text string, body func()) bool {
globalSuite.pushContainerNode(text, body, flagTypeNone, types.GenerateCodeLocation(1))
return true
}
func FDescribe(text string, body func()) bool {
globalSuite.pushContainerNode(text, body, flagTypeFocused, types.GenerateCodeLocation(1))
return true
}
func PDescribe(text string, body func()) bool {
globalSuite.pushContainerNode(text, body, flagTypePending, types.GenerateCodeLocation(1))
return true
}
func XDescribe(text string, body func()) bool {
globalSuite.pushContainerNode(text, body, flagTypePending, types.GenerateCodeLocation(1))
return true
}
//Context
func Context(text string, body func()) bool {
globalSuite.pushContainerNode(text, body, flagTypeNone, types.GenerateCodeLocation(1))
return true
}
func FContext(text string, body func()) bool {
globalSuite.pushContainerNode(text, body, flagTypeFocused, types.GenerateCodeLocation(1))
return true
}
func PContext(text string, body func()) bool {
globalSuite.pushContainerNode(text, body, flagTypePending, types.GenerateCodeLocation(1))
return true
}
func XContext(text string, body func()) bool {
globalSuite.pushContainerNode(text, body, flagTypePending, types.GenerateCodeLocation(1))
return true
}
//It
func It(text string, body interface{}, timeout ...float64) bool {
globalSuite.pushItNode(text, body, flagTypeNone, types.GenerateCodeLocation(1), parseTimeout(timeout...))
return true
}
func FIt(text string, body interface{}, timeout ...float64) bool {
globalSuite.pushItNode(text, body, flagTypeFocused, types.GenerateCodeLocation(1), parseTimeout(timeout...))
return true
}
func PIt(text string, body interface{}, timeout ...float64) bool {
globalSuite.pushItNode(text, body, flagTypePending, types.GenerateCodeLocation(1), parseTimeout(timeout...))
return true
}
func XIt(text string, body interface{}, timeout ...float64) bool {
globalSuite.pushItNode(text, body, flagTypePending, types.GenerateCodeLocation(1), parseTimeout(timeout...))
return true
}
//Benchmark
func Measure(text string, body func(Benchmarker), samples int) bool {
globalSuite.pushMeasureNode(text, body, flagTypeNone, types.GenerateCodeLocation(1), samples)
return true
}
func FMeasure(text string, body func(Benchmarker), samples int) bool {
globalSuite.pushMeasureNode(text, body, flagTypeFocused, types.GenerateCodeLocation(1), samples)
return true
}
func PMeasure(text string, body func(Benchmarker), samples int) bool {
globalSuite.pushMeasureNode(text, body, flagTypePending, types.GenerateCodeLocation(1), samples)
return true
}
func XMeasure(text string, body func(Benchmarker), samples int) bool {
globalSuite.pushMeasureNode(text, body, flagTypePending, types.GenerateCodeLocation(1), samples)
return true
}
//Before, JustBefore, and After
func BeforeEach(body interface{}, timeout ...float64) bool {
globalSuite.pushBeforeEachNode(body, types.GenerateCodeLocation(1), parseTimeout(timeout...))
return true
}
func BeforeAll(body interface{}, timeout ...float64) bool {
globalSuite.pushBeforeAllNode(body, types.GenerateCodeLocation(1), parseTimeout(timeout...))
return true
}
func JustBeforeEach(body interface{}, timeout ...float64) bool {
globalSuite.pushJustBeforeEachNode(body, types.GenerateCodeLocation(1), parseTimeout(timeout...))
return true
}
func AfterEach(body interface{}, timeout ...float64) bool {
globalSuite.pushAfterEachNode(body, types.GenerateCodeLocation(1), parseTimeout(timeout...))
return true
}
func AfterAll(body interface{}, timeout ...float64) bool {
globalSuite.pushAfterAllNode(body, types.GenerateCodeLocation(1), parseTimeout(timeout...))
return true
}
func parseTimeout(timeout ...float64) time.Duration {
if len(timeout) == 0 {
return time.Duration(defaultTimeout * int64(time.Second))
} else {
return time.Duration(timeout[0] * float64(time.Second))
}
}