-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathindex.test.js
More file actions
318 lines (281 loc) · 10.4 KB
/
index.test.js
File metadata and controls
318 lines (281 loc) · 10.4 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* eslint-env mocha */
const assert = require('assert')
const ByteBuffer = require('bytebuffer')
const Fcbuffer = require('.')
const Types = require('./src/types')
const Struct = require('./src/struct')
const {create} = require('./src/fcbuffer')
describe('API', function () {
it('Bytes', function () {
const {Bytes} = Types()
const type = Bytes()
assertSerializer(type, '00aaeeff')
assertRequired(type)
})
it('String', function () {
const {String} = Types()
const type = String()
assertSerializer(type, '爱')
assertRequired(type)
})
it('Vector', function () {
const {Vector, String} = Types()
const type = Vector(String())
throws(() => Vector('String'), /Vector type should be a serializer/)
assertSerializer(type, ['z', 'a', 'z']) // does not sort
assertRequired(type)
})
it('FixedBytes', function () {
const {FixedBytes16} = Types()
const type = FixedBytes16()
assertSerializer(type, Array(16 + 1).join('ff')) // hex string
throws(() => assertSerializer(type, Array(17 + 1).join('ff')), /FixedBytes16 length 17 does not equal 16/)
assertRequired(type)
})
it('FixedString', function () {
const {FixedString16} = Types()
const type = FixedString16()
assertSerializer(type, '1234567890123456')
throws(() => assertSerializer(type, '12345678901234567'), /exceeds maxLen 16/)
assertRequired(type)
})
it('TypesAll', function () {
const types = Types()
for (let typeName of Object.keys(types)) {
const fn = types[typeName]
if (typeof fn === 'function') {
fn(types.String())
}
}
})
it('Time', function () {
const {Time} = Types()
const type = Time()
throws(() => type.fromObject({}), /Unknown date type/)
type.fromObject(new Date())
type.fromObject(1000)
type.fromObject('1970-01-01T00:00:00')
assertSerializer(type, '1970-01-01T00:00:00Z')
assertSerializer(type, '2106-02-07T06:28:15Z')
throws(() => assertSerializer(type, '1969-12-31T23:59:59Z'), /Overflow/)
throws(() => assertSerializer(type, '2106-02-07T06:28:16Z'), /Overflow/)
assertRequired(type)
})
it('Optional', function () {
const {Optional, String} = Types()
const type = Optional(String())
throws(() => Optional('String'), /Optional parameter should be a serializer/)
assertSerializer(type, 'str')
assertSerializer(type, null)
assertSerializer(type, undefined)
})
it('UInt', function () {
const {UInt8} = Types()
const type = UInt8()
assertSerializer(type, 0)
assertSerializer(type, 255)
throws(() => assertSerializer(type, 256), /Overflow/)
throws(() => assertSerializer(type, -1), /Overflow/)
assertRequired(type)
})
it('UInt64', function () {
const {UInt64} = Types()
const type = UInt64()
assertSerializer(type, '18446744073709551615')
assertSerializer(type, '0')
throws(() => assertSerializer(type, '18446744073709551616'), /Overflow/)
throws(() => assertSerializer(type, '-1'), /Overflow/)
assertRequired(type)
})
it('Int', function () {
const {Int8} = Types()
const type = Int8()
assertSerializer(type, -128)
assertSerializer(type, 127)
throws(() => assertSerializer(type, -129), /Overflow/)
throws(() => assertSerializer(type, 128), /Overflow/)
assertRequired(type)
})
it('Int64', function () {
const {Int64} = Types()
const type = Int64()
assertSerializer(type, '9223372036854775807')
assertSerializer(type, '-9223372036854775808')
throws(() => assertSerializer(type, '9223372036854775808'), /Overflow/)
throws(() => assertSerializer(type, '-9223372036854775809'), /Overflow/)
assertRequired(type)
})
it('Struct', function () {
const {Vector, UInt16, FixedBytes33} = Types()
const KeyPermissionWeight = Struct('KeyPermissionWeight')
KeyPermissionWeight.add('key', FixedBytes33())
KeyPermissionWeight.add('weight', UInt16())
const type = Vector(KeyPermissionWeight)
assertSerializer(type, [
{key: Array(33 + 1).join('00'), weight: 1},
{key: Array(33 + 1).join('00'), weight: 1}
])
})
})
describe('JSON', function () {
it('Structure', function () {
assertCompile({Struct: {fields: {checksum: 'FixedBytes32'}}})
throws(() => assertCompile({Struct: {}}), /Expecting Struct.fields or Struct.base/)
throws(() => assertCompile({Struct: {base: {obj: 'val'}}}), /Expecting string/)
throws(() => assertCompile({Struct: {fields: 'String'}}), /Expecting object/)
throws(() => assertCompile({Struct: {fields: {name: {obj: 'val'}}}}), /Expecting string in/)
throws(() => assertCompile({Struct: 0}), /Expecting object or string/)
})
it('Debug', function () {
assertCompile(
{Name: 'String', Person: {fields: {name: 'Name'}}},
{defaults: true, debug: true}
)
})
it('typedef', function () {
throws(() => assertCompile({Type: 'UnknownType'}), /Unrecognized type/)
assertCompile({Name: 'String', Person: {fields: {name: 'Name'}}})
assertCompile({Name: 'String', MyName: 'Name', Person: {fields: {name: 'MyName'}}})
})
it('typedef', function () {
assertCompile({Event: {fields: {time: 'Time'}}})
})
it('Inherit', function () {
throws(() => assertCompile({Struct: {fields: {name: 'Name'}}}), /Missing Name/)
throws(() => assertCompile({Struct: {base: 'String'}}), /Missing String in Struct.base/)
throws(() => assertCompile({
Person: {base: 'Human', fields: {name: 'String'}}}
), /Missing Human/)
throws(() => assertCompile({
Human: 'String', // Human needs to be struct not a type
Person: {base: 'Human', fields: {name: 'String'}}}
), /Missing Human/)
assertCompile({
Boolean: 'UInt8',
Human: {fields: {Alive: 'Boolean'}},
Person: {base: 'Human', fields: {name: 'String'}}
})
})
it('Optional', function () {
const {Person} = assertCompile({Person: {fields: {name: 'String?'}}}, {defaults: false})
assertSerializer(Person, {name: 'Jane'})
assertSerializer(Person, {name: null})
assertSerializer(Person, {name: undefined})
// assertSerializer(Person, {}) {"name": [null]} // TODO ???
})
it('Vectors', function () {
throws(() => assertCompile({Person: {fields: {name: 'Vector[TypeArg]'}}}), /Missing TypeArg/)
throws(() => assertCompile({Person: {fields: {name: 'BaseType[]'}}}), /Missing BaseType/)
throws(() => assertCompile({Person: {fields: {name: 'BaseType[String]'}}}), /Missing BaseType/)
assertCompile({Person: {fields: {name: 'Vector[String]'}}})
assertCompile({Person: {fields: {name: 'String'}}, Conference: {fields: {attendees: 'Person[]'}}})
const {Person} = assertCompile({Person: {fields: {friends: 'String[]'}}})
assertSerializer(Person, {friends: ['Jane', 'Dan']})
})
it('Errors', function () {
const {structs} = create({Struct: {fields: {age: 'String'}}}, Types({defaults: true}))
const type = structs.Struct
throws(() => Fcbuffer.fromBuffer(type, Buffer.from('')), /Illegal offset/)
})
})
describe('Override', function () {
it('Struct', function () {
const definitions = {
Message: {
fields: {
type: 'String', // another definition (like transfer)
data: 'Bytes'
}
},
transfer: {
fields: {
from: 'String',
to: 'String'
}
}
}
const config = {
override: {
'Message.data.fromByteBuffer': ({fields, object, b, config}) => {
const ser = (object.type || '') == '' ? fields.data : structs[object.type]
b.readVarint32()
object.data = ser.fromByteBuffer(b, config)
},
'Message.data.appendByteBuffer': ({fields, object, b}) => {
const ser = (object.type || '') == '' ? fields.data : structs[object.type]
const b2 = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN)
ser.appendByteBuffer(b2, object.data)
b.writeVarint32(b2.offset)
b.append(b2.copy(0, b2.offset), 'binary')
},
'Message.data.fromObject': ({fields, serializedObject, result}) => {
const {data, type} = serializedObject
const ser = (type || '') == '' ? fields.data : structs[type]
result.data = ser.fromObject(data)
},
'Message.data.toObject': ({fields, serializedObject, result, config}) => {
const {data, type} = serializedObject || {}
const ser = (type || '') == '' ? fields.data : structs[type]
result.data = ser.toObject(data, config)
},
}
}
const {structs, errors} = create(definitions, Types(config))
assert.equal(errors.length, 0)
assertSerializer(structs.Message, {
type: 'transfer',
data: {
from: 'slim',
to: 'charles'
}
})
})
})
function assertCompile (definitions, config) {
config = Object.assign({defaults: true, debug: false}, config)
const {errors, structs} = create(definitions, Types(config))
assert.equal(errors.length, 0, errors[0])
assert(Object.keys(structs).length > 0, 'expecting struct(s)')
for (const struct in structs) {
const type = structs[struct]
// console.log(struct, JSON.stringify(structs[struct].toObject(), null, 0), '\n')
assertSerializer(type, type.toObject())
}
return structs
}
function assertSerializer (type, value) {
const obj = type.fromObject(value) // tests fromObject
const buf = Fcbuffer.toBuffer(type, obj) // tests appendByteBuffer
const obj2 = Fcbuffer.fromBuffer(type, buf) // tests fromByteBuffer
const obj3 = type.toObject(obj) // tests toObject
deepEqual(value, obj3, 'serialize object')
deepEqual(obj3, obj2, 'serialize buffer')
}
function assertRequired (type) {
throws(() => assertSerializer(type, null), /Required/)
throws(() => assertSerializer(type, undefined), /Required/)
}
/* istanbul ignore next */
function deepEqual (arg1, arg2, message) {
try {
assert.deepEqual(arg1, arg2, message)
// console.log('deepEqual arg1', arg1, '\n', JSON.stringify(arg1))
// console.log('deepEqual arg2', arg2, '\n', JSON.stringify(arg2))
} catch (error) {
// console.error('deepEqual arg1', arg1, '\n', JSON.stringify(arg1))
// console.error('deepEqual arg2', arg2, '\n', JSON.stringify(arg2))
throw error
}
}
/* istanbul ignore next */
function throws (fn, match) {
try {
fn()
assert(false, 'Expecting error')
} catch (error) {
if (!match.test(error)) {
error.message = `Error did not match ${match}\n${error.message}`
throw error
}
}
}