Skip to content

Commit 98cbcd5

Browse files
committed
Add tests for universal serialization/deserialization Json
1 parent 3734a3a commit 98cbcd5

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#include <gtest/gtest.h>
2+
#include <userver/formats/universal/common_checks.hpp>
3+
#include <userver/formats/json.hpp>
4+
USERVER_NAMESPACE_BEGIN
5+
6+
struct SomeStruct {
7+
int field1;
8+
int field2;
9+
constexpr auto operator==(const SomeStruct& other) const noexcept {
10+
return (this->field1 == other.field1) && (this->field2 == other.field2);
11+
};
12+
};
13+
14+
template <>
15+
inline constexpr auto userver::formats::universal::kSerialization<SomeStruct> =
16+
SerializationConfig<SomeStruct>::Create();
17+
18+
TEST(Serialize, Basic) {
19+
SomeStruct a{10, 100};
20+
const auto json = userver::formats::json::ValueBuilder(a).ExtractValue();
21+
EXPECT_EQ(userver::formats::json::ToString(json), "{\"field1\":10,\"field2\":100}");
22+
};
23+
24+
TEST(Parse, Basic) {
25+
const auto json = userver::formats::json::FromString("{\"field1\":10,\"field2\":100}");
26+
const auto fromJson = json.As<SomeStruct>();
27+
constexpr SomeStruct valid{10, 100};
28+
EXPECT_EQ(fromJson, valid);
29+
};
30+
31+
TEST(TryParse, Basic) {
32+
const auto json = userver::formats::json::FromString("{\"field1\":10,\"field2\":100}");
33+
const auto json2 = userver::formats::json::FromString("{\"field1\":10,\"field3\":100}");
34+
const auto json3 = userver::formats::json::FromString("{\"field1\":10,\"field2\":\"100\"}");
35+
EXPECT_EQ((bool)userver::formats::parse::TryParse(json, userver::formats::parse::To<SomeStruct>{}), true);
36+
EXPECT_EQ((bool)userver::formats::parse::TryParse(json2, userver::formats::parse::To<SomeStruct>{}), false);
37+
EXPECT_EQ((bool)userver::formats::parse::TryParse(json3, userver::formats::parse::To<SomeStruct>{}), false);
38+
};
39+
40+
41+
struct SomeStruct2 {
42+
std::optional<int> field1;
43+
std::optional<int> field2;
44+
std::optional<int> field3;
45+
constexpr bool operator==(const SomeStruct2& other) const noexcept {
46+
return this->field1 == other.field1 && this->field2 == other.field2 && this->field3 == other.field3;
47+
};
48+
};
49+
50+
template <>
51+
inline constexpr auto userver::formats::universal::kSerialization<SomeStruct2> =
52+
SerializationConfig<SomeStruct2>::Create()
53+
.With<"field1">(Configurator<Default<114>>{});
54+
55+
56+
TEST(Serialize, Optional) {
57+
SomeStruct2 a{{}, 100, {}};
58+
const auto json = userver::formats::json::ValueBuilder(a).ExtractValue();
59+
EXPECT_EQ(json, userver::formats::json::FromString("{\"field1\":114,\"field2\":100}"));
60+
};
61+
62+
TEST(Parse, Optional) {
63+
constexpr SomeStruct2 valid{{114}, {}, {}};
64+
const auto json = userver::formats::json::FromString("{}");
65+
EXPECT_EQ(json.As<SomeStruct2>(), valid);
66+
};
67+
68+
TEST(TryParse, Optional) {
69+
const auto json = userver::formats::json::FromString("{}");
70+
EXPECT_EQ((bool)userver::formats::parse::TryParse(json, userver::formats::parse::To<SomeStruct2>{}), true);
71+
};
72+
73+
74+
struct SomeStruct3 {
75+
std::unordered_map<std::string, int> field;
76+
auto operator==(const SomeStruct3& other) const noexcept {
77+
return this->field == other.field;
78+
};
79+
};
80+
81+
struct SomeStruct3Description {
82+
userver::formats::universal::Configurator<userver::formats::universal::Additional> field;
83+
};
84+
template <>
85+
inline constexpr auto userver::formats::universal::kSerialization<SomeStruct3> =
86+
SerializationConfig<SomeStruct3>::Create()
87+
.FromStruct<SomeStruct3Description>();
88+
89+
TEST(Serialize, Additional) {
90+
std::unordered_map<std::string, int> value;
91+
value["data1"] = 1;
92+
value["data2"] = 2;
93+
SomeStruct3 a{value};
94+
const auto json = userver::formats::json::ValueBuilder(a).ExtractValue();
95+
EXPECT_EQ(json, userver::formats::json::FromString("{\"data1\":1,\"data2\":2}"));
96+
};
97+
98+
99+
100+
TEST(Parse, Additional) {
101+
std::unordered_map<std::string, int> value;
102+
value["data1"] = 1;
103+
value["data2"] = 2;
104+
SomeStruct3 valid{value};
105+
const auto json = userver::formats::json::FromString("{\"data1\":1,\"data2\":2}");
106+
const auto fromJson = json.As<SomeStruct3>();
107+
EXPECT_EQ(valid, fromJson);
108+
};
109+
110+
111+
TEST(TryParse, Additional) {
112+
const auto json = userver::formats::json::FromString("{\"data1\":1,\"data2\":2}");
113+
EXPECT_EQ((bool)userver::formats::parse::TryParse(json, userver::formats::parse::To<SomeStruct3>{}), true);
114+
};
115+
116+
struct SomeStruct4 {
117+
int field;
118+
};
119+
120+
template <>
121+
inline constexpr auto userver::formats::universal::kSerialization<SomeStruct4> =
122+
SerializationConfig<SomeStruct4>::Create()
123+
.With<"field">(Configurator<Max<120>, Min<10>>{});
124+
125+
126+
127+
128+
TEST(TryParse, MinMax) {
129+
const auto json = userver::formats::json::FromString("{\"field\":1}");
130+
const auto json2 = userver::formats::json::FromString("{\"field\":11}");
131+
const auto json3 = userver::formats::json::FromString("{\"field\":121}");
132+
133+
EXPECT_EQ((bool)userver::formats::parse::TryParse(json, userver::formats::parse::To<SomeStruct4>{}), false);
134+
EXPECT_EQ((bool)userver::formats::parse::TryParse(json2, userver::formats::parse::To<SomeStruct4>{}), true);
135+
EXPECT_EQ((bool)userver::formats::parse::TryParse(json3, userver::formats::parse::To<SomeStruct4>{}), false);
136+
};
137+
138+
139+
140+
struct SomeStruct5 {
141+
std::string field;
142+
};
143+
144+
template <>
145+
inline constexpr auto userver::formats::universal::kSerialization<SomeStruct5> =
146+
SerializationConfig<SomeStruct5>::Create()
147+
.With<"field">(Configurator<Pattern<"^[0-9]+$">>());
148+
149+
TEST(TryParse, Pattern) {
150+
const auto json = userver::formats::json::FromString(R"({"field":"1234412"})");
151+
const auto json2 = userver::formats::json::FromString(R"({"field":"abcdefgh"})");
152+
EXPECT_EQ((bool)userver::formats::parse::TryParse(json, userver::formats::parse::To<SomeStruct5>{}), true);
153+
EXPECT_EQ((bool)userver::formats::parse::TryParse(json2, userver::formats::parse::To<SomeStruct5>{}), false);
154+
};
155+
156+
USERVER_NAMESPACE_END

0 commit comments

Comments
 (0)