forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery.go
103 lines (94 loc) · 2.39 KB
/
discovery.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
package convey
type actionSpecifier uint8
const (
noSpecifier actionSpecifier = iota
skipConvey
focusConvey
)
type suite struct {
Situation string
Test t
Focus bool
Func func(C) // nil means skipped
FailMode FailureMode
}
func newSuite(situation string, failureMode FailureMode, f func(C), test t, specifier actionSpecifier) *suite {
ret := &suite{
Situation: situation,
Test: test,
Func: f,
FailMode: failureMode,
}
switch specifier {
case skipConvey:
ret.Func = nil
case focusConvey:
ret.Focus = true
}
return ret
}
func discover(items []interface{}) *suite {
name, items := parseName(items)
test, items := parseGoTest(items)
failure, items := parseFailureMode(items)
action, items := parseAction(items)
specifier, items := parseSpecifier(items)
if len(items) != 0 {
conveyPanic(parseError)
}
return newSuite(name, failure, action, test, specifier)
}
func item(items []interface{}) interface{} {
if len(items) == 0 {
conveyPanic(parseError)
}
return items[0]
}
func parseName(items []interface{}) (string, []interface{}) {
if name, parsed := item(items).(string); parsed {
return name, items[1:]
}
conveyPanic(parseError)
panic("never get here")
}
func parseGoTest(items []interface{}) (t, []interface{}) {
if test, parsed := item(items).(t); parsed {
return test, items[1:]
}
return nil, items
}
func parseFailureMode(items []interface{}) (FailureMode, []interface{}) {
if mode, parsed := item(items).(FailureMode); parsed {
return mode, items[1:]
}
return FailureInherits, items
}
func parseAction(items []interface{}) (func(C), []interface{}) {
switch x := item(items).(type) {
case nil:
return nil, items[1:]
case func(C):
return x, items[1:]
case func():
return func(C) { x() }, items[1:]
}
conveyPanic(parseError)
panic("never get here")
}
func parseSpecifier(items []interface{}) (actionSpecifier, []interface{}) {
if len(items) == 0 {
return noSpecifier, items
}
if spec, ok := items[0].(actionSpecifier); ok {
return spec, items[1:]
}
conveyPanic(parseError)
panic("never get here")
}
// This interface allows us to pass the *testing.T struct
// throughout the internals of this package without ever
// having to import the "testing" package.
type t interface {
Fail()
}
const parseError = "You must provide a name (string), then a *testing.T (if in outermost scope), an optional FailureMode, and then an action (func())."