-
Notifications
You must be signed in to change notification settings - Fork 3
/
e2e.js
61 lines (53 loc) · 1.43 KB
/
e2e.js
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
const tap = require('tap')
const ti = require('../dist/node/ti')
const tiJsTests = require('../web/js/testCases')
function trimInput (text) {
const indent = tiJsTests.options.indent
if (text === undefined || text.indexOf('\n') === -1) {
return text
}
return text
.split('\n')
.filter((line, index, array) => index !== array.length - 1)
.map(line => line.substring(indent))
.join('\n')
}
function trimLastNewline (text) {
if (text.length > 0) {
const lastCharacter = text[text.length - 1]
if (lastCharacter === '\n') {
text = text.substring(0, text.length - 1)
}
}
return text
}
function handleTestResult (testCase, status, output) {
if (status === 'faulted') {
tap.fail(testCase.name, {
output,
expected: trimInput(testCase.expected),
})
} else if (status === 'done' || status === 'err') {
tap.equal(
output,
trimInput(testCase.expected),
testCase.name,
)
} else {
tap.fail(testCase.name, {
unexpectedStatus: status,
})
}
}
tap.plan(tiJsTests.testCases.length)
tiJsTests.testCases.forEach(testCase => {
let totalOutput = ''
const lines = ti.parse(trimInput(testCase.input))
ti.run(lines, {
stdin: trimInput(testCase.stdin),
outputCallback: (output, newline) => {
totalOutput += output + (newline ? '\n' : '')
},
callback: status => handleTestResult(testCase, status, trimLastNewline(totalOutput)),
})
})