Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 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> |
ubuntu | ad76a18 | 2021-06-07 08:21:05 +0530 | [diff] [blame] | 22 | #include <locale> |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 23 | |
zeshuai007 | 6b1cb30 | 2020-05-27 12:08:01 +0800 | [diff] [blame] | 24 | #include <boost/test/unit_test.hpp> |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 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 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(ToStringTest) |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 35 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 36 | BOOST_AUTO_TEST_CASE(base_types_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 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 | |
Mario Emmenlauer | e664ac4 | 2021-08-12 22:25:39 +0200 | [diff] [blame] | 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 |
ubuntu | ad76a18 | 2021-06-07 08:21:05 +0530 | [diff] [blame] | 48 | BOOST_AUTO_TEST_CASE(locale_en_US_int_to_string) { |
Mario Emmenlauer | e664ac4 | 2021-08-12 22:25:39 +0200 | [diff] [blame] | 49 | #ifdef _WIN32 |
ubuntu | ad76a18 | 2021-06-07 08:21:05 +0530 | [diff] [blame] | 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 | |
| 57 | BOOST_AUTO_TEST_CASE(locale_de_DE_floating_point_to_string) { |
Mario Emmenlauer | e664ac4 | 2021-08-12 22:25:39 +0200 | [diff] [blame] | 58 | #ifdef _WIN32 |
ubuntu | ad76a18 | 2021-06-07 08:21:05 +0530 | [diff] [blame] | 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 | } |
Mario Emmenlauer | e664ac4 | 2021-08-12 22:25:39 +0200 | [diff] [blame] | 67 | #endif |
ubuntu | ad76a18 | 2021-06-07 08:21:05 +0530 | [diff] [blame] | 68 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 69 | BOOST_AUTO_TEST_CASE(empty_vector_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 70 | std::vector<int> l; |
| 71 | BOOST_CHECK_EQUAL(to_string(l), "[]"); |
| 72 | } |
| 73 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 74 | BOOST_AUTO_TEST_CASE(single_item_vector_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 75 | std::vector<int> l; |
| 76 | l.push_back(100); |
| 77 | BOOST_CHECK_EQUAL(to_string(l), "[100]"); |
| 78 | } |
| 79 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 80 | BOOST_AUTO_TEST_CASE(multiple_item_vector_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 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 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 87 | BOOST_AUTO_TEST_CASE(empty_map_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 88 | std::map<int, std::string> m; |
| 89 | BOOST_CHECK_EQUAL(to_string(m), "{}"); |
| 90 | } |
| 91 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 92 | BOOST_AUTO_TEST_CASE(single_item_map_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 93 | std::map<int, std::string> m; |
| 94 | m[12] = "abc"; |
| 95 | BOOST_CHECK_EQUAL(to_string(m), "{12: abc}"); |
| 96 | } |
| 97 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 98 | BOOST_AUTO_TEST_CASE(multi_item_map_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 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 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 105 | BOOST_AUTO_TEST_CASE(empty_set_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 106 | std::set<char> s; |
| 107 | BOOST_CHECK_EQUAL(to_string(s), "{}"); |
| 108 | } |
| 109 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 110 | BOOST_AUTO_TEST_CASE(single_item_set_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 111 | std::set<char> s; |
| 112 | s.insert('c'); |
| 113 | BOOST_CHECK_EQUAL(to_string(s), "{c}"); |
| 114 | } |
| 115 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 116 | BOOST_AUTO_TEST_CASE(multi_item_set_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 117 | std::set<char> s; |
| 118 | s.insert('a'); |
| 119 | s.insert('z'); |
| 120 | BOOST_CHECK_EQUAL(to_string(s), "{a, z}"); |
| 121 | } |
| 122 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 123 | BOOST_AUTO_TEST_CASE(generated_empty_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 124 | thrift::test::EmptyStruct e; |
| 125 | BOOST_CHECK_EQUAL(to_string(e), "EmptyStruct()"); |
| 126 | } |
| 127 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 128 | BOOST_AUTO_TEST_CASE(generated_single_basic_field_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 129 | thrift::test::StructA a; |
| 130 | a.__set_s("abcd"); |
| 131 | BOOST_CHECK_EQUAL(to_string(a), "StructA(s=abcd)"); |
| 132 | } |
| 133 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 134 | BOOST_AUTO_TEST_CASE(generated_two_basic_fields_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 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 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 141 | BOOST_AUTO_TEST_CASE(generated_optional_fields_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 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 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 148 | BOOST_AUTO_TEST_CASE(generated_nested_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 149 | thrift::test::OneField a; |
| 150 | BOOST_CHECK_EQUAL(to_string(a), "OneField(field=EmptyStruct())"); |
| 151 | } |
| 152 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 153 | BOOST_AUTO_TEST_CASE(generated_nested_list_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 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 | |
CJCombrink | 1d886ca | 2024-03-23 21:32:28 +0100 | [diff] [blame] | 163 | BOOST_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 Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 172 | BOOST_AUTO_TEST_SUITE_END() |