mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 16:56:17 +08:00
Added tests
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
|
||||
// Makes sure that we can set default values.
|
||||
void test_default_values() {
|
||||
std::cout << "Testing default values..." << std::endl;
|
||||
std::cout << "test_default_values" << std::endl;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
// Makes sure that we can set default values.
|
||||
void test_literal() {
|
||||
std::cout << "test_literal" << std::endl;
|
||||
|
||||
using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">;
|
||||
using LastName = rfl::Literal<"Simpson">;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", FirstName> first_name;
|
||||
rfl::Field<"lastName", LastName> last_name = rfl::default_value;
|
||||
rfl::Field<"children", std::vector<Person>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
const auto bart = Person{.first_name = FirstName::make<"Bart">()};
|
||||
|
||||
write_and_read(
|
||||
bart,
|
||||
"{\"firstName\":\"Bart\",\"lastName\":\"Simpson\",\"children\":[]}");
|
||||
}
|
||||
@@ -4,9 +4,13 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
// Makes sure that the example in the README works as expected.
|
||||
// It would be embarrassing if it didn't...
|
||||
void test_readme_example() {
|
||||
std::cout << "test_readme_example" << std::endl;
|
||||
|
||||
// "firstName", "lastName" and "children" are the field names
|
||||
// as they will appear in the JSON. The C++ standard is
|
||||
// snake case, the JSON standard is camel case, so the names
|
||||
@@ -37,21 +41,10 @@ void test_readme_example() {
|
||||
.last_name = "Simpson",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
// We can now transform this into a JSON string.
|
||||
const std::string json_string = rfl::json::write(homer);
|
||||
|
||||
std::cout << json_string << std::endl;
|
||||
|
||||
// And we can parse the string to recreate the struct.
|
||||
auto homer2 = rfl::json::read<Person>(json_string).value();
|
||||
|
||||
// Fields can be accessed like this:
|
||||
std::cout << "Hello, my name is " << homer.first_name() << " "
|
||||
<< homer.last_name() << "." << std::endl;
|
||||
|
||||
// Since homer2 is mutable, we can also change the values like this:
|
||||
homer2.first_name = "Marge";
|
||||
|
||||
std::cout << "Hello, my name is " << homer2.first_name() << " "
|
||||
<< homer2.last_name() << "." << std::endl;
|
||||
write_and_read(
|
||||
homer,
|
||||
"{\"firstName\":\"Homer\",\"lastName\":\"Simpson\",\"children\":[{"
|
||||
"\"firstName\":\"Bart\",\"lastName\":\"Simpson\",\"children\":[]},{"
|
||||
"\"firstName\":\"Lisa\",\"lastName\":\"Simpson\",\"children\":[]},{"
|
||||
"\"firstName\":\"Maggie\",\"lastName\":\"Simpson\",\"children\":[]}]}");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_ref() {
|
||||
std::cout << "test_ref" << std::endl;
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", rfl::Ref<DecisionTree>> lesser;
|
||||
rfl::Field<"right", rfl::Ref<DecisionTree>> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
};
|
||||
|
||||
const auto leaf1 = DecisionTree::Leaf{.value = 3.0};
|
||||
|
||||
const auto leaf2 = DecisionTree::Leaf{.value = 5.0};
|
||||
|
||||
auto node =
|
||||
DecisionTree::Node{.critical_value = 10.0,
|
||||
.lesser = rfl::make_ref<DecisionTree>(leaf1),
|
||||
.greater = rfl::make_ref<DecisionTree>(leaf2)};
|
||||
|
||||
const DecisionTree tree{.leaf_or_node = std::move(node)};
|
||||
|
||||
write_and_read(
|
||||
tree,
|
||||
"{\"leafOrNode\":{\"type\":\"Node\",\"criticalValue\":10.0,\"left\":{"
|
||||
"\"leafOrNode\":{\"type\":\"Leaf\",\"value\":3.0}},\"right\":{"
|
||||
"\"leafOrNode\":{\"type\":\"Leaf\",\"value\":5.0}}}}");
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
// Makes sure that we can set default values.
|
||||
void test_tagged_union() {
|
||||
std::cout << "test_tagged_union" << std::endl;
|
||||
|
||||
struct Circle {
|
||||
rfl::Field<"shape", rfl::Literal<"Circle">> shape = rfl::default_value;
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
rfl::Field<"shape", rfl::Literal<"Rectangle">> shape =
|
||||
rfl::default_value;
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
rfl::Field<"shape", rfl::Literal<"Square">> shape = rfl::default_value;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
using Shapes = std::variant<Circle, Square, Rectangle>;
|
||||
|
||||
const Shapes r = Rectangle{.height = 10, .width = 5};
|
||||
|
||||
write_and_read(r,
|
||||
"{\"shape\":\"Rectangle\",\"height\":10.0,\"width\":5.0}");
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
// Makes sure that we can set default values.
|
||||
void test_variant() {
|
||||
std::cout << "test_variant" << std::endl;
|
||||
|
||||
struct Circle {
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
using Shapes = std::variant<Circle, Rectangle, Square>;
|
||||
|
||||
const Shapes r = Rectangle{.height = 10, .width = 5};
|
||||
|
||||
write_and_read(r, "{\"height\":10.0,\"width\":5.0}");
|
||||
}
|
||||
@@ -1,9 +1,17 @@
|
||||
#include "test_default_values.hpp"
|
||||
#include "test_literal.hpp"
|
||||
#include "test_readme_example.hpp"
|
||||
#include "test_ref.hpp"
|
||||
#include "test_tagged_union.hpp"
|
||||
#include "test_variant.hpp"
|
||||
|
||||
int main() {
|
||||
test_readme_example();
|
||||
test_default_values();
|
||||
test_literal();
|
||||
test_variant();
|
||||
test_tagged_union();
|
||||
test_ref();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ void write_and_read(const T& _struct, const std::string& _expected) {
|
||||
std::cout << "Test failed on write. Expected:" << std::endl
|
||||
<< _expected << std::endl
|
||||
<< "Got: " << std::endl
|
||||
<< json_string1 << std::endl;
|
||||
<< json_string1 << std::endl
|
||||
<< std::endl;
|
||||
return;
|
||||
}
|
||||
const auto struct2 = rfl::json::read<T>(json_string1).value();
|
||||
@@ -21,7 +22,8 @@ void write_and_read(const T& _struct, const std::string& _expected) {
|
||||
std::cout << "Test failed on read. Expected:" << std::endl
|
||||
<< _expected << std::endl
|
||||
<< "Got: " << std::endl
|
||||
<< json_string2 << std::endl;
|
||||
<< json_string2 << std::endl
|
||||
<< std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user