-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrunner.js
More file actions
302 lines (242 loc) · 7.05 KB
/
runner.js
File metadata and controls
302 lines (242 loc) · 7.05 KB
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
// This test utility runs the JSON-specified tests in build/test/test.json.
const { readFileSync } = require('node:fs')
const { join } = require('node:path')
const { deepEqual, fail, AssertionError } = require('node:assert')
const NULLMARK = '__NULL__' // Value is JSON null
const UNDEFMARK = '__UNDEF__' // Value is not present (thus, undefined).
const EXISTSMARK = '__EXISTS__' // Value exists (not undefined).
async function makeRunner(testfile, client) {
return async function runner(name, store = {}) {
store = store || {}
const utility = client.utility()
const structUtils = utility.struct
let spec = resolveSpec(name, testfile)
let clients = await resolveClients(client, spec, store, structUtils)
let subject = resolveSubject(name, utility)
let runsetflags = async (testspec, flags, testsubject) => {
subject = testsubject || subject
flags = resolveFlags(flags)
const testspecmap = fixJSON(testspec, flags)
const testset = testspecmap.set
for (let entry of testset) {
try {
entry = resolveEntry(entry, flags)
let testpack = resolveTestPack(name, entry, subject, client, clients)
let args = resolveArgs(entry, testpack, utility, structUtils)
let res = await testpack.subject(...args)
res = fixJSON(res, flags)
entry.res = res
checkResult(entry, args, res, structUtils)
} catch (err) {
handleError(entry, err, structUtils)
}
}
}
let runset = async (testspec, testsubject) => runsetflags(testspec, {}, testsubject)
const runpack = {
spec,
runset,
runsetflags,
subject,
client,
}
return runpack
}
}
function resolveSpec(name, testfile) {
const alltests = JSON.parse(readFileSync(join(__dirname, testfile), 'utf8'))
let spec = alltests.primary?.[name] || alltests[name] || alltests
return spec
}
async function resolveClients(client, spec, store, structUtils) {
const clients = {}
if (spec.DEF && spec.DEF.client) {
for (let cn in spec.DEF.client) {
const cdef = spec.DEF.client[cn]
const copts = cdef.test.options || {}
if ('object' === typeof store && structUtils?.inject) {
structUtils.inject(copts, store)
}
clients[cn] = await client.tester(copts)
}
}
return clients
}
function resolveSubject(name, container) {
const subject = container[name] || container.struct[name]
return subject
}
function resolveFlags(flags) {
if (null == flags) {
flags = {}
}
flags.null = null == flags.null ? true : !!flags.null
return flags
}
function resolveEntry(entry, flags) {
entry.out = null == entry.out && flags.null ? NULLMARK : entry.out
return entry
}
function checkResult(entry, args, res, structUtils) {
let matched = false
if (entry.err) {
return fail(
'Expected error did not occur: ' + entry.err + '\n\nENTRY: ' + JSON.stringify(entry, null, 2),
)
}
if (entry.match) {
const result = { in: entry.in, args, out: entry.res, ctx: entry.ctx }
match(entry.match, result, structUtils)
matched = true
}
const out = entry.out
if (out === res) {
return
}
// NOTE: allow match with no out.
if (matched && (NULLMARK === out || null == out)) {
return
}
deepEqual(null != res ? JSON.parse(JSON.stringify(res)) : res, entry.out)
}
// Handle errors from test execution
function handleError(entry, err, structUtils) {
entry.thrown = err
const entry_err = entry.err
if (null != entry_err) {
if (true === entry_err || matchval(entry_err, err.message, structUtils)) {
if (entry.match) {
match(
entry.match,
{ in: entry.in, out: entry.res, ctx: entry.ctx, err: fixJSON(err) },
structUtils,
)
}
return
}
fail('ERROR MATCH: [' + structUtils.stringify(entry_err) + '] <=> [' + err.message + ']')
}
// Unexpected error (test didn't specify an error expectation)
else if (err instanceof AssertionError) {
fail(err.message + '\n\nENTRY: ' + JSON.stringify(entry, null, 2))
} else {
fail(err.stack + '\\nnENTRY: ' + JSON.stringify(entry, null, 2))
}
}
function resolveArgs(entry, testpack, utility, structUtils) {
let args = []
if (entry.ctx) {
args = [entry.ctx]
} else if (entry.args) {
args = entry.args
} else {
args = [structUtils.clone(entry.in)]
}
if (entry.ctx || entry.args) {
let first = args[0]
if (structUtils.ismap(first)) {
first = structUtils.clone(first)
first = utility.contextify(first)
args[0] = first
entry.ctx = first
first.client = testpack.client
first.utility = testpack.utility
}
}
return args
}
function resolveTestPack(name, entry, subject, client, clients) {
const testpack = {
name,
client,
subject,
utility: client.utility(),
}
if (entry.client) {
testpack.client = clients[entry.client]
testpack.utility = testpack.client.utility()
testpack.subject = resolveSubject(name, testpack.utility)
}
return testpack
}
function match(check, base, structUtils) {
base = structUtils.clone(base)
structUtils.walk(check, (_key, val, _parent, path) => {
if (!structUtils.isnode(val)) {
let baseval = structUtils.getpath(base, path)
if (baseval === val) {
return val
}
// Explicit undefined expected
if (UNDEFMARK === val && undefined === baseval) {
return val
}
// Explicit defined expected
if (EXISTSMARK === val && null != baseval) {
return val
}
if (!matchval(val, baseval, structUtils)) {
fail(
'MATCH: ' +
path.join('.') +
': [' +
structUtils.stringify(val) +
'] <=> [' +
structUtils.stringify(baseval) +
']',
)
}
}
return val
})
}
function matchval(check, base, structUtils) {
// check = NULLMARK === check || UNDEFMARK === check ? undefined : check
// check = NULLMARK === check ? undefined : check
let pass = check === base
if (!pass) {
if ('string' === typeof check) {
let basestr = structUtils.stringify(base)
let rem = check.match(/^\/(.+)\/$/)
if (rem) {
pass = new RegExp(rem[1]).test(basestr)
} else {
pass = basestr.toLowerCase().includes(structUtils.stringify(check).toLowerCase())
}
} else if ('function' === typeof check) {
pass = true
}
}
return pass
}
function fixJSON(val, flags) {
if (null == val) {
return flags.null ? NULLMARK : val
}
const replacer = (_k, v) => {
if (null == v && flags.null) {
return NULLMARK
}
if (v instanceof Error) {
return {
...v,
name: v.name,
message: v.message,
}
}
return v
}
return JSON.parse(JSON.stringify(val, replacer))
}
function nullModifier(val, key, parent) {
if ('__NULL__' === val) {
parent[key] = null
} else if ('string' === typeof val) {
parent[key] = val.replaceAll('__NULL__', 'null')
}
}
module.exports = {
NULLMARK,
nullModifier,
makeRunner,
}