• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import assert from 'assert'
2import * as flatbuffers from 'flatbuffers'
3
4import { Character } from './union_vector/character'
5import { BookReader, BookReaderT } from './union_vector/book-reader'
6import { Attacker, AttackerT } from './union_vector/attacker'
7import { Movie, MovieT } from './union_vector/movie'
8
9var charTypes = [
10  Character.Belle,
11  Character.MuLan,
12  Character.BookFan,
13  Character.Other
14];
15
16function testMovieBuf(movie) {
17  assert.strictEqual(movie.charactersTypeLength(), charTypes.length);
18  assert.strictEqual(movie.charactersLength(), movie.charactersTypeLength());
19
20  for (var i = 0; i < charTypes.length; ++i) {
21    assert.strictEqual(movie.charactersType(i), charTypes[i]);
22  }
23
24  var bookReader7 = movie.characters(0, new BookReader());
25  assert.strictEqual(bookReader7.booksRead(), 7);
26
27  var attacker = movie.characters(1, new Attacker());
28  assert.strictEqual(attacker.swordAttackDamage(), 5);
29
30  var bookReader2 = movie.characters(2, new BookReader());
31  assert.strictEqual(bookReader2.booksRead(), 2);
32
33  var other = movie.characters(3, '');
34  assert.strictEqual(other, "I am other");
35}
36
37function testMovieUnpack(movie) {
38  assert.strictEqual(movie.charactersType.length, charTypes.length);
39  assert.strictEqual(movie.characters.length, movie.charactersType.length);
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];
46  assert.strictEqual(bookReader7 instanceof BookReaderT, true);
47  assert.strictEqual(bookReader7.booksRead, 7);
48
49  var attacker = movie.characters[1];
50  assert.strictEqual(attacker instanceof AttackerT, true);
51  assert.strictEqual(attacker.swordAttackDamage, 5);
52
53  var bookReader2 = movie.characters[2];
54  assert.strictEqual(bookReader2 instanceof BookReaderT, true);
55  assert.strictEqual(bookReader2.booksRead, 2);
56
57  var other = movie.characters[3];
58  assert.strictEqual(other, "I am other");
59}
60
61function createMovie(fbb) {
62  Attacker.startAttacker(fbb);
63  Attacker.addSwordAttackDamage(fbb, 5);
64  var attackerOffset = Attacker.endAttacker(fbb);
65
66  var charTypesOffset = Movie.createCharactersTypeVector(fbb, charTypes);
67  var charsOffset = 0;
68
69  let otherOffset = fbb.createString("I am other");
70
71  charsOffset = Movie.createCharactersVector(
72    fbb,
73    [
74      BookReader.createBookReader(fbb, 7),
75      attackerOffset,
76      BookReader.createBookReader(fbb, 2),
77      otherOffset
78    ]
79  );
80
81  Movie.startMovie(fbb);
82  Movie.addCharactersType(fbb, charTypesOffset);
83  Movie.addCharacters(fbb, charsOffset);
84  Movie.finishMovieBuffer(fbb, Movie.endMovie(fbb))
85}
86
87function main() {
88  var fbb = new flatbuffers.Builder();
89
90  createMovie(fbb);
91
92  var buf = new flatbuffers.ByteBuffer(fbb.asUint8Array());
93
94  var movie = Movie.getRootAsMovie(buf);
95  testMovieBuf(movie);
96
97  testMovieUnpack(movie.unpack());
98
99  var movie_to = new MovieT();
100  movie.unpackTo(movie_to);
101  testMovieUnpack(movie_to);
102
103  fbb.clear();
104  Movie.finishMovieBuffer(fbb, movie_to.pack(fbb));
105  var unpackBuf = new flatbuffers.ByteBuffer(fbb.asUint8Array());
106  testMovieBuf(Movie.getRootAsMovie(unpackBuf));
107
108  console.log('FlatBuffers union vector test: completed successfully');
109}
110
111main();
112