mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-11-29 14:26:19 +08:00
Added tests for custom classes
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.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);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace tcc2 {
|
||||
|
||||
struct Person {
|
||||
Person(const std::string& _first_name, const std::string& _last_name,
|
||||
const int _age)
|
||||
: first_name_(_first_name), last_name_(_last_name), age_(_age) {}
|
||||
|
||||
const auto& first_name() const { return first_name_; }
|
||||
|
||||
const auto& last_name() const { return last_name_; }
|
||||
|
||||
auto age() const { return age_; }
|
||||
|
||||
private:
|
||||
std::string first_name_;
|
||||
std::string last_name_;
|
||||
int age_;
|
||||
};
|
||||
|
||||
struct PersonImpl {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
|
||||
static PersonImpl from_class(const Person& _p) noexcept {
|
||||
return PersonImpl{.first_name = _p.first_name(),
|
||||
.last_name = _p.last_name(),
|
||||
.age = _p.age()};
|
||||
}
|
||||
|
||||
Person to_class() const { return Person(first_name(), last_name(), age()); }
|
||||
};
|
||||
|
||||
} // namespace tcc2
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class ReaderType, class WriterType>
|
||||
struct Parser<ReaderType, WriterType, tcc2::Person>
|
||||
: public CustomParser<ReaderType, WriterType, tcc2::Person,
|
||||
tcc2::PersonImpl> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
void test_custom_class2() {
|
||||
std::cout << "test_custom_class2" << std::endl;
|
||||
|
||||
const auto bart = tcc2::Person("Bart", "Simpson", 10);
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace tcc3 {
|
||||
|
||||
struct Person {
|
||||
Person(const std::string& _first_name,
|
||||
const rfl::Box<std::string>& _last_name, int _age)
|
||||
: first_name_(_first_name),
|
||||
last_name_(rfl::make_box<std::string>(*_last_name)),
|
||||
age_(_age) {}
|
||||
|
||||
const auto& first_name() const { return first_name_; }
|
||||
|
||||
const auto& last_name() const { return last_name_; }
|
||||
|
||||
auto age() const { return age_; }
|
||||
|
||||
private:
|
||||
std::string first_name_;
|
||||
rfl::Box<std::string> last_name_;
|
||||
int age_;
|
||||
};
|
||||
|
||||
struct PersonImpl {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", rfl::Box<std::string>> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
|
||||
static PersonImpl from_class(const Person& _p) noexcept {
|
||||
return PersonImpl{.first_name = _p.first_name(),
|
||||
.last_name = rfl::make_box<std::string>(*_p.last_name()),
|
||||
.age = _p.age()};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace tcc3
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class ReaderType, class WriterType>
|
||||
struct Parser<ReaderType, WriterType, tcc3::Person>
|
||||
: public CustomParser<ReaderType, WriterType, tcc3::Person,
|
||||
tcc3::PersonImpl> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
void test_custom_class3() {
|
||||
std::cout << "test_custom_class3" << std::endl;
|
||||
|
||||
const auto bart =
|
||||
tcc3::Person("Bart", rfl::make_box<std::string>("Simpson"), 10);
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
#include "test_all_of.hpp"
|
||||
#include "test_anonymous_fields.hpp"
|
||||
#include "test_custom_class1.hpp"
|
||||
#include "test_custom_class2.hpp"
|
||||
#include "test_custom_class3.hpp"
|
||||
#include "test_deque.hpp"
|
||||
#include "test_field_variant.hpp"
|
||||
#include "test_forward_list.hpp"
|
||||
@@ -32,6 +35,9 @@ int main() {
|
||||
test_variant();
|
||||
test_field_variant();
|
||||
test_tagged_union();
|
||||
test_custom_class1();
|
||||
test_custom_class2();
|
||||
test_custom_class3();
|
||||
|
||||
test_deque();
|
||||
test_forward_list();
|
||||
|
||||
Reference in New Issue
Block a user