blob: 68c82ad4be1df00265d1f380202de089d9eb69a5 [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
Mario Emmenlauere664ac42021-08-12 22:25:39 +020044// 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
ubuntuad76a182021-06-07 08:21:05 +053048BOOST_AUTO_TEST_CASE(locale_en_US_int_to_string) {
Mario Emmenlauere664ac42021-08-12 22:25:39 +020049#ifdef _WIN32
ubuntuad76a182021-06-07 08:21:05 +053050 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
57BOOST_AUTO_TEST_CASE(locale_de_DE_floating_point_to_string) {
Mario Emmenlauere664ac42021-08-12 22:25:39 +020058#ifdef _WIN32
ubuntuad76a182021-06-07 08:21:05 +053059 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}
Mario Emmenlauere664ac42021-08-12 22:25:39 +020067#endif
ubuntuad76a182021-06-07 08:21:05 +053068
Konrad Grochowski16a23a62014-11-13 15:33:38 +010069BOOST_AUTO_TEST_CASE(empty_vector_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020070 std::vector<int> l;
71 BOOST_CHECK_EQUAL(to_string(l), "[]");
72}
73
Konrad Grochowski16a23a62014-11-13 15:33:38 +010074BOOST_AUTO_TEST_CASE(single_item_vector_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020075 std::vector<int> l;
76 l.push_back(100);
77 BOOST_CHECK_EQUAL(to_string(l), "[100]");
78}
79
Konrad Grochowski16a23a62014-11-13 15:33:38 +010080BOOST_AUTO_TEST_CASE(multiple_item_vector_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020081 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
Konrad Grochowski16a23a62014-11-13 15:33:38 +010087BOOST_AUTO_TEST_CASE(empty_map_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020088 std::map<int, std::string> m;
89 BOOST_CHECK_EQUAL(to_string(m), "{}");
90}
91
Konrad Grochowski16a23a62014-11-13 15:33:38 +010092BOOST_AUTO_TEST_CASE(single_item_map_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020093 std::map<int, std::string> m;
94 m[12] = "abc";
95 BOOST_CHECK_EQUAL(to_string(m), "{12: abc}");
96}
97
Konrad Grochowski16a23a62014-11-13 15:33:38 +010098BOOST_AUTO_TEST_CASE(multi_item_map_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020099 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
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100105BOOST_AUTO_TEST_CASE(empty_set_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200106 std::set<char> s;
107 BOOST_CHECK_EQUAL(to_string(s), "{}");
108}
109
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100110BOOST_AUTO_TEST_CASE(single_item_set_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200111 std::set<char> s;
112 s.insert('c');
113 BOOST_CHECK_EQUAL(to_string(s), "{c}");
114}
115
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100116BOOST_AUTO_TEST_CASE(multi_item_set_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200117 std::set<char> s;
118 s.insert('a');
119 s.insert('z');
120 BOOST_CHECK_EQUAL(to_string(s), "{a, z}");
121}
122
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100123BOOST_AUTO_TEST_CASE(generated_empty_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200124 thrift::test::EmptyStruct e;
125 BOOST_CHECK_EQUAL(to_string(e), "EmptyStruct()");
126}
127
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100128BOOST_AUTO_TEST_CASE(generated_single_basic_field_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200129 thrift::test::StructA a;
130 a.__set_s("abcd");
131 BOOST_CHECK_EQUAL(to_string(a), "StructA(s=abcd)");
132}
133
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100134BOOST_AUTO_TEST_CASE(generated_two_basic_fields_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200135 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
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100141BOOST_AUTO_TEST_CASE(generated_optional_fields_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200142 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
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100148BOOST_AUTO_TEST_CASE(generated_nested_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200149 thrift::test::OneField a;
150 BOOST_CHECK_EQUAL(to_string(a), "OneField(field=EmptyStruct())");
151}
152
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100153BOOST_AUTO_TEST_CASE(generated_nested_list_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200154 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
CJCombrink1d886ca2024-03-23 21:32:28 +0100163BOOST_AUTO_TEST_CASE(generated_uuid_to_string) {
164 thrift::test::CrazyNesting l;
165 l.uuid_field = "{4b686716-5f20-4deb-8ce0-9eaf379e8a3d}";
166
167 BOOST_CHECK_EQUAL(to_string(l),
168 "CrazyNesting(string_field=, set_field=<null>, list_field=[], binary_field=, "
169 "uuid_field={4b686716-5f20-4deb-8ce0-9eaf379e8a3d})");
170}
171
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200172BOOST_AUTO_TEST_SUITE_END()