-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
238 lines (191 loc) · 6.26 KB
/
tests.cpp
File metadata and controls
238 lines (191 loc) · 6.26 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <string>
#include <variant>
#include "reflection.h"
#include "serializer.h"
#include <gtest/gtest.h>
class ReflectionSystemTest: public ::testing::Test
{
public:
class Base
{
int baseAttr = 5;
public:
int getAttr() { return baseAttr; }
};
class TestClass: public Base
{
public:
int attr1 = 0;
static const int attr2;
TestClass() = default;
TestClass(int attr) : attr1(attr) {}
void method1() { attr1 = 1; }
int method2() { return attr1; }
UseReflectionTrait
(
TestClass,
Attributes(attr1, attr2),
Methods(method1, method2),
Parents(Base)
)
};
void SetUp() override {
obj = new TestClass();
}
void TearDown() override {
delete obj;
}
TestClass* obj;
};
const int ReflectionSystemTest::TestClass::attr2 = 42;
// class methods' tests
TEST_F(ReflectionSystemTest, GetInstanceDefault)
{
TestClass instance = TestClass::GetInstance();
EXPECT_EQ(instance.attr1, 0);
}
TEST_F(ReflectionSystemTest, GetInstanceWithParams)
{
TestClass instance = TestClass::GetInstance(5);
EXPECT_EQ(instance.attr1, 5);
}
TEST_F(ReflectionSystemTest, Classname)
{
EXPECT_EQ(TestClass::Classname(), "ReflectionSystemTest::TestClass");
}
TEST_F(ReflectionSystemTest, Size)
{
EXPECT_EQ(TestClass::Size(), sizeof(TestClass));
}
TEST_F(ReflectionSystemTest, GetMethod)
{
auto method2 = std::get<int(TestClass::*)()>(obj->GetMethod("method2"));
EXPECT_EQ((obj->*method2)(), 0);
auto method1 = obj->GetMethod<void(TestClass::*)()>("method1");
EXPECT_NO_THROW((obj->*method1)());
EXPECT_EQ((obj->*method2)(), 1);
}
TEST_F(ReflectionSystemTest, GetAttribute)
{
auto attr = std::get<int TestClass::*>(obj->GetAttribute("attr1"));
EXPECT_EQ(obj->*attr, 0);
obj->attr1 = 5;
EXPECT_EQ(obj->*(obj->GetAttribute<int TestClass::*>("attr1")), 5);
}
TEST_F(ReflectionSystemTest, HasMethod)
{
EXPECT_TRUE(obj->HasMethod("method1"));
EXPECT_FALSE(obj->HasMethod("false_method"));
}
TEST_F(ReflectionSystemTest, HasAttribute)
{
EXPECT_TRUE(obj->HasAttribute("attr1"));
EXPECT_FALSE(obj->HasAttribute("false_attr"));
}
TEST_F(ReflectionSystemTest, GetMethods)
{
auto methods = obj->GetMethods();
EXPECT_EQ(methods.size(), 2);
}
TEST_F(ReflectionSystemTest, GetAttributes)
{
auto attributes = obj->GetAttributes();
EXPECT_EQ(attributes.size(), 2);
}
// namespace functions' tests
TEST(ReflectionSystemFunctions, Clear)
{
std::string token = "example __ptr64";
std::vector<std::string> substrs = { "__ptr64" };
EXPECT_EQ(reflection::Clear(token, substrs), "example ");
}
TEST(ReflectionSystemFunctions, Normalize)
{
std::string name = "example";
std::string sign = "void example() __cdecl";
EXPECT_EQ(reflection::Normalize(name, sign), "example: void example() ");
}
TEST(ReflectionSystemFunctions, HashCode)
{
const char* key = "example";
EXPECT_EQ(reflection::HashCode(key), 267647114368);
const std::string key_str = key;
EXPECT_EQ(reflection::HashCode(key_str), 267647114368);
}
TEST(ReflectionSystemFunctions, instance_of)
{
EXPECT_TRUE((reflection::instance_of<ReflectionSystemTest::Base, ReflectionSystemTest::TestClass>));
EXPECT_FALSE((reflection::instance_of<ReflectionSystemTest, ReflectionSystemTest::Base>));
}
TEST(ReflectionSystemFunctions, is_function)
{
EXPECT_TRUE(reflection::is_function<decltype(&ReflectionSystemTest::TestClass::method1)>);
EXPECT_FALSE(reflection::is_function<decltype(&ReflectionSystemTest::TestClass::attr2)>);
}
TEST(ReflectionSystemFunctions, is_attribute)
{
EXPECT_TRUE(reflection::is_attribute<decltype(&ReflectionSystemTest::TestClass::attr2)>);
EXPECT_FALSE(reflection::is_attribute<decltype(&ReflectionSystemTest::TestClass::method1)>);
}
TEST(ReflectionSystemFunctions, get_variant_index)
{
using type = std::variant<int, char, std::string, long long, double>;
size_t index = reflection::get_variant_index<char, type>;
EXPECT_EQ(index, 1);
index = reflection::get_variant_index<double, type>;
EXPECT_EQ(index, 4);
index = reflection::get_variant_index<float, type>;
EXPECT_EQ(index, std::variant_npos);
}
TEST(ReflectionSystemFunctions, holds_variant_option)
{
using type = std::variant<int, char, std::string, long long, double>;
bool condition = reflection::holds_variant_option<char, type>;
EXPECT_TRUE(condition);
condition = reflection::holds_variant_option<float, type>;
EXPECT_FALSE(condition);
}
TEST(ReflectionSystemFunctions, remove_class_pointer)
{
using type = decltype(&ReflectionSystemTest::TestClass::attr1);
bool condition = std::is_same_v<int,
reflection::remove_class_pointer_t<
decltype(&ReflectionSystemTest::TestClass::attr1),
ReflectionSystemTest::TestClass
>>;
ASSERT_TRUE(condition);
}
// serializer tests
TEST_F(ReflectionSystemTest, ToJson)
{
reflection::Serializer serializer;
Json::Value json = serializer.ToJson<ReflectionSystemTest::TestClass>();
EXPECT_EQ(json["name"].asString(), "ReflectionSystemTest::TestClass");
EXPECT_EQ(json["size"].asInt(), sizeof(TestClass));
EXPECT_EQ(json["parents"].size(), 1);
}
TEST_F(ReflectionSystemTest, ToYaml)
{
reflection::Serializer serializer;
YAML::Node yaml = serializer.ToYaml<ReflectionSystemTest::TestClass>();
EXPECT_EQ(yaml["name"].as<std::string>(), "ReflectionSystemTest::TestClass");
EXPECT_EQ(yaml["size"].as<int>(), sizeof(TestClass));
EXPECT_EQ(yaml["parents"].size(), 1);
}
TEST_F(ReflectionSystemTest, ToString)
{
reflection::Serializer serializer;
std::string str = serializer.ToString<ReflectionSystemTest::TestClass>();
EXPECT_FALSE(str.empty());
EXPECT_TRUE(str.find("ReflectionSystemTest::TestClass") != std::string::npos);
}
TEST_F(ReflectionSystemTest, is_serializable)
{
EXPECT_TRUE(reflection::is_serializable<ReflectionSystemTest::TestClass>);
EXPECT_FALSE(reflection::is_serializable<ReflectionSystemTest>);
}
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}