-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsuiteinfo.go
149 lines (115 loc) · 2.55 KB
/
suiteinfo.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
package barrier
import (
"fmt"
"hash/fnv"
"io"
)
// suiteInfo is runtime information for the suite
type suiteInfo struct {
Name string
Description string
Setup SetupSuiteFunction
tests testsMap
writer io.Writer
data interface{}
stash interface{}
}
// SuiteInfo is the interface for the test writer
type SuiteInfo interface {
// Register a test into a suite
RegisterTest(t Test)
// Write performs a write
Write(p []byte) (n int, err error)
}
// RegisterTest register a test in the main suite.
func (s *suiteInfo) RegisterTest(t Test) {
if t.Name == "" {
panic("test is missing name")
}
if t.Description == "" {
panic("test is missing description")
}
if s.Name != defaultSuiteName {
if t.SuiteName != "" {
panic("suite name in a test can not be provided while registering it as a part of a suite")
}
t.SuiteName = s.Name
}
if t.Author == "" {
panic("test is missing author")
}
if t.Function == nil {
panic("test is missing function")
}
if len(t.Tags) == 0 {
panic("test is missing tags")
}
h := fnv.New32()
if _, err := h.Write([]byte(s.Name + s.Description + t.Name + t.Description + t.Author)); err != nil {
panic(err)
}
t.id = fmt.Sprintf("%x", h.Sum32())
if _, ok := s.tests[t.Name]; ok {
panic("a test of the same name was previously registered: " + t.Name)
}
s.tests[t.Name] = t
}
// Write performs a write
func (s *suiteInfo) Write(p []byte) (n int, err error) {
return s.writer.Write(p)
}
func (s *suiteInfo) testsWithArgs(verbose, matchAll bool, tags []string) *suiteInfo {
ts := testsMap{}
if verbose {
fmt.Println("Running Tests:")
}
for _, t := range s.tests {
if !t.matchTags(tags, matchAll) {
continue
}
if verbose {
fmt.Println(" - " + t.Name)
}
ts[t.Name] = t
}
if verbose && len(ts) == 0 {
fmt.Println("No matching tests found.")
}
s.tests = ts
return s
}
func (s *suiteInfo) testsWithIDs(verbose bool, ids []string) *suiteInfo {
if len(ids) == 0 {
return s
}
ts := testsMap{}
if verbose {
fmt.Println("Running Tests:")
}
for _, t := range s.tests {
for _, id := range ids {
if id == t.id {
if verbose {
fmt.Println(" - " + t.Name)
}
ts[t.Name] = t
}
}
}
if verbose && len(ts) == 0 {
fmt.Println("No matching tests found.")
}
s.tests = ts
return s
}
func (s *suiteInfo) String() string {
return fmt.Sprintf(`suite name : %s
suite desc : %s
`, s.Name, s.Description)
}
func (s *suiteInfo) listTests() {
fmt.Printf("%s\n", s)
for _, test := range s.tests.sorted() {
fmt.Printf("%s\n", test)
}
}