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 CppTests: 19 def Flatten(self): 20 # Generate just foo with a "flatten" import of bar. 21 flatc(["--cpp", "foo.fbs"]) 22 23 # Foo should be generated in place and include bar flatten 24 assert_file_and_contents("foo_generated.h", '#include "bar_generated.h"') 25 26 def FlattenAbsolutePath(self): 27 # Generate just foo with a "flatten" import of bar. 28 flatc(["--cpp", make_absolute("foo.fbs")]) 29 30 # Foo should be generated in place and include bar flatten 31 assert_file_and_contents("foo_generated.h", '#include "bar_generated.h"') 32 33 def FlattenSubDirectory(self): 34 # Generate just foo with a "flatten" import of bar. 35 flatc(["--cpp", "bar/bar.fbs"]) 36 37 # Bar should be generated in place and include baz 38 assert_file_and_contents("bar_generated.h", '#include "baz_generated.h"') 39 40 def FlattenOutPath(self): 41 # Generate just foo with a "flatten" import of bar. 42 flatc(["--cpp", "-o", ".tmp", "foo.fbs"]) 43 44 # Foo should be generated in the out path and include bar flatten to the out path. 45 assert_file_and_contents(".tmp/foo_generated.h", '#include "bar_generated.h"') 46 47 def FlattenOutPathSuperDirectory(self): 48 # Generate just foo with a "flatten" import of bar. 49 flatc(["--cpp", "-o", "../.tmp", "foo.fbs"]) 50 51 # Foo should be generated in the out path and include bar flatten to the out path. 52 assert_file_and_contents( 53 "../.tmp/foo_generated.h", '#include "bar_generated.h"' 54 ) 55 56 def FlattenOutPathSubDirectory(self): 57 # Generate just foo with a "flatten" import of bar. 58 flatc(["--cpp", "-o", ".tmp", "bar/bar.fbs"]) 59 60 # Bar should be generated in the out path and include baz flatten to the out path. 61 assert_file_and_contents(".tmp/bar_generated.h", '#include "baz_generated.h"') 62 63 def KeepPrefix(self): 64 # Generate just foo with the import of bar keeping the prefix of where it is located. 65 flatc(["--cpp", "--keep-prefix", "foo.fbs"]) 66 67 assert_file_and_contents("foo_generated.h", '#include "bar/bar_generated.h"') 68 69 def KeepPrefixAbsolutePath(self): 70 # Generate just foo with the import of bar keeping the prefix of where it is located. 71 flatc(["--cpp", "--keep-prefix", make_absolute("foo.fbs")]) 72 73 assert_file_and_contents("foo_generated.h", '#include "bar/bar_generated.h"') 74 75 def KeepPrefixSubDirectory(self): 76 # Generate with the import of bar keeping the prefix of where it is located. 77 flatc(["--cpp", "--keep-prefix", "bar/bar.fbs"]) 78 79 assert_file_and_contents("bar_generated.h", '#include "baz/baz_generated.h"') 80 81 def KeepPrefixOutPath(self): 82 # Generate just foo with the import of bar keeping the prefix of where it is located. 83 flatc(["--cpp", "--keep-prefix", "-o", ".tmp", "foo.fbs"]) 84 85 assert_file_and_contents( 86 ".tmp/foo_generated.h", 87 '#include "bar/bar_generated.h"', 88 ) 89 90 def KeepPrefixOutPathSubDirectory(self): 91 # Generate with the import of bar keeping the prefix of where it is located. 92 flatc(["--cpp", "--keep-prefix", "-o", ".tmp", "bar/bar.fbs"]) 93 94 assert_file_and_contents( 95 ".tmp/bar_generated.h", '#include "baz/baz_generated.h"' 96 ) 97 98 def IncludePrefix(self): 99 # Generate just foo with the import of bar keeping the prefix of where it is located. 100 flatc(["--cpp", "--include-prefix", "test", "foo.fbs"]) 101 102 assert_file_and_contents("foo_generated.h", '#include "test/bar_generated.h"') 103 104 def IncludePrefixAbolutePath(self): 105 # Generate just foo with the import of bar keeping the prefix of where it is located. 106 flatc(["--cpp", "--include-prefix", "test", make_absolute("foo.fbs")]) 107 108 assert_file_and_contents("foo_generated.h", '#include "test/bar_generated.h"') 109 110 def IncludePrefixSubDirectory(self): 111 # Generate just foo with the import of bar keeping the prefix of where it is located. 112 flatc(["--cpp", "--include-prefix", "test", "bar/bar.fbs"]) 113 114 assert_file_and_contents("bar_generated.h", '#include "test/baz_generated.h"') 115 116 def IncludePrefixOutPath(self): 117 # Generate just foo with the import of bar keeping the prefix of where it is located. 118 flatc(["--cpp", "--include-prefix", "test", "-o", ".tmp", "foo.fbs"]) 119 120 assert_file_and_contents( 121 ".tmp/foo_generated.h", '#include "test/bar_generated.h"' 122 ) 123 124 def IncludePrefixOutPathSubDirectory(self): 125 # Generate just foo with the import of bar keeping the prefix of where it is located. 126 flatc(["--cpp", "--include-prefix", "test", "-o", ".tmp", "bar/bar.fbs"]) 127 128 assert_file_and_contents( 129 ".tmp/bar_generated.h", '#include "test/baz_generated.h"' 130 ) 131 132 def KeepPrefixIncludePrefix(self): 133 # Generate just foo with the import of bar keeping the prefix of where it is located. 134 flatc(["--cpp", "--keep-prefix", "--include-prefix", "test", "foo.fbs"]) 135 136 # The include prefix should come first, with the kept prefix next. 137 assert_file_and_contents( 138 "foo_generated.h", '#include "test/bar/bar_generated.h"' 139 ) 140 141 def KeepPrefixIncludePrefixAbsolutePath(self): 142 # Generate just foo with the import of bar keeping the prefix of where it is located. 143 flatc( 144 [ 145 "--cpp", 146 "--keep-prefix", 147 "--include-prefix", 148 "test", 149 make_absolute("foo.fbs"), 150 ] 151 ) 152 153 # The include prefix should come first, with the kept prefix next. 154 assert_file_and_contents( 155 "foo_generated.h", '#include "test/bar/bar_generated.h"' 156 ) 157 158 def KeepPrefixIncludePrefixSubDirectory(self): 159 # Generate just foo with the import of bar keeping the prefix of where it is located. 160 flatc(["--cpp", "--keep-prefix", "--include-prefix", "test", "bar/bar.fbs"]) 161 162 # The include prefix should come first, with the kept prefix next. 163 assert_file_and_contents( 164 "bar_generated.h", '#include "test/baz/baz_generated.h"' 165 ) 166 167 def KeepPrefixIncludePrefixOutPathSubDirectory(self): 168 # Generate just foo with the import of bar keeping the prefix of where it is located. 169 flatc( 170 [ 171 "--cpp", 172 "--keep-prefix", 173 "--include-prefix", 174 "test", 175 "-o", 176 ".tmp", 177 "bar/bar.fbs", 178 ] 179 ) 180 181 # The include prefix should come first, with the kept prefix next. 182 assert_file_and_contents( 183 ".tmp/bar_generated.h", '#include "test/baz/baz_generated.h"' 184 ) 185 186 def KeepPrefixIncludePrefixOutPathSuperDirectory(self): 187 # Generate just foo with the import of bar keeping the prefix of where it is located. 188 flatc( 189 [ 190 "--cpp", 191 "--keep-prefix", 192 "--include-prefix", 193 "test", 194 "-o", 195 "../.tmp", 196 "bar/bar.fbs", 197 ] 198 ) 199 200 # The include prefix should come first, with the kept prefix next. 201 assert_file_and_contents( 202 "../.tmp/bar_generated.h", '#include "test/baz/baz_generated.h"' 203 ) 204 205 def KeepPrefixIncludePrefixoutPathAbsoluePaths_SuperDirectoryReference(self): 206 # Generate bar_with_foo that references a type in a super directory. 207 flatc( 208 [ 209 "--cpp", 210 "--keep-prefix", 211 "--include-prefix", 212 "generated", 213 "-I", 214 str(script_path.absolute()), 215 "-o", 216 str(Path(script_path, ".tmp").absolute()), 217 str(Path(script_path, "bar/bar_with_foo.fbs").absolute()), 218 ] 219 ) 220 221 # The include prefix should come first, with the kept prefix next. 222 assert_file_and_contents( 223 ".tmp/bar_with_foo_generated.h", 224 [ 225 '#include "generated/baz/baz_generated.h"', 226 '#include "generated/foo_generated.h"', 227 ], 228 ) 229 230 def KeepPrefixIncludePrefixoutPath_SuperDirectoryReference(self): 231 # Generate bar_with_foo that references a type in a super directory. 232 flatc( 233 [ 234 "--cpp", 235 "--keep-prefix", 236 "--include-prefix", 237 "generated", 238 "-I", 239 "./", 240 "-o", 241 ".tmp", 242 "bar/bar_with_foo.fbs", 243 ] 244 ) 245 246 # The include prefix should come first, with the kept prefix next. 247 assert_file_and_contents( 248 ".tmp/bar_with_foo_generated.h", 249 [ 250 '#include "generated/baz/baz_generated.h"', 251 '#include "generated/foo_generated.h"', 252 ], 253 unlink=False, 254 ) 255