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> |
| 22 | |
zeshuai007 | 6b1cb30 | 2020-05-27 12:08:01 +0800 | [diff] [blame^] | 23 | #include <boost/test/unit_test.hpp> |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 24 | |
| 25 | #include <thrift/TToString.h> |
| 26 | |
| 27 | #include "gen-cpp/ThriftTest_types.h" |
| 28 | #include "gen-cpp/OptionalRequiredTest_types.h" |
| 29 | #include "gen-cpp/DebugProtoTest_types.h" |
| 30 | |
| 31 | using apache::thrift::to_string; |
| 32 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(ToStringTest) |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 34 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 35 | BOOST_AUTO_TEST_CASE(base_types_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 36 | BOOST_CHECK_EQUAL(to_string(10), "10"); |
| 37 | BOOST_CHECK_EQUAL(to_string(true), "1"); |
| 38 | BOOST_CHECK_EQUAL(to_string('a'), "a"); |
| 39 | BOOST_CHECK_EQUAL(to_string(1.2), "1.2"); |
| 40 | BOOST_CHECK_EQUAL(to_string("abc"), "abc"); |
| 41 | } |
| 42 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 43 | BOOST_AUTO_TEST_CASE(empty_vector_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 44 | std::vector<int> l; |
| 45 | BOOST_CHECK_EQUAL(to_string(l), "[]"); |
| 46 | } |
| 47 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 48 | BOOST_AUTO_TEST_CASE(single_item_vector_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 49 | std::vector<int> l; |
| 50 | l.push_back(100); |
| 51 | BOOST_CHECK_EQUAL(to_string(l), "[100]"); |
| 52 | } |
| 53 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 54 | BOOST_AUTO_TEST_CASE(multiple_item_vector_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 55 | std::vector<int> l; |
| 56 | l.push_back(100); |
| 57 | l.push_back(150); |
| 58 | BOOST_CHECK_EQUAL(to_string(l), "[100, 150]"); |
| 59 | } |
| 60 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 61 | BOOST_AUTO_TEST_CASE(empty_map_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 62 | std::map<int, std::string> m; |
| 63 | BOOST_CHECK_EQUAL(to_string(m), "{}"); |
| 64 | } |
| 65 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 66 | BOOST_AUTO_TEST_CASE(single_item_map_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 67 | std::map<int, std::string> m; |
| 68 | m[12] = "abc"; |
| 69 | BOOST_CHECK_EQUAL(to_string(m), "{12: abc}"); |
| 70 | } |
| 71 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 72 | BOOST_AUTO_TEST_CASE(multi_item_map_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 73 | std::map<int, std::string> m; |
| 74 | m[12] = "abc"; |
| 75 | m[31] = "xyz"; |
| 76 | BOOST_CHECK_EQUAL(to_string(m), "{12: abc, 31: xyz}"); |
| 77 | } |
| 78 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 79 | BOOST_AUTO_TEST_CASE(empty_set_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 80 | std::set<char> s; |
| 81 | BOOST_CHECK_EQUAL(to_string(s), "{}"); |
| 82 | } |
| 83 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 84 | BOOST_AUTO_TEST_CASE(single_item_set_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 85 | std::set<char> s; |
| 86 | s.insert('c'); |
| 87 | BOOST_CHECK_EQUAL(to_string(s), "{c}"); |
| 88 | } |
| 89 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 90 | BOOST_AUTO_TEST_CASE(multi_item_set_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 91 | std::set<char> s; |
| 92 | s.insert('a'); |
| 93 | s.insert('z'); |
| 94 | BOOST_CHECK_EQUAL(to_string(s), "{a, z}"); |
| 95 | } |
| 96 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 97 | BOOST_AUTO_TEST_CASE(generated_empty_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 98 | thrift::test::EmptyStruct e; |
| 99 | BOOST_CHECK_EQUAL(to_string(e), "EmptyStruct()"); |
| 100 | } |
| 101 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 102 | BOOST_AUTO_TEST_CASE(generated_single_basic_field_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 103 | thrift::test::StructA a; |
| 104 | a.__set_s("abcd"); |
| 105 | BOOST_CHECK_EQUAL(to_string(a), "StructA(s=abcd)"); |
| 106 | } |
| 107 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 108 | BOOST_AUTO_TEST_CASE(generated_two_basic_fields_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 109 | thrift::test::Bonk a; |
| 110 | a.__set_message("abcd"); |
| 111 | a.__set_type(1234); |
| 112 | BOOST_CHECK_EQUAL(to_string(a), "Bonk(message=abcd, type=1234)"); |
| 113 | } |
| 114 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 115 | BOOST_AUTO_TEST_CASE(generated_optional_fields_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 116 | thrift::test::Tricky2 a; |
| 117 | BOOST_CHECK_EQUAL(to_string(a), "Tricky2(im_optional=<null>)"); |
| 118 | a.__set_im_optional(123); |
| 119 | BOOST_CHECK_EQUAL(to_string(a), "Tricky2(im_optional=123)"); |
| 120 | } |
| 121 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 122 | BOOST_AUTO_TEST_CASE(generated_nested_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 123 | thrift::test::OneField a; |
| 124 | BOOST_CHECK_EQUAL(to_string(a), "OneField(field=EmptyStruct())"); |
| 125 | } |
| 126 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 127 | BOOST_AUTO_TEST_CASE(generated_nested_list_object_to_string) { |
Konrad Grochowski | b3f6ea1 | 2014-09-02 16:00:47 +0200 | [diff] [blame] | 128 | thrift::test::ListBonks l; |
| 129 | l.bonk.assign(2, thrift::test::Bonk()); |
| 130 | l.bonk[0].__set_message("a"); |
| 131 | l.bonk[1].__set_message("b"); |
| 132 | |
| 133 | BOOST_CHECK_EQUAL(to_string(l), |
| 134 | "ListBonks(bonk=[Bonk(message=a, type=0), Bonk(message=b, type=0)])"); |
| 135 | } |
| 136 | |
| 137 | BOOST_AUTO_TEST_SUITE_END() |