diff --git a/tests/test_optional_fields.hpp b/tests/test_optional_fields.hpp new file mode 100644 index 00000000..360724e2 --- /dev/null +++ b/tests/test_optional_fields.hpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include + +#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>> 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({bart, lisa, maggie})}; + + write_and_read( + homer, + "{\"firstName\":\"Homer\",\"lastName\":\"Simpson\",\"children\":[{" + "\"firstName\":\"Bart\",\"lastName\":\"Simpson\"},{" + "\"firstName\":\"Lisa\",\"lastName\":\"Simpson\"},{" + "\"firstName\":\"Maggie\",\"lastName\":\"Simpson\"}]}"); +} diff --git a/tests/tests.cpp b/tests/tests.cpp index 46fa6817..952bf5b2 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -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();