blob: 7281c2cc68130ffe71d2264e5a542a5b9cc9f691 [file] [log] [blame]
Austin Schuh2dd86a92022-09-14 21:19:23 -07001#include "proto_test.h"
2
3#include "flatbuffers/idl.h"
4#include "test_assert.h"
5
6namespace flatbuffers {
7namespace tests {
8
9// Parse a .proto schema, output as .fbs
10void ParseProtoTest(const std::string &tests_data_path) {
11 // load the .proto and the golden file from disk
12 std::string protofile;
13 std::string goldenfile;
14 std::string goldenunionfile;
15 TEST_EQ(
16 flatbuffers::LoadFile((tests_data_path + "prototest/test.proto").c_str(),
17 false, &protofile),
18 true);
19 TEST_EQ(
20 flatbuffers::LoadFile((tests_data_path + "prototest/test.golden").c_str(),
21 false, &goldenfile),
22 true);
23 TEST_EQ(flatbuffers::LoadFile(
24 (tests_data_path + "prototest/test_union.golden").c_str(), false,
25 &goldenunionfile),
26 true);
27
28 flatbuffers::IDLOptions opts;
29 opts.include_dependence_headers = false;
30 opts.proto_mode = true;
31
32 // Parse proto.
33 flatbuffers::Parser parser(opts);
34 auto protopath = tests_data_path + "prototest/";
35 const char *include_directories[] = { protopath.c_str(), nullptr };
36 TEST_EQ(parser.Parse(protofile.c_str(), include_directories), true);
37
38 // Generate fbs.
39 auto fbs = flatbuffers::GenerateFBS(parser, "test");
40
41 // Ensure generated file is parsable.
42 flatbuffers::Parser parser2;
43 TEST_EQ(parser2.Parse(fbs.c_str(), nullptr), true);
44 TEST_EQ_STR(fbs.c_str(), goldenfile.c_str());
45
46 // Parse proto with --oneof-union option.
47 opts.proto_oneof_union = true;
48 flatbuffers::Parser parser3(opts);
49 TEST_EQ(parser3.Parse(protofile.c_str(), include_directories), true);
50
51 // Generate fbs.
52 auto fbs_union = flatbuffers::GenerateFBS(parser3, "test");
53
54 // Ensure generated file is parsable.
55 flatbuffers::Parser parser4;
56 TEST_EQ(parser4.Parse(fbs_union.c_str(), nullptr), true);
57 TEST_EQ_STR(fbs_union.c_str(), goldenunionfile.c_str());
58}
59
60// Parse a .proto schema, output as .fbs
61void ParseProtoTestWithSuffix(const std::string &tests_data_path) {
62 // load the .proto and the golden file from disk
63 std::string protofile;
64 std::string goldenfile;
65 std::string goldenunionfile;
66 TEST_EQ(
67 flatbuffers::LoadFile((tests_data_path + "prototest/test.proto").c_str(),
68 false, &protofile),
69 true);
70 TEST_EQ(flatbuffers::LoadFile(
71 (tests_data_path + "prototest/test_suffix.golden").c_str(), false,
72 &goldenfile),
73 true);
74 TEST_EQ(flatbuffers::LoadFile(
75 (tests_data_path + "prototest/test_union_suffix.golden").c_str(),
76 false, &goldenunionfile),
77 true);
78
79 flatbuffers::IDLOptions opts;
80 opts.include_dependence_headers = false;
81 opts.proto_mode = true;
82 opts.proto_namespace_suffix = "test_namespace_suffix";
83
84 // Parse proto.
85 flatbuffers::Parser parser(opts);
86 auto protopath = tests_data_path + "prototest/";
87 const char *include_directories[] = { protopath.c_str(), nullptr };
88 TEST_EQ(parser.Parse(protofile.c_str(), include_directories), true);
89
90 // Generate fbs.
91 auto fbs = flatbuffers::GenerateFBS(parser, "test");
92
93 // Ensure generated file is parsable.
94 flatbuffers::Parser parser2;
95 TEST_EQ(parser2.Parse(fbs.c_str(), nullptr), true);
96 TEST_EQ_STR(fbs.c_str(), goldenfile.c_str());
97
98 // Parse proto with --oneof-union option.
99 opts.proto_oneof_union = true;
100 flatbuffers::Parser parser3(opts);
101 TEST_EQ(parser3.Parse(protofile.c_str(), include_directories), true);
102
103 // Generate fbs.
104 auto fbs_union = flatbuffers::GenerateFBS(parser3, "test");
105
106 // Ensure generated file is parsable.
107 flatbuffers::Parser parser4;
108 TEST_EQ(parser4.Parse(fbs_union.c_str(), nullptr), true);
109 TEST_EQ_STR(fbs_union.c_str(), goldenunionfile.c_str());
110}
111
112// Parse a .proto schema, output as .fbs
113void ParseProtoTestWithIncludes(const std::string &tests_data_path) {
114 // load the .proto and the golden file from disk
115 std::string protofile;
116 std::string goldenfile;
117 std::string goldenunionfile;
118 std::string importprotofile;
119 TEST_EQ(
120 flatbuffers::LoadFile((tests_data_path + "prototest/test.proto").c_str(),
121 false, &protofile),
122 true);
123 TEST_EQ(flatbuffers::LoadFile(
124 (tests_data_path + "prototest/imported.proto").c_str(), false,
125 &importprotofile),
126 true);
127 TEST_EQ(flatbuffers::LoadFile(
128 (tests_data_path + "prototest/test_include.golden").c_str(),
129 false, &goldenfile),
130 true);
131 TEST_EQ(flatbuffers::LoadFile(
132 (tests_data_path + "prototest/test_union_include.golden").c_str(),
133 false, &goldenunionfile),
134 true);
135
136 flatbuffers::IDLOptions opts;
137 opts.include_dependence_headers = true;
138 opts.proto_mode = true;
139
140 // Parse proto.
141 flatbuffers::Parser parser(opts);
142 auto protopath = tests_data_path + "prototest/";
143 const char *include_directories[] = { protopath.c_str(), nullptr };
144 TEST_EQ(parser.Parse(protofile.c_str(), include_directories), true);
145
146 // Generate fbs.
147 auto fbs = flatbuffers::GenerateFBS(parser, "test");
148
149 // Generate fbs from import.proto
150 flatbuffers::Parser import_parser(opts);
151 TEST_EQ(import_parser.Parse(importprotofile.c_str(), include_directories),
152 true);
153 auto import_fbs = flatbuffers::GenerateFBS(import_parser, "test");
154
155 // Ensure generated file is parsable.
156 flatbuffers::Parser parser2;
157 // Since `imported.fbs` isn't in the filesystem AbsolutePath can't figure it
158 // out by itself. We manually construct it so Parser works.
159 std::string imported_fbs = flatbuffers::PosixPath(
160 flatbuffers::AbsolutePath(protopath) + "/imported.fbs");
161 TEST_EQ(parser2.Parse(import_fbs.c_str(), include_directories,
162 imported_fbs.c_str()),
163 true);
164 TEST_EQ(parser2.Parse(fbs.c_str(), nullptr), true);
165 TEST_EQ_STR(fbs.c_str(), goldenfile.c_str());
166
167 // Parse proto with --oneof-union option.
168 opts.proto_oneof_union = true;
169 flatbuffers::Parser parser3(opts);
170 TEST_EQ(parser3.Parse(protofile.c_str(), include_directories), true);
171
172 // Generate fbs.
173 auto fbs_union = flatbuffers::GenerateFBS(parser3, "test");
174
175 // Ensure generated file is parsable.
176 flatbuffers::Parser parser4;
177 TEST_EQ(parser4.Parse(import_fbs.c_str(), nullptr, imported_fbs.c_str()),
178 true);
179 TEST_EQ(parser4.Parse(fbs_union.c_str(), nullptr), true);
180 TEST_EQ_STR(fbs_union.c_str(), goldenunionfile.c_str());
181}
182
183void ParseProtoBufAsciiTest() {
184 // We can put the parser in a mode where it will accept JSON that looks more
185 // like Protobuf ASCII, for users that have data in that format.
186 // This uses no "" for field names (which we already support by default,
187 // omits `,`, `:` before `{` and a couple of other features.
188 flatbuffers::Parser parser;
189 parser.opts.protobuf_ascii_alike = true;
190 TEST_EQ(
191 parser.Parse("table S { B:int; } table T { A:[int]; C:S; } root_type T;"),
192 true);
193 TEST_EQ(parser.Parse("{ A [1 2] C { B:2 }}"), true);
194 // Similarly, in text output, it should omit these.
195 std::string text;
196 auto ok = flatbuffers::GenerateText(
197 parser, parser.builder_.GetBufferPointer(), &text);
198 TEST_EQ(ok, true);
199 TEST_EQ_STR(text.c_str(),
200 "{\n A [\n 1\n 2\n ]\n C {\n B: 2\n }\n}\n");
201}
202
203} // namespace tests
204} // namespace flatbuffers