mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 00:26:19 +08:00
Added a test for optional fields
This commit is contained in:
@@ -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\"}]}");
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user