1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
#define CAF_SUITE json_writer
#include "caf/json_writer.hpp"
#include "core-test.hpp"
using namespace caf;
using namespace std::literals::string_literals;
namespace {
struct fixture {
template <class T>
expected<std::string>
to_json_string(T&& x, size_t indentation,
bool skip_empty_fields
= json_writer::skip_empty_fields_default) {
json_writer writer;
writer.indentation(indentation);
writer.skip_empty_fields(skip_empty_fields);
if (writer.apply(std::forward<T>(x))) {
auto buf = writer.str();
return {std::string{buf.begin(), buf.end()}};
} else {
MESSAGE("partial JSON output: " << writer.str());
return {writer.get_error()};
}
}
};
} // namespace
BEGIN_FIXTURE_SCOPE(fixture)
SCENARIO("the JSON writer converts builtin types to strings") {
GIVEN("an integer") {
auto x = 42;
WHEN("converting it to JSON with any indentation factor") {
THEN("the JSON output is the number") {
CHECK_EQ(to_json_string(x, 0), "42"s);
CHECK_EQ(to_json_string(x, 2), "42"s);
}
}
}
GIVEN("a string") {
std::string x = R"_(hello "world"!)_";
WHEN("converting it to JSON with any indentation factor") {
THEN("the JSON output is the escaped string") {
std::string out = R"_("hello \"world\"!")_";
CHECK_EQ(to_json_string(x, 0), out);
CHECK_EQ(to_json_string(x, 2), out);
}
}
}
GIVEN("a list") {
auto x = std::vector<int>{1, 2, 3};
WHEN("converting it to JSON with indentation factor 0") {
THEN("the JSON output is a single line") {
std::string out = "[1, 2, 3]";
CHECK_EQ(to_json_string(x, 0), out);
}
}
WHEN("converting it to JSON with indentation factor 2") {
THEN("the JSON output uses multiple lines") {
std::string out = R"_([
1,
2,
3
])_";
CHECK_EQ(to_json_string(x, 2), out);
}
}
}
GIVEN("a dictionary") {
std::map<std::string, std::string> x;
x.emplace("a", "A");
x.emplace("b", "B");
x.emplace("c", "C");
WHEN("converting it to JSON with indentation factor 0") {
THEN("the JSON output is a single line") {
CHECK_EQ(to_json_string(x, 0), R"_({"a": "A", "b": "B", "c": "C"})_"s);
}
}
WHEN("converting it to JSON with indentation factor 2") {
THEN("the JSON output uses multiple lines") {
std::string out = R"_({
"a": "A",
"b": "B",
"c": "C"
})_";
CHECK_EQ(to_json_string(x, 2), out);
}
}
}
GIVEN("a message") {
auto x = make_message(put_atom_v, "foo", 42);
WHEN("converting it to JSON with indentation factor 0") {
THEN("the JSON output is a single line") {
std::string out = R"_([{"@type": "caf::put_atom"}, "foo", 42])_";
CHECK_EQ(to_json_string(x, 0), out);
}
}
WHEN("converting it to JSON with indentation factor 2") {
THEN("the JSON output uses multiple lines") {
std::string out = R"_([
{
"@type": "caf::put_atom"
},
"foo",
42
])_";
CHECK_EQ(to_json_string(x, 2), out);
}
}
}
}
SCENARIO("the JSON writer converts simple structs to strings") {
GIVEN("a dummy_struct object") {
dummy_struct x{10, "foo"};
WHEN("converting it to JSON with indentation factor 0") {
THEN("the JSON output is a single line") {
std::string out = R"_({"@type": "dummy_struct", "a": 10, "b": "foo"})_";
CHECK_EQ(to_json_string(x, 0), out);
}
}
WHEN("converting it to JSON with indentation factor 2") {
THEN("the JSON output uses multiple lines") {
std::string out = R"_({
"@type": "dummy_struct",
"a": 10,
"b": "foo"
})_";
CHECK_EQ(to_json_string(x, 2), out);
}
}
}
}
SCENARIO("the JSON writer converts nested structs to strings") {
GIVEN("a rectangle object") {
auto x = rectangle{{100, 200}, {10, 20}};
WHEN("converting it to JSON with indentation factor 0") {
THEN("the JSON output is a single line") {
std::string out = R"({"@type": "rectangle", )"
R"("top-left": {"x": 100, "y": 200}, )"
R"("bottom-right": {"x": 10, "y": 20}})";
CHECK_EQ(to_json_string(x, 0), out);
}
}
WHEN("converting it to JSON with indentation factor 2") {
THEN("the JSON output uses multiple lines") {
std::string out = R"_({
"@type": "rectangle",
"top-left": {
"x": 100,
"y": 200
},
"bottom-right": {
"x": 10,
"y": 20
}
})_";
CHECK_EQ(to_json_string(x, 2), out);
}
}
}
}
SCENARIO("the JSON writer converts structs with member dictionaries") {
GIVEN("a phone_book object") {
phone_book x;
x.city = "Model City";
x.entries["Bob"] = 555'6837;
x.entries["Jon"] = 555'9347;
WHEN("converting it to JSON with indentation factor 0") {
THEN("the JSON output is a single line") {
std::string out = R"({"@type": "phone_book",)"
R"( "city": "Model City",)"
R"( "entries": )"
R"({"Bob": 5556837,)"
R"( "Jon": 5559347}})";
CHECK_EQ(to_json_string(x, 0), out);
}
}
WHEN("converting it to JSON with indentation factor 2") {
THEN("the JSON output uses multiple lines") {
std::string out = R"({
"@type": "phone_book",
"city": "Model City",
"entries": {
"Bob": 5556837,
"Jon": 5559347
}
})";
CHECK_EQ(to_json_string(x, 2), out);
}
}
}
}
SCENARIO("the JSON writer omits or nulls missing values") {
GIVEN("a dummy_user object without nickname") {
dummy_user user;
user.name = "Bjarne";
WHEN("converting it to JSON with skip_empty_fields = true (default)") {
THEN("the JSON output omits the field 'nickname'") {
std::string out = R"({"@type": "dummy_user", "name": "Bjarne"})";
CHECK_EQ(to_json_string(user, 0), out);
}
}
WHEN("converting it to JSON with skip_empty_fields = false") {
THEN("the JSON output includes the field 'nickname' with a null value") {
std::string out
= R"({"@type": "dummy_user", "name": "Bjarne", "nickname": null})";
CHECK_EQ(to_json_string(user, 0, false), out);
}
}
}
}
SCENARIO("the JSON writer annotates variant fields") {
GIVEN("a widget object with rectangle shape") {
widget x;
x.color = "red";
x.shape = rectangle{{10, 10}, {20, 20}};
WHEN("converting it to JSON with indentation factor 0") {
THEN("the JSON is a single line containing '@shape-type = rectangle'") {
std::string out = R"({"@type": "widget", )"
R"("color": "red", )"
R"("@shape-type": "rectangle", )"
R"("shape": )"
R"({"top-left": {"x": 10, "y": 10}, )"
R"("bottom-right": {"x": 20, "y": 20}}})";
CHECK_EQ(to_json_string(x, 0), out);
}
}
WHEN("converting it to JSON with indentation factor 2") {
THEN("the JSON is multiple lines containing '@shape-type = rectangle'") {
std::string out = R"({
"@type": "widget",
"color": "red",
"@shape-type": "rectangle",
"shape": {
"top-left": {
"x": 10,
"y": 10
},
"bottom-right": {
"x": 20,
"y": 20
}
}
})";
CHECK_EQ(to_json_string(x, 2), out);
}
}
}
GIVEN("a widget object with circle shape") {
widget x;
x.color = "red";
x.shape = circle{{15, 15}, 5};
WHEN("converting it to JSON with indentation factor 0") {
THEN("the JSON is a single line containing '@shape-type = circle'") {
std::string out = R"({"@type": "widget", )"
R"("color": "red", )"
R"("@shape-type": "circle", )"
R"("shape": )"
R"({"center": {"x": 15, "y": 15}, )"
R"("radius": 5}})";
CHECK_EQ(to_json_string(x, 0), out);
}
}
WHEN("converting it to JSON with indentation factor 2") {
THEN("the JSON is multiple lines containing '@shape-type = circle'") {
std::string out = R"({
"@type": "widget",
"color": "red",
"@shape-type": "circle",
"shape": {
"center": {
"x": 15,
"y": 15
},
"radius": 5
}
})";
CHECK_EQ(to_json_string(x, 2), out);
}
}
}
}
END_FIXTURE_SCOPE()
|