blob: 722e5904c262688b8386e0b48059021a118f0c47 [file] [log] [blame]
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +02001/*
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>
ubuntuad76a182021-06-07 08:21:05 +053022#include <locale>
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020023
zeshuai0076b1cb302020-05-27 12:08:01 +080024#include <boost/test/unit_test.hpp>
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020025
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
32using apache::thrift::to_string;
33
Konrad Grochowski16a23a62014-11-13 15:33:38 +010034BOOST_AUTO_TEST_SUITE(ToStringTest)
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020035
Konrad Grochowski16a23a62014-11-13 15:33:38 +010036BOOST_AUTO_TEST_CASE(base_types_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020037 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
ubuntuad76a182021-06-07 08:21:05 +053044BOOST_AUTO_TEST_CASE(locale_en_US_int_to_string) {
45#if _WIN32
46 std::locale::global(std::locale("en-US.UTF-8"));
47#else
48 std::locale::global(std::locale("en_US.UTF-8"));
49#endif
50 BOOST_CHECK_EQUAL(to_string(1000000), "1000000");
51}
52
53BOOST_AUTO_TEST_CASE(locale_de_DE_floating_point_to_string) {
54#if _WIN32
55 std::locale::global(std::locale("de-DE.UTF-8"));
56#else
57 std::locale::global(std::locale("de_DE.UTF-8"));
58#endif
59 BOOST_CHECK_EQUAL(to_string(1.5), "1.5");
60 BOOST_CHECK_EQUAL(to_string(1.5f), "1.5");
61 BOOST_CHECK_EQUAL(to_string(1.5L), "1.5");
62}
63
Konrad Grochowski16a23a62014-11-13 15:33:38 +010064BOOST_AUTO_TEST_CASE(empty_vector_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020065 std::vector<int> l;
66 BOOST_CHECK_EQUAL(to_string(l), "[]");
67}
68
Konrad Grochowski16a23a62014-11-13 15:33:38 +010069BOOST_AUTO_TEST_CASE(single_item_vector_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020070 std::vector<int> l;
71 l.push_back(100);
72 BOOST_CHECK_EQUAL(to_string(l), "[100]");
73}
74
Konrad Grochowski16a23a62014-11-13 15:33:38 +010075BOOST_AUTO_TEST_CASE(multiple_item_vector_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020076 std::vector<int> l;
77 l.push_back(100);
78 l.push_back(150);
79 BOOST_CHECK_EQUAL(to_string(l), "[100, 150]");
80}
81
Konrad Grochowski16a23a62014-11-13 15:33:38 +010082BOOST_AUTO_TEST_CASE(empty_map_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020083 std::map<int, std::string> m;
84 BOOST_CHECK_EQUAL(to_string(m), "{}");
85}
86
Konrad Grochowski16a23a62014-11-13 15:33:38 +010087BOOST_AUTO_TEST_CASE(single_item_map_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020088 std::map<int, std::string> m;
89 m[12] = "abc";
90 BOOST_CHECK_EQUAL(to_string(m), "{12: abc}");
91}
92
Konrad Grochowski16a23a62014-11-13 15:33:38 +010093BOOST_AUTO_TEST_CASE(multi_item_map_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020094 std::map<int, std::string> m;
95 m[12] = "abc";
96 m[31] = "xyz";
97 BOOST_CHECK_EQUAL(to_string(m), "{12: abc, 31: xyz}");
98}
99
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100100BOOST_AUTO_TEST_CASE(empty_set_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200101 std::set<char> s;
102 BOOST_CHECK_EQUAL(to_string(s), "{}");
103}
104
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100105BOOST_AUTO_TEST_CASE(single_item_set_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200106 std::set<char> s;
107 s.insert('c');
108 BOOST_CHECK_EQUAL(to_string(s), "{c}");
109}
110
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100111BOOST_AUTO_TEST_CASE(multi_item_set_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200112 std::set<char> s;
113 s.insert('a');
114 s.insert('z');
115 BOOST_CHECK_EQUAL(to_string(s), "{a, z}");
116}
117
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100118BOOST_AUTO_TEST_CASE(generated_empty_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200119 thrift::test::EmptyStruct e;
120 BOOST_CHECK_EQUAL(to_string(e), "EmptyStruct()");
121}
122
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100123BOOST_AUTO_TEST_CASE(generated_single_basic_field_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200124 thrift::test::StructA a;
125 a.__set_s("abcd");
126 BOOST_CHECK_EQUAL(to_string(a), "StructA(s=abcd)");
127}
128
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100129BOOST_AUTO_TEST_CASE(generated_two_basic_fields_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200130 thrift::test::Bonk a;
131 a.__set_message("abcd");
132 a.__set_type(1234);
133 BOOST_CHECK_EQUAL(to_string(a), "Bonk(message=abcd, type=1234)");
134}
135
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100136BOOST_AUTO_TEST_CASE(generated_optional_fields_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200137 thrift::test::Tricky2 a;
138 BOOST_CHECK_EQUAL(to_string(a), "Tricky2(im_optional=<null>)");
139 a.__set_im_optional(123);
140 BOOST_CHECK_EQUAL(to_string(a), "Tricky2(im_optional=123)");
141}
142
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100143BOOST_AUTO_TEST_CASE(generated_nested_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200144 thrift::test::OneField a;
145 BOOST_CHECK_EQUAL(to_string(a), "OneField(field=EmptyStruct())");
146}
147
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100148BOOST_AUTO_TEST_CASE(generated_nested_list_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200149 thrift::test::ListBonks l;
150 l.bonk.assign(2, thrift::test::Bonk());
151 l.bonk[0].__set_message("a");
152 l.bonk[1].__set_message("b");
153
154 BOOST_CHECK_EQUAL(to_string(l),
155 "ListBonks(bonk=[Bonk(message=a, type=0), Bonk(message=b, type=0)])");
156}
157
158BOOST_AUTO_TEST_SUITE_END()