blob: f8601edfbe8d78882f92f5d5690ef71819185052 [file] [log] [blame]
James Kuszmaul3b15b0c2022-11-08 14:03:16 -08001/* global BigInt */
2
3import assert from 'assert';
4import { readFileSync, writeFileSync } from 'fs';
5import * as flatbuffers from 'flatbuffers';
6import {
7 ArrayStructT,
8 ArrayTable,
9 ArrayTableT,
10 InnerStructT,
11 NestedStructT,
12 OuterStructT,
13 TestEnum,
14} from './arrays_test_complex/arrays_test_complex_generated.js';
15// eslint-disable-next-line @typescript-eslint/no-explicit-any
16BigInt.prototype.toJSON = function () {
17 return this.toString();
18};
19function fbObjToObj(fbObj) {
20 const ret = {};
21 for (const propName of Object.keys(fbObj)) {
22 const key = propName;
23 const prop = fbObj[key];
24 if (prop.valueOf) {
25 ret[key] = prop.valueOf();
26 } else if (typeof prop === 'object') {
27 ret[key] = fbObjToObj(prop);
28 }
29 }
30 return ret;
31}
32function testBuild(monFile, jsFile) {
33 const arrayTable = new ArrayTableT(
34 'Complex Array Test',
35 new ArrayStructT(
36 221.139008,
37 [-700, -600, -500, -400, -300, -200, -100, 0, 100, 200, 300, 400, 500, 600, 700],
38 13,
39 [
40 new NestedStructT(
41 [233, -123],
42 TestEnum.B,
43 [TestEnum.A, TestEnum.C],
44 [
45 new OuterStructT(
46 false,
47 123.456,
48 new InnerStructT(
49 123456792.0,
50 [13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
51 91,
52 BigInt('9007199254740999')
53 ),
54 [
55 new InnerStructT(
56 -987654321.9876,
57 [255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 245, 244, 243],
58 123,
59 BigInt('9007199254741000')
60 ),
61 new InnerStructT(
62 123000987.9876,
63 [101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113],
64 -123,
65 BigInt('9007199254741000')
66 ),
67 ],
68 new InnerStructT(
69 987654321.9876,
70 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
71 19,
72 BigInt('9007199254741000')
73 ),
74 [111000111.222, 222000222.111, 333000333.333, 444000444.444]
75 ),
76 ]
77 ),
78 ],
79 -123456789
80 )
81 );
82 const builder = new flatbuffers.Builder();
83 builder.finish(arrayTable.pack(builder));
84 if (jsFile) {
85 const obj = fbObjToObj(arrayTable);
86 writeFileSync(jsFile, `export default ${JSON.stringify(obj, null, 2)}`);
87 }
88 if (monFile) {
89 writeFileSync(monFile, builder.asUint8Array());
90 }
91 return builder.asUint8Array();
92}
93function testParse(monFile, jsFile, buffer) {
94 if (!buffer) {
95 if (!monFile) {
96 console.log(`Please specify mon file read the buffer from.`);
97 process.exit(1);
98 }
99 buffer = readFileSync(monFile);
100 }
101 const byteBuffer = new flatbuffers.ByteBuffer(new Uint8Array(buffer));
102 const arrayTable = ArrayTable.getRootAsArrayTable(byteBuffer).unpack();
103 const json = JSON.stringify(arrayTable, null, 2);
104 if (jsFile) {
105 writeFileSync(jsFile, `export default ${json}`);
106 }
107 return arrayTable;
108}
109if (process.argv[2] === 'build') {
110 testBuild(process.argv[3], process.argv[4]);
111} else if (process.argv[2] === 'parse') {
112 testParse(process.argv[3], process.argv[4], null);
113} else {
114 const arr = testBuild(null, null);
115 const parsed = testParse(null, null, Buffer.from(arr));
116 assert.strictEqual(parsed.a, 'Complex Array Test', 'String Test');
117 assert.strictEqual(parsed?.cUnderscore?.aUnderscore, 221.13900756835938, 'Float Test');
118 assert.deepEqual(parsed?.cUnderscore?.bUnderscore, [-700, -600, -500, -400, -300, -200, -100, 0, 100, 200, 300, 400, 500, 600, 700], 'Array of signed integers');
119 assert.strictEqual(parsed?.cUnderscore.d?.[0].dOuter[0].d[1].a, 123000987.9876, 'Float in deep');
120 assert.deepEqual(parsed?.cUnderscore?.d[0].dOuter?.[0]?.e, {
121 a: 987654321.9876,
122 b: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
123 c: 19,
124 dUnderscore: '9007199254741000',
125 }, 'Object in deep');
126 assert.deepEqual(parsed?.cUnderscore.g, ['0', '0'], 'Last object');
127
128 console.log('Arrays test: completed successfully');
129}