userver: /data/code/userver/libraries/protobuf/tests/json/uint32_from_json_test.cpp Source File
Loading...
Searching...
No Matches
uint32_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::uint32_t kMax = std::numeric_limits<std::uint32_t>::max(); // 4294967295
19
20struct UInt32FromJsonSuccessTestParam {
21 std::string input = {};
22 UInt32MessageData expected_message = {};
23 ParseOptions 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 UInt32FromJsonFailureTestParam {
31 std::string input = {};
32 ParseErrorCode expected_errc = {};
33 std::string expected_path = {};
34 ParseOptions options = {};
35};
36
37void PrintTo(const UInt32FromJsonSuccessTestParam& param, std::ostream* os) {
38 *os << fmt::format("{{ input = '{}' }}", param.input);
39}
40
41void PrintTo(const UInt32FromJsonFailureTestParam& param, std::ostream* os) {
42 *os << fmt::format("{{ input = '{}' }}", param.input);
43}
44
45class UInt32FromJsonSuccessTest : public ::testing::TestWithParam<UInt32FromJsonSuccessTestParam> {};
46class UInt32FromJsonFailureTest : public ::testing::TestWithParam<UInt32FromJsonFailureTestParam> {};
47
48INSTANTIATE_TEST_SUITE_P(
49 ,
50 UInt32FromJsonSuccessTest,
51 ::testing::Values(
52 UInt32FromJsonSuccessTestParam{R"({})", UInt32MessageData{0, 0}},
53 UInt32FromJsonSuccessTestParam{R"({"field1":null,"field2":null})", UInt32MessageData{0, 0}},
54 UInt32FromJsonSuccessTestParam{R"({"field1":0,"field2":1})", UInt32MessageData{0, 1}},
55 UInt32FromJsonSuccessTestParam{R"({"field1":"0","field2":"1"})", UInt32MessageData{0, 1}},
56 UInt32FromJsonSuccessTestParam{R"({"field1":4294967295,"field2":"4294967295"})", UInt32MessageData{kMax, kMax}},
57 UInt32FromJsonSuccessTestParam{R"({"field1":10e2,"field2":3e0})", UInt32MessageData{1000, 3}},
58 UInt32FromJsonSuccessTestParam{R"({"field1":"10e2","field2":"3e0"})", UInt32MessageData{1000, 3}, {}, true},
59 UInt32FromJsonSuccessTestParam{R"({"field1":8500E-2,"field2":1.8E+2})", UInt32MessageData{85, 180}},
60 UInt32FromJsonSuccessTestParam{
61 R"({"field1":"8500E-2","field2":"1.8E+2"})",
62 UInt32MessageData{85, 180},
63 {},
64 true
65 }
66 )
67);
68
69INSTANTIATE_TEST_SUITE_P(
70 ,
71 UInt32FromJsonFailureTest,
72 ::testing::Values(
73 UInt32FromJsonFailureTestParam{R"({"field1":[],"field2":1})", ParseErrorCode::kInvalidType, "field1"},
74 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":{}})", ParseErrorCode::kInvalidType, "field2"},
75 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":true})", ParseErrorCode::kInvalidType, "field2"},
76 UInt32FromJsonFailureTestParam{R"({"field1":4294967296,"field2":1})", ParseErrorCode::kInvalidValue, "field1"},
77 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":-1})", ParseErrorCode::kInvalidValue, "field2"},
78 UInt32FromJsonFailureTestParam{
79 R"({"field1":"4294967296","field2":1})",
80 ParseErrorCode::kInvalidValue,
81 "field1"
82 },
83 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":"-1"})", ParseErrorCode::kInvalidValue, "field2"},
84 UInt32FromJsonFailureTestParam{R"({"field1":2e12,"field2":1})", ParseErrorCode::kInvalidValue, "field1"},
85 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":-2e12})", ParseErrorCode::kInvalidValue, "field2"},
86 UInt32FromJsonFailureTestParam{R"({"field1":" 123","field2":1})", ParseErrorCode::kInvalidValue, "field1"},
87 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":"123 "})", ParseErrorCode::kInvalidValue, "field2"},
88 UInt32FromJsonFailureTestParam{R"({"field1":"1a3","field2":1})", ParseErrorCode::kInvalidValue, "field1"},
89 UInt32FromJsonFailureTestParam{R"({"field1":"1.1","field2":1})", ParseErrorCode::kInvalidValue, "field1"},
90 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":"2e12"})", ParseErrorCode::kInvalidValue, "field2"},
91 UInt32FromJsonFailureTestParam{R"({"field1":"-2e12","field2":1})", ParseErrorCode::kInvalidValue, "field1"},
92 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":"-1e50"})", ParseErrorCode::kInvalidValue, "field2"},
93 UInt32FromJsonFailureTestParam{R"({"field1":"1e50","field2":1})", ParseErrorCode::kInvalidValue, "field1"}
94 )
95);
96
97TEST_P(UInt32FromJsonSuccessTest, Test) {
98 const auto& param = GetParam();
99
100 proto_json::messages::UInt32Message message, expected_message, sample_message;
101 formats::json::Value input = PrepareJsonTestData(param.input);
102 expected_message = PrepareTestData(param.expected_message);
103
104 message.set_field1(100001);
105 message.set_field2(200002);
106
107 UASSERT_NO_THROW((message = JsonToMessage<proto_json::messages::UInt32Message>(input, param.options)));
108
109#if GOOGLE_PROTOBUF_VERSION >= 6030000
110 UASSERT_NO_THROW(InitSampleMessage(param.input, sample_message, param.options));
111 CheckMessageEqual(message, sample_message);
112#else
113 if (!param.skip_native_parsing_for_older_versions) {
114 UASSERT_NO_THROW(InitSampleMessage(param.input, sample_message, param.options));
115 CheckMessageEqual(message, sample_message);
116 }
117#endif
118
119 CheckMessageEqual(message, expected_message);
120}
121
122TEST_P(UInt32FromJsonFailureTest, Test) {
123 const auto& param = GetParam();
124
125 proto_json::messages::UInt32Message sample_message;
126 formats::json::Value input = PrepareJsonTestData(param.input);
127
129 (void)JsonToMessage<proto_json::messages::UInt32Message>(input, param.options),
130 param.expected_errc,
131 param.expected_path
132 );
133 UEXPECT_THROW(InitSampleMessage(param.input, sample_message, param.options), SampleError);
134}
135
136} // namespace protobuf::json::tests
137
138USERVER_NAMESPACE_END