blob: d204cb346edf7bde093c560d32fd706a149c81ce [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>
22
23#include <boost/test/auto_unit_test.hpp>
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
31using apache::thrift::to_string;
32
Konrad Grochowski16a23a62014-11-13 15:33:38 +010033BOOST_AUTO_TEST_SUITE(ToStringTest)
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020034
Konrad Grochowski16a23a62014-11-13 15:33:38 +010035BOOST_AUTO_TEST_CASE(base_types_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020036 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 Grochowski16a23a62014-11-13 15:33:38 +010043BOOST_AUTO_TEST_CASE(empty_vector_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020044 std::vector<int> l;
45 BOOST_CHECK_EQUAL(to_string(l), "[]");
46}
47
Konrad Grochowski16a23a62014-11-13 15:33:38 +010048BOOST_AUTO_TEST_CASE(single_item_vector_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020049 std::vector<int> l;
50 l.push_back(100);
51 BOOST_CHECK_EQUAL(to_string(l), "[100]");
52}
53
Konrad Grochowski16a23a62014-11-13 15:33:38 +010054BOOST_AUTO_TEST_CASE(multiple_item_vector_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020055 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 Grochowski16a23a62014-11-13 15:33:38 +010061BOOST_AUTO_TEST_CASE(empty_map_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020062 std::map<int, std::string> m;
63 BOOST_CHECK_EQUAL(to_string(m), "{}");
64}
65
Konrad Grochowski16a23a62014-11-13 15:33:38 +010066BOOST_AUTO_TEST_CASE(single_item_map_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020067 std::map<int, std::string> m;
68 m[12] = "abc";
69 BOOST_CHECK_EQUAL(to_string(m), "{12: abc}");
70}
71
Konrad Grochowski16a23a62014-11-13 15:33:38 +010072BOOST_AUTO_TEST_CASE(multi_item_map_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020073 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 Grochowski16a23a62014-11-13 15:33:38 +010079BOOST_AUTO_TEST_CASE(empty_set_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020080 std::set<char> s;
81 BOOST_CHECK_EQUAL(to_string(s), "{}");
82}
83
Konrad Grochowski16a23a62014-11-13 15:33:38 +010084BOOST_AUTO_TEST_CASE(single_item_set_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020085 std::set<char> s;
86 s.insert('c');
87 BOOST_CHECK_EQUAL(to_string(s), "{c}");
88}
89
Konrad Grochowski16a23a62014-11-13 15:33:38 +010090BOOST_AUTO_TEST_CASE(multi_item_set_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020091 std::set<char> s;
92 s.insert('a');
93 s.insert('z');
94 BOOST_CHECK_EQUAL(to_string(s), "{a, z}");
95}
96
Konrad Grochowski16a23a62014-11-13 15:33:38 +010097BOOST_AUTO_TEST_CASE(generated_empty_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +020098 thrift::test::EmptyStruct e;
99 BOOST_CHECK_EQUAL(to_string(e), "EmptyStruct()");
100}
101
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100102BOOST_AUTO_TEST_CASE(generated_single_basic_field_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200103 thrift::test::StructA a;
104 a.__set_s("abcd");
105 BOOST_CHECK_EQUAL(to_string(a), "StructA(s=abcd)");
106}
107
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100108BOOST_AUTO_TEST_CASE(generated_two_basic_fields_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200109 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 Grochowski16a23a62014-11-13 15:33:38 +0100115BOOST_AUTO_TEST_CASE(generated_optional_fields_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200116 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 Grochowski16a23a62014-11-13 15:33:38 +0100122BOOST_AUTO_TEST_CASE(generated_nested_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200123 thrift::test::OneField a;
124 BOOST_CHECK_EQUAL(to_string(a), "OneField(field=EmptyStruct())");
125}
126
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100127BOOST_AUTO_TEST_CASE(generated_nested_list_object_to_string) {
Konrad Grochowskib3f6ea12014-09-02 16:00:47 +0200128 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
137BOOST_AUTO_TEST_SUITE_END()