forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
272 lines (223 loc) · 7.01 KB
/
context.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package convey
import (
"fmt"
"github.com/jtolds/gls"
"github.com/smartystreets/goconvey/convey/reporting"
)
type conveyErr struct {
fmt string
params []interface{}
}
func (e *conveyErr) Error() string {
return fmt.Sprintf(e.fmt, e.params...)
}
func conveyPanic(fmt string, params ...interface{}) {
panic(&conveyErr{fmt, params})
}
const (
missingGoTest = `Top-level calls to Convey(...) need a reference to the *testing.T.
Hint: Convey("description here", t, func() { /* notice that the second argument was the *testing.T (t)! */ }) `
extraGoTest = `Only the top-level call to Convey(...) needs a reference to the *testing.T.`
noStackContext = "Convey operation made without context on goroutine stack.\n" +
"Hint: Perhaps you meant to use `Convey(..., func(c C){...})` ?"
differentConveySituations = "Different set of Convey statements on subsequent pass!\nDid not expect %#v."
multipleIdenticalConvey = "Multiple convey suites with identical names: %#v"
)
const (
failureHalt = "___FAILURE_HALT___"
nodeKey = "node"
)
///////////////////////////////// Stack Context /////////////////////////////////
func getCurrentContext() *context {
ctx, ok := ctxMgr.GetValue(nodeKey)
if ok {
return ctx.(*context)
}
return nil
}
func mustGetCurrentContext() *context {
ctx := getCurrentContext()
if ctx == nil {
conveyPanic(noStackContext)
}
return ctx
}
//////////////////////////////////// Context ////////////////////////////////////
// context magically handles all coordination of Convey's and So assertions.
//
// It is tracked on the stack as goroutine-local-storage with the gls package,
// or explicitly if the user decides to call convey like:
//
// Convey(..., func(c C) {
// c.So(...)
// })
//
// This implements the `C` interface.
type context struct {
reporter reporting.Reporter
children map[string]*context
resets []func()
executedOnce bool
expectChildRun *bool
complete bool
focus bool
failureMode FailureMode
}
// rootConvey is the main entry point to a test suite. This is called when
// there's no context in the stack already, and items must contain a `t` object,
// or this panics.
func rootConvey(items ...interface{}) {
entry := discover(items)
if entry.Test == nil {
conveyPanic(missingGoTest)
}
expectChildRun := true
ctx := &context{
reporter: buildReporter(),
children: make(map[string]*context),
expectChildRun: &expectChildRun,
focus: entry.Focus,
failureMode: defaultFailureMode.combine(entry.FailMode),
}
ctxMgr.SetValues(gls.Values{nodeKey: ctx}, func() {
ctx.reporter.BeginStory(reporting.NewStoryReport(entry.Test))
defer ctx.reporter.EndStory()
for ctx.shouldVisit() {
ctx.conveyInner(entry.Situation, entry.Func)
expectChildRun = true
}
})
}
//////////////////////////////////// Methods ////////////////////////////////////
func (ctx *context) SkipConvey(items ...interface{}) {
ctx.Convey(items, skipConvey)
}
func (ctx *context) FocusConvey(items ...interface{}) {
ctx.Convey(items, focusConvey)
}
func (ctx *context) Convey(items ...interface{}) {
entry := discover(items)
// we're a branch, or leaf (on the wind)
if entry.Test != nil {
conveyPanic(extraGoTest)
}
if ctx.focus && !entry.Focus {
return
}
var inner_ctx *context
if ctx.executedOnce {
var ok bool
inner_ctx, ok = ctx.children[entry.Situation]
if !ok {
conveyPanic(differentConveySituations, entry.Situation)
}
} else {
if _, ok := ctx.children[entry.Situation]; ok {
conveyPanic(multipleIdenticalConvey, entry.Situation)
}
inner_ctx = &context{
reporter: ctx.reporter,
children: make(map[string]*context),
expectChildRun: ctx.expectChildRun,
focus: entry.Focus,
failureMode: ctx.failureMode.combine(entry.FailMode),
}
ctx.children[entry.Situation] = inner_ctx
}
if inner_ctx.shouldVisit() {
ctxMgr.SetValues(gls.Values{nodeKey: inner_ctx}, func() {
inner_ctx.conveyInner(entry.Situation, entry.Func)
})
}
}
func (ctx *context) SkipSo(stuff ...interface{}) {
ctx.assertionReport(reporting.NewSkipReport())
}
func (ctx *context) So(actual interface{}, assert assertion, expected ...interface{}) {
if result := assert(actual, expected...); result == assertionSuccess {
ctx.assertionReport(reporting.NewSuccessReport())
} else {
ctx.assertionReport(reporting.NewFailureReport(result))
}
}
func (ctx *context) Reset(action func()) {
/* TODO: Failure mode configuration */
ctx.resets = append(ctx.resets, action)
}
func (ctx *context) Print(items ...interface{}) (int, error) {
fmt.Fprint(ctx.reporter, items...)
return fmt.Print(items...)
}
func (ctx *context) Println(items ...interface{}) (int, error) {
fmt.Fprintln(ctx.reporter, items...)
return fmt.Println(items...)
}
func (ctx *context) Printf(format string, items ...interface{}) (int, error) {
fmt.Fprintf(ctx.reporter, format, items...)
return fmt.Printf(format, items...)
}
//////////////////////////////////// Private ////////////////////////////////////
// shouldVisit returns true iff we should traverse down into a Convey. Note
// that just because we don't traverse a Convey this time, doesn't mean that
// we may not traverse it on a subsequent pass.
func (c *context) shouldVisit() bool {
return !c.complete && *c.expectChildRun
}
// conveyInner is the function which actually executes the user's anonymous test
// function body. At this point, Convey or RootConvey has decided that this
// function should actually run.
func (ctx *context) conveyInner(situation string, f func(C)) {
// Record/Reset state for next time.
defer func() {
ctx.executedOnce = true
// This is only needed at the leaves, but there's no harm in also setting it
// when returning from branch Convey's
*ctx.expectChildRun = false
}()
// Set up+tear down our scope for the reporter
ctx.reporter.Enter(reporting.NewScopeReport(situation))
defer ctx.reporter.Exit()
// Recover from any panics in f, and assign the `complete` status for this
// node of the tree.
defer func() {
ctx.complete = true
if problem := recover(); problem != nil {
if problem, ok := problem.(*conveyErr); ok {
panic(problem)
}
if problem != failureHalt {
ctx.reporter.Report(reporting.NewErrorReport(problem))
}
} else {
for _, child := range ctx.children {
if !child.complete {
ctx.complete = false
return
}
}
}
}()
// Resets are registered as the `f` function executes, so nil them here.
// All resets are run in registration order (FIFO).
ctx.resets = []func(){}
defer func() {
for _, r := range ctx.resets {
// panics handled by the previous defer
r()
}
}()
if f == nil {
// if f is nil, this was either a Convey(..., nil), or a SkipConvey
ctx.reporter.Report(reporting.NewSkipReport())
} else {
f(ctx)
}
}
// assertionReport is a helper for So and SkipSo which makes the report and
// then possibly panics, depending on the current context's failureMode.
func (ctx *context) assertionReport(r *reporting.AssertionResult) {
ctx.reporter.Report(r)
if r.Failure != "" && ctx.failureMode == FailureHalts {
panic(failureHalt)
}
}