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 #include <locale>
23 
24 #include <boost/test/unit_test.hpp>
25 
26 #include <thrift/TToString.h>
27 
28 #include "gen-cpp/ThriftTest_types.h"
29 #include "gen-cpp/OptionalRequiredTest_types.h"
30 #include "gen-cpp/DebugProtoTest_types.h"
31 
32 using apache::thrift::to_string;
33 
34 BOOST_AUTO_TEST_SUITE(ToStringTest)
35 
BOOST_AUTO_TEST_CASE(base_types_to_string)36 BOOST_AUTO_TEST_CASE(base_types_to_string) {
37   BOOST_CHECK_EQUAL(to_string(10), "10");
38   BOOST_CHECK_EQUAL(to_string(true), "1");
39   BOOST_CHECK_EQUAL(to_string('a'), "a");
40   BOOST_CHECK_EQUAL(to_string(1.2), "1.2");
41   BOOST_CHECK_EQUAL(to_string("abc"), "abc");
42 }
43 
44 // NOTE: Currently (as of 2021.08.12) the locale-based tests do not work on
45 // Windows in the AppVeyor Thrift CI build correctly. Therefore disabled on
46 // Windows:
47 #ifndef _WIN32
BOOST_AUTO_TEST_CASE(locale_en_US_int_to_string)48 BOOST_AUTO_TEST_CASE(locale_en_US_int_to_string) {
49 #ifdef _WIN32
50   std::locale::global(std::locale("en-US.UTF-8"));
51 #else
52   std::locale::global(std::locale("en_US.UTF-8"));
53 #endif
54   BOOST_CHECK_EQUAL(to_string(1000000), "1000000");
55 }
56 
BOOST_AUTO_TEST_CASE(locale_de_DE_floating_point_to_string)57 BOOST_AUTO_TEST_CASE(locale_de_DE_floating_point_to_string) {
58 #ifdef _WIN32
59   std::locale::global(std::locale("de-DE.UTF-8"));
60 #else
61   std::locale::global(std::locale("de_DE.UTF-8"));
62 #endif
63   BOOST_CHECK_EQUAL(to_string(1.5), "1.5");
64   BOOST_CHECK_EQUAL(to_string(1.5f), "1.5");
65   BOOST_CHECK_EQUAL(to_string(1.5L), "1.5");
66 }
67 #endif
68 
BOOST_AUTO_TEST_CASE(empty_vector_to_string)69 BOOST_AUTO_TEST_CASE(empty_vector_to_string) {
70   std::vector<int> l;
71   BOOST_CHECK_EQUAL(to_string(l), "[]");
72 }
73 
BOOST_AUTO_TEST_CASE(single_item_vector_to_string)74 BOOST_AUTO_TEST_CASE(single_item_vector_to_string) {
75   std::vector<int> l;
76   l.push_back(100);
77   BOOST_CHECK_EQUAL(to_string(l), "[100]");
78 }
79 
BOOST_AUTO_TEST_CASE(multiple_item_vector_to_string)80 BOOST_AUTO_TEST_CASE(multiple_item_vector_to_string) {
81   std::vector<int> l;
82   l.push_back(100);
83   l.push_back(150);
84   BOOST_CHECK_EQUAL(to_string(l), "[100, 150]");
85 }
86 
BOOST_AUTO_TEST_CASE(empty_map_to_string)87 BOOST_AUTO_TEST_CASE(empty_map_to_string) {
88   std::map<int, std::string> m;
89   BOOST_CHECK_EQUAL(to_string(m), "{}");
90 }
91 
BOOST_AUTO_TEST_CASE(single_item_map_to_string)92 BOOST_AUTO_TEST_CASE(single_item_map_to_string) {
93   std::map<int, std::string> m;
94   m[12] = "abc";
95   BOOST_CHECK_EQUAL(to_string(m), "{12: abc}");
96 }
97 
BOOST_AUTO_TEST_CASE(multi_item_map_to_string)98 BOOST_AUTO_TEST_CASE(multi_item_map_to_string) {
99   std::map<int, std::string> m;
100   m[12] = "abc";
101   m[31] = "xyz";
102   BOOST_CHECK_EQUAL(to_string(m), "{12: abc, 31: xyz}");
103 }
104 
BOOST_AUTO_TEST_CASE(empty_set_to_string)105 BOOST_AUTO_TEST_CASE(empty_set_to_string) {
106   std::set<char> s;
107   BOOST_CHECK_EQUAL(to_string(s), "{}");
108 }
109 
BOOST_AUTO_TEST_CASE(single_item_set_to_string)110 BOOST_AUTO_TEST_CASE(single_item_set_to_string) {
111   std::set<char> s;
112   s.insert('c');
113   BOOST_CHECK_EQUAL(to_string(s), "{c}");
114 }
115 
BOOST_AUTO_TEST_CASE(multi_item_set_to_string)116 BOOST_AUTO_TEST_CASE(multi_item_set_to_string) {
117   std::set<char> s;
118   s.insert('a');
119   s.insert('z');
120   BOOST_CHECK_EQUAL(to_string(s), "{a, z}");
121 }
122 
BOOST_AUTO_TEST_CASE(generated_empty_object_to_string)123 BOOST_AUTO_TEST_CASE(generated_empty_object_to_string) {
124   thrift::test::EmptyStruct e;
125   BOOST_CHECK_EQUAL(to_string(e), "EmptyStruct()");
126 }
127 
BOOST_AUTO_TEST_CASE(generated_single_basic_field_object_to_string)128 BOOST_AUTO_TEST_CASE(generated_single_basic_field_object_to_string) {
129   thrift::test::StructA a;
130   a.__set_s("abcd");
131   BOOST_CHECK_EQUAL(to_string(a), "StructA(s=abcd)");
132 }
133 
BOOST_AUTO_TEST_CASE(generated_two_basic_fields_object_to_string)134 BOOST_AUTO_TEST_CASE(generated_two_basic_fields_object_to_string) {
135   thrift::test::Bonk a;
136   a.__set_message("abcd");
137   a.__set_type(1234);
138   BOOST_CHECK_EQUAL(to_string(a), "Bonk(message=abcd, type=1234)");
139 }
140 
BOOST_AUTO_TEST_CASE(generated_optional_fields_object_to_string)141 BOOST_AUTO_TEST_CASE(generated_optional_fields_object_to_string) {
142   thrift::test::Tricky2 a;
143   BOOST_CHECK_EQUAL(to_string(a), "Tricky2(im_optional=<null>)");
144   a.__set_im_optional(123);
145   BOOST_CHECK_EQUAL(to_string(a), "Tricky2(im_optional=123)");
146 }
147 
BOOST_AUTO_TEST_CASE(generated_nested_object_to_string)148 BOOST_AUTO_TEST_CASE(generated_nested_object_to_string) {
149   thrift::test::OneField a;
150   BOOST_CHECK_EQUAL(to_string(a), "OneField(field=EmptyStruct())");
151 }
152 
BOOST_AUTO_TEST_CASE(generated_nested_list_object_to_string)153 BOOST_AUTO_TEST_CASE(generated_nested_list_object_to_string) {
154   thrift::test::ListBonks l;
155   l.bonk.assign(2, thrift::test::Bonk());
156   l.bonk[0].__set_message("a");
157   l.bonk[1].__set_message("b");
158 
159   BOOST_CHECK_EQUAL(to_string(l),
160                     "ListBonks(bonk=[Bonk(message=a, type=0), Bonk(message=b, type=0)])");
161 }
162 
163 BOOST_AUTO_TEST_SUITE_END()
164