1#include <gtest/gtest.h>
9#include <userver/protobuf/json/convert.hpp>
10#include <userver/utest/assert_macros.hpp>
14USERVER_NAMESPACE_BEGIN
18constexpr std::uint64_t kMax = std::numeric_limits<std::uint64_t>::max();
20struct UInt64FromJsonSuccessTestParam {
21 std::string input = {};
23 ReadOptions options = {};
27 bool skip_native_parsing_for_older_versions =
false;
30struct UInt64FromJsonFailureTestParam {
31 std::string input = {};
32 ReadErrorCode expected_errc = {};
33 std::string expected_path = {};
34 ReadOptions options = {};
37void PrintTo(
const UInt64FromJsonSuccessTestParam& param, std::ostream* os) {
38 *os << fmt::format(
"{{ input = '{}' }}", param.input);
41void PrintTo(
const UInt64FromJsonFailureTestParam& param, std::ostream* os) {
42 *os << fmt::format(
"{{ input = '{}' }}", param.input);
45class UInt64FromJsonSuccessTest :
public ::testing::TestWithParam<UInt64FromJsonSuccessTestParam> {};
46class UInt64FromJsonFailureTest :
public ::testing::TestWithParam<UInt64FromJsonFailureTestParam> {};
48INSTANTIATE_TEST_SUITE_P(
50 UInt64FromJsonSuccessTest,
52 UInt64FromJsonSuccessTestParam{R"({})", UInt64MessageData{0, 0}},
53 UInt64FromJsonSuccessTestParam{R"({"field1":null,"field2":null})", UInt64MessageData{0, 0}},
54 UInt64FromJsonSuccessTestParam{R"({"field1":0,"field2":1})", UInt64MessageData{0, 1}},
55 UInt64FromJsonSuccessTestParam{R"({"field1":"0","field2":"1"})", UInt64MessageData{0, 1}},
56 UInt64FromJsonSuccessTestParam{
57 R"({"field1":18446744073709551615,"field2":"18446744073709551615"})",
58 UInt64MessageData{kMax, kMax}
60 UInt64FromJsonSuccessTestParam{R"({"field1":10e2,"field2":3e0})", UInt64MessageData{1000, 3}},
61 UInt64FromJsonSuccessTestParam{R"({"field1":"10e2","field2":"3e0"})", UInt64MessageData{1000, 3}, {},
true},
62 UInt64FromJsonSuccessTestParam{R"({"field1":8500E-2,"field2":1.8E+2})", UInt64MessageData{85, 180}},
63 UInt64FromJsonSuccessTestParam{
64 R"({"field1":"8500E-2","field2":"1.8E+2"})",
65 UInt64MessageData{85, 180},
72INSTANTIATE_TEST_SUITE_P(
74 UInt64FromJsonFailureTest,
76 UInt64FromJsonFailureTestParam{R"({"field1":[],"field2":1})", ReadErrorCode::kInvalidType,
"field1"},
77 UInt64FromJsonFailureTestParam{R"({"field1":1,"field2":{}})", ReadErrorCode::kInvalidType,
"field2"},
78 UInt64FromJsonFailureTestParam{R"({"field1":1,"field2":true})", ReadErrorCode::kInvalidType,
"field2"},
79 UInt64FromJsonFailureTestParam{
80 R"({"field1":18446744073709551616,"field2":1})",
81 ReadErrorCode::kInvalidValue,
84 UInt64FromJsonFailureTestParam{R"({"field1":1,"field2":-1})", ReadErrorCode::kInvalidValue,
"field2"},
85 UInt64FromJsonFailureTestParam{
86 R"({"field1":"18446744073709551616","field2":1})",
87 ReadErrorCode::kInvalidValue,
90 UInt64FromJsonFailureTestParam{R"({"field1":1,"field2":"-1"})", ReadErrorCode::kInvalidValue,
"field2"},
91 UInt64FromJsonFailureTestParam{R"({"field1":1e50,"field2":1})", ReadErrorCode::kInvalidValue,
"field1"},
92 UInt64FromJsonFailureTestParam{R"({"field1":1,"field2":-1e50})", ReadErrorCode::kInvalidValue,
"field2"},
93 UInt64FromJsonFailureTestParam{R"({"field1":" 123","field2":1})", ReadErrorCode::kInvalidValue,
"field1"},
94 UInt64FromJsonFailureTestParam{R"({"field1":1,"field2":"123 "})", ReadErrorCode::kInvalidValue,
"field2"},
95 UInt64FromJsonFailureTestParam{R"({"field1":"1a3","field2":1})", ReadErrorCode::kInvalidValue,
"field1"},
96 UInt64FromJsonFailureTestParam{R"({"field1":"1.1","field2":1})", ReadErrorCode::kInvalidValue,
"field1"},
97 UInt64FromJsonFailureTestParam{R"({"field1":1,"field2":"-1e50"})", ReadErrorCode::kInvalidValue,
"field2"},
98 UInt64FromJsonFailureTestParam{R"({"field1":"1e50","field2":1})", ReadErrorCode::kInvalidValue,
"field1"}
102TEST_P(UInt64FromJsonSuccessTest, Test) {
103 const auto& param = GetParam();
105 proto_json::messages::UInt64Message message, expected_message, sample_message;
106 formats::json::Value input = PrepareJsonTestData(param.input);
107 expected_message = PrepareTestData(param.expected_message);
109 message.set_field1(100001);
110 message.set_field2(200002);
112 UASSERT_NO_THROW((message = JsonToMessage<proto_json::messages::UInt64Message>(input, param.options)));
114#if GOOGLE_PROTOBUF_VERSION >= 6030000
115 UASSERT_NO_THROW(InitSampleMessage(param.input, param.options, sample_message));
116 CheckMessageEqual(message, sample_message);
118 if (!param.skip_native_parsing_for_older_versions) {
119 UASSERT_NO_THROW(InitSampleMessage(param.input, param.options, sample_message));
120 CheckMessageEqual(message, sample_message);
124 CheckMessageEqual(message, expected_message);
127TEST_P(UInt64FromJsonFailureTest, Test) {
128 const auto& param = GetParam();
130 proto_json::messages::UInt64Message sample_message;
131 formats::json::Value input = PrepareJsonTestData(param.input);
134 (
void)JsonToMessage<proto_json::messages::UInt64Message>(input, param.options),
138 UEXPECT_THROW(InitSampleMessage(param.input, param.options, sample_message), SampleError);