Skip to content

Commit

Permalink
Move TS tests to dedicated folder and deps upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornharrtell committed Sep 3, 2022
1 parent 4c95418 commit db88c1f
Show file tree
Hide file tree
Showing 119 changed files with 15,068 additions and 1,187 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -133,6 +133,7 @@ grpc/google/
**/Package.resolved
.clangd/**
package-lock.json
yarn.lock
/*.ilk
/*.pdb
.clwb
Expand Down
16 changes: 8 additions & 8 deletions package.json
Expand Up @@ -16,7 +16,7 @@
"test": "tests"
},
"scripts": {
"test": "npm run compile && cd tests && python3 ./TypeScriptTest.py",
"test": "npm run compile && cd tests/ts && python3 ./TypeScriptTest.py",
"compile": "tsc && tsc -p tsconfig.mjs.json && rollup -c",
"prepublishOnly": "npm install --only=dev && npm run compile"
},
Expand All @@ -35,12 +35,12 @@
"homepage": "https://google.github.io/flatbuffers/",
"dependencies": {},
"devDependencies": {
"@bazel/typescript": "^5.2.0",
"@types/node": "17.0.21",
"@typescript-eslint/eslint-plugin": "^4.12.0",
"@typescript-eslint/parser": "^4.12.0",
"eslint": "^7.17.0",
"rollup": "^2.78.0",
"typescript": "^4.5.5"
"@bazel/typescript": "^5.5.3",
"@types/node": "18.7.14",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"eslint": "^8.23.0",
"rollup": "^2.79.0",
"typescript": "^4.8.2"
}
}
4 changes: 2 additions & 2 deletions src/idl_gen_ts.cpp
Expand Up @@ -760,9 +760,9 @@ class TsGenerator : public BaseGenerator {
import.bare_file_path = bare_file_path;
import.rel_file_path = rel_file_path;
import.import_statement =
"import { " + symbols_expression + " } from '" + rel_file_path + "';";
"import { " + symbols_expression + " } from '" + rel_file_path + ".js';";
import.export_statement =
"export { " + symbols_expression + " } from '." + bare_file_path + "';";
"export { " + symbols_expression + " } from '." + bare_file_path + ".js';";
import.dependency = &dependency;
import.dependent = &dependent;

Expand Down
7 changes: 0 additions & 7 deletions tests/package.json

This file was deleted.

@@ -1,7 +1,7 @@
// Run this using JavaScriptTest.sh
import assert from 'assert'
import fs from 'fs'
import * as flexbuffers from 'flatbuffers/js/flexbuffers'
import * as flexbuffers from 'flatbuffers/js/flexbuffers.js'

function main() {
testSingleValueBuffers();
Expand Down Expand Up @@ -341,7 +341,7 @@ function testRoundTripWithBuilder() {
}

function testGoldBuffer() {
const data = new Uint8Array(fs.readFileSync('gold_flexbuffer_example.bin')).buffer;
const data = new Uint8Array(fs.readFileSync('../gold_flexbuffer_example.bin')).buffer;
const b1 = flexbuffers.toReference(data).get("bools").get(1);
assert.strictEqual(b1.isBool(), true);
assert.strictEqual(b1.boolValue(), false);
Expand Down
18 changes: 9 additions & 9 deletions tests/JavaScriptTest.js → tests/ts/JavaScriptTest.js
Expand Up @@ -3,18 +3,18 @@ import assert from 'assert'
import fs from 'fs'
import * as flatbuffers from 'flatbuffers'

import { Monster, MonsterT } from './my-game/example/monster'
import { Test, TestT } from './my-game/example/test'
import { Stat } from './my-game/example/stat'
import { Vec3 } from './my-game/example/vec3'
import { Color } from './my-game/example/color';
import { Any } from './my-game/example/any';
import { Monster, MonsterT } from './my-game/example/monster.js'
import { Test, TestT } from './my-game/example/test.js'
import { Stat } from './my-game/example/stat.js'
import { Vec3 } from './my-game/example/vec3.js'
import { Color } from './my-game/example/color.js';
import { Any } from './my-game/example/any.js';

function main() {

// First, let's test reading a FlatBuffer generated by C++ code:
// This file was generated from monsterdata_test.json
var data = new Uint8Array(fs.readFileSync('monsterdata_test.mon'));
var data = new Uint8Array(fs.readFileSync('../monsterdata_test.mon'));

// Now test it:

Expand Down Expand Up @@ -285,8 +285,8 @@ function test64bit() {
}

function testUnicode() {
var correct = fs.readFileSync('unicode_test.mon');
var json = JSON.parse(fs.readFileSync('unicode_test.json', 'utf8'));
var correct = fs.readFileSync('../unicode_test.mon');
var json = JSON.parse(fs.readFileSync('../unicode_test.json', 'utf8'));

// Test reading
function testReadingUnicode(bb) {
Expand Down
@@ -1,10 +1,10 @@
import assert from 'assert'
import * as flatbuffers from 'flatbuffers'

import { Character } from './union_vector/character'
import { BookReader, BookReaderT } from './union_vector/book-reader'
import { Attacker, AttackerT } from './union_vector/attacker'
import { Movie, MovieT } from './union_vector/movie'
import { Character } from './union_vector/character.js'
import { BookReader, BookReaderT } from './union_vector/book-reader.js'
import { Attacker, AttackerT } from './union_vector/attacker.js'
import { Movie, MovieT } from './union_vector/movie.js'

var charTypes = [
Character.Belle,
Expand Down
26 changes: 13 additions & 13 deletions tests/TypeScriptTest.py → tests/ts/TypeScriptTest.py
Expand Up @@ -25,7 +25,7 @@
tests_path = Path(__file__).parent.resolve()

# Get the root path as an absolute path, so all derived paths are absolute.
root_path = tests_path.parent.absolute()
root_path = tests_path.parent.parent.absolute()

# Windows works with subprocess.run a bit differently.
is_windows = platform.system() == "Windows"
Expand Down Expand Up @@ -62,26 +62,26 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)
print("Invoking flatc...")
flatc(
options=["--ts", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
schema="monster_test.fbs",
include="include_test",
schema="../monster_test.fbs",
include="../include_test",
)

flatc(
options=["--gen-object-api", "-b"],
schema="monster_test.fbs",
include="include_test",
data="unicode_test.json",
schema="../monster_test.fbs",
include="../include_test",
data="../unicode_test.json",
)

flatc(
options=["--ts", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
schema="union_vector/union_vector.fbs",
schema="../union_vector/union_vector.fbs",
prefix="union_vector",
)

flatc(
options=["--ts", "--gen-name-strings"],
schema="optional_scalars.fbs",
schema="../optional_scalars.fbs",
)

flatc(
Expand All @@ -90,9 +90,9 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)
"typescript_keywords.fbs",
"test_dir/typescript_include.fbs",
"test_dir/typescript_transitive_include.fbs",
"../reflection/reflection.fbs",
"../../reflection/reflection.fbs",
],
include="../",
include="../../",
)

flatc(
Expand All @@ -107,15 +107,15 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)
"typescript_keywords.fbs",
"test_dir/typescript_include.fbs",
"test_dir/typescript_transitive_include.fbs",
"../reflection/reflection.fbs",
"../../reflection/reflection.fbs",
],
include="../",
include="../../",
)

print("Running TypeScript Compiler...")
check_call(["tsc"])

NODE_CMD = ["node", "-r", "esm"]
NODE_CMD = ["node"]

print("Running TypeScript Tests...")
check_call(NODE_CMD + ["JavaScriptTest"])
Expand Down
5 changes: 5 additions & 0 deletions tests/ts/foobar/abc.js
@@ -0,0 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
export var Abc;
(function (Abc) {
Abc[Abc["a"] = 0] = "a";
})(Abc || (Abc = {}));
5 changes: 5 additions & 0 deletions tests/ts/foobar/abc.ts
@@ -0,0 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify

export enum Abc {
a = 0
}
5 changes: 5 additions & 0 deletions tests/ts/foobar/class.js
@@ -0,0 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
export var class_;
(function (class_) {
class_[class_["arguments_"] = 0] = "arguments_";
})(class_ || (class_ = {}));
5 changes: 5 additions & 0 deletions tests/ts/foobar/class.ts
@@ -0,0 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify

export enum class_ {
arguments_ = 0
}
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions tests/ts/monster_test_generated.ts
@@ -0,0 +1,19 @@
// automatically generated by the FlatBuffers compiler, do not modify

export { Monster as MyGame_Example2_Monster, MonsterT as MyGame_Example2_MonsterT } from './my-game/example2/monster.js';
export { Ability, AbilityT } from './my-game/example/ability.js';
export { Any, unionToAny, unionListToAny } from './my-game/example/any.js';
export { AnyAmbiguousAliases, unionToAnyAmbiguousAliases, unionListToAnyAmbiguousAliases } from './my-game/example/any-ambiguous-aliases.js';
export { AnyUniqueAliases, unionToAnyUniqueAliases, unionListToAnyUniqueAliases } from './my-game/example/any-unique-aliases.js';
export { Color } from './my-game/example/color.js';
export { Monster, MonsterT } from './my-game/example/monster.js';
export { Race } from './my-game/example/race.js';
export { Referrable, ReferrableT } from './my-game/example/referrable.js';
export { Stat, StatT } from './my-game/example/stat.js';
export { StructOfStructs, StructOfStructsT } from './my-game/example/struct-of-structs.js';
export { StructOfStructsOfStructs, StructOfStructsOfStructsT } from './my-game/example/struct-of-structs-of-structs.js';
export { Test, TestT } from './my-game/example/test.js';
export { TestSimpleTableWithEnum, TestSimpleTableWithEnumT } from './my-game/example/test-simple-table-with-enum.js';
export { TypeAliases, TypeAliasesT } from './my-game/example/type-aliases.js';
export { Vec3, Vec3T } from './my-game/example/vec3.js';
export { InParentNamespace, InParentNamespaceT } from './my-game/in-parent-namespace.js';
File renamed without changes.
File renamed without changes.
Binary file added tests/ts/monsterdata_javascript_wire.mon
Binary file not shown.
54 changes: 54 additions & 0 deletions tests/ts/my-game/example/ability.js
@@ -0,0 +1,54 @@
// automatically generated by the FlatBuffers compiler, do not modify
export class Ability {
constructor() {
this.bb = null;
this.bb_pos = 0;
}
__init(i, bb) {
this.bb_pos = i;
this.bb = bb;
return this;
}
id() {
return this.bb.readUint32(this.bb_pos);
}
mutate_id(value) {
this.bb.writeUint32(this.bb_pos + 0, value);
return true;
}
distance() {
return this.bb.readUint32(this.bb_pos + 4);
}
mutate_distance(value) {
this.bb.writeUint32(this.bb_pos + 4, value);
return true;
}
static getFullyQualifiedName() {
return 'MyGame_Example_Ability';
}
static sizeOf() {
return 8;
}
static createAbility(builder, id, distance) {
builder.prep(4, 8);
builder.writeInt32(distance);
builder.writeInt32(id);
return builder.offset();
}
unpack() {
return new AbilityT(this.id(), this.distance());
}
unpackTo(_o) {
_o.id = this.id();
_o.distance = this.distance();
}
}
export class AbilityT {
constructor(id = 0, distance = 0) {
this.id = id;
this.distance = distance;
}
pack(builder) {
return Ability.createAbility(builder, this.id, this.distance);
}
}
77 changes: 77 additions & 0 deletions tests/ts/my-game/example/ability.ts
@@ -0,0 +1,77 @@
// automatically generated by the FlatBuffers compiler, do not modify

import * as flatbuffers from 'flatbuffers';



export class Ability {
bb: flatbuffers.ByteBuffer|null = null;
bb_pos = 0;
__init(i:number, bb:flatbuffers.ByteBuffer):Ability {
this.bb_pos = i;
this.bb = bb;
return this;
}

id():number {
return this.bb!.readUint32(this.bb_pos);
}

mutate_id(value:number):boolean {
this.bb!.writeUint32(this.bb_pos + 0, value);
return true;
}

distance():number {
return this.bb!.readUint32(this.bb_pos + 4);
}

mutate_distance(value:number):boolean {
this.bb!.writeUint32(this.bb_pos + 4, value);
return true;
}

static getFullyQualifiedName():string {
return 'MyGame_Example_Ability';
}

static sizeOf():number {
return 8;
}

static createAbility(builder:flatbuffers.Builder, id: number, distance: number):flatbuffers.Offset {
builder.prep(4, 8);
builder.writeInt32(distance);
builder.writeInt32(id);
return builder.offset();
}


unpack(): AbilityT {
return new AbilityT(
this.id(),
this.distance()
);
}


unpackTo(_o: AbilityT): void {
_o.id = this.id();
_o.distance = this.distance();
}
}

export class AbilityT {
constructor(
public id: number = 0,
public distance: number = 0
){}


pack(builder:flatbuffers.Builder): flatbuffers.Offset {
return Ability.createAbility(builder,
this.id,
this.distance
);
}
}

0 comments on commit db88c1f

Please sign in to comment.