This repository has been archived by the owner on Sep 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
301 lines (265 loc) · 6.32 KB
/
main.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Package tap implements a basic parser for the Test Anything Protocol
package tap
import (
"bufio"
"fmt"
"io"
"regexp"
"strconv"
"strings"
)
var (
planRE = regexp.MustCompile(`^1..(\d+)$`)
versionRE = regexp.MustCompile(`^TAP version (\d+)$`)
testlineRE = regexp.MustCompile(`^(not )?ok(?:\s+(\d+)\s*)?(?:\s*([\D\S][^#]+?)\s*)?(?i:#\s+(todo|skip)(?:\s+(.*))?)?$`)
diagnosticsRE = regexp.MustCompile(`^\s*#\s+(.*)$`)
yamlStartRE = regexp.MustCompile(`\s*---\s*$`)
yamlEndRE = regexp.MustCompile(`\s*...\s*$`)
)
// A TAP-Directive (Todo/Skip)
//
// Deprecated: Project is unmaintained.
type Directive int
const (
None Directive = iota // No directive given
Todo // Testpoint is a TODO
Skip // Testpoint was skipped
)
// Deprecated: Project is unmaintained.
func (d Directive) String() string {
switch d {
case None:
return "None"
case Todo:
return "TODO"
case Skip:
return "SKIP"
}
return ""
}
// A single TAP-Testline
//
// Deprecated: Project is unmaintained.
type Testline struct {
Ok bool // Whether the Testpoint executed ok
Num uint // The number of the test
Description string // A short description
Directive Directive // Whether the test was skipped or is a todo
Explanation string // A short explanation why the test was skipped/is a todo
Diagnostic string // A more detailed diagnostic message about the failed test
Yaml []byte // The inline Yaml-document, if given
}
// The outcome of a Testsuite
//
// Deprecated: Project is unmaintained.
type Testsuite struct {
Ok bool // Whether the Testsuite as a whole succeded
Tests []*Testline // Description of all Testlines
plan int // Number of tests intended to run
}
// Parses TAP
//
// Deprecated: Project is unmaintained.
type Parser struct {
r *bufio.Reader
line string
suite Testsuite
}
func (p *Parser) parseLine(line string) (*Testline, error) {
var err error
matches := testlineRE.FindStringSubmatch(line)
if matches == nil {
return nil, fmt.Errorf("Does not match Testline: \"%s\"", line)
}
t := new(Testline)
t.Ok = (len(matches[1]) == 0)
if len(matches[2]) > 0 {
var i int
i, err = strconv.Atoi(matches[2])
if err != nil {
return nil, fmt.Errorf("Could not parse Testnumber \"%s\"", matches[2])
}
t.Num = uint(i)
}
t.Description = matches[3]
switch strings.ToLower(matches[4]) {
case "":
t.Directive = None
case "todo":
t.Directive = Todo
case "skip":
t.Directive = Skip
}
t.Explanation = matches[5]
return t, nil
}
// Create a new TAP-Parser from the given reader
//
// Deprecated: Project is unmaintained.
func NewParser(r io.Reader) (*Parser, error) {
p := &Parser{bufio.NewReader(r), "", Testsuite{true, nil, -1}}
line, err := p.r.ReadString('\n')
if err != nil {
return nil, err
}
line = strings.TrimSpace(line)
if versionRE.MatchString(line) {
line, err = p.r.ReadString('\n')
if err != nil {
return nil, err
}
line = strings.TrimSpace(line)
}
var matches []string
if matches = planRE.FindStringSubmatch(line); matches != nil {
i, err := strconv.Atoi(matches[1])
if err != nil {
return nil, fmt.Errorf("Could not parse plan \"%s\": %s", matches[0], err)
}
p.suite.plan = i
line, err = p.r.ReadString('\n')
if err != nil && err != io.EOF {
return nil, err
}
line = strings.TrimSpace(line)
}
p.line = line
return p, nil
}
// Get the next Testline
//
// Deprecated: Project is unmaintained.
func (p *Parser) Next() (*Testline, error) {
if len(p.line) == 0 {
return nil, io.EOF
}
t, err := p.parseLine(p.line)
if err != nil {
return nil, err
}
p.line = ""
var line string
for {
line, err = p.r.ReadString('\n')
switch err {
case nil:
case io.EOF:
if len(line) == 0 {
p.suite.Tests = append(p.suite.Tests, t)
return t, nil
}
default:
return nil, err
}
line = strings.TrimSpace(line)
var matches []string
if matches = diagnosticsRE.FindStringSubmatch(line); matches != nil {
if len(t.Diagnostic) == 0 {
t.Diagnostic = matches[1]
} else {
t.Diagnostic = t.Diagnostic + "\n" + matches[1]
}
continue
}
if yamlStartRE.MatchString(line) {
for {
yaml, err := p.r.ReadBytes('\n')
switch err {
case nil:
case io.EOF:
if len(line) == 0 {
p.suite.Tests = append(p.suite.Tests, t)
return t, nil
}
default:
return nil, err
}
if yamlEndRE.Match(yaml) {
break
}
buf := make([]byte, len(t.Yaml)+len(yaml))
copy(buf[:len(t.Yaml)], t.Yaml)
copy(buf[len(t.Yaml):], yaml)
}
continue
}
if matches = planRE.FindStringSubmatch(line); matches != nil {
if p.suite.plan != -1 {
return nil, fmt.Errorf("Double plan")
}
i, err := strconv.Atoi(matches[1])
if err != nil {
return nil, fmt.Errorf("Could not parse plan \"%s\": %s", matches[0], err)
}
p.suite.plan = i
p.suite.Tests = append(p.suite.Tests, t)
return t, nil
}
break
}
p.line = line
p.suite.Tests = append(p.suite.Tests, t)
return t, nil
}
// Get the whole Testsuite.
// This will block until the underlying reader reaches EOF or there is an error.
//
// Deprecated: Project is unmaintained.
func (p *Parser) Suite() (*Testsuite, error) {
for {
t, err := p.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if !t.Ok {
p.suite.Ok = false
}
}
if p.suite.plan == 0 {
p.suite.Ok = false
return &p.suite, nil
}
if len(p.suite.Tests) != p.suite.plan {
p.suite.Ok = false
return &p.suite, nil
}
return &p.suite, nil
}
// Summarizes the Testline into a short String.
//
// The string will be formatted as followed: First a status (ok/fail/todo/skip)
// in […], then the description of the test, last the diagnostic-message, if
// the test failed and had such a message attached to it, or an explanation, if
// the test had a TODO/SKIP directive with an explanation attached.
//
// Deprecated: Project is unmaintained.
func (t *Testline) String() string {
s := "["
switch t.Directive {
case None:
if t.Ok {
s += " ok "
} else {
s += "fail"
}
case Todo:
s += "todo"
case Skip:
s += "skip"
}
s += "] "
s += t.Description
if t.Directive != None {
if len(t.Explanation) > 0 {
s += " # " + t.Explanation
}
} else if !t.Ok {
if len(t.Diagnostic) > 0 {
s += " # " + t.Diagnostic
}
}
return s
}