userver: /data/code/userver/libraries/protobuf/tests/json/bool_from_json_test.cpp Source File
Loading...
Searching...
No Matches
bool_from_json_test.cpp
1#include <gtest/gtest.h>
2
3#include <ostream>
4#include <string>
5
6#include <fmt/format.h>
7
8#include <userver/protobuf/json/convert.hpp>
9#include <userver/utest/assert_macros.hpp>
10
11#include "utils.hpp"
12
13USERVER_NAMESPACE_BEGIN
14
15namespace protobuf::json::tests {
16
17struct BoolFromJsonSuccessTestParam {
18 std::string input = {};
19 BoolMessageData expected_message = {};
20 ReadOptions options = {};
21};
22
23struct BoolFromJsonFailureTestParam {
24 std::string input = {};
25 ReadErrorCode expected_errc = {};
26 std::string expected_path = {};
27 ReadOptions options = {};
28
29 // Protobuf ProtoJSON legacy syntax supports out-of-spec "true"/"false" strings as a value
30 // for bool, which we want to prohibit (because we do not want our clients to use syntax
31 // that may break in the newer protobuf versions). This variable is used disable some checks
32 // that will fail for legacy syntax.
33 bool skip_native_check = false;
34};
35
36void PrintTo(const BoolFromJsonSuccessTestParam& param, std::ostream* os) {
37 *os << fmt::format("{{ input = '{}' }}", param.input);
38}
39
40void PrintTo(const BoolFromJsonFailureTestParam& param, std::ostream* os) {
41 *os << fmt::format("{{ input = '{}' }}", param.input);
42}
43
44class BoolFromJsonSuccessTest : public ::testing::TestWithParam<BoolFromJsonSuccessTestParam> {};
45class BoolFromJsonFailureTest : public ::testing::TestWithParam<BoolFromJsonFailureTestParam> {};
46
47INSTANTIATE_TEST_SUITE_P(
48 ,
49 BoolFromJsonSuccessTest,
50 ::testing::Values(
51 BoolFromJsonSuccessTestParam{R"({})", BoolMessageData{false}},
52 BoolFromJsonSuccessTestParam{R"({"field1":null})", BoolMessageData{false}},
53 BoolFromJsonSuccessTestParam{R"({"field1":false})", BoolMessageData{false}},
54 BoolFromJsonSuccessTestParam{R"({"field1":true})", BoolMessageData{true}}
55 )
56);
57
58INSTANTIATE_TEST_SUITE_P(
59 ,
60 BoolFromJsonFailureTest,
61 ::testing::Values(
62 BoolFromJsonFailureTestParam{R"({"field1":[]})", ReadErrorCode::kInvalidType, "field1"},
63 BoolFromJsonFailureTestParam{R"({"field1":{}})", ReadErrorCode::kInvalidType, "field1"},
64 BoolFromJsonFailureTestParam{R"({"field1":"false"})", ReadErrorCode::kInvalidType, "field1", {}, true},
65 BoolFromJsonFailureTestParam{R"({"field1":"true"})", ReadErrorCode::kInvalidType, "field1", {}, true},
66 BoolFromJsonFailureTestParam{R"({"field1":0})", ReadErrorCode::kInvalidType, "field1"},
67 BoolFromJsonFailureTestParam{R"({"field1":1})", ReadErrorCode::kInvalidType, "field1"}
68 )
69);
70
71TEST_P(BoolFromJsonSuccessTest, Test) {
72 const auto& param = GetParam();
73
74 proto_json::messages::BoolMessage message, expected_message, sample_message;
75 formats::json::Value input = PrepareJsonTestData(param.input);
76 expected_message = PrepareTestData(param.expected_message);
77
78 UASSERT_NO_THROW((message = JsonToMessage<proto_json::messages::BoolMessage>(input, param.options)));
79 UASSERT_NO_THROW(InitSampleMessage(param.input, param.options, sample_message));
80
81 CheckMessageEqual(message, sample_message);
82 CheckMessageEqual(message, expected_message);
83}
84
85TEST_P(BoolFromJsonFailureTest, Test) {
86 const auto& param = GetParam();
87
88 proto_json::messages::BoolMessage sample_message;
89 formats::json::Value input = PrepareJsonTestData(param.input);
90
91 EXPECT_READ_ERROR(
92 (void)JsonToMessage<proto_json::messages::BoolMessage>(input, param.options),
93 param.expected_errc,
94 param.expected_path
95 );
96
97 if (!param.skip_native_check) {
98 UEXPECT_THROW(InitSampleMessage(param.input, param.options, sample_message), SampleError);
99 }
100}
101
102} // namespace protobuf::json::tests
103
104USERVER_NAMESPACE_END