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 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 UInt32FromJsonFailureTestParam {
31 std::string input = {};
32 ReadErrorCode expected_errc = {};
33 std::string expected_path = {};
34 ReadOptions 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})", ReadErrorCode::kInvalidType, "field1"},
74 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":{}})", ReadErrorCode::kInvalidType, "field2"},
75 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":true})", ReadErrorCode::kInvalidType, "field2"},
76 UInt32FromJsonFailureTestParam{R"({"field1":4294967296,"field2":1})", ReadErrorCode::kInvalidValue, "field1"},
77 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":-1})", ReadErrorCode::kInvalidValue, "field2"},
78 UInt32FromJsonFailureTestParam{R"({"field1":"4294967296","field2":1})", ReadErrorCode::kInvalidValue, "field1"},
79 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":"-1"})", ReadErrorCode::kInvalidValue, "field2"},
80 UInt32FromJsonFailureTestParam{R"({"field1":2e12,"field2":1})", ReadErrorCode::kInvalidValue, "field1"},
81 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":-2e12})", ReadErrorCode::kInvalidValue, "field2"},
82 UInt32FromJsonFailureTestParam{R"({"field1":" 123","field2":1})", ReadErrorCode::kInvalidValue, "field1"},
83 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":"123 "})", ReadErrorCode::kInvalidValue, "field2"},
84 UInt32FromJsonFailureTestParam{R"({"field1":"1a3","field2":1})", ReadErrorCode::kInvalidValue, "field1"},
85 UInt32FromJsonFailureTestParam{R"({"field1":"1.1","field2":1})", ReadErrorCode::kInvalidValue, "field1"},
86 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":"2e12"})", ReadErrorCode::kInvalidValue, "field2"},
87 UInt32FromJsonFailureTestParam{R"({"field1":"-2e12","field2":1})", ReadErrorCode::kInvalidValue, "field1"},
88 UInt32FromJsonFailureTestParam{R"({"field1":1,"field2":"-1e50"})", ReadErrorCode::kInvalidValue, "field2"},
89 UInt32FromJsonFailureTestParam{R"({"field1":"1e50","field2":1})", ReadErrorCode::kInvalidValue, "field1"}
90 )
91);
92
93TEST_P(UInt32FromJsonSuccessTest, Test) {
94 const auto& param = GetParam();
95
96 proto_json::messages::UInt32Message message, expected_message, sample_message;
97 formats::json::Value input = PrepareJsonTestData(param.input);
98 expected_message = PrepareTestData(param.expected_message);
99
100 message.set_field1(100001);
101 message.set_field2(200002);
102
103 UASSERT_NO_THROW((message = JsonToMessage<proto_json::messages::UInt32Message>(input, param.options)));
104
105#if GOOGLE_PROTOBUF_VERSION >= 6030000
106 UASSERT_NO_THROW(InitSampleMessage(param.input, param.options, sample_message));
107 CheckMessageEqual(message, sample_message);
108#else
109 if (!param.skip_native_parsing_for_older_versions) {
110 UASSERT_NO_THROW(InitSampleMessage(param.input, param.options, sample_message));
111 CheckMessageEqual(message, sample_message);
112 }
113#endif
114
115 CheckMessageEqual(message, expected_message);
116}
117
118TEST_P(UInt32FromJsonFailureTest, Test) {
119 const auto& param = GetParam();
120
121 proto_json::messages::UInt32Message sample_message;
122 formats::json::Value input = PrepareJsonTestData(param.input);
123
124 EXPECT_READ_ERROR(
125 (void)JsonToMessage<proto_json::messages::UInt32Message>(input, param.options),
126 param.expected_errc,
127 param.expected_path
128 );
129 UEXPECT_THROW(InitSampleMessage(param.input, param.options, sample_message), SampleError);
130}
131
132} // namespace protobuf::json::tests
133
134USERVER_NAMESPACE_END