1#include <gtest/gtest.h>
3#include <userver/proto-structs/json.hpp>
4#include <userver/utest/assert_macros.hpp>
6#include "messages.pb.h"
11namespace proto_structs::tests {
13TEST(StructToJson, JsonValue) {
14 auto expected_builder = formats::json::ValueBuilder{formats::common::Type::kObject};
15 expected_builder[
"f1"] =
true;
16 expected_builder[
"f2"] = std::numeric_limits<int32_t>::min();
17 expected_builder[
"f3"] = std::numeric_limits<uint32_t>::max();
19 expected_builder[
"f4"] = std::to_string(std::numeric_limits<int64_t>::min());
20 expected_builder[
"f5"] = std::to_string(std::numeric_limits<uint64_t>::max());
21 expected_builder[
"f6"] =
static_cast<
float>(1.5);
22 expected_builder[
"f7"] = -2.5;
23 expected_builder[
"f8"] =
"test1";
24 expected_builder[
"f9"] =
"dGVzdDI=";
25 expected_builder[
"f10"] =
"TEST_ENUM_VALUE1";
26 expected_builder[
"f11"] = 987;
30 .f2 = std::numeric_limits<int32_t>::min(),
31 .f3 = std::numeric_limits<uint32_t>::max(),
32 .f4 = std::numeric_limits<int64_t>::min(),
33 .f5 = std::numeric_limits<uint64_t>::max(),
34 .f6 =
static_cast<
float>(1.5),
38 .f10 = structs::TestEnum::kValue1,
42 formats::json::Value json;
43 UASSERT_NO_THROW(json = StructToJson(obj, {}));
44 ASSERT_EQ(json, expected_builder.ExtractValue());
47TEST(StructToJson, JsonString) {
48 auto expected_builder = formats::json::ValueBuilder{formats::common::Type::kObject};
49 expected_builder[
"f1"] =
true;
50 expected_builder[
"f2"] = std::numeric_limits<int32_t>::min();
51 expected_builder[
"f3"] = std::numeric_limits<uint32_t>::max();
53 expected_builder[
"f4"] = std::to_string(std::numeric_limits<int64_t>::min());
54 expected_builder[
"f5"] = std::to_string(std::numeric_limits<uint64_t>::max());
55 expected_builder[
"f6"] =
static_cast<
float>(1.5);
56 expected_builder[
"f7"] = -2.5;
57 expected_builder[
"f8"] =
"test1";
58 expected_builder[
"f9"] =
"dGVzdDI=";
59 expected_builder[
"f10"] =
"TEST_ENUM_VALUE1";
60 expected_builder[
"f11"] = 987;
64 .f2 = std::numeric_limits<int32_t>::min(),
65 .f3 = std::numeric_limits<uint32_t>::max(),
66 .f4 = std::numeric_limits<int64_t>::min(),
67 .f5 = std::numeric_limits<uint64_t>::max(),
68 .f6 =
static_cast<
float>(1.5),
72 .f10 = structs::TestEnum::kValue1,
76 std::string json_string;
77 UASSERT_NO_THROW(json_string = StructToJsonString(obj, {}));
78 ASSERT_EQ(formats::json::FromString(json_string), expected_builder.ExtractValue());