• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var assert = require('assert');
2
3var flatbuffers = require('../js/flatbuffers').flatbuffers;
4var Test = require(process.argv[2]);
5
6function main() {
7  var fbb = new flatbuffers.Builder();
8
9  var charTypes = [
10    Test.Character.Belle,
11    Test.Character.MuLan,
12    Test.Character.BookFan,
13  ];
14
15  Test.Attacker.startAttacker(fbb);
16  Test.Attacker.addSwordAttackDamage(fbb, 5);
17  var attackerOffset = Test.Attacker.endAttacker(fbb);
18
19  var charTypesOffset = Test.Movie.createCharactersTypeVector(fbb, charTypes);
20  var charsOffset = Test.Movie.createCharactersVector(
21    fbb,
22    [
23      Test.BookReader.createBookReader(fbb, 7),
24      attackerOffset,
25      Test.BookReader.createBookReader(fbb, 2),
26    ]
27  );
28
29  Test.Movie.startMovie(fbb);
30  Test.Movie.addCharactersType(fbb, charTypesOffset);
31  Test.Movie.addCharacters(fbb, charsOffset);
32  Test.Movie.finishMovieBuffer(fbb, Test.Movie.endMovie(fbb));
33
34  var buf = new flatbuffers.ByteBuffer(fbb.asUint8Array());
35
36  var movie = Test.Movie.getRootAsMovie(buf);
37
38  assert.strictEqual(movie.charactersTypeLength(), charTypes.length);
39  assert.strictEqual(movie.charactersLength(), movie.charactersTypeLength());
40
41  for (var i = 0; i < charTypes.length; ++i) {
42    assert.strictEqual(movie.charactersType(i), charTypes[i]);
43  }
44
45  var bookReader7 = movie.characters(0, new Test.BookReader());
46  assert.strictEqual(bookReader7.booksRead(), 7);
47
48  var attacker = movie.characters(1, new Test.Attacker());
49  assert.strictEqual(attacker.swordAttackDamage(), 5);
50
51  var bookReader2 = movie.characters(2, new Test.BookReader());
52  assert.strictEqual(bookReader2.booksRead(), 2);
53
54  console.log('FlatBuffers union vector test: completed successfully');
55}
56
57main();
58