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 | |
ubuntu | ad76a18 | 2021-06-07 08:21:05 +0530 | [diff] [blame^] | 44 | BOOST_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 | |
| 53 | BOOST_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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 64 | BOOST_AUTO_TEST_CASE(empty_vector_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 65 | std::vector<int> l; |
| 66 | BOOST_CHECK_EQUAL(to_string(l), "[]"); |
| 67 | } |
| 68 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 69 | BOOST_AUTO_TEST_CASE(single_item_vector_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 70 | std::vector<int> l; |
| 71 | l.push_back(100); |
| 72 | BOOST_CHECK_EQUAL(to_string(l), "[100]"); |
| 73 | } |
| 74 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 75 | BOOST_AUTO_TEST_CASE(multiple_item_vector_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 76 | 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 82 | BOOST_AUTO_TEST_CASE(empty_map_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 83 | std::map<int, std::string> m; |
| 84 | BOOST_CHECK_EQUAL(to_string(m), "{}"); |
| 85 | } |
| 86 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 87 | BOOST_AUTO_TEST_CASE(single_item_map_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 88 | std::map<int, std::string> m; |
| 89 | m[12] = "abc"; |
| 90 | BOOST_CHECK_EQUAL(to_string(m), "{12: abc}"); |
| 91 | } |
| 92 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 93 | BOOST_AUTO_TEST_CASE(multi_item_map_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 94 | 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 100 | BOOST_AUTO_TEST_CASE(empty_set_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 101 | std::set<char> s; |
| 102 | BOOST_CHECK_EQUAL(to_string(s), "{}"); |
| 103 | } |
| 104 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 105 | BOOST_AUTO_TEST_CASE(single_item_set_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 106 | std::set<char> s; |
| 107 | s.insert('c'); |
| 108 | BOOST_CHECK_EQUAL(to_string(s), "{c}"); |
| 109 | } |
| 110 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 111 | BOOST_AUTO_TEST_CASE(multi_item_set_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 112 | std::set<char> s; |
| 113 | s.insert('a'); |
| 114 | s.insert('z'); |
| 115 | BOOST_CHECK_EQUAL(to_string(s), "{a, z}"); |
| 116 | } |
| 117 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 118 | BOOST_AUTO_TEST_CASE(generated_empty_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 119 | thrift::test::EmptyStruct e; |
| 120 | BOOST_CHECK_EQUAL(to_string(e), "EmptyStruct()"); |
| 121 | } |
| 122 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 123 | BOOST_AUTO_TEST_CASE(generated_single_basic_field_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 124 | thrift::test::StructA a; |
| 125 | a.__set_s("abcd"); |
| 126 | BOOST_CHECK_EQUAL(to_string(a), "StructA(s=abcd)"); |
| 127 | } |
| 128 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 129 | BOOST_AUTO_TEST_CASE(generated_two_basic_fields_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 130 | 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 136 | BOOST_AUTO_TEST_CASE(generated_optional_fields_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 137 | 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 Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 143 | BOOST_AUTO_TEST_CASE(generated_nested_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 144 | thrift::test::OneField a; |
| 145 | BOOST_CHECK_EQUAL(to_string(a), "OneField(field=EmptyStruct())"); |
| 146 | } |
| 147 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 148 | BOOST_AUTO_TEST_CASE(generated_nested_list_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 149 | 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 | |
| 158 | BOOST_AUTO_TEST_SUITE_END() |