-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathdatatypes.js
More file actions
102 lines (93 loc) · 3.38 KB
/
datatypes.js
File metadata and controls
102 lines (93 loc) · 3.38 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
/* eslint-env mocha */
const expect = require('chai').expect
const Validator = require('jsonschema').Validator
const v = new Validator()
const assert = require('assert')
const { testData, proto, compiledProto } = require('./prepareTests')
function testValue (type, value, buffer) {
it('writes', function () {
expect(proto.createPacketBuffer(type, value)).to.deep.equal(buffer)
})
it('reads', function () {
const actualResult = proto.parsePacketBuffer(type, buffer)
if (typeof actualResult.data === 'bigint') value = BigInt(value)
if (value === null) {
assert.ok(actualResult.data === undefined)
} else {
expect(actualResult.data).to.deep.equal(value)
}
expect(actualResult.metadata.size).to.deep.equal(buffer.length)
})
it('writes (compiled)', function () {
expect(compiledProto.createPacketBuffer(type, value)).to.deep.equal(buffer)
})
it('reads (compiled)', function () {
const actualResult = compiledProto.parsePacketBuffer(type, buffer)
if (value === null) { assert.ok(actualResult.data === undefined) } else { expect(actualResult.data).to.deep.equal(value) }
expect(actualResult.metadata.size).to.deep.equal(buffer.length)
})
if (type === 'i64' || type === 'u64') {
it('reads bigint correctly ' + type, function () {
const [top, lower] = value.map(BigInt)
const joined = type === 'i64' ? BigInt.asIntN(64, (top << 32n) | lower) : BigInt.asUintN(64, (top << 32n) | lower)
// read
const actualResult = proto.parsePacketBuffer(type, buffer)
expect(actualResult.data.valueOf() === joined)
expect(actualResult.metadata.size).to.deep.equal(buffer.length)
})
}
}
function testType (type, values) {
if (values.length === 0) {
it.skip('Has no tests', () => {
})
}
values.forEach((value) => {
if (value.description) {
describe(value.description, () => {
testValue(type, value.value, value.buffer)
})
} else { testValue(type, value.value, value.buffer) }
})
if (type !== 'void' && type !== 'restBuffer' && !type.startsWith('loop_')) {
it('reads 0 bytes and throw a PartialReadError', () => {
try {
proto.parsePacketBuffer(type, Buffer.alloc(0))
} catch (e) {
if (!e.partialReadError) { throw e }
return
}
throw Error('no PartialReadError thrown')
})
it('reads 0 bytes and throw a PartialReadError (compiled)', () => {
try {
compiledProto.parsePacketBuffer(type, Buffer.alloc(0))
} catch (e) {
if (!e.partialReadError) { throw e }
return
}
throw Error('no PartialReadError thrown')
})
}
}
testData.forEach(tests => {
describe(tests.kind, () => {
it('validates the json schema', () => {
const schema = require('../../ProtoDef/test/datatype_tests_schema.json')
v.addSchema(require('../../ProtoDef/schemas/datatype'), 'dataType')
const result = v.validate(tests.originalData, schema)
assert.strictEqual(result.errors.length, 0, require('util').inspect(result.errors, { depth: null }))
})
tests.data.forEach(test => {
describe(test.type, () => {
test.subtypes.forEach((subtype) => {
if (subtype.description) {
describe(subtype.description, () => {
testType(subtype.type, subtype.values)
})
} else { testType(test.type, subtype.values) }
})
})
})
})
})