Added tests for custom classes

This commit is contained in:
Patrick Urbanke
2023-10-03 14:44:33 +02:00
parent fb656f51cc
commit e932ed86f2
3 changed files with 85 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>
#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<Person>> 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\":[]}");
}
+42
View File
@@ -0,0 +1,42 @@
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <stdexcept>
#include <string>
#include <vector>
#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<FiveDigitCode>("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");
}
+4
View File
@@ -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;
}