userver: /data/code/userver/libraries/protobuf/tests/json/uint64_from_json_test.cpp Source File
Loading...
Searching...
No Matches
uint64_from_json_test.cpp
1#include <gtest/gtest.h>
2
3#include <limits>
4#include <ostream>
5#include <string>
6
7#include <fmt/format.h>
8
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
18constexpr std::uint64_t kMax = std::numeric_limits<std::uint64_t>::max(); // 18446744073709551615
19
20struct UInt64FromJsonSuccessTestParam {
21 std::string input = {};
22 UInt64MessageData expected_message = {};
23 ReadOptions options = {};
24
25 // Older protobuf libraries does not support exponential notation for the quoted integers.
26 // This was fixed in the "v30.0-rc1" / "v6.30.0-rc1" (GOOGLE_PROTOBUF_VERSION = 6030000).
27 bool skip_native_parsing_for_older_versions = false;
28};
29
30struct UInt64FromJsonFailureTestParam {
31 std::string input = {};
32 ReadErrorCode expected_errc = {};
33 std::string expected_path = {};
34 ReadOptions options = {};
35};
36
37void PrintTo(const UInt64FromJsonSuccessTestParam& param, std::ostream* os) {
38 *os << fmt::format("{{ input = '{}' }}", param.input);
39}
40
41void PrintTo(const UInt64FromJsonFailureTestParam& param, std::ostream* os) {
42 *os << fmt::format("{{ input = '{}' }}", param.input);
43}
44
45class UInt64FromJsonSuccessTest : public ::testing::TestWithParam<UInt64FromJsonSuccessTestParam> {};
46class UInt64FromJsonFailureTest : public ::testing::TestWithParam<UInt64FromJsonFailureTestParam> {};
47
48INSTANTIATE_TEST_SUITE_P(
49 ,
50 UInt64FromJsonSuccessTest,
51 ::testing::Values(
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}
59 },
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},
66 {},
67 true
68 }
69 )
70);
71
72INSTANTIATE_TEST_SUITE_P(
73 ,
74 UInt64FromJsonFailureTest,
75 ::testing::Values(
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,
82 "field1"
83 },
84 UInt64FromJsonFailureTestParam{R"({"field1":1,"field2":-1})", ReadErrorCode::kInvalidValue, "field2"},
85 UInt64FromJsonFailureTestParam{
86 R"({"field1":"18446744073709551616","field2":1})",
87 ReadErrorCode::kInvalidValue,
88 "field1"
89 },
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"}
99 )
100);
101
102TEST_P(UInt64FromJsonSuccessTest, Test) {
103 const auto& param = GetParam();
104
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);
108
109 message.set_field1(100001);
110 message.set_field2(200002);
111
112 UASSERT_NO_THROW((message = JsonToMessage<proto_json::messages::UInt64Message>(input, param.options)));
113
114#if GOOGLE_PROTOBUF_VERSION >= 6030000
115 UASSERT_NO_THROW(InitSampleMessage(param.input, param.options, sample_message));
116 CheckMessageEqual(message, sample_message);
117#else
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);
121 }
122#endif
123
124 CheckMessageEqual(message, expected_message);
125}
126
127TEST_P(UInt64FromJsonFailureTest, Test) {
128 const auto& param = GetParam();
129
130 proto_json::messages::UInt64Message sample_message;
131 formats::json::Value input = PrepareJsonTestData(param.input);
132
133 EXPECT_READ_ERROR(
134 (void)JsonToMessage<proto_json::messages::UInt64Message>(input, param.options),
135 param.expected_errc,
136 param.expected_path
137 );
138 UEXPECT_THROW(InitSampleMessage(param.input, param.options, sample_message), SampleError);
139}
140
141} // namespace protobuf::json::tests
142
143USERVER_NAMESPACE_END