mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 04:04:19 +00:00
[TS] Add Obj API (#5788)
* added basic code * backup work * got class property to work * backup progress * implementented fmt for creating code * added docs for genFieldUtils * back up work * added base helper js func * added union js code * added unpackTo and base for pack * added pack code * added null check for packing struct list * passes compile test * fixed some spacing of generated functions * added annotations for constructors * added obj api unpack test * tested pack to work * merge branch * separated js and ts test * fixed union signature to include string * fixed generator to support string union * hardcoded fb builder name * refactored struct vector creation * work around createLong * handle default value in constructor * update typescript docs * added notes about import flag * fixed formatting stuffs * undo TypescriptTest change * refactored fmt * updated generated code * remove ignoring union_vector for js * revert changes for .project * revert changes for package.json * don't generate js in ts test * fixed android project file * removed unused js function * removed package-lock.json * adjust createObjList to new signature * changed regex to callback style * fixed package.json * used existing func for generating annotation * changed ternary to !! * added return type for lambda * removed callback style for obj api generator * fixed js file indentation * removed unused header * added tests for string only union * handle string only union and refactor union conv func * updated generated ts files * renamed union conv func * made js test create files like other languages * removed union string only handling * don't allow null in createObjectOffsetList * updated generated ts code * changed the line that triggers Windows build errors * hopefully fix CI error
This commit is contained in:
@@ -3,8 +3,12 @@ var assert = require('assert');
|
||||
var fs = require('fs');
|
||||
|
||||
var flatbuffers = require('../js/flatbuffers').flatbuffers;
|
||||
global.flatbuffers = flatbuffers;
|
||||
|
||||
var MyGame = require(process.argv[2]).MyGame;
|
||||
|
||||
var isTsTest = !!process.env.FB_TS_TEST;
|
||||
|
||||
function main() {
|
||||
|
||||
// First, let's test reading a FlatBuffer generated by C++ code:
|
||||
@@ -24,6 +28,10 @@ function main() {
|
||||
createMonster(fbb);
|
||||
serializeAndTest(fbb);
|
||||
|
||||
if(isTsTest) {
|
||||
testObjApiPack(fbb);
|
||||
}
|
||||
|
||||
// clear the builder, repeat tests
|
||||
var clearIterations = 100;
|
||||
var startingCapacity = fbb.bb.capacity();
|
||||
@@ -31,6 +39,10 @@ function main() {
|
||||
fbb.clear();
|
||||
createMonster(fbb);
|
||||
serializeAndTest(fbb);
|
||||
|
||||
if(isTsTest) {
|
||||
testObjApiPack(fbb);
|
||||
}
|
||||
}
|
||||
// the capacity of our buffer shouldn't increase with the same size payload
|
||||
assert.strictEqual(fbb.bb.capacity(), startingCapacity);
|
||||
@@ -109,6 +121,56 @@ function testMutation(bb) {
|
||||
// TODO: There is not the availability to mutate structs or vectors.
|
||||
}
|
||||
|
||||
function testObjApiPack(fbb) {
|
||||
fbb.clear();
|
||||
createMonster(fbb);
|
||||
let monster_t = MyGame.Example.Monster.getRootAsMonster(fbb.dataBuffer()).unpack();
|
||||
fbb.clear();
|
||||
MyGame.Example.Monster.finishMonsterBuffer(fbb, monster_t.pack(fbb));
|
||||
serializeAndTest(fbb);
|
||||
}
|
||||
|
||||
function testObjApiUnpack(monster) {
|
||||
assert.strictEqual(monster.hp, 80);
|
||||
assert.strictEqual(monster.mana, 150); // default
|
||||
|
||||
assert.strictEqual(monster.name, 'MyMonster');
|
||||
|
||||
let pos = monster.pos;
|
||||
assert.strictEqual(pos.x, 1);
|
||||
assert.strictEqual(pos.y, 2);
|
||||
assert.strictEqual(pos.z, 3);
|
||||
assert.strictEqual(pos.test1, 3);
|
||||
assert.strictEqual(pos.test2, MyGame.Example.Color.Green);
|
||||
let test3 = pos.test3;
|
||||
assert.strictEqual(test3.a, 5);
|
||||
assert.strictEqual(test3.b, 6);
|
||||
|
||||
assert.strictEqual(monster.testType, MyGame.Example.Any.Monster);
|
||||
let monster2 = monster.test;
|
||||
assert.strictEqual(monster2 != null, true);
|
||||
assert.strictEqual(monster2 instanceof MyGame.Example.MonsterT, true);
|
||||
assert.strictEqual(monster2.name, 'Fred');
|
||||
|
||||
assert.strictEqual(monster.inventory.length, 5);
|
||||
let invsum = 0;
|
||||
for (let i = 0; i < monster.inventory.length; i++) {
|
||||
invsum += monster.inventory[i];
|
||||
}
|
||||
assert.strictEqual(invsum, 10);
|
||||
|
||||
let test_0 = monster.test4[0];
|
||||
let test_1 = monster.test4[1];
|
||||
assert.strictEqual(monster.test4.length, 2);
|
||||
assert.strictEqual(test_0.a + test_0.b + test_1.a + test_1.b, 100);
|
||||
|
||||
assert.strictEqual(monster.testarrayofstring.length, 2);
|
||||
assert.strictEqual(monster.testarrayofstring[0], 'test1');
|
||||
assert.strictEqual(monster.testarrayofstring[1], 'test2');
|
||||
|
||||
assert.strictEqual(monster.testbool, true);
|
||||
}
|
||||
|
||||
function testBuffer(bb) {
|
||||
assert.ok(MyGame.Example.Monster.bufferHasIdentifier(bb));
|
||||
|
||||
@@ -158,6 +220,15 @@ function testBuffer(bb) {
|
||||
assert.strictEqual(monster.testarrayofstring(1), 'test2');
|
||||
|
||||
assert.strictEqual(monster.testbool(), true);
|
||||
|
||||
if(isTsTest) {
|
||||
let monster_t = monster.unpack();
|
||||
testObjApiUnpack(monster_t);
|
||||
|
||||
let monster2_t = new MyGame.Example.MonsterT();
|
||||
monster.unpackTo(monster2_t);
|
||||
testObjApiUnpack(monster2_t);
|
||||
}
|
||||
}
|
||||
|
||||
function test64bit() {
|
||||
|
||||
Reference in New Issue
Block a user