blob: 5159ba484f1d80c3d14d10c50a8afb50449202f1 [file] [log] [blame]
Nobuaki Sukegawa11da87e2016-09-10 14:02:19 +09001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
dtmuller052abc32016-07-26 11:58:28 +020020#include "thrift/parse/t_program.h"
21#include "thrift/plugin/type_util.h"
22#include "thrift/plugin/plugin_types.h"
Nobuaki Sukegawa11da87e2016-09-10 14:02:19 +090023
24#include <map>
25#include <vector>
26
27#include <boost/preprocessor.hpp>
28#include <boost/test/included/unit_test.hpp>
29#include <boost/test/parameterized_test.hpp>
30
31using namespace apache::thrift;
32using namespace boost::unit_test;
33
34namespace test_data {
35#define T_TEST_TYPES \
36 BOOST_PP_TUPLE_TO_LIST(14, \
37 (program, \
38 base_type, \
39 enum_value, \
40 enum, \
41 const_value, \
42 const, \
43 list, \
44 set, \
45 map, \
46 field, \
47 struct, \
48 typedef, \
49 function, \
50 service))
51#define T_DELETE_TESTDATA(r, d, elem) \
52 for (std::vector<t_##elem*>::reverse_iterator it = elem##s.rbegin(); it != elem##s.rend(); it++) \
53 delete *it;
54#define T_DECL_TESTDATA(r, d, elem) static std::vector< ::t_##elem*> elem##s;
55BOOST_PP_LIST_FOR_EACH(T_DECL_TESTDATA, _, T_TEST_TYPES)
56#undef T_DECL_TESTDATA
57
58bool has_data = false;
59void cleanup() {
60 if (has_data) {
61 has_data = false;
62 BOOST_PP_LIST_FOR_EACH(T_DELETE_TESTDATA, _, T_TEST_TYPES)
63 }
64}
65
66void init_programs() {
67 programs.push_back(new t_program("prog path", "prog_name"));
68}
69
70void init_base_types() {
71 base_types.push_back(new ::t_base_type("name0", ::t_base_type::TYPE_VOID));
72 base_types.push_back(new ::t_base_type("name1", ::t_base_type::TYPE_STRING));
73 base_types.push_back(new ::t_base_type("name2", ::t_base_type::TYPE_BOOL));
74 base_types.push_back(new ::t_base_type("name3", ::t_base_type::TYPE_I8));
75 base_types.push_back(new ::t_base_type("name4", ::t_base_type::TYPE_I16));
76 base_types.push_back(new ::t_base_type("name5", ::t_base_type::TYPE_I32));
77 base_types.push_back(new ::t_base_type("name6", ::t_base_type::TYPE_I64));
78 base_types.push_back(new ::t_base_type("name7", ::t_base_type::TYPE_DOUBLE));
79}
80
81void init_const_values() {
82 const_values.push_back(new t_const_value(42));
83 const_values.push_back(new t_const_value("foo"));
84 {
85 t_const_value* x = new t_const_value;
86 x->set_double(3.1415);
87 const_values.push_back(x);
88 }
89 {
90 t_const_value* x = new t_const_value;
91 x->set_identifier("bar");
92 x->set_enum(enums[0]);
93 const_values.push_back(x);
94 }
95 {
96 t_const_value* x = new t_const_value;
97 x->set_map();
98 x->add_map(const_values[0], const_values[1]);
99 x->add_map(const_values[1], const_values[0]);
100 const_values.push_back(x);
101 }
102 {
103 t_const_value* x = new t_const_value;
104 x->set_list();
105 x->add_list(const_values[0]);
106 x->add_list(const_values[1]);
107 const_values.push_back(x);
108 }
109}
110
111void init_consts() {
112 // base_type/enum indexes for this and other tests are arbitrary
113 consts.push_back(new t_const(base_types[2], "aaa", const_values[0]));
114 consts.back()->set_doc("soem doc");
115 consts.push_back(new t_const(base_types[3], "bbb", const_values[1]));
116}
117
118void init_enum_values() {
119 enum_values.push_back(new t_enum_value("VAL1", 11));
120 enum_values.back()->set_doc("enum doc 1");
121 enum_values.back()->annotations_.insert(std::make_pair("anno1", "val1"));
122
123 enum_values.push_back(new t_enum_value("VAL2", 22));
124}
125
126void init_enums() {
127 enums.push_back(new t_enum(programs[0]));
128 enums.back()->set_doc("enum doc 1");
129 enums.back()->annotations_.insert(std::make_pair("anno1", "val1"));
130 enums.back()->set_name("fooo");
131 enums.back()->append(enum_values[0]);
132 enums.back()->append(enum_values[1]);
133}
134
135void init_lists() {
136 lists.push_back(new t_list(enums[0]));
137 lists.push_back(new t_list(base_types[5]));
138 lists.back()->set_cpp_name("list_cpp_name_1");
139}
140void init_sets() {
141 sets.push_back(new t_set(base_types[4]));
142 sets.push_back(new t_set(enums[0]));
143 sets.back()->set_cpp_name("set_cpp_name_1");
144}
145void init_maps() {
146 maps.push_back(new t_map(base_types[4], base_types[1]));
147 maps.push_back(new t_map(base_types[5], enums[0]));
148 maps.back()->set_cpp_name("map_cpp_name_1");
149}
150
151void init_typedefs() {
152 typedefs.push_back(new t_typedef(programs[0], base_types[3], "VAL1"));
153}
154void init_fields() {
155 fields.push_back(new t_field(base_types[1], "f1"));
156 fields.back()->set_reference(false);
157 fields.back()->set_req(t_field::T_OPTIONAL);
158 fields.push_back(new t_field(base_types[2], "f2", 9));
159 fields.back()->set_reference(true);
160 fields.push_back(new t_field(base_types[3], "f3", 11));
161 fields.back()->set_req(t_field::T_REQUIRED);
162 fields.back()->set_value(const_values[0]);
163}
164void init_structs() {
165 structs.push_back(new t_struct(programs[0], "struct1"));
166 structs.back()->append(fields[0]);
167 structs.back()->append(fields[1]);
168 structs.push_back(new t_struct(programs[0], "union1"));
169 structs.back()->append(fields[0]);
170 structs.back()->append(fields[1]);
171 structs.back()->set_union(true);
172 structs.push_back(new t_struct(programs[0], "xcept1"));
173 structs.back()->set_xception(true);
174}
175void init_functions() {
176 structs.push_back(new t_struct(programs[0], "errs1"));
177 t_struct* errors = structs.back();
178 structs.push_back(new t_struct(programs[0], "args1"));
179 t_struct* arglist = structs.back();
180 functions.push_back(new t_function(base_types[0], "func1", errors, arglist, false));
181 functions.push_back(new t_function(base_types[0], "func2", errors, arglist, true));
182}
183void init_services() {
184 services.push_back(new t_service(programs[0]));
185 services.back()->set_doc("srv1 doc");
186 services.back()->set_name("srv1");
187 services.back()->add_function(functions[0]);
188 services.back()->add_function(functions[1]);
189
190 services.push_back(new t_service(programs[0]));
191 services.back()->set_name("srv2");
192 services.back()->set_extends(services[0]);
193}
194
195std::vector<t_type*> types;
196void init_types() {
197#define T_COPY_TYPES(type) std::copy(type##s.begin(), type##s.end(), std::back_inserter(types))
198 T_COPY_TYPES(base_type);
199 T_COPY_TYPES(enum);
200 T_COPY_TYPES(typedef);
201 T_COPY_TYPES(struct);
202 T_COPY_TYPES(list);
203 T_COPY_TYPES(set);
204 T_COPY_TYPES(map);
205// T_COPY_TYPES(service);
206#undef T_COPY_TYPES
207}
208
209void init() {
210 if (!has_data) {
211 has_data = true;
212#define T_INIT_TESTDATA(r, d, elem) init_##elem##s();
213 BOOST_PP_LIST_FOR_EACH(T_INIT_TESTDATA, _, T_TEST_TYPES)
214 init_types();
215#undef T_INIT_TESTDATA
216 }
217}
218}
219struct GlobalFixture {
220 ~GlobalFixture() { test_data::cleanup(); }
221};
222#if (BOOST_VERSION >= 105900)
223BOOST_GLOBAL_FIXTURE(GlobalFixture);
224#else
225BOOST_GLOBAL_FIXTURE(GlobalFixture)
226#endif
227
228void migrate_global_cache() {
229 plugin::TypeRegistry reg;
230 plugin_output::get_global_cache(reg);
231 plugin::set_global_cache(reg);
232 plugin_output::clear_global_cache();
233}
234template <typename T>
235T* round_trip(T* t) {
236 typename plugin::ToType<T>::type p;
237 plugin_output::convert(t, p);
238 migrate_global_cache();
239 return plugin::convert(p);
240}
241
242void test_base_type(::t_base_type* sut) {
243 plugin::t_base_type p;
244 plugin_output::convert(sut, p);
245 boost::scoped_ptr< ::t_base_type> sut2(plugin::convert(p));
246
247#define THRIFT_CHECK(r, data, elem) BOOST_PP_EXPAND(BOOST_CHECK_EQUAL(data elem, sut2->elem));
248 BOOST_PP_LIST_FOR_EACH(THRIFT_CHECK,
249 sut->,
250 BOOST_PP_TUPLE_TO_LIST(7,
251 (is_void(),
252 is_string(),
253 is_bool(),
254 is_string_list(),
255 is_binary(),
256 is_string_enum(),
257 is_base_type())))
258}
259
260void test_const_value(t_const_value* sut) {
261 boost::scoped_ptr<t_const_value> sut2(round_trip(sut));
262
263 BOOST_CHECK_EQUAL(sut->get_type(), sut2->get_type());
264 switch (sut->get_type()) {
265#define T_CONST_VALUE_CASE(type, name) \
266 case t_const_value::type: \
267 BOOST_CHECK_EQUAL(sut->get_##name(), sut2->get_##name()); \
268 break
269 T_CONST_VALUE_CASE(CV_INTEGER, integer);
270 T_CONST_VALUE_CASE(CV_DOUBLE, double);
271 T_CONST_VALUE_CASE(CV_STRING, string);
272 T_CONST_VALUE_CASE(CV_IDENTIFIER, identifier);
273#undef T_CONST_VALUE_CASE
274 case t_const_value::CV_MAP:
275 BOOST_CHECK_EQUAL(sut->get_map().size(), sut2->get_map().size());
276 {
277 std::map<t_const_value::t_const_value_type, t_const_value::t_const_value_type> sut_values;
278 for (std::map<t_const_value*, t_const_value*>::const_iterator it = sut->get_map().begin();
279 it != sut->get_map().end(); it++) {
280 sut_values[it->first->get_type()] = it->second->get_type();
281 }
282 std::map<t_const_value::t_const_value_type, t_const_value::t_const_value_type> sut2_values;
283 for (std::map<t_const_value*, t_const_value*>::const_iterator it = sut2->get_map().begin();
284 it != sut2->get_map().end(); it++) {
285 sut2_values[it->first->get_type()] = it->second->get_type();
286 }
287 BOOST_CHECK_EQUAL(sut_values.begin()->first, sut2_values.begin()->first);
288 BOOST_CHECK_EQUAL(sut_values.begin()->second, sut2_values.begin()->second);
289 }
290 break;
291 case t_const_value::CV_LIST:
292 BOOST_CHECK_EQUAL(sut->get_list().size(), sut2->get_list().size());
293 BOOST_CHECK_EQUAL(sut->get_list().front()->get_type(), sut2->get_list().front()->get_type());
294 break;
295 default:
296 BOOST_ASSERT(false);
297 break;
298 }
299}
300
301void test_const(t_const* sut) {
302 boost::scoped_ptr< ::t_const> sut2(round_trip(sut));
303
304 BOOST_PP_LIST_FOR_EACH(THRIFT_CHECK,
305 sut->,
306 BOOST_PP_TUPLE_TO_LIST(4,
307 (get_type()->get_name(),
308 get_name(),
309 get_value()->get_type(),
310 get_doc())))
311}
312
313void test_enum_value(t_enum_value* sut) {
314 boost::scoped_ptr<t_enum_value> sut2(round_trip(sut));
315
316 BOOST_PP_LIST_FOR_EACH(THRIFT_CHECK,
317 sut->,
318 BOOST_PP_TUPLE_TO_LIST(3, (get_name(), get_value(), get_doc())))
319}
320
321void test_enum(t_enum* sut) {
322 boost::scoped_ptr< ::t_enum> sut2(round_trip(sut));
323
324 BOOST_PP_LIST_FOR_EACH(THRIFT_CHECK,
325 sut->,
326 BOOST_PP_TUPLE_TO_LIST(6,
327 (get_name(),
328 get_min_value()->get_value(),
329 get_max_value()->get_value(),
330 get_constant_by_value(11)->get_value(),
331 get_constant_by_name("VAL1")->get_value(),
332 get_doc())))
333}
334
335void test_list(t_list* sut) {
336 boost::scoped_ptr<t_list> sut2(round_trip(sut));
337
338 BOOST_PP_LIST_FOR_EACH(THRIFT_CHECK,
339 sut->,
340 BOOST_PP_TUPLE_TO_LIST(4,
341 (get_elem_type()->get_name(),
342 has_cpp_name(),
343 get_doc(),
344 get_name())))
345 if (sut->has_cpp_name())
346 BOOST_CHECK_EQUAL(sut->get_cpp_name(), sut2->get_cpp_name());
347}
348void test_set(t_set* sut) {
349 boost::scoped_ptr<t_set> sut2(round_trip(sut));
350
351 BOOST_PP_LIST_FOR_EACH(THRIFT_CHECK,
352 sut->,
353 BOOST_PP_TUPLE_TO_LIST(4,
354 (get_elem_type()->get_name(),
355 has_cpp_name(),
356 get_doc(),
357 get_name())))
358 if (sut->has_cpp_name())
359 BOOST_CHECK_EQUAL(sut->get_cpp_name(), sut2->get_cpp_name());
360}
361void test_map(t_map* sut) {
362 boost::scoped_ptr<t_map> sut2(round_trip(sut));
363
364 BOOST_PP_LIST_FOR_EACH(THRIFT_CHECK,
365 sut->,
366 BOOST_PP_TUPLE_TO_LIST(5,
367 (get_key_type()->get_name(),
368 get_val_type()->get_name(),
369 has_cpp_name(),
370 get_doc(),
371 get_name())))
372 if (sut->has_cpp_name())
373 BOOST_CHECK_EQUAL(sut->get_cpp_name(), sut2->get_cpp_name());
374}
375
376void test_typedef(t_typedef* sut) {
377 boost::scoped_ptr<t_typedef> sut2(round_trip(sut));
378
379 BOOST_PP_LIST_FOR_EACH(THRIFT_CHECK,
380 sut->,
381 BOOST_PP_TUPLE_TO_LIST(4,
382 (get_doc(),
383 get_name(),
384 get_symbolic(),
385 is_forward_typedef())))
386}
387
388void test_type(t_type* sut) {
389 boost::scoped_ptr<t_type> sut2(round_trip(sut));
390
391 BOOST_PP_LIST_FOR_EACH(THRIFT_CHECK,
392 sut->,
393 BOOST_PP_TUPLE_TO_LIST(15,
394 (is_void(),
395 is_base_type(),
396 is_string(),
397 is_bool(),
398 is_typedef(),
399 is_enum(),
400 is_struct(),
401 is_xception(),
402 is_container(),
403 is_list(),
404 is_set(),
405 is_map(),
406 is_service(),
407 get_doc(),
408 get_name())))
409}
410
411void test_field(t_field* sut) {
412 boost::scoped_ptr<t_field> sut2(round_trip(sut));
413
414 BOOST_PP_LIST_FOR_EACH(THRIFT_CHECK,
415 sut->,
416 BOOST_PP_TUPLE_TO_LIST(5,
417 (get_req(),
418 get_reference(),
419 get_key(),
420 get_doc(),
421 get_name())))
422 if (sut->get_value()) {
423 THRIFT_CHECK(, sut->, get_value()->get_type());
424 } else {
425 BOOST_CHECK(!sut2->get_value());
426 }
427 if (sut->get_type()) {
428 THRIFT_CHECK(, sut->, get_type()->get_name());
429 } else {
430 BOOST_CHECK(!sut2->get_type());
431 }
432}
433void test_struct(t_struct* sut) {
434 boost::scoped_ptr<t_struct> sut2(round_trip(sut));
435
436 BOOST_PP_LIST_FOR_EACH(THRIFT_CHECK,
437 sut->,
438 BOOST_PP_TUPLE_TO_LIST(5,
439 (is_union(),
440 is_xception(),
441 is_struct(),
442 get_doc(),
443 get_name())))
444}
445
446void test_function(t_function* sut) {
447 boost::scoped_ptr<t_function> sut2(round_trip(sut));
448
449 BOOST_PP_LIST_FOR_EACH(
450 THRIFT_CHECK,
451 sut->,
452 BOOST_PP_TUPLE_TO_LIST(4, (get_doc(), get_name(), get_returntype()->get_name(), is_oneway())))
453}
454void test_service(t_service* sut) {
455 boost::scoped_ptr<t_service> sut2(round_trip(sut));
456
457 BOOST_PP_LIST_FOR_EACH(THRIFT_CHECK,
458 sut->,
459 BOOST_PP_TUPLE_TO_LIST(3, (get_doc(), get_name(), get_functions().size())))
460 if (sut->get_extends()) {
461 THRIFT_CHECK(, sut->, get_extends()->get_name());
462 } else {
463 BOOST_CHECK(!sut2->get_extends());
464 }
465}
466
467void test_program(t_program* sut) {
468 boost::scoped_ptr<t_program> sut2(round_trip(sut));
469
470 BOOST_PP_LIST_FOR_EACH(THRIFT_CHECK, sut->, BOOST_PP_TUPLE_TO_LIST(2, (get_doc(), get_name())))
471}
472boost::unit_test::test_suite* do_init_unit_test_suite() {
473 test_data::init();
474 test_suite* ts = BOOST_TEST_SUITE("PluginConversionTest");
475
476#define T_TEST_CASE(r, d, type) \
477 ts->add(BOOST_PARAM_TEST_CASE(test_##type, test_data::type##s.begin(), test_data::type##s.end()));
478 BOOST_PP_LIST_FOR_EACH(T_TEST_CASE, _, T_TEST_TYPES)
479 T_TEST_CASE(_, _, type)
480#undef T_TEST_CASE
481 return ts;
482}
483
484#ifdef BOOST_TEST_DYN_LINK
485bool init_unit_test_suite() {
486 framework::master_test_suite().add(do_init_unit_test_suite());
487 return true;
488}
489int main(int argc, char* argv[]) {
490 return ::boost::unit_test::unit_test_main(&init_unit_test_suite, argc, argv);
491}
492#else
493boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
494 return do_init_unit_test_suite();
495}
496#endif