[TypeScript] Fix namespaceless schema generation (#7432)

* Add tests for ts flatc

* fix typescript generation of namespaceless file

* Add typescript genereated code from generate_code.py

* Support multiple module flatc testing
This commit is contained in:
Derek Bailey
2022-08-14 11:34:19 -07:00
committed by GitHub
parent 83d4e2a100
commit fa1174aa7b
8 changed files with 204 additions and 55 deletions

View File

@@ -1,5 +1,3 @@
#!/usr/bin/env python3
#
# Copyright 2022 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,10 +13,7 @@
# limitations under the License.
import argparse
import filecmp
import glob
import platform
import shutil
import subprocess
from pathlib import Path
@@ -53,7 +48,7 @@ assert flatc_path.exists(), "Cannot find the flatc compiler " + str(flatc_path)
# Execute the flatc compiler with the specified parameters
def flatc(options, cwd=script_path):
cmd = [str(flatc_path)] + options
result = subprocess.run(cmd, cwd=str(cwd), check=True)
subprocess.check_call(cmd, cwd=str(cwd))
def make_absolute(filename, path=script_path):
@@ -66,33 +61,53 @@ def assert_file_exists(filename, path=script_path):
return file
def assert_file_contains(file, needle):
assert needle in open(file).read(), (
"coudn't find '" + needle + "' in file: " + str(file)
)
def assert_file_doesnt_exists(filename, path=script_path):
file = Path(path, filename)
assert not file.exists(), "file exists but shouldn't: " + filename
return file
def assert_file_and_contents(file, needle, path=script_path):
assert_file_contains(assert_file_exists(file, path), needle).unlink()
def assert_file_contains(file, needles):
with open(file) as file:
contents = file.read()
for needle in [needles] if isinstance(needles, str) else needles:
assert needle in contents, (
"coudn't find '" + needle + "' in file: " + str(file)
)
return file
def run_all(module):
methods = [
func
for func in dir(module)
if callable(getattr(module, func)) and not func.startswith("__")
]
def assert_file_and_contents(file, needle, path=script_path, unlink=True):
assert_file_contains(assert_file_exists(file, path), needle)
if unlink:
Path(path, file).unlink()
def run_all(*modules):
failing = 0
passing = 0
for method in methods:
try:
print(method)
getattr(module, method)(module)
print(" [PASSED]")
passing = passing + 1
except Exception as e:
print(" [FAILED]: " + str(e))
failing = failing + 1
print("{0}: {1} of {2} passsed".format(module.__name__, passing, passing + failing))
for module in modules:
methods = [
func
for func in dir(module)
if callable(getattr(module, func)) and not func.startswith("__")
]
module_failing = 0
module_passing = 0
for method in methods:
try:
print("{0}.{1}".format(module.__name__, method))
getattr(module, method)(module)
print(" [PASSED]")
module_passing = module_passing + 1
except Exception as e:
print(" [FAILED]: " + str(e))
failingmodule_failing = failingmodule_failing + 1
print(
"{0}: {1} of {2} passsed".format(
module.__name__, module_passing, module_passing + module_failing
)
)
passing = passing + module_passing
failing = failing + module_failing
return passing, failing

69
tests/flatc/flatc_ts_tests.py Executable file
View File

@@ -0,0 +1,69 @@
# Copyright 2022 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from flatc_test import *
class TsTests():
def Base(self):
# Generate just foo with no extra arguments
flatc(["--ts", "foo.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 {")
# Included files, like bar, should not be generated.
assert_file_doesnt_exists("bar.ts")
def BaseWithNamespace(self):
# Generate foo with namespacing, with no extra arguments
flatc(["--ts", "foo_with_ns.fbs"])
# Should generate the module that exports both foo in its namespace
# directory and its direct include, bar.
assert_file_and_contents(
"foo_with_ns_generated.ts",
["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';"],
)
# Included files, like bar, should not be generated.
assert_file_doesnt_exists("bar.ts")
def FlatFiles(self):
# Generate just foo the flat files option
flatc(["--ts", "--ts-flat-files", "foo.fbs"])
# 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 {"],
)
# The root type Foo should not be generated in its own file.
assert_file_doesnt_exists("foo.ts")

View File

@@ -0,0 +1,9 @@
include "bar/bar.fbs";
namespace something;
table Foo {
bar:Bar;
}
root_type Foo;

View File

@@ -1,11 +1,29 @@
#!/usr/bin/env python3
#
# Copyright 2022 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys;
import sys
from flatc_test import run_all
from flatc_cpp_tests import CppTests
from flatc_ts_tests import TsTests
passing, failing = run_all(CppTests)
passing, failing = run_all(CppTests, TsTests)
print("")
print("{0} of {1} tests passed".format(passing, passing + failing))
if failing > 0:
sys.exit(1)
sys.exit(1)