-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathprepareTests.js
More file actions
82 lines (75 loc) · 2.11 KB
/
prepareTests.js
File metadata and controls
82 lines (75 loc) · 2.11 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
const ProtoDef = require('protodef').ProtoDef
const { ProtoDefCompiler } = require('protodef').Compiler
const proto = new ProtoDef()
const compiler = new ProtoDefCompiler()
const testData = [
{
kind: 'conditional',
data: require('../../ProtoDef/test/conditional.json')
},
{
kind: 'numeric',
data: require('../../ProtoDef/test/numeric.json')
},
{
kind: 'structures',
data: require('../../ProtoDef/test/structures.json')
},
{
kind: 'utils',
data: require('../../ProtoDef/test/utils.json')
},
{
kind: 'extras',
data: require('../../ProtoDef/test/extras.json')
}
]
function arrayToBuffer (arr) {
return Buffer.from(arr.map(e => parseInt(e)))
}
function transformValues (type, values) {
return values.map(val => {
let value = val.value
if (type.indexOf('buffer') === 0 || type.endsWith('Buffer')) {
value = arrayToBuffer(value)
} else if (value) {
// we cannot use undefined type in JSON so need to convert it here to pass strictEquals test
for (const key in value) {
if (value[key] === 'undefined') value[key] = undefined
}
}
return {
buffer: arrayToBuffer(val.buffer),
value,
description: val.description
}
})
}
testData.forEach(tests => {
tests.originalData = JSON.parse(JSON.stringify(tests.data))
tests.data.forEach(test => {
const subTypes = []
if (test.subtypes) {
test.subtypes.forEach((subtype, i) => {
const type = test.type + '_' + i
proto.addType(type, subtype.type)
const types = {}
types[type] = subtype.type
compiler.addTypesToCompile(types)
subtype.vars?.forEach(([k, v]) => { proto.setVariable(k, v); compiler.addVariable(k, v) })
subtype.values = transformValues(test.type, subtype.values)
subtype.type = type
subTypes.push(subtype)
})
} else {
test.values = transformValues(test.type, test.values)
subTypes.push({ type: test.type, values: test.values })
}
test.subtypes = subTypes
})
})
module.exports = {
testData,
proto,
compiledProto: compiler.compileProtoDefSync()
}