Skip to content

Commit df96f7c

Browse files
committed
boost serialisation (tests)
1 parent 7974898 commit df96f7c

9 files changed

Lines changed: 207 additions & 0 deletions

File tree

tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,7 @@ endif()
6161
if (REFLECTCPP_YAML)
6262
add_subdirectory(yaml)
6363
endif()
64+
65+
if (REFLECTCPP_BOOST_SERIALIZATION)
66+
add_subdirectory(boost_serialization)
67+
endif()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
project(reflect-cpp-boost-serialization-tests)
2+
3+
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")
4+
5+
add_executable(
6+
reflect-cpp-boost-serialization-tests
7+
${SOURCES}
8+
)
9+
target_precompile_headers(reflect-cpp-boost-serialization-tests PRIVATE [["rfl.hpp"]] <iostream> <string> <functional> <gtest/gtest.h>)
10+
11+
12+
target_link_libraries(reflect-cpp-boost-serialization-tests PRIVATE reflectcpp_tests_crt)
13+
14+
add_custom_command(TARGET reflect-cpp-boost-serialization-tests POST_BUILD
15+
COMMAND ${CMAKE_COMMAND} -E copy -t $<TARGET_FILE_DIR:reflect-cpp-boost-serialization-tests> $<TARGET_RUNTIME_DLLS:reflect-cpp-boost-serialization-tests>
16+
COMMAND_EXPAND_LISTS
17+
)
18+
19+
find_package(GTest)
20+
gtest_discover_tests(reflect-cpp-boost-serialization-tests)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <rfl.hpp>
2+
#include <string>
3+
#include <vector>
4+
5+
#include "write_and_read.hpp"
6+
7+
namespace test_array {
8+
9+
struct Person {
10+
std::string first_name;
11+
int age;
12+
std::vector<std::string> hobbies;
13+
};
14+
15+
TEST(boost_serialization, test_array) {
16+
const auto homer = Person{
17+
.first_name = "Homer",
18+
.age = 45,
19+
.hobbies = std::vector<std::string>({"bowling", "eating", "sleeping"})};
20+
21+
write_and_read_with_json(homer);
22+
}
23+
} // namespace test_array
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <rfl.hpp>
2+
#include <string>
3+
4+
#include "write_and_read.hpp"
5+
6+
namespace test_enum {
7+
8+
enum class Color { red, green, blue };
9+
10+
struct Circle {
11+
float radius;
12+
Color color;
13+
};
14+
15+
TEST(boost_serialization, test_enum) {
16+
const auto circle = Circle{.radius = 2.0f, .color = Color::green};
17+
18+
write_and_read_with_json(circle);
19+
}
20+
} // namespace test_enum
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <map>
2+
#include <rfl.hpp>
3+
#include <string>
4+
5+
#include "write_and_read.hpp"
6+
7+
namespace test_map {
8+
9+
struct Person {
10+
std::string first_name;
11+
std::string last_name = "Simpson";
12+
std::map<std::string, std::string> attributes;
13+
};
14+
15+
TEST(boost_serialization, test_map) {
16+
const auto homer =
17+
Person{.first_name = "Homer",
18+
.attributes = {{"occupation", "Safety Inspector"},
19+
{"town", "Springfield"}}};
20+
21+
write_and_read(homer);
22+
}
23+
} // namespace test_map
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <rfl.hpp>
2+
#include <string>
3+
#include <vector>
4+
5+
#include "write_and_read.hpp"
6+
7+
namespace test_optional_fields {
8+
9+
struct Person {
10+
std::string first_name;
11+
std::string last_name = "Simpson";
12+
std::optional<std::vector<Person>> children;
13+
};
14+
15+
TEST(boost_serialization, test_optional_fields) {
16+
const auto bart = Person{.first_name = "Bart"};
17+
18+
const auto lisa = Person{.first_name = "Lisa"};
19+
20+
const auto maggie = Person{.first_name = "Maggie"};
21+
22+
const auto homer =
23+
Person{.first_name = "Homer",
24+
.children = std::vector<Person>({bart, lisa, maggie})};
25+
26+
write_and_read_with_json(homer);
27+
}
28+
} // namespace test_optional_fields
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <rfl.hpp>
2+
#include <string>
3+
#include <vector>
4+
5+
#include "write_and_read.hpp"
6+
7+
namespace test_readme_example {
8+
9+
struct Person {
10+
std::string first_name;
11+
std::string last_name = "Simpson";
12+
std::string town = "Springfield";
13+
int age;
14+
};
15+
16+
TEST(boost_serialization, test_readme_example) {
17+
const auto homer =
18+
Person{.first_name = "Homer", .last_name = "Simpson", .age = 45};
19+
20+
write_and_read_with_json(homer);
21+
}
22+
} // namespace test_readme_example
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <rfl.hpp>
2+
#include <rfl/json.hpp>
3+
#include <string>
4+
#include <variant>
5+
6+
#include "write_and_read.hpp"
7+
8+
namespace test_variant {
9+
10+
struct Circle {
11+
double radius;
12+
};
13+
14+
struct Rectangle {
15+
double height;
16+
double width;
17+
};
18+
19+
struct Square {
20+
double width;
21+
};
22+
23+
using Shapes = std::variant<Circle, Rectangle, Square>;
24+
25+
struct Drawing {
26+
Shapes shape;
27+
};
28+
29+
TEST(boost_serialization, test_variant) {
30+
const auto drawing = Drawing{.shape = Circle{.radius = 5.0}};
31+
32+
write_and_read(drawing);
33+
}
34+
} // namespace test_variant
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef WRITE_AND_READ_
2+
#define WRITE_AND_READ_
3+
4+
#include <gtest/gtest.h>
5+
6+
#include <rfl/boost_serialization.hpp>
7+
#include <rfl/json.hpp>
8+
#include <string>
9+
10+
template <class... Ps>
11+
void write_and_read(const auto& _struct) {
12+
using T = std::remove_cvref_t<decltype(_struct)>;
13+
const auto serialized1 = rfl::boost_serialization::write<Ps...>(_struct);
14+
const auto res = rfl::boost_serialization::read<T, Ps...>(serialized1);
15+
EXPECT_TRUE(res && true) << "Test failed on read. Error: "
16+
<< res.error().what();
17+
const auto serialized2 = rfl::boost_serialization::write<Ps...>(res.value());
18+
EXPECT_EQ(serialized1, serialized2);
19+
}
20+
21+
template <class... Ps>
22+
void write_and_read_with_json(const auto& _struct) {
23+
using T = std::remove_cvref_t<decltype(_struct)>;
24+
const auto serialized1 = rfl::boost_serialization::write<Ps...>(_struct);
25+
const auto res = rfl::boost_serialization::read<T, Ps...>(serialized1);
26+
EXPECT_TRUE(res && true) << "Test failed on read. Error: "
27+
<< res.error().what();
28+
const auto serialized2 = rfl::boost_serialization::write<Ps...>(res.value());
29+
EXPECT_EQ(serialized1, serialized2);
30+
EXPECT_EQ(rfl::json::write<Ps...>(_struct),
31+
rfl::json::write<Ps...>(res.value()));
32+
}
33+
#endif

0 commit comments

Comments
 (0)