userver: /data/code/userver/libraries/protobuf/tests/json/bytes_to_json_test.cpp Source File
Loading...
Searching...
No Matches
bytes_to_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#include <userver/utils/encoding/hex.hpp>
11
12#include "utils.hpp"
13
14USERVER_NAMESPACE_BEGIN
15
16namespace protobuf::json::tests {
17
18struct BytesToJsonSuccessTestParam {
19 BytesMessageData input = {};
20 std::string expected_json = {};
21 WriteOptions options = {};
22};
23
24void PrintTo(const BytesToJsonSuccessTestParam& param, std::ostream* os) {
25 *os << fmt::format("{{ input = {{.field1='{}'}} }}", utils::encoding::ToHex(param.input.field1));
26}
27
28class BytesToJsonSuccessTest : public ::testing::TestWithParam<BytesToJsonSuccessTestParam> {};
29
30INSTANTIATE_TEST_SUITE_P(
31 ,
32 BytesToJsonSuccessTest,
33 ::testing::Values(
34 BytesToJsonSuccessTestParam{BytesMessageData{""}, R"({})"},
35 BytesToJsonSuccessTestParam{
36 BytesMessageData{""},
37 R"({"field1":""})",
38 WriteOptions{.always_print_fields_with_no_presence = true}
39 },
40 BytesToJsonSuccessTestParam{BytesMessageData{std::string("\xfb\xfb", 2)}, R"({"field1":"+/s="})"}
41 )
42);
43
44TEST_P(BytesToJsonSuccessTest, Test) {
45 const auto& param = GetParam();
46
47 auto input = PrepareTestData(param.input);
48 formats::json::Value json, expected_json, sample_json;
49
50 UASSERT_NO_THROW((json = MessageToJson(input, param.options)));
51 UASSERT_NO_THROW((expected_json = PrepareJsonTestData(param.expected_json)));
52 UASSERT_NO_THROW((sample_json = CreateSampleJson(input, param.options)));
53
54 EXPECT_EQ(json, expected_json);
55 EXPECT_EQ(expected_json, sample_json);
56}
57
58} // namespace protobuf::json::tests
59
60USERVER_NAMESPACE_END