1#include <gtest/gtest.h>
7#include <google/protobuf/struct.pb.h>
9#include <userver/formats/json/serialize.hpp>
10#include <userver/formats/json/value.hpp>
11#include <userver/protobuf/json/convert.hpp>
12#include <userver/utest/parameter_names.hpp>
14USERVER_NAMESPACE_BEGIN
16namespace protobuf::json::tests {
19constexpr std::string_view kEmpty{};
20constexpr std::string_view kInt = R"(12)";
21constexpr std::string_view kDouble = R"(12.123)";
22constexpr std::string_view kBool = R"(false)";
23constexpr std::string_view kString = R"("blah-blah-blah")";
24constexpr std::string_view kSimpleObject = R"(
25{"field1":123,"field2":1.23,"field3":"abacabad"}
26)";
27constexpr std::string_view kNestedObject = R"(
28{"field1":{"field1":345,"field2":3.456},"field2":{},"field3":"abacaba"}
29)";
30constexpr std::string_view kObjectWithArray = R"(
31{"field1":[1,2,3,true,"FaLsE"]}
32)";
33constexpr std::string_view kObjectWithNestedArray = R"(
34{"field1":[["abacaba",4,5,6],2,3,true,"FaLsE"]}
35)";
36constexpr std::string_view kObjectWithObjectWithArray = R"(
37{"field1":{"field1":[1,2,3,"false"]},"field2":"abaCaba"}
38)";
39constexpr std::string_view kEmptyArray = R"([])";
40constexpr std::string_view kSimpleArray = R"(
41[true, false, "blah", 1243, 123.321]
42)";
43constexpr std::string_view kObjectsArray = R"(
44[{"field1":123,"field2":1.23,"field3":"abacaba"},
45{"field1":489,"field2":4.23,"field3":"abacaba2"}]
46)";
47constexpr std::string_view kNestedArray = R"(
48[[1,2,3,false,true],[],[3,4,5,"aba","caba"],[]]
49)";
50constexpr std::string_view kNestedArrayOfObjects = R"(
51[[],[1,{"aba":123,"caba":false},false],
52[{"aba":123,"caba":[1,1.1,true],"daba":{"aba":"caba"}}],[]]
53)";
54constexpr std::string_view kNestedNestedArray = R"(
55[[[1,false,true],[1,false,true]],[],[[1,false,true],[1,false,true],
56{"field1":123,"field2":false,"field3":true}],
57[{"field1":[1,false,true,25.56],"field2":false,"field3":true},
58[1,false,true],{"field1":123,"field2":false,"field3":[1,false,true,25.56]}]]
59)";
60constexpr std::string_view kRandom1 = R"(
61[430432054.89273405,false,-1729588561.6063952,
62[{"correct":1921625716,"hurt":[true,[false,[344852348.8847656,[false,
63[true,"suggest",{"bottom":{"anyone":false,"clay":false,
64"gun":"orbit","hardly":1728677734,"public":918187319.5833945,"tea":"scared"},
65"construction":1412974965,"front":true,"prize":-1913771482,
66"public":1017557609.0607877,"whether":"road"},false,true,true],
671447611265.204443,false,"now","solve"],true,false,"famous",1498336369],
68true,1573964445.510466,-1737527993,47745880.630069256],
69-602633450.4563785,true,1796198824.705258,"give"],"own":"present",
70"smoke":true,"this":false,"wheat":"anyone"},"tightly",
711769816023.5432687,2140279846,"charge",-152924466],"union","does"]
72)";
73constexpr std::string_view kRandom2 = R"(
74{"border":1720446263,"boy":-1350556571,"exclaimed":true,
75"leave":[{"dear":{"almost":true,"everybody":true,
76"grown":false,"hall":false,"middle":"poor","shout":
77{"among":["to",{"against":1740687013.9191742,"at":"at",
78"clothes":[[[1538950668,true,"dust","grade",true,true],
79-2035008309.8318982,false,"floor",{"cap":"himself",
80"early":"owner","own":"offer","oxygen":false,"pride":
81-1966584589.4698162,"rise":150708630},false],-1691993923.457893,
82"habit",-1925581036,true,true],"hold":-169594077.32530355,
83"round":1110484028,"war":true},-237807713,"observe",-775512415,-780439803.8418674],
84"battle":737377501,"both":"frog","powerful":"warn","skin":true,
85"slight":-39832290.47358084}},"know":false,"recent":-1920960342.2496037,
86"sharp":"form","softly":false,"write":false},10247500.801391125,
87-134568968,true,"chief","pressure"],"my":462154457,"nest":106930535.56662798}
88)";
89constexpr std::string_view kRandom3 = R"(
90[{"cannot":"bring","chamber":[[{"camp":"born","creature":-1902377750.185803,
91"drive":"play","grass":false,"research":{"cell":false,"colony":
921412158193.952931,"mill":[64558505,true,354303063.59697485,["bright",
93{"break":[false,true,"point",true,-1282596705.9991045,-780715057],
94"draw":"handle","flies":"blank","path":false,"plant":false,"though":"earn"},
95"silver",1755711518,false,false],1907759588.761578,"sale"],"other":1191178829,
96"whatever":2110094181,"zero":2029006959},"stairs":true},-1427737613.0292025,
97true,-518593868,-2000451860.4843307,true],"ear",2134274955.953373,
98false,false,true],"necessary":"tropical","negative":"spread",
99"sharp":false,"tall":false},"valuable",false,302765324.49991226,false,"deep"]
100)";
101constexpr std::string_view kRandom4 = R"(
102[{"appearance":1821127394.9021797,"avoid":-2136622017,"cap":false,
103"double":false,"laid":false,"mostly":"length","poem":true,
104"rhyme":-1655541012.3937745,"town":false,"yourself":"evening"},
105{"beneath":"brief","its":583470167.3982387,"judge":"practical",
106"poor":false,"pull":[{"ants":-745957571.1039724,"die":-1652209275.7126856,
107"equal":"smallest","final":-1995981894.8087854,"general":false,
108"had":"rich","jar":-1197244504.3537493,"organization":-943767711.1343532,
109"village":true,"whatever":{"advice":"theory","alone":true,"atom":"wait",
110"chart":-389409297,"driven":true,"dust":"knowledge","exercise":"nodded",
111"frozen":{"activity":488296329,"development":-1064678971,"dozen":1016131007,
112"fifteen":false,"flight":false,"knew":[-1641945149.919229,true,"repeat"
113,false,1768450891,"southern",{"after":false,"aloud":1411891803,
114"announced":true,"border":"would","forgot":[1850399147.8201718,
115true,{"breath":"soap","buffalo":"origin","drove":"slept","faster":"steady",
116"lips":"large","nature":"appropriate","organized":-828589361,
117"shallow":269577649,"touch":-1255965389,"usually":-1830294075.2045627},
118false,-1053184311,"fly",false,-1713275431.5087686,
119-488395463.6128659,true],"fourth":-1901113943.8041863,"island":
120"figure","principle":"daughter","raw":false,"tide":"level"},
121803569739.5550513,"main",1702317161],"mix":false,"operation":true,
122"part":"disappear","run":"cabin"},"home":"yellow","upward":"farther"}},
123402157061.3341732,"package",1377860589.8480487,-1026442458.8432412,"vote",
124"connected","metal","community","easy"],"riding":"wish",
125"sets":true,"talk":"flies","tomorrow":-983073546,"warm":false},
126318362813,1559189536,true,1048458332.8802476,
127false,true,"line",-1812281629.0818615]
128)";
131 explicit Param(std::string test_name, std::string_view json)
132 : test_name(test_name)
135 to_cast = formats::json::Value();
137 to_cast = formats::json::FromString(json);
141 std::string test_name;
145std::vector<Param> TestParams() {
147 Param(
"empty", kEmpty),
149 Param(
"double", kDouble),
150 Param(
"bool", kBool),
151 Param(
"string", kString),
152 Param(
"simple_object", kSimpleObject),
153 Param(
"nested_object", kNestedObject),
154 Param(
"object_with_array", kObjectWithArray),
155 Param(
"object_with_nested_array", kObjectWithNestedArray),
156 Param(
"object_with_object_with_array", kObjectWithObjectWithArray),
157 Param(
"empty_array", kEmptyArray),
158 Param(
"simple_array", kSimpleArray),
159 Param(
"objects_array", kObjectsArray),
160 Param(
"nested_array", kNestedArray),
161 Param(
"nested_array_of_objects", kNestedArrayOfObjects),
162 Param(
"nested_nested_array", kNestedNestedArray),
163 Param(
"random1", kRandom1),
164 Param(
"random2", kRandom2),
165 Param(
"random3", kRandom3),
166 Param(
"random4", kRandom4),
172class ValueRoundTripTest :
public testing::TestWithParam<Param> {};
174TEST_P(ValueRoundTripTest, Test) {
175 const auto& param = GetParam();
177 auto proto_struct = formats::
parse::Parse(param.to_cast, formats::parse::To<google::protobuf::Value>{});
178 auto result = formats::
serialize::Serialize(proto_struct, formats::serialize::To<formats::json::Value>{});
179 EXPECT_EQ(param.to_cast, result);
182INSTANTIATE_TEST_SUITE_P(
185 testing::ValuesIn(TestParams()),
186 utest::PrintTestName()