-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_types_reflection.cpp
More file actions
104 lines (81 loc) · 2.82 KB
/
Copy pathtest_types_reflection.cpp
File metadata and controls
104 lines (81 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "common.hpp"
#include <vide/types/reflection.hpp>
TEST_SUITE_BEGIN("types_reflection");
// -------------------------------------------------------------------------------------------------
struct is_odd {
template <typename T>
static inline void operator()(const T& var) {
if (var % 2 == 0)
throw vide::Exception("Is-odd validation failed during serialization: object is even.");
}
};
// -------------------------------------------------------------------------------------------------
struct TestType0_empty {
using serialize_enable_reflection = void;
bool operator==(const TestType0_empty&) const = default;
};
struct TestType1 {
using serialize_enable_reflection = void;
int a = 1;
bool operator==(const TestType1&) const = default;
};
struct TestType2 {
using serialize_enable_reflection = void;
int a = 2;
int b = 3;
int c = 4;
bool operator==(const TestType2&) const = default;
};
struct TestType3_nested {
using serialize_enable_reflection = void;
TestType0_empty a;
TestType1 b;
TestType2 c;
bool operator==(const TestType3_nested&) const = default;
};
struct TestType4_nested_versioned {
static constexpr uint32_t serialize_class_version = 100;
using serialize_enable_reflection = void;
TestType0_empty a;
TestType1 b;
TestType2 c;
bool operator==(const TestType4_nested_versioned&) const = default;
};
struct TestType5_validator {
using serialize_enable_reflection = void;
[[=vide::notnull]]
[[=is_odd{}]]
int a = 1;
bool operator==(const TestType5_validator&) const = default;
};
struct TestType6_validator_with_unrelated {
using serialize_enable_reflection = void;
[[=3]] // Unrelated
[[=vide::notnull]]
// [[= [](int x) {
// std::cout << "Validating: " << x << std::endl;
// if (x == 1) throw vide::Exception{"Its one!"};
// } ]]
int a = 1;
bool operator==(const TestType6_validator_with_unrelated&) const = default;
};
// -------------------------------------------------------------------------------------------------
template <typename IArchive, typename OArchive>
void test_types_reflection() {
SaveLoadTester<IArchive, OArchive> test{};
CHECK(test(TestType0_empty{}));
CHECK(test(TestType1{}));
CHECK(test(TestType2{}));
CHECK(test(TestType3_nested{}));
CHECK(test(TestType4_nested_versioned{}));
CHECK(test(TestType5_validator{1}));
CHECK_THROWS_AS(test(TestType5_validator{0}), vide::Exception); // notnull fails
CHECK_THROWS_AS(test(TestType5_validator{2}), vide::Exception); // is_odd fails
CHECK(test(TestType6_validator_with_unrelated{2}));
// CHECK_THROWS_AS(test(TestType6_validator_with_unrelated{1}), vide::Exception); // lambda fails
CHECK_THROWS_AS(test(TestType6_validator_with_unrelated{0}), vide::Exception); // notnull fails
}
CREATE_TEST_CASES_FOR_ALL_ARCHIVE("types_reflection", test_types_reflection)
TEST_SUITE_END();