Skip to content

Commit

Permalink
Added tests for YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Jan 1, 2024
1 parent 04aa71c commit 571ea68
Show file tree
Hide file tree
Showing 59 changed files with 1,335 additions and 41 deletions.
40 changes: 40 additions & 0 deletions tests/yaml/test_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "test_array.hpp"

#include <array>
#include <iostream>
#include <memory>
#include <rfl/yaml.hpp>
#include <source_location>
#include <string>

// Make sure things still compile when
// rfl.hpp is included after rfl/yaml.hpp.
#include <rfl.hpp>

#include "write_and_read.hpp"

namespace test_array {

struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::array<Person, 3>> children = nullptr;
};

void test() {
std::cout << std::source_location::current().function_name() << std::endl;

auto bart = Person{.first_name = "Bart"};

auto lisa = Person{.first_name = "Lisa"};

auto maggie = Person{.first_name = "Maggie"};

const auto homer = Person{
.first_name = "Homer",
.children = std::make_unique<std::array<Person, 3>>(std::array<Person, 3>{
std::move(bart), std::move(lisa), std::move(maggie)})};

write_and_read(homer);
}
} // namespace test_array
4 changes: 4 additions & 0 deletions tests/yaml/test_array.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace test_array{
void test();
}

50 changes: 50 additions & 0 deletions tests/yaml/test_box.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "test_box.hpp"

#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <source_location>
#include <string>
#include <vector>

#include "write_and_read.hpp"

namespace test_box {

struct DecisionTree {
struct Leaf {
using Tag = rfl::Literal<"Leaf">;
double value;
};

struct Node {
using Tag = rfl::Literal<"Node">;
rfl::Rename<"criticalValue", double> critical_value;
rfl::Box<DecisionTree> lesser;
rfl::Box<DecisionTree> greater;
};

using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;

rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
};

void test() {
std::cout << std::source_location::current().function_name() << std::endl;

auto leaf1 = DecisionTree::Leaf{.value = 3.0};

auto leaf2 = DecisionTree::Leaf{.value = 5.0};

auto node = DecisionTree::Node{
.critical_value = 10.0,
.lesser = rfl::make_box<DecisionTree>(DecisionTree{leaf1}),
.greater = rfl::make_box<DecisionTree>(DecisionTree{leaf2})};

const DecisionTree tree{.leaf_or_node = std::move(node)};

write_and_read(tree);

}
} // namespace test_box
4 changes: 4 additions & 0 deletions tests/yaml/test_box.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace test_box{
void test();
}

42 changes: 42 additions & 0 deletions tests/yaml/test_custom_class1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "test_custom_class1.hpp"

#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <source_location>
#include <string>
#include <vector>

#include "write_and_read.hpp"

namespace test_custom_class1 {

struct Person {
struct PersonImpl {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::vector<Person> children;
};

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;
};

void test() {
std::cout << std::source_location::current().function_name() << std::endl;

const auto bart = Person("Bart");

write_and_read(bart);
}
} // namespace test_custom_class1
4 changes: 4 additions & 0 deletions tests/yaml/test_custom_class1.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace test_custom_class1{
void test();
}

68 changes: 68 additions & 0 deletions tests/yaml/test_custom_class3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "test_custom_class3.hpp"

#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <source_location>
#include <string>
#include <vector>

#include "write_and_read.hpp"

namespace test_custom_class3 {

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::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
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 test_custom_class3

namespace rfl {
namespace parsing {

template <class ReaderType, class WriterType>
struct Parser<ReaderType, WriterType, test_custom_class3::Person>
: public CustomParser<ReaderType, WriterType, test_custom_class3::Person,
test_custom_class3::PersonImpl> {};

} // namespace parsing
} // namespace rfl

namespace test_custom_class3 {

void test() {
std::cout << std::source_location::current().function_name() << std::endl;

const auto bart = Person("Bart", "Simpson", 10);

write_and_read(bart);
}

} // namespace test_custom_class3
4 changes: 4 additions & 0 deletions tests/yaml/test_custom_class3.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace test_custom_class3{
void test();
}

69 changes: 69 additions & 0 deletions tests/yaml/test_custom_class4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "test_custom_class4.hpp"

#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <source_location>
#include <string>
#include <vector>

#include "write_and_read.hpp"

namespace test_custom_class4 {

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 test_custom_class4

namespace rfl {
namespace parsing {

template <class ReaderType, class WriterType>
struct Parser<ReaderType, WriterType, test_custom_class4::Person>
: public CustomParser<ReaderType, WriterType, test_custom_class4::Person,
test_custom_class4::PersonImpl> {};

} // namespace parsing
} // namespace rfl

namespace test_custom_class4 {

void test() {
std::cout << std::source_location::current().function_name() << std::endl;

const auto bart = test_custom_class4::Person(
"Bart", rfl::make_box<std::string>("Simpson"), 10);

write_and_read(bart);
}
} // namespace test_custom_class4
4 changes: 4 additions & 0 deletions tests/yaml/test_custom_class4.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace test_custom_class4{
void test();
}

32 changes: 32 additions & 0 deletions tests/yaml/test_default_values.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "test_default_values.hpp"

#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>

#include "write_and_read.hpp"

namespace test_default_values {

struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::vector<Person> children;
};

void test() {
std::cout << std::source_location::current().function_name() << std::endl;

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<Person>({bart, lisa, maggie})};

write_and_read(homer);
}
} // namespace test_default_values
4 changes: 4 additions & 0 deletions tests/yaml/test_default_values.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace test_default_values{
void test();
}

33 changes: 33 additions & 0 deletions tests/yaml/test_deque.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "test_deque.hpp"

#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>

#include "write_and_read.hpp"

namespace test_deque {

struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::deque<Person>> children;
};

void test() {
std::cout << std::source_location::current().function_name() << std::endl;

auto children = std::make_unique<std::deque<Person>>();
children->emplace_back(Person{.first_name = "Bart"});
children->emplace_back(Person{.first_name = "Lisa"});
children->emplace_back(Person{.first_name = "Maggie"});

const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};

write_and_read(homer);

}
} // namespace test_deque
4 changes: 4 additions & 0 deletions tests/yaml/test_deque.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace test_deque{
void test();
}

Loading

0 comments on commit 571ea68

Please sign in to comment.