1# Copyright 2022 Google Inc. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15from flatc_test import * 16 17 18class TsTests(): 19 20 def Base(self): 21 # Generate just foo with no extra arguments 22 flatc(["--ts", "foo.fbs"]) 23 24 # Should generate the module that exports both foo and its direct 25 # include, bar. 26 assert_file_and_contents( 27 "foo_generated.ts", 28 [ 29 "export { Bar } from './bar';", 30 "export { Foo } from './foo';", 31 ], 32 ) 33 34 # Foo should be generated in place and exports the Foo table. 35 assert_file_and_contents("foo.ts", "export class Foo {") 36 37 # Included files, like bar, should not be generated. 38 assert_file_doesnt_exists("bar.ts") 39 40 def BaseMultipleFiles(self): 41 # Generate both foo and bar with no extra arguments 42 flatc(["--ts", "foo.fbs", "bar/bar.fbs"]) 43 44 # Should generate the module that exports both foo and its direct 45 # include, bar. 46 assert_file_and_contents( 47 "foo_generated.ts", 48 [ 49 "export { Bar } from './bar';", 50 "export { Foo } from './foo';", 51 ], 52 ) 53 54 # Foo should be generated in place and exports the Foo table. 55 assert_file_and_contents("foo.ts", "export class Foo {") 56 57 # Bar should also be generatd in place and exports the Bar table. 58 assert_file_and_contents("bar.ts", "export class Bar {") 59 60 def BaseWithNamespace(self): 61 # Generate foo with namespacing, with no extra arguments 62 flatc(["--ts", "foo_with_ns.fbs"]) 63 64 # Should generate the module that exports both foo in its namespace 65 # directory and its direct include, bar. 66 assert_file_and_contents( 67 "foo_with_ns_generated.ts", 68 [ 69 "export { Bar } from './bar/bar';", 70 "export { Foo } from './something/foo';", 71 ], 72 ) 73 74 # Foo should be placed in the namespaced directory. It should export 75 # Foo, and the import of Bar should be relative to its location. 76 assert_file_and_contents( 77 "something/foo.ts", 78 [ 79 "export class Foo {", 80 "import { Bar } from '../bar/bar';", 81 ], 82 ) 83 84 # Included files, like bar, should not be generated. 85 assert_file_doesnt_exists("bar.ts") 86 87 def GenAll(self): 88 # Generate foo with generate all options 89 flatc(["--ts", "--gen-all", "foo.fbs"]) 90 91 # Should generate a single file that exports all the generated types. 92 assert_file_and_contents( 93 "foo_generated.ts", 94 [ 95 "export { Bar } from './bar'", 96 "export { Baz } from './baz'", 97 "export { Foo } from './foo'", 98 ], 99 ) 100 101 # Foo should be generated with an import to Bar and an export of itself. 102 assert_file_and_contents( 103 "foo.ts", 104 [ 105 "import { Bar } from './bar';", 106 "export class Foo {", 107 ], 108 ) 109 110 # Bar should be generated with an import to Baz and an export of itself. 111 assert_file_and_contents( 112 "bar.ts", 113 [ 114 "import { Baz } from './baz';", 115 "export class Bar {", 116 ], 117 ) 118 119 # Baz should be generated with an export of itself. 120 assert_file_and_contents( 121 "baz.ts", 122 [ 123 "export enum Baz {", 124 ], 125 ) 126 127 128 def FlatFiles(self): 129 # Generate just foo with the flat files option 130 flatc(["--ts", "--ts-flat-files", "foo.fbs"]) 131 132 # Should generate a single file that imports bar as a single file, and 133 # exports the Foo table. 134 assert_file_and_contents( 135 "foo_generated.ts", 136 [ 137 "import {Bar as Bar} from './bar_generated';", 138 "export class Foo {", 139 ], 140 ) 141 142 # The root type Foo should not be generated in its own file. 143 assert_file_doesnt_exists("foo.ts") 144 145 def FlatFilesWithNamespace(self): 146 # Generate just foo with the flat files option 147 flatc(["--ts", "--ts-flat-files", "foo_with_ns.fbs"]) 148 149 # Should generate a single file that imports bar as a single file, and 150 # exports the Foo table. 151 assert_file_and_contents( 152 "foo_with_ns_generated.ts", 153 [ 154 "import {Bar as Bar} from './bar_with_ns_generated';", 155 "export class Foo {", 156 ], 157 ) 158 159 # The root type Foo should not be generated in its own file. 160 assert_file_doesnt_exists("foo.ts") 161 162 def FlatFilesMultipleFiles(self): 163 # Generate both foo and bar with the flat files option 164 flatc(["--ts", "--ts-flat-files", "foo.fbs", "bar/bar.fbs"]) 165 166 # Should generate a single foo file that imports bar as a single file, 167 # and exports the Foo table. 168 assert_file_and_contents( 169 "foo_generated.ts", 170 [ 171 "import {Bar as Bar} from './bar_generated';", 172 "export class Foo {", 173 ], 174 ) 175 176 # Should generate a single bar file that imports bar as a single file, 177 # and exports the Bar table. 178 assert_file_and_contents( 179 "bar_generated.ts", 180 [ 181 "import {Baz as Baz} from './baz_generated';", 182 "export class Bar {", 183 ], 184 ) 185 186 # The types Foo and Bar should not be generated in their own files 187 assert_file_doesnt_exists("foo.ts") 188 assert_file_doesnt_exists("bar.ts") 189 190 def FlatFilesGenAll(self): 191 # Generate foo with all of its dependents with the flat files option 192 flatc(["--ts", "--ts-flat-files", "--gen-all", "foo.fbs"]) 193 194 # Should generate a single foo file 195 assert_file_and_contents( 196 "foo_generated.ts", 197 # Should export each of the types within the single file 198 [ 199 "export class Foo {", 200 "export class Bar {", 201 "export enum Baz {", 202 ], 203 # No includes for the dependent types should be present. 204 doesnt_contain=[ 205 "import {Bar as Bar}", 206 "import {Baz as Baz}", 207 ], 208 ) 209 210 # The types Foo, Bar and Baz should not be generated in their own files. 211 assert_file_doesnt_exists("foo.ts") 212 assert_file_doesnt_exists("bar.ts") 213 assert_file_doesnt_exists("baz.ts") 214 215 216 def ZFlatFilesGenAllWithNamespacing(self): 217 # Generate foo with all of its dependents with the flat files option 218 flatc(["--ts", "--ts-flat-files", "--gen-all", "foo_with_ns.fbs"]) 219 220 # Should generate a single foo file 221 assert_file_and_contents( 222 "foo_with_ns_generated.ts", 223 # Should export each of the types within the single file 224 [ 225 "export class bar_Bar {", 226 "export class bar_Foo {", 227 "export enum Baz {", 228 "export enum baz_Baz {", 229 "export class something_Foo {" 230 ], 231 # No includes for the dependent types should be present. 232 doesnt_contain=[ 233 "import {Bar as Bar}", 234 "import {Baz as Baz}", 235 ], 236 ) 237 238 # The types Foo, Bar and Baz should not be generated in their own files. 239 assert_file_doesnt_exists("foo.ts") 240 assert_file_doesnt_exists("bar.ts") 241 assert_file_doesnt_exists("baz.ts") 242 243