xref: /aosp_15_r20/external/flatbuffers/tests/flatc/flatc_cpp_tests.py (revision 890232f25432b36107d06881e0a25aaa6b473652)
1*890232f2SAndroid Build Coastguard Worker# Copyright 2022 Google Inc. All rights reserved.
2*890232f2SAndroid Build Coastguard Worker#
3*890232f2SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*890232f2SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*890232f2SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*890232f2SAndroid Build Coastguard Worker#
7*890232f2SAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*890232f2SAndroid Build Coastguard Worker#
9*890232f2SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*890232f2SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*890232f2SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*890232f2SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*890232f2SAndroid Build Coastguard Worker# limitations under the License.
14*890232f2SAndroid Build Coastguard Worker
15*890232f2SAndroid Build Coastguard Workerfrom flatc_test import *
16*890232f2SAndroid Build Coastguard Worker
17*890232f2SAndroid Build Coastguard Worker
18*890232f2SAndroid Build Coastguard Workerclass CppTests:
19*890232f2SAndroid Build Coastguard Worker    def Flatten(self):
20*890232f2SAndroid Build Coastguard Worker        # Generate just foo with a "flatten" import of bar.
21*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "foo.fbs"])
22*890232f2SAndroid Build Coastguard Worker
23*890232f2SAndroid Build Coastguard Worker        # Foo should be generated in place and include bar flatten
24*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents("foo_generated.h", '#include "bar_generated.h"')
25*890232f2SAndroid Build Coastguard Worker
26*890232f2SAndroid Build Coastguard Worker    def FlattenAbsolutePath(self):
27*890232f2SAndroid Build Coastguard Worker        # Generate just foo with a "flatten" import of bar.
28*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", make_absolute("foo.fbs")])
29*890232f2SAndroid Build Coastguard Worker
30*890232f2SAndroid Build Coastguard Worker        # Foo should be generated in place and include bar flatten
31*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents("foo_generated.h", '#include "bar_generated.h"')
32*890232f2SAndroid Build Coastguard Worker
33*890232f2SAndroid Build Coastguard Worker    def FlattenSubDirectory(self):
34*890232f2SAndroid Build Coastguard Worker        # Generate just foo with a "flatten" import of bar.
35*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "bar/bar.fbs"])
36*890232f2SAndroid Build Coastguard Worker
37*890232f2SAndroid Build Coastguard Worker        # Bar should be generated in place and include baz
38*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents("bar_generated.h", '#include "baz_generated.h"')
39*890232f2SAndroid Build Coastguard Worker
40*890232f2SAndroid Build Coastguard Worker    def FlattenOutPath(self):
41*890232f2SAndroid Build Coastguard Worker        # Generate just foo with a "flatten" import of bar.
42*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "-o", ".tmp", "foo.fbs"])
43*890232f2SAndroid Build Coastguard Worker
44*890232f2SAndroid Build Coastguard Worker        # Foo should be generated in the out path and include bar flatten to the out path.
45*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents(".tmp/foo_generated.h", '#include "bar_generated.h"')
46*890232f2SAndroid Build Coastguard Worker
47*890232f2SAndroid Build Coastguard Worker    def FlattenOutPathSuperDirectory(self):
48*890232f2SAndroid Build Coastguard Worker        # Generate just foo with a "flatten" import of bar.
49*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "-o", "../.tmp", "foo.fbs"])
50*890232f2SAndroid Build Coastguard Worker
51*890232f2SAndroid Build Coastguard Worker        # Foo should be generated in the out path and include bar flatten to the out path.
52*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents(
53*890232f2SAndroid Build Coastguard Worker            "../.tmp/foo_generated.h", '#include "bar_generated.h"'
54*890232f2SAndroid Build Coastguard Worker        )
55*890232f2SAndroid Build Coastguard Worker
56*890232f2SAndroid Build Coastguard Worker    def FlattenOutPathSubDirectory(self):
57*890232f2SAndroid Build Coastguard Worker        # Generate just foo with a "flatten" import of bar.
58*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "-o", ".tmp", "bar/bar.fbs"])
59*890232f2SAndroid Build Coastguard Worker
60*890232f2SAndroid Build Coastguard Worker        # Bar should be generated in the out path and include baz flatten to the out path.
61*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents(".tmp/bar_generated.h", '#include "baz_generated.h"')
62*890232f2SAndroid Build Coastguard Worker
63*890232f2SAndroid Build Coastguard Worker    def KeepPrefix(self):
64*890232f2SAndroid Build Coastguard Worker        # Generate just foo with the import of bar keeping the prefix of where it is located.
65*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "--keep-prefix", "foo.fbs"])
66*890232f2SAndroid Build Coastguard Worker
67*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents("foo_generated.h", '#include "bar/bar_generated.h"')
68*890232f2SAndroid Build Coastguard Worker
69*890232f2SAndroid Build Coastguard Worker    def KeepPrefixAbsolutePath(self):
70*890232f2SAndroid Build Coastguard Worker        # Generate just foo with the import of bar keeping the prefix of where it is located.
71*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "--keep-prefix", make_absolute("foo.fbs")])
72*890232f2SAndroid Build Coastguard Worker
73*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents("foo_generated.h", '#include "bar/bar_generated.h"')
74*890232f2SAndroid Build Coastguard Worker
75*890232f2SAndroid Build Coastguard Worker    def KeepPrefixSubDirectory(self):
76*890232f2SAndroid Build Coastguard Worker        # Generate with the import of bar keeping the prefix of where it is located.
77*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "--keep-prefix", "bar/bar.fbs"])
78*890232f2SAndroid Build Coastguard Worker
79*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents("bar_generated.h", '#include "baz/baz_generated.h"')
80*890232f2SAndroid Build Coastguard Worker
81*890232f2SAndroid Build Coastguard Worker    def KeepPrefixOutPath(self):
82*890232f2SAndroid Build Coastguard Worker        # Generate just foo with the import of bar keeping the prefix of where it is located.
83*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "--keep-prefix", "-o", ".tmp", "foo.fbs"])
84*890232f2SAndroid Build Coastguard Worker
85*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents(
86*890232f2SAndroid Build Coastguard Worker            ".tmp/foo_generated.h",
87*890232f2SAndroid Build Coastguard Worker            '#include "bar/bar_generated.h"',
88*890232f2SAndroid Build Coastguard Worker        )
89*890232f2SAndroid Build Coastguard Worker
90*890232f2SAndroid Build Coastguard Worker    def KeepPrefixOutPathSubDirectory(self):
91*890232f2SAndroid Build Coastguard Worker        # Generate with the import of bar keeping the prefix of where it is located.
92*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "--keep-prefix", "-o", ".tmp", "bar/bar.fbs"])
93*890232f2SAndroid Build Coastguard Worker
94*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents(
95*890232f2SAndroid Build Coastguard Worker            ".tmp/bar_generated.h", '#include "baz/baz_generated.h"'
96*890232f2SAndroid Build Coastguard Worker        )
97*890232f2SAndroid Build Coastguard Worker
98*890232f2SAndroid Build Coastguard Worker    def IncludePrefix(self):
99*890232f2SAndroid Build Coastguard Worker        # Generate just foo with the import of bar keeping the prefix of where it is located.
100*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "--include-prefix", "test", "foo.fbs"])
101*890232f2SAndroid Build Coastguard Worker
102*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents("foo_generated.h", '#include "test/bar_generated.h"')
103*890232f2SAndroid Build Coastguard Worker
104*890232f2SAndroid Build Coastguard Worker    def IncludePrefixAbolutePath(self):
105*890232f2SAndroid Build Coastguard Worker        # Generate just foo with the import of bar keeping the prefix of where it is located.
106*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "--include-prefix", "test", make_absolute("foo.fbs")])
107*890232f2SAndroid Build Coastguard Worker
108*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents("foo_generated.h", '#include "test/bar_generated.h"')
109*890232f2SAndroid Build Coastguard Worker
110*890232f2SAndroid Build Coastguard Worker    def IncludePrefixSubDirectory(self):
111*890232f2SAndroid Build Coastguard Worker        # Generate just foo with the import of bar keeping the prefix of where it is located.
112*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "--include-prefix", "test", "bar/bar.fbs"])
113*890232f2SAndroid Build Coastguard Worker
114*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents("bar_generated.h", '#include "test/baz_generated.h"')
115*890232f2SAndroid Build Coastguard Worker
116*890232f2SAndroid Build Coastguard Worker    def IncludePrefixOutPath(self):
117*890232f2SAndroid Build Coastguard Worker        # Generate just foo with the import of bar keeping the prefix of where it is located.
118*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "--include-prefix", "test", "-o", ".tmp", "foo.fbs"])
119*890232f2SAndroid Build Coastguard Worker
120*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents(
121*890232f2SAndroid Build Coastguard Worker            ".tmp/foo_generated.h", '#include "test/bar_generated.h"'
122*890232f2SAndroid Build Coastguard Worker        )
123*890232f2SAndroid Build Coastguard Worker
124*890232f2SAndroid Build Coastguard Worker    def IncludePrefixOutPathSubDirectory(self):
125*890232f2SAndroid Build Coastguard Worker        # Generate just foo with the import of bar keeping the prefix of where it is located.
126*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "--include-prefix", "test", "-o", ".tmp", "bar/bar.fbs"])
127*890232f2SAndroid Build Coastguard Worker
128*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents(
129*890232f2SAndroid Build Coastguard Worker            ".tmp/bar_generated.h", '#include "test/baz_generated.h"'
130*890232f2SAndroid Build Coastguard Worker        )
131*890232f2SAndroid Build Coastguard Worker
132*890232f2SAndroid Build Coastguard Worker    def KeepPrefixIncludePrefix(self):
133*890232f2SAndroid Build Coastguard Worker        # Generate just foo with the import of bar keeping the prefix of where it is located.
134*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "--keep-prefix", "--include-prefix", "test", "foo.fbs"])
135*890232f2SAndroid Build Coastguard Worker
136*890232f2SAndroid Build Coastguard Worker        # The include prefix should come first, with the kept prefix next.
137*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents(
138*890232f2SAndroid Build Coastguard Worker            "foo_generated.h", '#include "test/bar/bar_generated.h"'
139*890232f2SAndroid Build Coastguard Worker        )
140*890232f2SAndroid Build Coastguard Worker
141*890232f2SAndroid Build Coastguard Worker    def KeepPrefixIncludePrefixAbsolutePath(self):
142*890232f2SAndroid Build Coastguard Worker        # Generate just foo with the import of bar keeping the prefix of where it is located.
143*890232f2SAndroid Build Coastguard Worker        flatc(
144*890232f2SAndroid Build Coastguard Worker            [
145*890232f2SAndroid Build Coastguard Worker                "--cpp",
146*890232f2SAndroid Build Coastguard Worker                "--keep-prefix",
147*890232f2SAndroid Build Coastguard Worker                "--include-prefix",
148*890232f2SAndroid Build Coastguard Worker                "test",
149*890232f2SAndroid Build Coastguard Worker                make_absolute("foo.fbs"),
150*890232f2SAndroid Build Coastguard Worker            ]
151*890232f2SAndroid Build Coastguard Worker        )
152*890232f2SAndroid Build Coastguard Worker
153*890232f2SAndroid Build Coastguard Worker        # The include prefix should come first, with the kept prefix next.
154*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents(
155*890232f2SAndroid Build Coastguard Worker            "foo_generated.h", '#include "test/bar/bar_generated.h"'
156*890232f2SAndroid Build Coastguard Worker        )
157*890232f2SAndroid Build Coastguard Worker
158*890232f2SAndroid Build Coastguard Worker    def KeepPrefixIncludePrefixSubDirectory(self):
159*890232f2SAndroid Build Coastguard Worker        # Generate just foo with the import of bar keeping the prefix of where it is located.
160*890232f2SAndroid Build Coastguard Worker        flatc(["--cpp", "--keep-prefix", "--include-prefix", "test", "bar/bar.fbs"])
161*890232f2SAndroid Build Coastguard Worker
162*890232f2SAndroid Build Coastguard Worker        # The include prefix should come first, with the kept prefix next.
163*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents(
164*890232f2SAndroid Build Coastguard Worker            "bar_generated.h", '#include "test/baz/baz_generated.h"'
165*890232f2SAndroid Build Coastguard Worker        )
166*890232f2SAndroid Build Coastguard Worker
167*890232f2SAndroid Build Coastguard Worker    def KeepPrefixIncludePrefixOutPathSubDirectory(self):
168*890232f2SAndroid Build Coastguard Worker        # Generate just foo with the import of bar keeping the prefix of where it is located.
169*890232f2SAndroid Build Coastguard Worker        flatc(
170*890232f2SAndroid Build Coastguard Worker            [
171*890232f2SAndroid Build Coastguard Worker                "--cpp",
172*890232f2SAndroid Build Coastguard Worker                "--keep-prefix",
173*890232f2SAndroid Build Coastguard Worker                "--include-prefix",
174*890232f2SAndroid Build Coastguard Worker                "test",
175*890232f2SAndroid Build Coastguard Worker                "-o",
176*890232f2SAndroid Build Coastguard Worker                ".tmp",
177*890232f2SAndroid Build Coastguard Worker                "bar/bar.fbs",
178*890232f2SAndroid Build Coastguard Worker            ]
179*890232f2SAndroid Build Coastguard Worker        )
180*890232f2SAndroid Build Coastguard Worker
181*890232f2SAndroid Build Coastguard Worker        # The include prefix should come first, with the kept prefix next.
182*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents(
183*890232f2SAndroid Build Coastguard Worker            ".tmp/bar_generated.h", '#include "test/baz/baz_generated.h"'
184*890232f2SAndroid Build Coastguard Worker        )
185*890232f2SAndroid Build Coastguard Worker
186*890232f2SAndroid Build Coastguard Worker    def KeepPrefixIncludePrefixOutPathSuperDirectory(self):
187*890232f2SAndroid Build Coastguard Worker        # Generate just foo with the import of bar keeping the prefix of where it is located.
188*890232f2SAndroid Build Coastguard Worker        flatc(
189*890232f2SAndroid Build Coastguard Worker            [
190*890232f2SAndroid Build Coastguard Worker                "--cpp",
191*890232f2SAndroid Build Coastguard Worker                "--keep-prefix",
192*890232f2SAndroid Build Coastguard Worker                "--include-prefix",
193*890232f2SAndroid Build Coastguard Worker                "test",
194*890232f2SAndroid Build Coastguard Worker                "-o",
195*890232f2SAndroid Build Coastguard Worker                "../.tmp",
196*890232f2SAndroid Build Coastguard Worker                "bar/bar.fbs",
197*890232f2SAndroid Build Coastguard Worker            ]
198*890232f2SAndroid Build Coastguard Worker        )
199*890232f2SAndroid Build Coastguard Worker
200*890232f2SAndroid Build Coastguard Worker        # The include prefix should come first, with the kept prefix next.
201*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents(
202*890232f2SAndroid Build Coastguard Worker            "../.tmp/bar_generated.h", '#include "test/baz/baz_generated.h"'
203*890232f2SAndroid Build Coastguard Worker        )
204*890232f2SAndroid Build Coastguard Worker
205*890232f2SAndroid Build Coastguard Worker    def KeepPrefixIncludePrefixoutPathAbsoluePaths_SuperDirectoryReference(self):
206*890232f2SAndroid Build Coastguard Worker        # Generate bar_with_foo that references a type in a super directory.
207*890232f2SAndroid Build Coastguard Worker        flatc(
208*890232f2SAndroid Build Coastguard Worker            [
209*890232f2SAndroid Build Coastguard Worker                "--cpp",
210*890232f2SAndroid Build Coastguard Worker                "--keep-prefix",
211*890232f2SAndroid Build Coastguard Worker                "--include-prefix",
212*890232f2SAndroid Build Coastguard Worker                "generated",
213*890232f2SAndroid Build Coastguard Worker                "-I",
214*890232f2SAndroid Build Coastguard Worker                str(script_path.absolute()),
215*890232f2SAndroid Build Coastguard Worker                "-o",
216*890232f2SAndroid Build Coastguard Worker                str(Path(script_path, ".tmp").absolute()),
217*890232f2SAndroid Build Coastguard Worker                str(Path(script_path, "bar/bar_with_foo.fbs").absolute()),
218*890232f2SAndroid Build Coastguard Worker            ]
219*890232f2SAndroid Build Coastguard Worker        )
220*890232f2SAndroid Build Coastguard Worker
221*890232f2SAndroid Build Coastguard Worker        # The include prefix should come first, with the kept prefix next.
222*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents(
223*890232f2SAndroid Build Coastguard Worker            ".tmp/bar_with_foo_generated.h",
224*890232f2SAndroid Build Coastguard Worker            [
225*890232f2SAndroid Build Coastguard Worker                '#include "generated/baz/baz_generated.h"',
226*890232f2SAndroid Build Coastguard Worker                '#include "generated/foo_generated.h"',
227*890232f2SAndroid Build Coastguard Worker            ],
228*890232f2SAndroid Build Coastguard Worker        )
229*890232f2SAndroid Build Coastguard Worker
230*890232f2SAndroid Build Coastguard Worker    def KeepPrefixIncludePrefixoutPath_SuperDirectoryReference(self):
231*890232f2SAndroid Build Coastguard Worker        # Generate bar_with_foo that references a type in a super directory.
232*890232f2SAndroid Build Coastguard Worker        flatc(
233*890232f2SAndroid Build Coastguard Worker            [
234*890232f2SAndroid Build Coastguard Worker                "--cpp",
235*890232f2SAndroid Build Coastguard Worker                "--keep-prefix",
236*890232f2SAndroid Build Coastguard Worker                "--include-prefix",
237*890232f2SAndroid Build Coastguard Worker                "generated",
238*890232f2SAndroid Build Coastguard Worker                "-I",
239*890232f2SAndroid Build Coastguard Worker                "./",
240*890232f2SAndroid Build Coastguard Worker                "-o",
241*890232f2SAndroid Build Coastguard Worker                ".tmp",
242*890232f2SAndroid Build Coastguard Worker                "bar/bar_with_foo.fbs",
243*890232f2SAndroid Build Coastguard Worker            ]
244*890232f2SAndroid Build Coastguard Worker        )
245*890232f2SAndroid Build Coastguard Worker
246*890232f2SAndroid Build Coastguard Worker        # The include prefix should come first, with the kept prefix next.
247*890232f2SAndroid Build Coastguard Worker        assert_file_and_contents(
248*890232f2SAndroid Build Coastguard Worker            ".tmp/bar_with_foo_generated.h",
249*890232f2SAndroid Build Coastguard Worker            [
250*890232f2SAndroid Build Coastguard Worker                '#include "generated/baz/baz_generated.h"',
251*890232f2SAndroid Build Coastguard Worker                '#include "generated/foo_generated.h"',
252*890232f2SAndroid Build Coastguard Worker            ],
253*890232f2SAndroid Build Coastguard Worker            unlink=False,
254*890232f2SAndroid Build Coastguard Worker        )
255