Debian Codegraph @

hyprtester/src/shared.hpp

raw
1 #pragma once
2 #include "Log.hpp"
3 #include "tests/shared.hpp"
4
5 #include <cmath>
6 #include <map>
7 #include <memory>
8 #include <string>
9
10 // TODO: localize these global variables
11 inline std::string HIS = "";
12 inline std::string WLDISPLAY = "";
13
14 // =================================
15 // TEST CASES DEFINITION
16 // =================================
17
18 class CTestCase {
19 public:
20 CTestCase() = default;
21 CTestCase(const CTestCase&) = delete; // Test cases probably should not be copied
22 virtual ~CTestCase() = default;
23
24 /// Indicates that some check has failed
25 bool failed = false;
26 /// Indicates that the _last_ check has failed
27 bool just_failed = false;
28
29 /// Run the test. The test result will be stored in `.failed`.
30 virtual void test() = 0;
31
32 // TODO: provide an adequate API. For instance, make all the members above private/protected,
33 // and expose a method that will return a bool indicating test success.
34
35 /// Test name, as defined in the source file
36 virtual std::string name() const = 0;
37
38 /// Name of the source file where the test is defined
39 virtual std::string filename() const = 0;
40
41 /// Test group name (defined in tests/*/tests.hpp files)
42 virtual std::string groupName() const = 0;
43 };
44
45 // Methods in the generated definition are marked with `[[maybe_unused]]` to suppress warnings.
46 // That's because:
47 // 1. As of this writing, some are indeed unused. They are there for future convenience.
48 // 2. The rest are only used behind a `std::shared_ptr` but my compiler does not detect it.
49 #define TEST_CASE(NAME) \
50 namespace { \
51 class TestCase_##NAME : public CTestCase { \
52 public: \
53 void test() override; \
54 std::string name() const override; \
55 std::string filename() const override; \
56 std::string groupName() const override; \
57 }; \
58 } \
59 \
60 static auto test_case_##NAME = std::make_shared<TestCase_##NAME>(); \
61 static auto register_test_case_##NAME = [] { \
62 /* Common test storage used when running selected tests (declared below) */ \
63 testCases.emplace(#NAME, test_case_##NAME); \
64 /* Group-specific test storage used when running all tests (declared by our includer) */ \
65 GROUP_TEST_CASE_STORAGE.push_back(test_case_##NAME); \
66 return 1; \
67 }(); \
68 \
69 [[maybe_unused]] \
70 std::string TestCase_##NAME::name() const { \
71 return #NAME; \
72 } \
73 \
74 [[maybe_unused]] \
75 std::string TestCase_##NAME::filename() const { \
76 return __FILE__; \
77 } \
78 \
79 [[maybe_unused]] \
80 std::string TestCase_##NAME::groupName() const { \
81 /* Defined by our includer */ \
82 return TEST_GROUP_NAME; \
83 } \
84 \
85 [[maybe_unused]] \
86 void TestCase_##NAME::test()
87
88 #define SUBTEST(NAME, ...) \
89 namespace { \
90 class Subtest_##NAME { \
91 public: \
92 bool failed = false; \
93 bool just_failed = false; \
94 \
95 void main(__VA_ARGS__); \
96 }; \
97 } \
98 \
99 void Subtest_##NAME::main(__VA_ARGS__)
100
101 #define CALL_SUBTEST(NAME, ...) \
102 do { \
103 auto subtest_##NAME = Subtest_##NAME{}; \
104 subtest_##NAME.main(__VA_ARGS__); \
105 if (subtest_##NAME.failed) { \
106 FAIL_TEST("Subtest {}({}) failed", #NAME, #__VA_ARGS__); \
107 } else { \
108 LOG_OK("Subtest {}({})", #NAME, #__VA_ARGS__); \
109 } \
110 } while (0)
111
112 // =================================
113 // DECLARAITIONS USED BY TESTS
114 // =================================
115
116 /// Stores all test cases regardless of their place of definition.
117 inline std::map<std::string, std::shared_ptr<CTestCase>> testCases;
118
119 // =================================
120 // IN-TEST MACROS
121 // =================================
122
123 /// Marks the test as failed without terminating it
124 #define MARK_TEST_FAILED_SILENT() this->just_failed = this->failed = true
125
126 /// Prints a failure message and makrs the test as failed without terminating it
127 #define MARK_TEST_FAILED(fmt, ...) \
128 do { \
129 NLog::red("Failed:{} " fmt ". Source: {}@{}.", Colors::RESET __VA_OPT__(, ) __VA_ARGS__, __FILE__, __LINE__); \
130 MARK_TEST_FAILED_SILENT(); \
131 } while (0)
132
133 /// Terminates the test execution and marks it as failed
134 #define FAIL_TEST_SILENT() \
135 do { \
136 MARK_TEST_FAILED_SILENT(); \
137 return; \
138 } while (0)
139
140 /// Prints a failure message, terminates the test execution, and marks it as failed
141 #define FAIL_TEST(fmt, ...) \
142 do { \
143 MARK_TEST_FAILED(fmt, __VA_ARGS__); \
144 return; \
145 } while (0)
146
147 #define LOG_OK(fmt, ...) \
148 do { \
149 this->just_failed = false; \
150 NLog::green("OK:{} " fmt ". Source: {}@{}", Colors::RESET, __VA_ARGS__, __FILE__, __LINE__); \
151 } while (0)
152
153 // In case of failure:
154 // - All the `EXPECT*` macros will print a message and mark the test as failed without terminating its execution;
155 // - All the `ASSERT*` macros will print a message, mark the test as failed, and terminate it.
156 //
157 // `OK` acts like `ASSERT_OK`.
158
159 #define EXPECT_MAX_DELTA(expr, desired, delta) \
160 if (const auto RESULT = expr; std::abs(RESULT - (desired)) > delta) { \
161 MARK_TEST_FAILED("{}, expected max delta of {}, got delta {} ({} - {})", #expr, delta, (RESULT - (desired)), RESULT, desired); \
162 } else { \
163 LOG_OK("{}. Got {}", #expr, (RESULT - (desired))); \
164 }
165
166 #define EXPECT(expr, val) \
167 do { \
168 if (const auto RESULT = expr; RESULT != (val)) { \
169 MARK_TEST_FAILED("{}, expected {}, got {}", #expr, val, RESULT); \
170 } else { \
171 LOG_OK("{}. Got {}", #expr, val); \
172 } \
173 } while (0)
174
175 #define EXPECT_NOT(expr, val) \
176 if (const auto RESULT = expr; RESULT == (val)) { \
177 MARK_TEST_FAILED("{}, expected not {}, got {}", #expr, val, RESULT); \
178 } else { \
179 LOG_OK("{}. Got {}", #expr, val); \
180 }
181
182 #define EXPECT_VECTOR2D(expr, val) \
183 do { \
184 const auto& RESULT = expr; \
185 const auto& ASSERTED = val; \
186 if (!(std::abs(RESULT.x - ASSERTED.x) < 1e-6 && std::abs(RESULT.y - ASSERTED.y) < 1e-6)) { \
187 MARK_TEST_FAILED("{}, expected [{}, {}], got [{}, {}]", #expr, ASSERTED.x, ASSERTED.y, RESULT.x, RESULT.y); \
188 } else { \
189 LOG_OK("{}. Got [{}, {}].", #expr, RESULT.x, RESULT.y); \
190 } \
191 } while (0)
192
193 // String check macros below use a special error message layout, putting the Source reference before `haystack`,
194 // since `haystack` may be a large multi-line string. Thus, they use bare `NLog::log` + `MARK_TEST_FAILED_SILENT`.
195
196 #define EXPECT_CONTAINS(haystack, needle) \
197 if (const auto ASSERTED = needle; !std::string{haystack}.contains(ASSERTED)) { \
198 NLog::red("Failed: {}{} should contain {} but doesn't. Source: {}@{}. Haystack is:\n{}", Colors::RESET, #haystack, #needle, __FILE__, __LINE__, std::string{haystack}); \
199 MARK_TEST_FAILED_SILENT(); \
200 } else { \
201 LOG_OK("{} contains {}.", #haystack, #needle); \
202 }
203
204 #define EXPECT_NOT_CONTAINS(haystack, needle) \
205 if (std::string{haystack}.contains(needle)) { \
206 NLog::red("Failed: {}{} shouldn't contain {} but does. Source: {}@{}. Haystack is:\n{}", Colors::RESET, #haystack, #needle, __FILE__, __LINE__, std::string{haystack}); \
207 MARK_TEST_FAILED_SILENT(); \
208 } else { \
209 LOG_OK("{} doesn't contain {}.", #haystack, #needle); \
210 }
211
212 #define EXPECT_STARTS_WITH(str, what) \
213 if (!std::string{str}.starts_with(what)) { \
214 NLog::red("Failed: {}{} should start with {} but doesn't. Source: {}@{}. String is:\n{}", Colors::RESET, #str, #what, __FILE__, __LINE__, std::string{str}); \
215 MARK_TEST_FAILED_SILENT(); \
216 } else { \
217 LOG_OK("{} starts with {}.", #str, #what); \
218 }
219
220 #define EXPECT_COUNT_STRING(str, what, no) \
221 if (Tests::countOccurrences(str, what) != no) { \
222 NLog::red("Failed: {}{} should contain {} {} times, but doesn't. Source: {}@{}. String is:\n{}", Colors::RESET, #str, #what, no, __FILE__, __LINE__, std::string{str}); \
223 MARK_TEST_FAILED_SILENT(); \
224 } else { \
225 LOG_OK("{} contains {} {} times.", #str, #what, no); \
226 }
227
228 #define EXPECT_OK(x) EXPECT(x, "ok")
229
230 #define ASSERT_MAX_DELTA(expr, desired, delta) \
231 do { \
232 EXPECT_MAX_DELTA(expr, desired, delta); \
233 if (this->just_failed) \
234 return; \
235 } while (0)
236
237 #define ASSERT(expr, val) \
238 do { \
239 EXPECT(expr, val); \
240 if (this->just_failed) \
241 return; \
242 } while (0)
243
244 #define ASSERT_NOT(expr, val) \
245 do { \
246 EXPECT_NOT(expr, val); \
247 if (this->just_failed) \
248 return; \
249 } while (0)
250
251 #define ASSERT_VECTOR2D(expr, val) \
252 do { \
253 EXPECT_VECTOR2D(expr, val); \
254 if (this->just_failed) \
255 return; \
256 } while (0)
257
258 #define ASSERT_CONTAINS(haystack, needle) \
259 do { \
260 EXPECT_CONTAINS(haystack, needle); \
261 if (this->just_failed) \
262 return; \
263 } while (0)
264
265 #define ASSERT_NOT_CONTAINS(haystack, needle) \
266 do { \
267 EXPECT_NOT_CONTAINS(haystack, needle); \
268 if (this->just_failed) \
269 return; \
270 } while (0)
271
272 #define ASSERT_STARTS_WITH(str, what) \
273 do { \
274 EXPECT_STARTS_WITH(str, what); \
275 if (this->just_failed) \
276 return; \
277 } while (0)
278
279 #define ASSERT_COUNT_STRING(str, what, no) \
280 do { \
281 EXPECT_COUNT_STRING(str, what, no); \
282 if (this->just_failed) \
283 return; \
284 } while (0)
285
286 #define SPAWN_KITTY(class_, ...) \
287 do { \
288 if (!Tests::spawnKitty(class_ __VA_OPT__(, ) __VA_ARGS__)) \
289 FAIL_TEST("Could not spawn kitty with class: {}", class_); \
290 } while (0)
291
292 #define SPAWN_LAYER_KITTY(class_, ...) \
293 do { \
294 if (!Tests::spawnLayerKitty(class_ __VA_OPT__(, ) __VA_ARGS__)) \
295 FAIL_TEST("Could not spawn layer kitty with class: {}", class_); \
296 } while (0)
297
298 #define OK(x) ASSERT(x, "ok")
299 #define NOK(x) ASSERT_NOT(x, "ok")