-
Notifications
You must be signed in to change notification settings - Fork 52
/
test.go
390 lines (319 loc) · 8.8 KB
/
test.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package main
import (
`bufio`
`bytes`
`flag`
`fmt`
`os`
`os/exec`
`path/filepath`
`regexp`
`strconv`
`strings`
)
var green = "\u001b[32m"
var magenta = "\u001b[35m"
var red = "\u001b[31m"
var yellow = "\u001b[33m"
var gray = "\u001b[1;30m"
var none = "\u001b[0m"
var resetColor = "\u001b[39m"
var expectedOutputPattern, _ = regexp.Compile(`// 期待:(.*)`)
var expectedErrorPattern, _ = regexp.Compile(`// (错误.*)`)
var errorLinePattern, _ = regexp.Compile(`//【行 (\d )】(错误.*)`)
var expectedRuntimeErrorPattern, _ = regexp.Compile(`// 期待运行时错误:(. )`)
var syntaxErrorPattern, _ = regexp.Compile(`【.*行 (\d )】(错误. )`)
var stackTracePattern, _ = regexp.Compile(`【行 (\d )】`)
var nonTestPattern, _ = regexp.Compile(`// 不考`)
var passed int
var failed int
var skipped int
var expectations int
var interpreter string
func main() {
interpreterPtr := flag.String("interpreter", "", "Path to interpreter.")
flag.Parse()
interpreter = *interpreterPtr
if interpreter == "" {
err := fmt.Errorf("must pass an interpreter path (pass -h for help)")
fmt.Println(err.Error())
os.Exit(1)
}
if !runSuite() {
os.Exit(1)
}
}
func runSuite() bool {
paths, err := WalkMatch("../test", "*.qi")
if err != nil {
fmt.Println(err.Error())
}
for _, path := range paths {
runTest(path)
fmt.Printf("\r\033[K")
}
if failed == 0 {
fmt.Printf("All %s%d%s tests passed (%d expectations).",
green, passed, resetColor, expectations)
} else {
fmt.Printf("%s%d%s tests passed. %s%d%s tests failed.",
green, passed, resetColor, red, failed, resetColor)
}
fmt.Println()
return failed == 0
}
func runTest(path string) {
if strings.Contains(path, "benchmark") { return }
fmt.Printf("Passed: %s%d%s Failed: %s%d%s Skipped: %s%d%s %s%s%s",
green, passed, resetColor, red, failed, resetColor,
yellow, skipped, resetColor, gray, path, none)
test, success := parse(newTest(path))
if !success { return }
failures := run(test)
if len(failures) == 0 {
passed
} else {
failed
fmt.Printf("\r\033[K")
fmt.Printf("%sFAIL%s %s\n", red, resetColor, path)
for _, failure := range failures {
fmt.Printf(" %s%s%s\n", red, failure, resetColor)
}
}
}
type ExpectedOutput struct {
line int
output string
}
type Test struct {
path string
expectedOutput []ExpectedOutput
expectedErrors []string
expectedRuntimeError string
runtimeErrorLine int
expectedExitCode int
failures []string
}
func newTest(path string) Test {
test := Test{}
test.path = path
test.runtimeErrorLine = 0
test.expectedExitCode = 0
return test
}
func parse(test Test) (Test, bool) {
parts := strings.Split(test.path, "/")
var subpath string
for _, part := range parts {
if subpath != "" { subpath = "/" }
subpath = part
}
file, err := os.Open(test.path)
if err != nil {
fmt.Println(err.Error())
}
scanner := bufio.NewScanner(file)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Println(err.Error())
}
for lineNum := 1; lineNum <= len(lines); lineNum {
line := lines[lineNum - 1]
match := nonTestPattern.FindStringSubmatch(line)
if len(match) != 0 { return test, false }
match = expectedOutputPattern.FindStringSubmatch(line)
if len(match) != 0 {
test.expectedOutput = append(test.expectedOutput, ExpectedOutput{lineNum, match[1]})
expectations
continue
}
match = expectedErrorPattern.FindStringSubmatch(line)
if len(match) != 0 {
test.expectedErrors = append(test.expectedErrors, fmt.Sprintf("[%d] %s", lineNum, match[1]))
test.expectedExitCode = 65
expectations
continue
}
match = errorLinePattern.FindStringSubmatch(line)
if len(match) != 0 {
test.expectedErrors = append(test.expectedErrors, fmt.Sprintf("[%s] %s", match[1], match[2]))
test.expectedExitCode = 65
expectations
continue
}
match = expectedRuntimeErrorPattern.FindStringSubmatch(line)
if len(match) != 0 {
test.runtimeErrorLine = lineNum
test.expectedRuntimeError = match[1]
test.expectedExitCode = 70
expectations
}
}
if len(test.expectedErrors) != 0 && test.expectedRuntimeError != "" {
fmt.Printf("%sTEST_ERROR%s %s\n", magenta, resetColor, test.path)
fmt.Printf(" Cannot expect both compile and runtime errors.\n\n")
return test, false
}
err = file.Close()
if err != nil {
fmt.Println(err.Error())
}
return test, true
}
func run(test Test) []string {
cmd := exec.Command(interpreter, test.path)
var outb, errb bytes.Buffer
exitCode := 0
cmd.Stdout = &outb
cmd.Stderr = &errb
if err := cmd.Run(); err != nil {
exitError, ok := err.(*exec.ExitError)
if ok {
exitCode = exitError.ExitCode()
}
}
outputLines := strings.Split(outb.String(), "\n")
errorLines := strings.Split(errb.String(), "\n")
if test.expectedRuntimeError != "" {
test = validateRuntimeError(test, errorLines)
} else {
test = validateCompileErrors(test, errorLines)
}
test = validateExitCode(test, exitCode, errorLines)
test = validateOutput(test, outputLines)
return test.failures
}
func validateRuntimeError(test Test, errorLines []string) Test {
if len(errorLines) < 2 {
test = fail(test, fmt.Sprintf("Expected runtime error '%s' and got none.", test.expectedRuntimeError))
return test
}
if errorLines[0] != test.expectedRuntimeError {
test = fail(test, fmt.Sprintf("Expected runtime error '%s' and got:", test.expectedRuntimeError))
test = fail(test, errorLines[0])
}
var match []string
stackLines := errorLines[1:]
for _, line := range stackLines {
match = stackTracePattern.FindStringSubmatch(line)
if len(match) != 0 { break }
}
if len(match) == 0 {
test = fail(test, "Expected stack trace and got:", stackLines)
} else {
stackLine, _ := strconv.Atoi(match[1])
if stackLine != test.runtimeErrorLine {
test = fail(test, fmt.Sprintf("Expected runtime error on line %d but was on line %d",
test.runtimeErrorLine, stackLine))
}
}
return test
}
func validateCompileErrors(test Test, errorLines []string) Test {
var foundErrors []string
var unexpectedCount int
for _, line := range errorLines {
match := syntaxErrorPattern.FindStringSubmatch(line)
if len(match) != 0 {
err := fmt.Sprintf("[%s] %s", match[1], match[2])
var found bool
for _, e := range test.expectedErrors {
if err == e {
found = true
break
}
}
if found {
foundErrors = append(foundErrors, err)
} else {
if unexpectedCount < 10 {
test = fail(test, "Unexpected error:")
test = fail(test, line)
}
unexpectedCount
}
} else if line != "" {
if unexpectedCount < 10 {
test = fail(test, "Unexpected output on stderr:")
test = fail(test, line)
}
unexpectedCount
}
}
if unexpectedCount > 10 {
test = fail(test, fmt.Sprintf("(truncated %d more...)", unexpectedCount - 10))
}
for _, expected := range test.expectedErrors {
var isFound bool
for _, found := range foundErrors {
if expected == found {
isFound = true
break
}
}
if !isFound {
test = fail(test, fmt.Sprintf("Missing expected error: %s", expected))
}
}
return test
}
func validateExitCode(test Test, exitCode int, errorLines []string) Test {
if exitCode == test.expectedExitCode { return test }
if len(errorLines) > 10 {
errorLines = errorLines[:10]
errorLines = append(errorLines, "(truncated...)")
}
test = fail(test, fmt.Sprintf("Expected return code %d and got %d. Stderr:",
exitCode, test.expectedExitCode), errorLines)
return test
}
func validateOutput(test Test, outputLines []string) Test {
if len(outputLines) != 0 && outputLines[len(outputLines) - 1] == "" {
outputLines = outputLines[:len(outputLines) - 1]
}
index := 0
for ; index < len(outputLines); index {
line := outputLines[index]
if index >= len(test.expectedOutput) {
test = fail(test, fmt.Sprintf("Got output '%s' when none was expected.", line))
continue
}
expected := test.expectedOutput[index]
if expected.output != line {
test = fail(test, fmt.Sprintf("Expected output '%s' on line %d and got '%s'.",
expected.output, expected.line, line))
}
}
return test
}
func fail(test Test, message string, lines ...[]string) Test {
test.failures = append(test.failures, message)
if len(lines) != 0 {
test.failures = append(test.failures, lines[0]...)
}
return test
}
func WalkMatch(root, pattern string) ([]string, error) {
var matches []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if matched, err := filepath.Match(pattern, filepath.Base(path)); err != nil {
return err
} else if matched {
matches = append(matches, path)
}
return nil
})
if err != nil {
return nil, err
}
return matches, nil
}