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
|
#include <iostream>
#include <memory>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <span>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_span {
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
rfl::Ref<std::span<Person>> children;
};
void delete_children(const Person& _p) {
for (const auto& child : *_p.children) {
delete_children(child);
}
if (!_p.children->empty()) {
delete[] _p.children->data();
}
};
TEST(json, test_span) {
auto children = std::vector<Person>({Person{.first_name = "Bart"},
Person{.first_name = "Lisa"},
Person{.first_name = "Maggie"}});
const auto homer =
Person{.first_name = "Homer",
.children = rfl::Ref<std::span<Person>>::make(
children.data() + 1, children.data() + children.size())};
const auto json_string = rfl::json::write(homer);
const std::string expected =
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Lisa","lastName":"Simpson","children":[]},{"firstName":"Maggie","lastName":"Simpson","children":[]}]})";
const auto json_string1 = rfl::json::write(homer);
EXPECT_EQ(json_string1, expected)
<< "Test failed on write. Expected:" << std::endl
<< expected << std::endl
<< "Got: " << std::endl
<< json_string1 << std::endl
<< std::endl;
const auto res = rfl::json::read<Person, rfl::AllowRawPtrs>(json_string1);
EXPECT_TRUE(res && true) << "Test failed on read. Error: "
<< res.error().what();
const auto json_string2 = rfl::json::write(res.value());
EXPECT_EQ(json_string2, expected)
<< "Test failed on read. Expected:" << std::endl
<< expected << std::endl
<< "Got: " << std::endl
<< json_string2 << std::endl
<< std::endl;
if (res) {
delete_children(*res);
}
}
} // namespace test_span
|