1 /*
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 
20 #include <vector>
21 #include <map>
22 
23 #include <boost/test/auto_unit_test.hpp>
24 
25 #include <thrift/TToString.h>
26 
27 #include "gen-cpp/ThriftTest_types.h"
28 #include "gen-cpp/OptionalRequiredTest_types.h"
29 #include "gen-cpp/DebugProtoTest_types.h"
30 
31 using apache::thrift::to_string;
32 
33 BOOST_AUTO_TEST_SUITE(ToStringTest)
34 
BOOST_AUTO_TEST_CASE(base_types_to_string)35 BOOST_AUTO_TEST_CASE(base_types_to_string) {
36   BOOST_CHECK_EQUAL(to_string(10), "10");
37   BOOST_CHECK_EQUAL(to_string(true), "1");
38   BOOST_CHECK_EQUAL(to_string('a'), "a");
39   BOOST_CHECK_EQUAL(to_string(1.2), "1.2");
40   BOOST_CHECK_EQUAL(to_string("abc"), "abc");
41 }
42 
BOOST_AUTO_TEST_CASE(empty_vector_to_string)43 BOOST_AUTO_TEST_CASE(empty_vector_to_string) {
44   std::vector<int> l;
45   BOOST_CHECK_EQUAL(to_string(l), "[]");
46 }
47 
BOOST_AUTO_TEST_CASE(single_item_vector_to_string)48 BOOST_AUTO_TEST_CASE(single_item_vector_to_string) {
49   std::vector<int> l;
50   l.push_back(100);
51   BOOST_CHECK_EQUAL(to_string(l), "[100]");
52 }
53 
BOOST_AUTO_TEST_CASE(multiple_item_vector_to_string)54 BOOST_AUTO_TEST_CASE(multiple_item_vector_to_string) {
55   std::vector<int> l;
56   l.push_back(100);
57   l.push_back(150);
58   BOOST_CHECK_EQUAL(to_string(l), "[100, 150]");
59 }
60 
BOOST_AUTO_TEST_CASE(empty_map_to_string)61 BOOST_AUTO_TEST_CASE(empty_map_to_string) {
62   std::map<int, std::string> m;
63   BOOST_CHECK_EQUAL(to_string(m), "{}");
64 }
65 
BOOST_AUTO_TEST_CASE(single_item_map_to_string)66 BOOST_AUTO_TEST_CASE(single_item_map_to_string) {
67   std::map<int, std::string> m;
68   m[12] = "abc";
69   BOOST_CHECK_EQUAL(to_string(m), "{12: abc}");
70 }
71 
BOOST_AUTO_TEST_CASE(multi_item_map_to_string)72 BOOST_AUTO_TEST_CASE(multi_item_map_to_string) {
73   std::map<int, std::string> m;
74   m[12] = "abc";
75   m[31] = "xyz";
76   BOOST_CHECK_EQUAL(to_string(m), "{12: abc, 31: xyz}");
77 }
78 
BOOST_AUTO_TEST_CASE(empty_set_to_string)79 BOOST_AUTO_TEST_CASE(empty_set_to_string) {
80   std::set<char> s;
81   BOOST_CHECK_EQUAL(to_string(s), "{}");
82 }
83 
BOOST_AUTO_TEST_CASE(single_item_set_to_string)84 BOOST_AUTO_TEST_CASE(single_item_set_to_string) {
85   std::set<char> s;
86   s.insert('c');
87   BOOST_CHECK_EQUAL(to_string(s), "{c}");
88 }
89 
BOOST_AUTO_TEST_CASE(multi_item_set_to_string)90 BOOST_AUTO_TEST_CASE(multi_item_set_to_string) {
91   std::set<char> s;
92   s.insert('a');
93   s.insert('z');
94   BOOST_CHECK_EQUAL(to_string(s), "{a, z}");
95 }
96 
BOOST_AUTO_TEST_CASE(generated_empty_object_to_string)97 BOOST_AUTO_TEST_CASE(generated_empty_object_to_string) {
98   thrift::test::EmptyStruct e;
99   BOOST_CHECK_EQUAL(to_string(e), "EmptyStruct()");
100 }
101 
BOOST_AUTO_TEST_CASE(generated_single_basic_field_object_to_string)102 BOOST_AUTO_TEST_CASE(generated_single_basic_field_object_to_string) {
103   thrift::test::StructA a;
104   a.__set_s("abcd");
105   BOOST_CHECK_EQUAL(to_string(a), "StructA(s=abcd)");
106 }
107 
BOOST_AUTO_TEST_CASE(generated_two_basic_fields_object_to_string)108 BOOST_AUTO_TEST_CASE(generated_two_basic_fields_object_to_string) {
109   thrift::test::Bonk a;
110   a.__set_message("abcd");
111   a.__set_type(1234);
112   BOOST_CHECK_EQUAL(to_string(a), "Bonk(message=abcd, type=1234)");
113 }
114 
BOOST_AUTO_TEST_CASE(generated_optional_fields_object_to_string)115 BOOST_AUTO_TEST_CASE(generated_optional_fields_object_to_string) {
116   thrift::test::Tricky2 a;
117   BOOST_CHECK_EQUAL(to_string(a), "Tricky2(im_optional=<null>)");
118   a.__set_im_optional(123);
119   BOOST_CHECK_EQUAL(to_string(a), "Tricky2(im_optional=123)");
120 }
121 
BOOST_AUTO_TEST_CASE(generated_nested_object_to_string)122 BOOST_AUTO_TEST_CASE(generated_nested_object_to_string) {
123   thrift::test::OneField a;
124   BOOST_CHECK_EQUAL(to_string(a), "OneField(field=EmptyStruct())");
125 }
126 
BOOST_AUTO_TEST_CASE(generated_nested_list_object_to_string)127 BOOST_AUTO_TEST_CASE(generated_nested_list_object_to_string) {
128   thrift::test::ListBonks l;
129   l.bonk.assign(2, thrift::test::Bonk());
130   l.bonk[0].__set_message("a");
131   l.bonk[1].__set_message("b");
132 
133   BOOST_CHECK_EQUAL(to_string(l),
134                     "ListBonks(bonk=[Bonk(message=a, type=0), Bonk(message=b, type=0)])");
135 }
136 
137 BOOST_AUTO_TEST_SUITE_END()
138