userver: /data/code/userver/libraries/protobuf/tests/json/int32_from_json_test.cpp Source File
Loading...
Searching...
No Matches
int32_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::int32_t kMax = std::numeric_limits<std::int32_t>::max(); // 2147483647
19constexpr std::int32_t kMin = std::numeric_limits<std::int32_t>::min(); // -2147483648
20
21struct Int32FromJsonSuccessTestParam {
22 std::string input = {};
23 Int32MessageData expected_message = {};
24 ReadOptions options = {};
25
26 // Older protobuf libraries does not support exponential notation for the quoted integers.
27 // This was fixed in the "v30.0-rc1" / "v6.30.0-rc1" (GOOGLE_PROTOBUF_VERSION = 6030000).
28 bool skip_native_parsing_for_older_versions = false;
29};
30
31struct Int32FromJsonFailureTestParam {
32 std::string input = {};
33 ReadErrorCode expected_errc = {};
34 std::string expected_path = {};
35 ReadOptions options = {};
36};
37
38void PrintTo(const Int32FromJsonSuccessTestParam& param, std::ostream* os) {
39 *os << fmt::format("{{ input = '{}' }}", param.input);
40}
41
42void PrintTo(const Int32FromJsonFailureTestParam& param, std::ostream* os) {
43 *os << fmt::format("{{ input = '{}' }}", param.input);
44}
45
46class Int32FromJsonSuccessTest : public ::testing::TestWithParam<Int32FromJsonSuccessTestParam> {};
47class Int32FromJsonFailureTest : public ::testing::TestWithParam<Int32FromJsonFailureTestParam> {};
48
49INSTANTIATE_TEST_SUITE_P(
50 ,
51 Int32FromJsonSuccessTest,
52 ::testing::Values(
53 Int32FromJsonSuccessTestParam{R"({})", Int32MessageData{0, 0, 0}},
54 Int32FromJsonSuccessTestParam{R"({"field1":0,"field2":null,"field3":"0"})", Int32MessageData{0, 0, 0}},
55 Int32FromJsonSuccessTestParam{R"({"field1":1,"field2":-2,"field3":3})", Int32MessageData{1, -2, 3}},
56 Int32FromJsonSuccessTestParam{R"({"field1":"1","field2":"-2","field3":"3"})", Int32MessageData{1, -2, 3}},
57 Int32FromJsonSuccessTestParam{
58 R"({"field1":2147483647,"field2":"2147483647","field3":-2147483648})",
59 Int32MessageData{kMax, kMax, kMin}
60 },
61 Int32FromJsonSuccessTestParam{
62 R"({"field1":-2147483648,"field2":"-2147483648","field3":2147483647})",
63 Int32MessageData{kMin, kMin, kMax}
64 },
65 Int32FromJsonSuccessTestParam{
66 R"({"field1":10e2,"field2":-5e3,"field3":3e0})",
67 Int32MessageData{1000, -5000, 3}
68 },
69 Int32FromJsonSuccessTestParam{
70 R"({"field1":"10e2","field2":"-5e3","field3":"3e0"})",
71 Int32MessageData{1000, -5000, 3},
72 {},
73 true
74 },
75 Int32FromJsonSuccessTestParam{
76 R"({"field1":8500E-2,"field2":-1.8E+2,"field3":6.0E0})",
77 Int32MessageData{85, -180, 6}
78 },
79 Int32FromJsonSuccessTestParam{
80 R"({"field1":"8500E-2","field2":"-1.8E+2","field3":"6.0E0"})",
81 Int32MessageData{85, -180, 6},
82 {},
83 true
84 }
85 )
86);
87
88INSTANTIATE_TEST_SUITE_P(
89 ,
90 Int32FromJsonFailureTest,
91 ::testing::Values(
92 Int32FromJsonFailureTestParam{R"({"field1":[],"field2":1,"field3":1})", ReadErrorCode::kInvalidType, "field1"},
93 Int32FromJsonFailureTestParam{R"({"field1":1,"field2":{},"field3":1})", ReadErrorCode::kInvalidType, "field2"},
94 Int32FromJsonFailureTestParam{
95 R"({"field1":1,"field2":1,"field3":true})",
96 ReadErrorCode::kInvalidType,
97 "field3"
98 },
99 Int32FromJsonFailureTestParam{
100 R"({"field1":2147483648,"field2":1,"field3":1})",
101 ReadErrorCode::kInvalidValue,
102 "field1"
103 },
104 Int32FromJsonFailureTestParam{
105 R"({"field1":1,"field2":-2147483649,"field3":1})",
106 ReadErrorCode::kInvalidValue,
107 "field2"
108 },
109 Int32FromJsonFailureTestParam{
110 R"({"field1":1,"field2":1,"field3":"2147483648"})",
111 ReadErrorCode::kInvalidValue,
112 "field3"
113 },
114 Int32FromJsonFailureTestParam{
115 R"({"field1":"-2147483649","field2":1,"field3":1})",
116 ReadErrorCode::kInvalidValue,
117 "field1"
118 },
119 Int32FromJsonFailureTestParam{
120 R"({"field1":1,"field2":2e12,"field3":1})",
121 ReadErrorCode::kInvalidValue,
122 "field2"
123 },
124 Int32FromJsonFailureTestParam{
125 R"({"field1":1,"field2":1,"field3":-2e12})",
126 ReadErrorCode::kInvalidValue,
127 "field3"
128 },
129 Int32FromJsonFailureTestParam{
130 R"({"field1":" 123","field2":1,"field3":1})",
131 ReadErrorCode::kInvalidValue,
132 "field1"
133 },
134 Int32FromJsonFailureTestParam{
135 R"({"field1":1,"field2":"-123 ","field3":1})",
136 ReadErrorCode::kInvalidValue,
137 "field2"
138 },
139 Int32FromJsonFailureTestParam{
140 R"({"field1":1,"field2":1,"field3":"1a3"})",
141 ReadErrorCode::kInvalidValue,
142 "field3"
143 },
144 Int32FromJsonFailureTestParam{
145 R"({"field1":"1.1","field2":1,"field3":1})",
146 ReadErrorCode::kInvalidValue,
147 "field1"
148 },
149 Int32FromJsonFailureTestParam{
150 R"({"field1":1,"field2":"2e12","field3":1})",
151 ReadErrorCode::kInvalidValue,
152 "field2"
153 },
154 Int32FromJsonFailureTestParam{
155 R"({"field1":1,"field2":1,"field3":"-2e12"})",
156 ReadErrorCode::kInvalidValue,
157 "field3"
158 },
159 Int32FromJsonFailureTestParam{
160 R"({"field1":"1e50","field2":1,"field3":1})",
161 ReadErrorCode::kInvalidValue,
162 "field1"
163 },
164 Int32FromJsonFailureTestParam{
165 R"({"field1":1,"field2":"-1e50","field3":1})",
166 ReadErrorCode::kInvalidValue,
167 "field2"
168 }
169 )
170);
171
172TEST_P(Int32FromJsonSuccessTest, Test) {
173 const auto& param = GetParam();
174
175 proto_json::messages::Int32Message message, expected_message, sample_message;
176 formats::json::Value input = PrepareJsonTestData(param.input);
177 expected_message = PrepareTestData(param.expected_message);
178
179 message.set_field1(100001);
180 message.set_field2(200002);
181 message.set_field3(300003);
182
183 UASSERT_NO_THROW((message = JsonToMessage<proto_json::messages::Int32Message>(input, param.options)));
184
185#if GOOGLE_PROTOBUF_VERSION >= 6030000
186 UASSERT_NO_THROW(InitSampleMessage(param.input, param.options, sample_message));
187 CheckMessageEqual(message, sample_message);
188#else
189 if (!param.skip_native_parsing_for_older_versions) {
190 UASSERT_NO_THROW(InitSampleMessage(param.input, param.options, sample_message));
191 CheckMessageEqual(message, sample_message);
192 }
193#endif
194
195 CheckMessageEqual(message, expected_message);
196}
197
198TEST_P(Int32FromJsonFailureTest, Test) {
199 const auto& param = GetParam();
200
201 proto_json::messages::Int32Message sample_message;
202 formats::json::Value input = PrepareJsonTestData(param.input);
203
204 EXPECT_READ_ERROR(
205 (void)JsonToMessage<proto_json::messages::Int32Message>(input, param.options),
206 param.expected_errc,
207 param.expected_path
208 );
209 UEXPECT_THROW(InitSampleMessage(param.input, param.options, sample_message), SampleError);
210}
211
212} // namespace protobuf::json::tests
213
214USERVER_NAMESPACE_END