-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
63 lines (58 loc) · 1.88 KB
/
index.test.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
62
63
const fs = require('fs')
const path = require('path')
const { buildTestcaseTable } = require('./lib/testcase')
const { TreeNode, Node } = require('./lib/tree')
const { ListNode } = require('./lib/list')
const serialize = require('./lib/serialize')
const deserialize = require('./lib/deserialize')
const problemsDir = path.join(__dirname, 'problems')
beforeAll(() => {
global.TreeNode = TreeNode
global.ListNode = ListNode
global.Node = Node
})
afterAll(() => {
delete global.TreeNode
delete global.ListNode
delete global.Node
})
for (const problem of fs.readdirSync(problemsDir)) {
const problemDir = path.resolve(problemsDir, problem)
if (fs.statSync(problemDir).isDirectory()) {
describe(problem, () => {
const testcasesFile = path.resolve(problemDir, 'testcases')
if (fs.existsSync(testcasesFile)) {
const configFile = path.resolve(problemDir, 'config.js')
const config = fs.existsSync(configFile) ? require(configFile) : {}
test.each(buildTestcaseTable(testcasesFile))(
'Case #%#',
(...args) => {
const fn = require(problemDir)
const assert = config.assert ?? defaultAssert
assert(
fn.apply(
null,
args.slice(0, -1)
.map(
config.deserializeInput ?? (
(arg, i) => deserialize(
arg,
Array.isArray(config.inputType)
? config.inputType[i]
: config.inputType
)
)
)
),
args.slice(-1)[0]
)
function defaultAssert (result, expected) {
expect((config.serializeOutput ?? serialize)(result))
.toEqual(expected)
}
}
)
}
})
}
}