userver: /data/code/userver/libraries/protobuf/tests/json/custom_name_test.cpp Source File
Loading...
Searching...
No Matches
custom_name_test.cpp
1#include <gtest/gtest.h>
2
3#include <ostream>
4#include <string>
5
6#include <fmt/format.h>
7
8#include <userver/formats/json/serialize.hpp>
9#include <userver/protobuf/json/convert.hpp>
10#include <userver/utest/assert_macros.hpp>
11
12#include "utils.hpp"
13
14USERVER_NAMESPACE_BEGIN
15
16namespace protobuf::json::tests {
17
18struct CustomNameToJsonTestParam {
19 WriteOptions options = {};
20 std::string expected_json = {};
21};
22
23struct CustomNameFromJsonTestParam {
24 std::string input = {};
25};
26
27void PrintTo(const CustomNameToJsonTestParam& param, std::ostream* os) {
28 *os << fmt::format("{{ options = {} }}", param.options);
29}
30
31void PrintTo(const CustomNameFromJsonTestParam& param, std::ostream* os) {
32 *os << fmt::format("{{ input = '{}' }}", param.input);
33}
34
35class CustomNameToJsonTest : public ::testing::TestWithParam<CustomNameToJsonTestParam> {};
36class CustomNameFromJsonTest : public ::testing::TestWithParam<CustomNameFromJsonTestParam> {};
37
38INSTANTIATE_TEST_SUITE_P(
39 ,
40 CustomNameToJsonTest,
41 ::testing::Values(
42 CustomNameToJsonTestParam{
43 {.preserve_proto_field_names = false},
44 R"({"___":1001,"_Custom_namE_":1.5,"oneMoreField": "hello","last_field": true})"
45 },
46 CustomNameToJsonTestParam{
47 {.preserve_proto_field_names = true},
48 R"({"field":1001,"another_field":1.5,"one_more_field": "hello","last_field": true})"
49 }
50 )
51);
52
53INSTANTIATE_TEST_SUITE_P(
54 ,
55 CustomNameFromJsonTest,
56 ::testing::Values(
57 CustomNameFromJsonTestParam{R"({"___":1001,"_Custom_namE_":1.5,"oneMoreField":"hello","last_field":true})"},
58 CustomNameFromJsonTestParam{R"({"field":1001,"another_field":1.5,"one_more_field":"hello","last_field":true})"},
59 CustomNameFromJsonTestParam{R"({"field":1001,"_Custom_namE_":1.5,"oneMoreField":"hello","last_field":true})"}
60 )
61);
62
63TEST_P(CustomNameToJsonTest, Test) {
64 using Message = proto_json::messages::CustomNameMessage;
65 const auto& param = GetParam();
66
67 Message input;
68 input.set_field(1001);
69 input.set_another_field(1.5);
70 input.set_one_more_field("hello");
71 input.set_last_field(true);
72
73 formats::json::Value json, expected_json, sample_json;
74 expected_json = formats::json::FromString(param.expected_json);
75
76 UASSERT_NO_THROW((json = MessageToJson(input, param.options)));
77 UASSERT_NO_THROW((sample_json = CreateSampleJson(input, param.options)));
78
79 EXPECT_EQ(json, expected_json);
80 EXPECT_EQ(expected_json, sample_json);
81}
82
83TEST_P(CustomNameFromJsonTest, Test) {
84 using Message = proto_json::messages::CustomNameMessage;
85 const auto& param = GetParam();
86
87 Message message, sample;
88 formats::json::Value input = formats::json::FromString(param.input);
89
90 UASSERT_NO_THROW((message = JsonToMessage<Message>(input)));
91 UASSERT_NO_THROW(InitSampleMessage(param.input, {}, sample));
92
93 EXPECT_EQ(message.field(), sample.field());
94 EXPECT_EQ(message.another_field(), sample.another_field());
95 EXPECT_EQ(message.one_more_field(), sample.one_more_field());
96 EXPECT_EQ(message.last_field(), sample.last_field());
97
98 EXPECT_EQ(message.field(), 1001);
99 EXPECT_EQ(message.another_field(), 1.5);
100 EXPECT_EQ(message.one_more_field(), "hello");
101 EXPECT_EQ(message.last_field(), true);
102}
103
104} // namespace protobuf::json::tests
105
106USERVER_NAMESPACE_END