mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-19 05:23:04 +00:00
Partial support for --ts-flat-files and --gen-all (#7446)
* Partial support for --ts-flat-files and --gen-all * Add generated code changes * remove some debugging code left over * missed grpc files
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
table Baz {
|
||||
a:int;
|
||||
enum Baz : short {
|
||||
None = 0,
|
||||
Red,
|
||||
Green,
|
||||
Blue,
|
||||
}
|
||||
@@ -77,8 +77,22 @@ def assert_file_contains(file, needles):
|
||||
return file
|
||||
|
||||
|
||||
def assert_file_and_contents(file, needle, path=script_path, unlink=True):
|
||||
def assert_file_doesnt_contains(file, needles):
|
||||
with open(file) as file:
|
||||
contents = file.read()
|
||||
for needle in [needles] if isinstance(needles, str) else needles:
|
||||
assert needle not in contents, (
|
||||
"Found unexpected '" + needle + "' in file: " + str(file)
|
||||
)
|
||||
return file
|
||||
|
||||
|
||||
def assert_file_and_contents(
|
||||
file, needle, doesnt_contain=None, path=script_path, unlink=True
|
||||
):
|
||||
assert_file_contains(assert_file_exists(file, path), needle)
|
||||
if doesnt_contain:
|
||||
assert_file_doesnt_contains(assert_file_exists(file, path), doesnt_contain)
|
||||
if unlink:
|
||||
Path(path, file).unlink()
|
||||
|
||||
@@ -102,7 +116,7 @@ def run_all(*modules):
|
||||
module_passing = module_passing + 1
|
||||
except Exception as e:
|
||||
print(" [FAILED]: " + str(e))
|
||||
failingmodule_failing = failingmodule_failing + 1
|
||||
module_failing = module_failing + 1
|
||||
print(
|
||||
"{0}: {1} of {2} passsed".format(
|
||||
module.__name__, module_passing, module_passing + module_failing
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
from flatc_test import *
|
||||
|
||||
|
||||
class TsTests():
|
||||
|
||||
def Base(self):
|
||||
@@ -24,7 +25,10 @@ class TsTests():
|
||||
# include, bar.
|
||||
assert_file_and_contents(
|
||||
"foo_generated.ts",
|
||||
["export { Bar } from './bar';", "export { Foo } from './foo';"],
|
||||
[
|
||||
"export { Bar } from './bar';",
|
||||
"export { Foo } from './foo';",
|
||||
],
|
||||
)
|
||||
|
||||
# Foo should be generated in place and exports the Foo table.
|
||||
@@ -33,6 +37,26 @@ class TsTests():
|
||||
# Included files, like bar, should not be generated.
|
||||
assert_file_doesnt_exists("bar.ts")
|
||||
|
||||
def BaseMultipleFiles(self):
|
||||
# Generate both foo and bar with no extra arguments
|
||||
flatc(["--ts", "foo.fbs", "bar/bar.fbs"])
|
||||
|
||||
# Should generate the module that exports both foo and its direct
|
||||
# include, bar.
|
||||
assert_file_and_contents(
|
||||
"foo_generated.ts",
|
||||
[
|
||||
"export { Bar } from './bar';",
|
||||
"export { Foo } from './foo';",
|
||||
],
|
||||
)
|
||||
|
||||
# Foo should be generated in place and exports the Foo table.
|
||||
assert_file_and_contents("foo.ts", "export class Foo {")
|
||||
|
||||
# Bar should also be generatd in place and exports the Bar table.
|
||||
assert_file_and_contents("bar.ts", "export class Bar {")
|
||||
|
||||
def BaseWithNamespace(self):
|
||||
# Generate foo with namespacing, with no extra arguments
|
||||
flatc(["--ts", "foo_with_ns.fbs"])
|
||||
@@ -41,29 +65,132 @@ class TsTests():
|
||||
# directory and its direct include, bar.
|
||||
assert_file_and_contents(
|
||||
"foo_with_ns_generated.ts",
|
||||
["export { Bar } from './bar';", "export { Foo } from './something/foo';"],
|
||||
[
|
||||
"export { Bar } from './bar';",
|
||||
"export { Foo } from './something/foo';",
|
||||
],
|
||||
)
|
||||
|
||||
# Foo should be placed in the namespaced directory. It should export
|
||||
# Foo, and the import of Bar should be relative to its location.
|
||||
assert_file_and_contents(
|
||||
"something/foo.ts",
|
||||
["export class Foo {", "import { Bar } from '../bar';"],
|
||||
[
|
||||
"export class Foo {",
|
||||
"import { Bar } from '../bar';",
|
||||
],
|
||||
)
|
||||
|
||||
# Included files, like bar, should not be generated.
|
||||
assert_file_doesnt_exists("bar.ts")
|
||||
|
||||
def GenAll(self):
|
||||
# Generate foo with generate all options
|
||||
flatc(["--ts", "--gen-all", "foo.fbs"])
|
||||
|
||||
# Should generate a single file that exports all the generated types.
|
||||
assert_file_and_contents(
|
||||
"foo_generated.ts",
|
||||
[
|
||||
"export { Bar } from './bar'",
|
||||
"export { Baz } from './baz'",
|
||||
"export { Foo } from './foo'",
|
||||
],
|
||||
)
|
||||
|
||||
# Foo should be generated with an import to Bar and an export of itself.
|
||||
assert_file_and_contents(
|
||||
"foo.ts",
|
||||
[
|
||||
"import { Bar } from './bar';",
|
||||
"export class Foo {",
|
||||
],
|
||||
)
|
||||
|
||||
# Bar should be generated with an import to Baz and an export of itself.
|
||||
assert_file_and_contents(
|
||||
"bar.ts",
|
||||
[
|
||||
"import { Baz } from './baz';",
|
||||
"export class Bar {",
|
||||
],
|
||||
)
|
||||
|
||||
# Baz should be generated with an export of itself.
|
||||
assert_file_and_contents(
|
||||
"baz.ts",
|
||||
[
|
||||
"export enum Baz {",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
def FlatFiles(self):
|
||||
# Generate just foo the flat files option
|
||||
# Generate just foo with the flat files option
|
||||
flatc(["--ts", "--ts-flat-files", "foo.fbs"])
|
||||
|
||||
# Should generate a single file that imports bar as a single file, and]
|
||||
# Should generate a single file that imports bar as a single file, and
|
||||
# exports the Foo table.
|
||||
assert_file_and_contents(
|
||||
"foo_generated.ts",
|
||||
["import {Bar as Bar} from './bar_generated';", "export class Foo {"],
|
||||
[
|
||||
"import {Bar as Bar} from './bar_generated';",
|
||||
"export class Foo {",
|
||||
],
|
||||
)
|
||||
|
||||
# The root type Foo should not be generated in its own file.
|
||||
assert_file_doesnt_exists("foo.ts")
|
||||
|
||||
def FlatFilesMultipleFiles(self):
|
||||
# Generate both foo and bar with the flat files option
|
||||
flatc(["--ts", "--ts-flat-files", "foo.fbs", "bar/bar.fbs"])
|
||||
|
||||
# Should generate a single foo file that imports bar as a single file,
|
||||
# and exports the Foo table.
|
||||
assert_file_and_contents(
|
||||
"foo_generated.ts",
|
||||
[
|
||||
"import {Bar as Bar} from './bar_generated';",
|
||||
"export class Foo {",
|
||||
],
|
||||
)
|
||||
|
||||
# Should generate a single bar file that imports bar as a single file,
|
||||
# and exports the Bar table.
|
||||
assert_file_and_contents(
|
||||
"bar_generated.ts",
|
||||
[
|
||||
"import {Baz as Baz} from './baz_generated';",
|
||||
"export class Bar {",
|
||||
],
|
||||
)
|
||||
|
||||
# The types Foo and Bar should not be generated in their own files
|
||||
assert_file_doesnt_exists("foo.ts")
|
||||
assert_file_doesnt_exists("bar.ts")
|
||||
|
||||
def FlatFilesGenAll(self):
|
||||
# Generate foo with all of its dependents with the flat files option
|
||||
flatc(["--ts", "--ts-flat-files", "--gen-all", "foo.fbs"])
|
||||
|
||||
# Should generate a single foo file
|
||||
assert_file_and_contents(
|
||||
"foo_generated.ts",
|
||||
# Should export each of the types within the single file
|
||||
[
|
||||
"export class Foo {",
|
||||
"export class Bar {",
|
||||
"export enum Baz {",
|
||||
],
|
||||
# No includes for the dependent types should be present.
|
||||
doesnt_contain=[
|
||||
"import {Bar as Bar}",
|
||||
"import {Baz as Baz}",
|
||||
],
|
||||
)
|
||||
|
||||
# The types Foo, Bar and Baz should not be generated in their own files.
|
||||
assert_file_doesnt_exists("foo.ts")
|
||||
assert_file_doesnt_exists("bar.ts")
|
||||
assert_file_doesnt_exists("baz.ts")
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
export { Monster } from './my-game/example/monster';
|
||||
export { Monster as MyGameExample2Monster, MonsterT as MyGameExample2MonsterT } from './my-game/example2/monster';
|
||||
export { Ability, AbilityT } from './my-game/example/ability';
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as flatbuffers from 'flatbuffers';
|
||||
export class Ability {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Ability {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Ability {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import { Monster, MonsterT } from '../../my-game/example/monster';
|
||||
|
||||
|
||||
export enum AnyAmbiguousAliases{
|
||||
export enum AnyAmbiguousAliases {
|
||||
NONE = 0,
|
||||
M1 = 1,
|
||||
M2 = 2,
|
||||
|
||||
@@ -5,7 +5,7 @@ import { Monster, MonsterT } from '../../my-game/example/monster';
|
||||
import { TestSimpleTableWithEnum, TestSimpleTableWithEnumT } from '../../my-game/example/test-simple-table-with-enum';
|
||||
|
||||
|
||||
export enum AnyUniqueAliases{
|
||||
export enum AnyUniqueAliases {
|
||||
NONE = 0,
|
||||
M = 1,
|
||||
TS = 2,
|
||||
|
||||
@@ -5,7 +5,7 @@ import { Monster, MonsterT } from '../../my-game/example/monster';
|
||||
import { TestSimpleTableWithEnum, TestSimpleTableWithEnumT } from '../../my-game/example/test-simple-table-with-enum';
|
||||
|
||||
|
||||
export enum Any{
|
||||
export enum Any {
|
||||
NONE = 0,
|
||||
Monster = 1,
|
||||
TestSimpleTableWithEnum = 2,
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
/**
|
||||
* Composite components of Monster color.
|
||||
*/
|
||||
export enum Color{
|
||||
export enum Color {
|
||||
Red = 1,
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
export enum LongEnum{
|
||||
export enum LongEnum {
|
||||
LongOne = '2',
|
||||
LongTwo = '4',
|
||||
LongBig = '1099511627776'
|
||||
|
||||
@@ -23,7 +23,7 @@ import { InParentNamespace, InParentNamespaceT } from '../../my-game/in-parent-n
|
||||
export class Monster {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Monster {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Monster {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
export enum Race{
|
||||
export enum Race {
|
||||
None = -1,
|
||||
Human = 0,
|
||||
Dwarf = 1,
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as flatbuffers from 'flatbuffers';
|
||||
export class Referrable {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Referrable {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Referrable {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as flatbuffers from 'flatbuffers';
|
||||
export class Stat {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Stat {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Stat {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -8,7 +8,7 @@ import { StructOfStructs, StructOfStructsT } from '../../my-game/example/struct-
|
||||
export class StructOfStructsOfStructs {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):StructOfStructsOfStructs {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):StructOfStructsOfStructs {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -9,7 +9,7 @@ import { Test, TestT } from '../../my-game/example/test';
|
||||
export class StructOfStructs {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):StructOfStructs {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):StructOfStructs {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -8,7 +8,7 @@ import { Color } from '../../my-game/example/color';
|
||||
export class TestSimpleTableWithEnum {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):TestSimpleTableWithEnum {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):TestSimpleTableWithEnum {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as flatbuffers from 'flatbuffers';
|
||||
export class Test {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Test {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Test {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as flatbuffers from 'flatbuffers';
|
||||
export class TypeAliases {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):TypeAliases {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):TypeAliases {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -9,7 +9,7 @@ import { Test, TestT } from '../../my-game/example/test';
|
||||
export class Vec3 {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Vec3 {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Vec3 {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as flatbuffers from 'flatbuffers';
|
||||
export class Monster {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Monster {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Monster {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as flatbuffers from 'flatbuffers';
|
||||
export class InParentNamespace {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):InParentNamespace {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):InParentNamespace {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
export enum OptionalByte{
|
||||
export enum OptionalByte {
|
||||
None = 0,
|
||||
One = 1,
|
||||
Two = 2
|
||||
|
||||
@@ -8,7 +8,7 @@ import { OptionalByte } from '../optional-scalars/optional-byte';
|
||||
export class ScalarStuff {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):ScalarStuff {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):ScalarStuff {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
export { ScalarStuff } from './optional-scalars/scalar-stuff';
|
||||
export { OptionalByte } from './optional-scalars/optional-byte';
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as flatbuffers from 'flatbuffers';
|
||||
export class Attacker {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Attacker {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Attacker {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as flatbuffers from 'flatbuffers';
|
||||
export class BookReader {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):BookReader {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):BookReader {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -5,7 +5,7 @@ import { BookReader, BookReaderT } from './book-reader';
|
||||
import { Rapunzel, RapunzelT } from './rapunzel';
|
||||
|
||||
|
||||
export enum Character{
|
||||
export enum Character {
|
||||
NONE = 0,
|
||||
MuLan = 1,
|
||||
Rapunzel = 2,
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as flatbuffers from 'flatbuffers';
|
||||
export class FallingTub {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):FallingTub {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):FallingTub {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -4,7 +4,7 @@ import { FallingTub, FallingTubT } from './falling-tub';
|
||||
import { HandFan, HandFanT } from './hand-fan';
|
||||
|
||||
|
||||
export enum Gadget{
|
||||
export enum Gadget {
|
||||
NONE = 0,
|
||||
FallingTub = 1,
|
||||
HandFan = 2
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as flatbuffers from 'flatbuffers';
|
||||
export class HandFan {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):HandFan {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):HandFan {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -11,7 +11,7 @@ import { Rapunzel, RapunzelT } from './rapunzel';
|
||||
export class Movie {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Movie {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Movie {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -7,7 +7,7 @@ import * as flatbuffers from 'flatbuffers';
|
||||
export class Rapunzel {
|
||||
bb: flatbuffers.ByteBuffer|null = null;
|
||||
bb_pos = 0;
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Rapunzel {
|
||||
__init(i:number, bb:flatbuffers.ByteBuffer):Rapunzel {
|
||||
this.bb_pos = i;
|
||||
this.bb = bb;
|
||||
return this;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
export { Attacker, AttackerT } from './attacker';
|
||||
export { BookReader, BookReaderT } from './book-reader';
|
||||
export { Character, unionToCharacter, unionListToCharacter } from './character';
|
||||
|
||||
Reference in New Issue
Block a user