From 992115c6a08a62b3cd5fe707e23838f20865eb2c Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 12 Nov 2023 20:51:00 +0100 Subject: [PATCH] Added test_save_load for JSON --- .gitignore | 5 +++ include/rfl/json/load.hpp | 3 +- include/rfl/json/save.hpp | 2 +- tests/json/test_save_load.hpp | 66 +++++++++++++++++++++++++++++++++++ tests/json/tests.cpp | 3 ++ 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 tests/json/test_save_load.hpp diff --git a/.gitignore b/.gitignore index 6407405d..02def0a0 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,8 @@ **/CMakeFiles/** **/reflect-cpp-tests **/build/** +**/build_clang/** + +# Output files +*.json +*.fb diff --git a/include/rfl/json/load.hpp b/include/rfl/json/load.hpp index 2f34febf..c5fff274 100644 --- a/include/rfl/json/load.hpp +++ b/include/rfl/json/load.hpp @@ -10,7 +10,8 @@ namespace json { template Result load(const std::string& _fname) { - return rfl::io::load_string(_fname).and_then(read); + const auto read_string = [](const auto& _str) { return read(_str); }; + return rfl::io::load_string(_fname).and_then(read_string); } } // namespace json diff --git a/include/rfl/json/save.hpp b/include/rfl/json/save.hpp index 614f3917..433e159e 100644 --- a/include/rfl/json/save.hpp +++ b/include/rfl/json/save.hpp @@ -14,7 +14,7 @@ namespace json { template Result save(const std::string& _fname, const T& _obj) { - return rfl::io::save_string(write(_obj)); + return rfl::io::save_string(_fname, write(_obj)); } } // namespace json diff --git a/tests/json/test_save_load.hpp b/tests/json/test_save_load.hpp new file mode 100644 index 00000000..f47dca18 --- /dev/null +++ b/tests/json/test_save_load.hpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include + +void test_save_load() { + std::cout << "test_save_load" << std::endl; + + using Age = rfl::Validator, rfl::Maximum<130>>>; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; + rfl::Field<"age", Age> age; + rfl::Field<"email", rfl::Email> email; + rfl::Field<"children", std::vector> children; + }; + + const auto bart = Person{.first_name = "Bart", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com", + .children = std::vector()}; + + const auto lisa = Person{ + .first_name = "Lisa", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com", + .children = rfl::default_value // same as std::vector() + }; + + const auto maggie = + rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")), + rfl::make_field<"email">(std::string("maggie@simpson.com")), + rfl::make_field<"age">(0)); + + const auto homer1 = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .children = std::vector({bart, lisa, maggie})}; + + rfl::json::save("homer.json", homer1); + + const auto homer2 = rfl::json::load("homer.json").value(); + + const auto string1 = rfl::json::write(homer1); + const auto string2 = rfl::json::write(homer2); + + if (string1 != string2) { + std::cout << "Test failed. Content was not identical." << std::endl + << std::endl; + return; + } + + std::cout << "OK" << std::endl << std::endl; +} diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index 89c7ea04..72e01bca 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -31,6 +31,7 @@ #include "test_replace_flatten.hpp" #include "test_replace_with_other_struct.hpp" #include "test_result.hpp" +#include "test_save_load.hpp" #include "test_set.hpp" #include "test_size.hpp" #include "test_string_map.hpp" @@ -97,5 +98,7 @@ int main() { test_email(); test_size(); + test_save_load(); + return 0; }