|
15 | 15 | */
|
16 | 16 |
|
17 | 17 | #include <folly/portability/GTest.h>
|
| 18 | +#include <thrift/conformance/cpp2/internal/AnyStructSerializer.h> |
| 19 | +#include <thrift/lib/cpp/util/EnumUtils.h> |
18 | 20 | #include <thrift/lib/cpp2/op/Encode.h>
|
| 21 | +#include <thrift/lib/cpp2/protocol/Object.h> |
| 22 | +#include <thrift/lib/cpp2/type/Tag.h> |
| 23 | +#include <thrift/test/AdapterTest.h> |
| 24 | +#include <thrift/test/testset/Testset.h> |
| 25 | + |
| 26 | +using namespace ::apache::thrift::conformance; |
| 27 | + |
| 28 | +using detail::protocol_reader_t; |
| 29 | +using detail::protocol_writer_t; |
19 | 30 |
|
20 | 31 | namespace apache::thrift::op {
|
21 | 32 | namespace {
|
| 33 | +using apache::thrift::protocol::asValueStruct; |
22 | 34 | using apache::thrift::protocol::TType;
|
23 | 35 | using detail::typeTagToTType;
|
24 | 36 |
|
@@ -52,5 +64,154 @@ TEST(EncodeTest, TypeTagToTType) {
|
52 | 64 | (typeTagToTType<type::adapted<void, type::struct_t<void>>>),
|
53 | 65 | TType::T_STRUCT);
|
54 | 66 | }
|
| 67 | + |
| 68 | +template <conformance::StandardProtocol Protocol, typename Tag, typename T> |
| 69 | +protocol::Value encodeAndParseValue( |
| 70 | + T value, TType ttype, bool string_to_binary = true) { |
| 71 | + protocol_writer_t<Protocol> writer; |
| 72 | + folly::IOBufQueue queue; |
| 73 | + writer.setOutput(&queue); |
| 74 | + encode<Tag>(writer, value); |
| 75 | + protocol_reader_t<Protocol> reader; |
| 76 | + auto serialized = queue.move(); |
| 77 | + reader.setInput(serialized.get()); |
| 78 | + return protocol::detail::parseValue(reader, ttype, string_to_binary); |
| 79 | +} |
| 80 | + |
| 81 | +template <conformance::StandardProtocol Protocol, typename Tag, typename T> |
| 82 | +void testEncode(T value, bool string_to_binary = true) { |
| 83 | + SCOPED_TRACE(folly::pretty_name<Tag>()); |
| 84 | + auto result = encodeAndParseValue<Protocol, Tag>( |
| 85 | + value, typeTagToTType<Tag>, string_to_binary); |
| 86 | + EXPECT_EQ(result, asValueStruct<Tag>(value)); |
| 87 | +} |
| 88 | + |
| 89 | +template <conformance::StandardProtocol Protocol> |
| 90 | +void testEncodeBasicTypes() { |
| 91 | + SCOPED_TRACE(apache::thrift::util::enumNameSafe(Protocol)); |
| 92 | + testEncode<Protocol, type::bool_t>(true); |
| 93 | + testEncode<Protocol, type::byte_t>(1); |
| 94 | + testEncode<Protocol, type::i16_t>(1); |
| 95 | + testEncode<Protocol, type::i32_t>(1); |
| 96 | + testEncode<Protocol, type::i64_t>(1); |
| 97 | + testEncode<Protocol, type::float_t>(1.5); |
| 98 | + testEncode<Protocol, type::double_t>(1.5); |
| 99 | + testEncode<Protocol, type::string_t>("foo", false); |
| 100 | + testEncode<Protocol, type::binary_t>("foo"); |
| 101 | + testEncode<Protocol, type::enum_t<int>>(1); |
| 102 | +} |
| 103 | + |
| 104 | +template <conformance::StandardProtocol Protocol> |
| 105 | +void testEncodeContainers() { |
| 106 | + SCOPED_TRACE(apache::thrift::util::enumNameSafe(Protocol)); |
| 107 | + testEncode<Protocol, type::list<type::enum_t<int>>>( |
| 108 | + std::vector<int32_t>{1, 2, 3}); |
| 109 | + testEncode<Protocol, type::set<type::bool_t>>(std::set<bool>{true, false}); |
| 110 | + testEncode<Protocol, type::map<type::string_t, type::byte_t>>( |
| 111 | + std::map<std::string, int8_t>{{"foo", 1}, {"bar", 2}}, false); |
| 112 | +} |
| 113 | + |
| 114 | +template <conformance::StandardProtocol Protocol> |
| 115 | +void testEncodeCppType() { |
| 116 | + SCOPED_TRACE(apache::thrift::util::enumNameSafe(Protocol)); |
| 117 | + { |
| 118 | + // test cpp_type with primitive type |
| 119 | + auto result = |
| 120 | + encodeAndParseValue<Protocol, type::cpp_type<int, type::i16_t>>( |
| 121 | + 1, TType::T_I16); |
| 122 | + EXPECT_EQ(result, asValueStruct<type::i16_t>(1)); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// If IsAdapted is true, test op::encode with the given object with |
| 127 | +// type::adapted<test::TemplatedTestAdapter, Tag> tag and fromThrift. |
| 128 | +template < |
| 129 | + conformance::StandardProtocol Protocol, |
| 130 | + typename Struct, |
| 131 | + typename Tag, |
| 132 | + bool IsAdapted = false, |
| 133 | + typename T> |
| 134 | +void testEncodeObject(T value) { |
| 135 | + SCOPED_TRACE(folly::pretty_name<Tag>()); |
| 136 | + Struct foo; |
| 137 | + foo.field_1_ref() = value; |
| 138 | + protocol_writer_t<Protocol> w1, w2; |
| 139 | + folly::IOBufQueue o1, o2; |
| 140 | + w1.setOutput(&o1); |
| 141 | + w2.setOutput(&o2); |
| 142 | + if constexpr (IsAdapted) { |
| 143 | + using AdaptedTag = type::adapted<test::TemplatedTestAdapter, Tag>; |
| 144 | + encode<AdaptedTag>(w1, test::TemplatedTestAdapter::fromThrift(foo)); |
| 145 | + } else { |
| 146 | + encode<Tag>(w1, foo); |
| 147 | + } |
| 148 | + foo.write(&w2); |
| 149 | + EXPECT_TRUE(folly::IOBufEqualTo{}(*o1.move(), *o2.move())); |
| 150 | +} |
| 151 | + |
| 152 | +template <conformance::StandardProtocol Protocol> |
| 153 | +void testEncodeStruct() { |
| 154 | + SCOPED_TRACE(apache::thrift::util::enumNameSafe(Protocol)); |
| 155 | + using Struct = |
| 156 | + test::testset::struct_with<type::map<type::string_t, type::i32_t>>; |
| 157 | + std::map<std::string, int> mapValues = {{"one", 1}, {"four", 4}, {"two", 2}}; |
| 158 | + testEncodeObject<Protocol, Struct, type::struct_t<Struct>>(mapValues); |
| 159 | + using Union = test::testset::union_with<type::set<type::string_t>>; |
| 160 | + std::set<std::string> setValues = {"foo", "bar", "baz"}; |
| 161 | + testEncodeObject<Protocol, Union, type::union_t<Union>>(setValues); |
| 162 | + using Exception = test::testset::exception_with<type::i64_t>; |
| 163 | + testEncodeObject<Protocol, Exception, type::exception_t<Exception>>(1); |
| 164 | +} |
| 165 | + |
| 166 | +template <conformance::StandardProtocol Protocol> |
| 167 | +void testEncodeAdapted() { |
| 168 | + SCOPED_TRACE(apache::thrift::util::enumNameSafe(Protocol)); |
| 169 | + { |
| 170 | + // test op::encode with adapted struct |
| 171 | + using Struct = test::testset::struct_with<type::i32_t>; |
| 172 | + testEncodeObject<Protocol, Struct, type::struct_t<Struct>, true>(1); |
| 173 | + } |
| 174 | + { |
| 175 | + // test op::encode with adapted primitive type |
| 176 | + using AdaptedTag = type::adapted<test::TemplatedTestAdapter, type::i16_t>; |
| 177 | + auto result = encodeAndParseValue<Protocol, AdaptedTag>( |
| 178 | + test::TemplatedTestAdapter::fromThrift(1), TType::T_I16); |
| 179 | + EXPECT_EQ(result, asValueStruct<type::i16_t>(1)); |
| 180 | + } |
| 181 | + { |
| 182 | + // test op::encode with adapted container |
| 183 | + auto value = std::map<std::string, int32_t>{{"foo", 1}, {"bar", 2}}; |
| 184 | + using Tag = type::map<type::string_t, type::i32_t>; |
| 185 | + using AdaptedTag = type::adapted<test::TemplatedTestAdapter, Tag>; |
| 186 | + auto result = encodeAndParseValue<Protocol, AdaptedTag>( |
| 187 | + test::TemplatedTestAdapter::fromThrift(value), TType::T_MAP, false); |
| 188 | + EXPECT_EQ(result, asValueStruct<Tag>(value)); |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +TEST(EncodeTest, EncodeBasicTypes) { |
| 193 | + testEncodeBasicTypes<conformance::StandardProtocol::Binary>(); |
| 194 | + testEncodeBasicTypes<conformance::StandardProtocol::Compact>(); |
| 195 | +} |
| 196 | + |
| 197 | +TEST(EncodeTest, EncodeContainers) { |
| 198 | + testEncodeContainers<conformance::StandardProtocol::Binary>(); |
| 199 | + testEncodeContainers<conformance::StandardProtocol::Compact>(); |
| 200 | +} |
| 201 | + |
| 202 | +TEST(EncodeTest, EncodeStruct) { |
| 203 | + testEncodeStruct<conformance::StandardProtocol::Binary>(); |
| 204 | + testEncodeStruct<conformance::StandardProtocol::Compact>(); |
| 205 | +} |
| 206 | + |
| 207 | +TEST(EncodeTest, EncodeCppType) { |
| 208 | + testEncodeCppType<conformance::StandardProtocol::Binary>(); |
| 209 | + testEncodeCppType<conformance::StandardProtocol::Compact>(); |
| 210 | +} |
| 211 | + |
| 212 | +TEST(EncodeTest, EncodeAdapted) { |
| 213 | + testEncodeAdapted<conformance::StandardProtocol::Binary>(); |
| 214 | + testEncodeAdapted<conformance::StandardProtocol::Compact>(); |
| 215 | +} |
55 | 216 | } // namespace
|
56 | 217 | } // namespace apache::thrift::op
|
0 commit comments