mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 20:15:34 +00:00
[TS/JS] New gen TS code gen (#6302)
* TS/ES6 modules spike iteration 1 * Initial modularized dasherized output * Remove obsoleted parts and namespace wrapping * Use _flatbuffers_ prefix * First part of imports logic * Second part of imports logic * Fix TS/JS code removal mixup * Alias imported symbols if same name from different namespaces and some fixes * Use star import for bare imports * Fix messed up string concat * var to const and remove not needed semi * Remove some cases of ns prefixing * Add missing space * Cleanups * Completed initial import tracking logic * Compilable output * Adjust TypeScriptTest and dependents to work * Use local flatbuffers package for tests * Refactor away use of any * Remove obsolete imported_fileset and reexport_map * Still need any and fix JavaScriptTest.sh * Fix test runs out of the box * Temp add generated files * TypeScriptTest replaces JavaScriptTest and cleanups * Also remove reference to JavaScriptTest in TestAll.sh * Remove old generated ts/js files * Remove use of --js in generate_code scripts * idl_gen_js_ts to idl_gen_ts and removal of js gen * Remove obsoleted options * Fix obsolete ts test detection * Tweak ts compilation be as strict as possible * Remove jsdoc type annotation generation * Generated test ts files * Fix search and replace messup * Regenerated ts test output * Use CharToLower * Use normal for loop * Rework namespacedir * Revert "Rework namespacedir" This reverts commit 6f4eb0104ceeb86011bb076ebca901138c48e068. * Revert "Use normal for loop" This reverts commit 676b2135bfaa1853dfbb06c92b5c16a0d81bb13a. * Revert "Use CharToLower" This reverts commit 2d08648d0d72d0af201fad80d54cdc76412b35e9. * Again do rework but correct * Avoid runtime cast * Fix test runs * Also add npm install to get tsc * Bump node test versions * for range to std for loop * Clang format * Missed one clang format * Move accessor to later * Attempt to make windows version of TypeScriptTest * Want to see the output * Try to get newer node at appveyor * Style changes
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
var assert = require('assert');
|
||||
import assert from 'assert'
|
||||
import * as flatbuffers from 'flatbuffers'
|
||||
|
||||
var flatbuffers = require('../js/flatbuffers').flatbuffers;
|
||||
var Test = require(process.argv[2]);
|
||||
|
||||
var isTsTest = !!process.env.FB_TS_TEST;
|
||||
import { Character } from './ts/character'
|
||||
import { BookReader, BookReaderT } from './ts/book-reader'
|
||||
import { Attacker, AttackerT } from './ts/attacker'
|
||||
import { Movie, MovieT } from './ts/movie'
|
||||
|
||||
var charTypes = [
|
||||
Test.Character.Belle,
|
||||
Test.Character.MuLan,
|
||||
Test.Character.BookFan,
|
||||
Character.Belle,
|
||||
Character.MuLan,
|
||||
Character.BookFan,
|
||||
Character.Other
|
||||
];
|
||||
if(isTsTest) { charTypes.push(Test.Character.Other); }
|
||||
|
||||
function testMovieBuf(movie) {
|
||||
assert.strictEqual(movie.charactersTypeLength(), charTypes.length);
|
||||
@@ -20,19 +21,17 @@ function testMovieBuf(movie) {
|
||||
assert.strictEqual(movie.charactersType(i), charTypes[i]);
|
||||
}
|
||||
|
||||
var bookReader7 = movie.characters(0, new Test.BookReader());
|
||||
var bookReader7 = movie.characters(0, new BookReader());
|
||||
assert.strictEqual(bookReader7.booksRead(), 7);
|
||||
|
||||
var attacker = movie.characters(1, new Test.Attacker());
|
||||
var attacker = movie.characters(1, new Attacker());
|
||||
assert.strictEqual(attacker.swordAttackDamage(), 5);
|
||||
|
||||
var bookReader2 = movie.characters(2, new Test.BookReader());
|
||||
var bookReader2 = movie.characters(2, new BookReader());
|
||||
assert.strictEqual(bookReader2.booksRead(), 2);
|
||||
|
||||
if(isTsTest) {
|
||||
var other = movie.characters(3, '');
|
||||
assert.strictEqual(other, "I am other");
|
||||
}
|
||||
var other = movie.characters(3, '');
|
||||
assert.strictEqual(other, "I am other");
|
||||
}
|
||||
|
||||
function testMovieUnpack(movie) {
|
||||
@@ -44,58 +43,45 @@ function testMovieUnpack(movie) {
|
||||
}
|
||||
|
||||
var bookReader7 = movie.characters[0];
|
||||
assert.strictEqual(bookReader7 instanceof Test.BookReaderT, true);
|
||||
assert.strictEqual(bookReader7 instanceof BookReaderT, true);
|
||||
assert.strictEqual(bookReader7.booksRead, 7);
|
||||
|
||||
|
||||
var attacker = movie.characters[1];
|
||||
assert.strictEqual(attacker instanceof Test.AttackerT, true);
|
||||
assert.strictEqual(attacker instanceof AttackerT, true);
|
||||
assert.strictEqual(attacker.swordAttackDamage, 5);
|
||||
|
||||
|
||||
var bookReader2 = movie.characters[2];
|
||||
assert.strictEqual(bookReader2 instanceof Test.BookReaderT, true);
|
||||
assert.strictEqual(bookReader2 instanceof BookReaderT, true);
|
||||
assert.strictEqual(bookReader2.booksRead, 2);
|
||||
|
||||
if(isTsTest) {
|
||||
var other = movie.characters[3];
|
||||
assert.strictEqual(other, "I am other");
|
||||
}
|
||||
var other = movie.characters[3];
|
||||
assert.strictEqual(other, "I am other");
|
||||
}
|
||||
|
||||
function createMovie(fbb) {
|
||||
Test.Attacker.startAttacker(fbb);
|
||||
Test.Attacker.addSwordAttackDamage(fbb, 5);
|
||||
var attackerOffset = Test.Attacker.endAttacker(fbb);
|
||||
Attacker.startAttacker(fbb);
|
||||
Attacker.addSwordAttackDamage(fbb, 5);
|
||||
var attackerOffset = Attacker.endAttacker(fbb);
|
||||
|
||||
var charTypesOffset = Test.Movie.createCharactersTypeVector(fbb, charTypes);
|
||||
var charTypesOffset = Movie.createCharactersTypeVector(fbb, charTypes);
|
||||
var charsOffset = 0;
|
||||
|
||||
if(isTsTest) {
|
||||
let otherOffset = fbb.createString("I am other");
|
||||
let otherOffset = fbb.createString("I am other");
|
||||
|
||||
charsOffset = Test.Movie.createCharactersVector(
|
||||
fbb,
|
||||
[
|
||||
Test.BookReader.createBookReader(fbb, 7),
|
||||
attackerOffset,
|
||||
Test.BookReader.createBookReader(fbb, 2),
|
||||
otherOffset
|
||||
]
|
||||
);
|
||||
} else {
|
||||
charsOffset = Test.Movie.createCharactersVector(
|
||||
fbb,
|
||||
[
|
||||
Test.BookReader.createBookReader(fbb, 7),
|
||||
attackerOffset,
|
||||
Test.BookReader.createBookReader(fbb, 2)
|
||||
]
|
||||
);
|
||||
}
|
||||
charsOffset = Movie.createCharactersVector(
|
||||
fbb,
|
||||
[
|
||||
BookReader.createBookReader(fbb, 7),
|
||||
attackerOffset,
|
||||
BookReader.createBookReader(fbb, 2),
|
||||
otherOffset
|
||||
]
|
||||
);
|
||||
|
||||
Test.Movie.startMovie(fbb);
|
||||
Test.Movie.addCharactersType(fbb, charTypesOffset);
|
||||
Test.Movie.addCharacters(fbb, charsOffset);
|
||||
Test.Movie.finishMovieBuffer(fbb, Test.Movie.endMovie(fbb))
|
||||
Movie.startMovie(fbb);
|
||||
Movie.addCharactersType(fbb, charTypesOffset);
|
||||
Movie.addCharacters(fbb, charsOffset);
|
||||
Movie.finishMovieBuffer(fbb, Movie.endMovie(fbb))
|
||||
}
|
||||
|
||||
function main() {
|
||||
@@ -105,22 +91,20 @@ function main() {
|
||||
|
||||
var buf = new flatbuffers.ByteBuffer(fbb.asUint8Array());
|
||||
|
||||
var movie = Test.Movie.getRootAsMovie(buf);
|
||||
var movie = Movie.getRootAsMovie(buf);
|
||||
testMovieBuf(movie);
|
||||
|
||||
if(isTsTest) {
|
||||
testMovieUnpack(movie.unpack());
|
||||
testMovieUnpack(movie.unpack());
|
||||
|
||||
var movie_to = new Test.MovieT();
|
||||
movie.unpackTo(movie_to);
|
||||
testMovieUnpack(movie_to);
|
||||
|
||||
fbb.clear();
|
||||
Test.Movie.finishMovieBuffer(fbb, movie_to.pack(fbb));
|
||||
var unpackBuf = new flatbuffers.ByteBuffer(fbb.asUint8Array());
|
||||
testMovieBuf(Test.Movie.getRootAsMovie(unpackBuf));
|
||||
}
|
||||
var movie_to = new MovieT();
|
||||
movie.unpackTo(movie_to);
|
||||
testMovieUnpack(movie_to);
|
||||
|
||||
fbb.clear();
|
||||
Movie.finishMovieBuffer(fbb, movie_to.pack(fbb));
|
||||
var unpackBuf = new flatbuffers.ByteBuffer(fbb.asUint8Array());
|
||||
testMovieBuf(Movie.getRootAsMovie(unpackBuf));
|
||||
|
||||
console.log('FlatBuffers union vector test: completed successfully');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user