1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
4#include <userver/proto-structs/convert.hpp>
5#include <userver/utest/assert_macros.hpp>
7#include "messages.pb.h"
10USERVER_NAMESPACE_BEGIN
12namespace proto_structs::tests {
14TEST(BinaryToStruct, Scalar) {
18 msg.set_f2(std::numeric_limits<int32_t>::min());
19 msg.set_f3(std::numeric_limits<uint32_t>::max());
20 msg.set_f4(std::numeric_limits<int64_t>::min());
21 msg.set_f5(std::numeric_limits<uint64_t>::max());
22 msg.set_f6(
static_cast<
float>(1.5));
26 msg.set_f10(messages::TestEnum::TEST_ENUM_VALUE1);
29 protobuf::ProtoStringType serialized_from_msg_tstring;
30 ASSERT_TRUE(msg.SerializeToString(&serialized_from_msg_tstring));
31 std::string_view serialized_from_msg = serialized_from_msg_tstring;
35 UASSERT_NO_THROW(BinaryToStruct(serialized_from_msg, obj));
36 CheckScalarEqual(obj, msg);
40 auto obj = BinaryToStruct<structs::
Scalar>(serialized_from_msg);
41 CheckScalarEqual(obj, msg);
46TEST(BinaryToStruct, ThrowsOnInvalidBinary) {
50 std::string binary_data;
52 binary_data +=
'\x42';
54 binary_data +=
'\x64';
55 binary_data +=
"hello";
57 UEXPECT_THROW(BinaryToStruct<structs::Scalar>(binary_data, obj), ReadError);
58 UEXPECT_THROW((
void)BinaryToStruct<structs::Scalar>(binary_data), ReadError);