Added a test for optional fields

This commit is contained in:
Patrick Urbanke
2023-10-03 17:19:56 +02:00
parent c2f4e11cca
commit cb4ffaf216
2 changed files with 37 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
void test_optional_fields() {
std::cout << "test_optional_fields" << std::endl;
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name = "Simpson";
rfl::Field<"children", std::optional<std::vector<Person>>> children =
rfl::default_value;
};
const auto bart = Person{.first_name = "Bart"};
const auto lisa = Person{.first_name = "Lisa"};
const auto maggie = Person{.first_name = "Maggie"};
const auto homer =
Person{.first_name = "Homer",
.children = std::vector<Person>({bart, lisa, maggie})};
write_and_read(
homer,
"{\"firstName\":\"Homer\",\"lastName\":\"Simpson\",\"children\":[{"
"\"firstName\":\"Bart\",\"lastName\":\"Simpson\"},{"
"\"firstName\":\"Lisa\",\"lastName\":\"Simpson\"},{"
"\"firstName\":\"Maggie\",\"lastName\":\"Simpson\"}]}");
}
+2
View File
@@ -4,6 +4,7 @@
#include "test_default_values.hpp"
#include "test_field_variant.hpp"
#include "test_literal.hpp"
#include "test_optional_fields.hpp"
#include "test_readme_example.hpp"
#include "test_ref.hpp"
#include "test_tagged_union.hpp"
@@ -12,6 +13,7 @@
int main() {
test_readme_example();
test_default_values();
test_optional_fields();
test_literal();
test_variant();
test_tagged_union();