From e932ed86f294e21cf8761f4d4c87011e0bbb4559 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Tue, 3 Oct 2023 14:44:33 +0200 Subject: [PATCH] Added tests for custom classes --- tests/test_custom_class1.hpp | 39 +++++++++++++++++++++++++++++++++ tests/test_custom_class2.hpp | 42 ++++++++++++++++++++++++++++++++++++ tests/tests.cpp | 4 ++++ 3 files changed, 85 insertions(+) create mode 100644 tests/test_custom_class1.hpp create mode 100644 tests/test_custom_class2.hpp diff --git a/tests/test_custom_class1.hpp b/tests/test_custom_class1.hpp new file mode 100644 index 00000000..dc3924b6 --- /dev/null +++ b/tests/test_custom_class1.hpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_custom_class1() { + std::cout << "test_custom_class1" << std::endl; + + struct Person { + struct PersonImpl { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", std::vector> children = + rfl::default_value; + }; + + using ReflectionType = PersonImpl; + + Person(const PersonImpl& _impl) : impl(_impl) {} + + Person(const std::string& _first_name) + : impl(PersonImpl{.first_name = _first_name}) {} + + const ReflectionType& reflection() const { return impl; }; + + private: + PersonImpl impl; + }; + + const auto bart = Person("Bart"); + + write_and_read( + bart, + "{\"firstName\":\"Bart\",\"lastName\":\"Simpson\",\"children\":[]}"); +} diff --git a/tests/test_custom_class2.hpp b/tests/test_custom_class2.hpp new file mode 100644 index 00000000..7025695f --- /dev/null +++ b/tests/test_custom_class2.hpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_custom_class2() { + std::cout << "test_custom_class2" << std::endl; + + struct FiveDigitCode { + using ReflectionType = int; + + FiveDigitCode(const int _code) : code(_code) { + if (_code < 10000) { + throw std::runtime_error("Less than five digits."); + } + if (_code > 99999) { + throw std::runtime_error("More than five digits."); + } + } + + ReflectionType reflection() const { return code; }; + + private: + int code; + }; + + const auto result = rfl::json::read("123"); + + if (result) { + std::cout << "Failed: Expected an error, but got none." << std::endl; + return; + } + + const auto c = FiveDigitCode(12345); + + write_and_read(c, "12345"); +} diff --git a/tests/tests.cpp b/tests/tests.cpp index b2e4b313..4485ad88 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -1,3 +1,5 @@ +#include "test_custom_class1.hpp" +#include "test_custom_class2.hpp" #include "test_default_values.hpp" #include "test_field_variant.hpp" #include "test_literal.hpp" @@ -14,6 +16,8 @@ int main() { test_tagged_union(); test_field_variant(); test_ref(); + test_custom_class1(); + test_custom_class2(); return 0; }