Debian Codegraph @

src/unittest/test.h

raw symbols SCIP
1 /************************************************************************************
2 *
3 * D++, A Lightweight C++ library for Discord
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 * Copyright 2021 Craig Edwards and D++ contributors
7 * (https://github.com/brainboxdotcc/DPP/graphs/contributors)
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 ************************************************************************************/
22 #undef DPP_BUILD
23 #ifdef _WIN32
24 _Pragma("warning( disable : 4251 )"); // 4251 warns when we export classes or structures with stl member variables
25 _Pragma("warning( disable : 5105 )"); // 4251 warns when we export classes or structures with stl member variables
26 #endif
27 #include <dpp/dpp.h>
28 #include <dpp/json_fwd.h>
29 #include <iomanip>
30 #include <type_traits>
31
32 #ifdef _WIN32
33 #define SHARED_OBJECT "dpp.dll"
34 #elif __APPLE__
35 #define SHARED_OBJECT "libdpp.dylib"
36 #else
37 #define SHARED_OBJECT "libdpp.so"
38 #endif
39
40 using json = nlohmann::json;
41
42 extern std::mutex loglock;
43
44 enum test_flags_t {
45 tf_offline = 0,
46 /* A test that requires discord connectivity */
47 tf_online = 1,
48 /* A test that requires full tests to be enabled */
49 tf_extended = 1 << 1,
50 /* A test that requires coro to be enabled */
51 tf_coro = 1 << 2
52 };
53
54 enum test_status_t {
55 /* Test was not executed */
56 ts_not_executed = 0,
57 /* Test was started */
58 ts_started,
59 /* Test was skipped */
60 ts_skipped,
61 /* Test succeeded */
62 ts_success,
63 /* Test failed */
64 ts_failed
65 };
66
67 struct test_t;
68
69 inline std::vector<test_t *> tests = {};
70
71 /* Represents a test case */
72 struct test_t {
73 std::string_view name;
74 /* Description of test */
75 std::string_view description;
76 /* Test type */
77 test_flags_t flags;
78 /* Test status */
79 test_status_t status = ts_not_executed;
80
81 test_t(std::string_view testname, std::string_view testdesc, int testflags);
82 };
83
84 #define DPP_TEST(name, desc, flags) inline test_t name = {#name, desc, flags}
85
86 /* Current list of unit tests */
87 DPP_TEST(SNOWFLAKE, "dpp::snowflake class", tf_offline);
88 DPP_TEST(BIGNUM, "dpp::bignumber decimal to raw buffer", tf_offline);
89 DPP_TEST(BIGNUM2, "dpp::bignumber raw buffer to hex", tf_offline);
90 DPP_TEST(BIGNUM3, "dpp::bignumber to_binary()", tf_offline);
91 DPP_TEST(JSON_INTERFACE, "dpp::json_interface class", tf_offline);
92 DPP_TEST(CLUSTER, "Instantiate DPP cluster", tf_offline);
93 DPP_TEST(BOTSTART, "cluster::start method", tf_online);
94 DPP_TEST(CONNECTION, "Connection to client websocket", tf_online);
95 DPP_TEST(APPCOMMAND, "Creation of application command", tf_online);
96 DPP_TEST(DELCOMMAND, "Deletion of application command", tf_online);
97 DPP_TEST(GETAPP, "cluster::current_application_get()", tf_online);
98 DPP_TEST(LOGGER, "Log events", tf_online);
99 DPP_TEST(MESSAGECREATE, "Creation of a channel message", tf_online);
100 DPP_TEST(MESSAGEEDIT, "Editing a channel message", tf_online);
101 DPP_TEST(EDITEVENT, "Message edit event", tf_online);
102 DPP_TEST(MESSAGEDELETE, "Deletion of a channel message", tf_online);
103 DPP_TEST(MESSAGERECEIVE, "Receipt of a created message", tf_online);
104 DPP_TEST(MESSAGEFILE, "Message attachment send and check", tf_online);
105 DPP_TEST(CACHE, "Test guild cache", tf_online);
106 DPP_TEST(USERCACHE, "Test user cache", tf_online);
107 DPP_TEST(REACT, "React to a message", tf_online);
108 DPP_TEST(REACTEVENT, "Reaction event", tf_online);
109 DPP_TEST(GUILDCREATE, "Receive guild create event", tf_online);
110 DPP_TEST(MESSAGESGET, "Get messages", tf_online);
111 DPP_TEST(TIMESTAMP, "crossplatform_strptime()", tf_online);
112 DPP_TEST(ICONHASH, "utility::iconhash", tf_offline);
113 DPP_TEST(CURRENTUSER, "cluster::current_user_get()", tf_online);
114 DPP_TEST(GETGUILD, "cluster::guild_get()", tf_online);
115 DPP_TEST(GETCHAN, "cluster::channel_get()", tf_online);
116 DPP_TEST(GETCHANS, "cluster::channels_get()", tf_online);
117 DPP_TEST(GETCHANNELINVS, "cluster::channel_invites_get()", tf_online);
118 DPP_TEST(GETROLES, "cluster::roles_get()", tf_online);
119 DPP_TEST(GETGUILDINVS, "cluster::guild_get_invites()", tf_online);
120 DPP_TEST(GETBANS, "cluster::guild_get_bans()", tf_online);
121 DPP_TEST(GETPINS, "cluster::channel_pins_get()", tf_online);
122 DPP_TEST(GETEVENTS, "cluster::guild_events_get()", tf_online);
123 DPP_TEST(GETEVENT, "cluster::guild_event_get()", tf_online);
124 DPP_TEST(MSGCREATESEND, "message_create_t::send()", tf_online);
125 DPP_TEST(GETEVENTUSERS, "cluster::guild_event_users_get()", tf_online);
126 DPP_TEST(TIMERSTART, "start timer", tf_online);
127 DPP_TEST(TIMERSTOP, "stop timer", tf_online);
128 DPP_TEST(ONESHOT, "one-shot timer", tf_online);
129 DPP_TEST(TIMEDLISTENER, "timed listener", tf_online);
130 DPP_TEST(PRESENCE, "Presence intent", tf_online);
131 DPP_TEST(CUSTOMCACHE, "Instantiate a cache", tf_offline);
132 DPP_TEST(MSGCOLLECT, "message_collector", tf_online);
133 DPP_TEST(TS, "managed::get_creation_date()", tf_online);
134 DPP_TEST(READFILE, "utility::read_file()", tf_offline);
135 DPP_TEST(TIMESTAMPTOSTRING, "ts_to_string()", tf_offline);
136 DPP_TEST(TIMESTRINGTOTIMESTAMP, "ts_not_null()", tf_offline);
137 DPP_TEST(OPTCHOICE_DOUBLE, "command_option_choice::fill_from_json: double", tf_offline);
138 DPP_TEST(OPTCHOICE_INT, "command_option_choice::fill_from_json: int64_t", tf_offline);
139 DPP_TEST(OPTCHOICE_BOOL, "command_option_choice::fill_from_json: bool", tf_offline);
140 DPP_TEST(OPTCHOICE_SNOWFLAKE, "command_option_choice::fill_from_json: snowflake", tf_offline);
141 DPP_TEST(OPTCHOICE_STRING, "command_option_choice::fill_from_json: string", tf_offline);
142 DPP_TEST(HOSTINFO, "https_client::get_host_info()", tf_offline);
143 DPP_TEST(HTTPS, "https_client HTTPS request", tf_online);
144 DPP_TEST(HTTP, "https_client HTTP request", tf_online);
145 DPP_TEST(RUNONCE, "run_once<T>", tf_offline);
146 DPP_TEST(WEBHOOK, "webhook construct from URL", tf_offline);
147 DPP_TEST(MD_ESC_1, "Markdown escaping (ignore code block contents)", tf_offline);
148 DPP_TEST(MD_ESC_2, "Markdown escaping (escape code block contents)", tf_offline);
149 DPP_TEST(URLENC, "URL encoding", tf_offline);
150 DPP_TEST(BASE64ENC, "Base 64 encoding", tf_offline);
151 DPP_TEST(COMPARISON, "manged object comparison", tf_offline);
152 DPP_TEST(CHANNELCACHE, "find_channel()", tf_online);
153 DPP_TEST(CHANNELTYPES, "channel type flags", tf_online);
154 DPP_TEST(FORUM_CREATION, "create a forum channel", tf_online);
155 DPP_TEST(FORUM_CHANNEL_GET, "retrieve the created forum channel", tf_online);
156 DPP_TEST(FORUM_CHANNEL_DELETE, "delete the created forum channel", tf_online);
157 DPP_TEST(ERRORS, "Human readable error translation", tf_offline);
158 DPP_TEST(INVALIDUTF8, "Invalid UTF-8 handling", tf_online);
159
160 DPP_TEST(GUILD_EDIT, "cluster::guild_edit", tf_online);
161 DPP_TEST(GUILD_BAN_CREATE, "cluster::guild_ban_add ban three well-known discord accounts", tf_online);
162 DPP_TEST(GUILD_BAN_GET, "cluster::guild_get_ban getting one of the banned accounts", tf_online);
163 DPP_TEST(GUILD_BANS_GET, "cluster::guild_get_bans get bans using the after-parameter", tf_online);
164 DPP_TEST(GUILD_BAN_DELETE, "cluster::guild_ban_delete unban the banned discord accounts", tf_online);
165
166 DPP_TEST(THREAD_CREATE, "cluster::thread_create", tf_online);
167 DPP_TEST(THREAD_CREATE_EVENT, "cluster::on_thread_create event", tf_online);
168 DPP_TEST(THREAD_DELETE, "cluster::channel_delete with thread", tf_online);
169 DPP_TEST(THREAD_DELETE_EVENT, "cluster::on_thread_delete event", tf_online);
170 DPP_TEST(THREAD_EDIT, "cluster::thread_edit", tf_online);
171 DPP_TEST(THREAD_UPDATE_EVENT, "cluster::on_thread_update event", tf_online);
172 DPP_TEST(THREAD_GET_ACTIVE, "cluster::threads_get_active", tf_online);
173
174 DPP_TEST(VOICE_CHANNEL_CREATE, "creating a voice channel", tf_online);
175 DPP_TEST(VOICE_CHANNEL_EDIT, "editing the created voice channel", tf_online);
176 DPP_TEST(VOICE_CHANNEL_DELETE, "deleting the created voice channel", tf_online);
177
178 DPP_TEST(PERMISSION_CLASS, "permission", tf_offline);
179 DPP_TEST(EVENT_CLASS, "event class", tf_offline);
180 DPP_TEST(USER_GET, "cluster::user_get", tf_online);
181 DPP_TEST(USER_GET_FLAGS, "cluster::user_get flag parsing", tf_online);
182 DPP_TEST(MEMBER_GET, "cluster::guild_get_member", tf_online);
183 DPP_TEST(USER_GET_MENTION, "user::get_mention", tf_offline);
184 DPP_TEST(USER_GET_URL, "user::get_url", tf_offline);
185 DPP_TEST(USER_FORMAT_USERNAME, "user::format_username", tf_offline);
186 DPP_TEST(USER_GET_CREATION_TIME, "user::get_creation_time", tf_offline);
187 DPP_TEST(USER_GET_AVATAR_URL, "user::get_avatar_url", tf_offline);
188 DPP_TEST(CHANNEL_SET_TYPE, "channel::set_type", tf_offline);
189 DPP_TEST(CHANNEL_GET_MENTION, "channel::get_mention", tf_offline);
190 DPP_TEST(CHANNEL_GET_URL, "channel::get_url", tf_offline);
191 DPP_TEST(MESSAGE_GET_URL,"message::get_url",tf_offline);
192 DPP_TEST(UTILITY_GUILD_NAVIGATION, "utility::guild_navigation", tf_offline);
193 DPP_TEST(UTILITY_ICONHASH, "utility::iconhash", tf_offline);
194 DPP_TEST(UTILITY_MAKE_URL_PARAMETERS, "utility::make_url_parameters", tf_offline);
195 DPP_TEST(UTILITY_MARKDOWN_ESCAPE, "utility::markdown_escape", tf_offline);
196 DPP_TEST(UTILITY_TOKENIZE, "utility::tokenize", tf_offline);
197 DPP_TEST(UTILITY_URL_ENCODE, "utility::url_encode", tf_offline);
198 DPP_TEST(UTILITY_SLASHCOMMAND_MENTION, "utility::slashcommand_mention", tf_offline);
199 DPP_TEST(UTILITY_CHANNEL_MENTION, "utility::channel_mention", tf_offline);
200 DPP_TEST(UTILITY_USER_MENTION, "utility::user_mention", tf_offline);
201 DPP_TEST(UTILITY_ROLE_MENTION, "utility::role_mention", tf_offline);
202 DPP_TEST(UTILITY_EMOJI_MENTION, "utility::emoji_mention", tf_offline);
203 DPP_TEST(UTILITY_USER_URL, "utility::user_url", tf_offline);
204 DPP_TEST(UTILITY_MESSAGE_URL, "utility::message_url", tf_offline);
205 DPP_TEST(UTILITY_CHANNEL_URL, "utility::channel_url", tf_offline);
206 DPP_TEST(UTILITY_THREAD_URL, "utility::thread_url", tf_offline);
207 DPP_TEST(UTILITY_AVATAR_SIZE, "utility::avatar_size", tf_offline);
208 DPP_TEST(UTILITY_CDN_ENDPOINT_URL_HASH, "utility::cdn_endpoint_url_hash", tf_offline);
209 DPP_TEST(STICKER_GET_URL, "sticker::get_url aka utility::cdn_endpoint_url_sticker", tf_offline);
210 DPP_TEST(EMOJI_GET_URL, "emoji::get_url", tf_offline);
211 DPP_TEST(ROLE_COMPARE, "role::operator<", tf_offline);
212 DPP_TEST(ROLE_COMPARE_CONSIDERING_ID, "role::operator<", tf_offline);
213 DPP_TEST(ROLE_CREATE, "cluster::role_create", tf_online);
214 DPP_TEST(ROLE_EDIT, "cluster::role_edit", tf_online);
215 DPP_TEST(ROLE_DELETE, "cluster::role_delete", tf_online);
216 DPP_TEST(JSON_PARSE_ERROR, "JSON parse error for post_rest", tf_online);
217 DPP_TEST(USER_GET_CACHED_PRESENT, "cluster::user_get_cached_sync() with present member", tf_online);
218 DPP_TEST(USER_GET_CACHED_ABSENT, "cluster::user_get_cached_sync() with not present member", tf_online);
219 DPP_TEST(GET_PARAMETER_WITH_SUBCOMMANDS, "interaction_create_t::get_parameter() with subcommands", tf_offline);
220 DPP_TEST(GET_PARAMETER_WITHOUT_SUBCOMMANDS, "interaction_create_t::get_parameter() without subcommands", tf_offline);
221 DPP_TEST(AUTOMOD_RULE_CREATE, "cluster::automod_rule_create", tf_online);
222 DPP_TEST(AUTOMOD_RULE_GET, "cluster::automod_rule_get", tf_online);
223 DPP_TEST(AUTOMOD_RULE_GET_ALL, "cluster::automod_rules_get", tf_online);
224 DPP_TEST(AUTOMOD_RULE_DELETE, "cluster::automod_rule_delete", tf_online);
225 DPP_TEST(REQUEST_GET_IMAGE, "using the cluster::request method to fetch an image", tf_online);
226 DPP_TEST(EMOJI_CREATE, "cluster::guild_emoji_create", tf_online);
227 DPP_TEST(EMOJI_GET, "cluster::guild_emoji_get", tf_online);
228 DPP_TEST(EMOJI_DELETE, "cluster::guild_emoji_delete", tf_online);
229 DPP_TEST(INVITE_CREATE_EVENT, "cluster::on_invite_create", tf_online);
230 DPP_TEST(INVITE_DELETE_EVENT, "cluster::on_invite_delete", tf_online);
231 DPP_TEST(INVITE_CREATE, "cluster::channel_invite_create", tf_online);
232 DPP_TEST(INVITE_GET, "cluster::invite_get", tf_online);
233 DPP_TEST(INVITE_DELETE, "cluster::invite_delete", tf_online);
234
235 /* Extended set -- Less important, skipped on the master branch due to rate limits and GitHub actions limitations */
236 /* To execute, run unittests with "full" command line argument */
237 DPP_TEST(MULTIHEADER, "multiheader cookie test", tf_online | tf_extended);
238
239 DPP_TEST(VOICECONN, "Connect to voice channel", tf_online | tf_extended);
240 DPP_TEST(VOICESEND, "Send audio to voice channel", tf_online | tf_extended); // udp unreliable on gitbub
241 DPP_TEST(MESSAGEPIN, "Pinning a channel message", tf_online | tf_extended);
242 DPP_TEST(MESSAGEUNPIN, "Unpinning a channel message", tf_online | tf_extended);
243
244 DPP_TEST(POLL_CREATE, "Creating a poll", tf_online);
245 DPP_TEST(POLL_END, "Ending a poll", tf_online);
246
247 DPP_TEST(THREAD_MEMBER_ADD, "cluster::thread_member_add", tf_online | tf_extended);
248 DPP_TEST(THREAD_MEMBER_GET, "cluster::thread_member_get", tf_online | tf_extended);
249 DPP_TEST(THREAD_MEMBERS_GET, "cluster::thread_members_get", tf_online | tf_extended);
250 DPP_TEST(THREAD_MEMBER_REMOVE, "cluster::thread_member_remove", tf_online | tf_extended);
251 DPP_TEST(THREAD_MEMBERS_ADD_EVENT, "cluster::on_thread_members_update event with member addition", tf_online | tf_extended);
252 DPP_TEST(THREAD_MEMBERS_REMOVE_EVENT, "cluster::on_thread_members_update event with member removal", tf_online | tf_extended);
253 DPP_TEST(THREAD_CREATE_MESSAGE, "cluster::thread_create_with_message", tf_online | tf_extended);
254
255 DPP_TEST(THREAD_MESSAGE, "message manipulation in thread", tf_online | tf_extended);
256 DPP_TEST(THREAD_MESSAGE_CREATE_EVENT, "cluster::on_message_create in thread", tf_online | tf_extended);
257 DPP_TEST(THREAD_MESSAGE_EDIT_EVENT, "cluster::on_message_edit in thread", tf_online | tf_extended);
258 DPP_TEST(THREAD_MESSAGE_DELETE_EVENT, "cluster::on_message_delete in thread", tf_online | tf_extended);
259 DPP_TEST(THREAD_MESSAGE_REACT_ADD_EVENT, "cluster::on_reaction_add in thread", tf_online | tf_extended);
260 DPP_TEST(THREAD_MESSAGE_REACT_REMOVE_EVENT, "cluster::on_reaction_remove in thread", tf_online | tf_extended);
261
262 DPP_TEST(CORO_JOB_OFFLINE, "coro: offline job", tf_offline | tf_coro);
263 DPP_TEST(CORO_AWAITABLE_OFFLINE, "coro: offline promise & awaitable", tf_offline | tf_coro);
264 DPP_TEST(CORO_COROUTINE_OFFLINE, "coro: offline coroutine", tf_offline | tf_coro);
265 DPP_TEST(CORO_TASK_OFFLINE, "coro: offline task", tf_offline | tf_coro);
266 DPP_TEST(CORO_ASYNC_OFFLINE, "coro: offline async", tf_offline | tf_coro);
267 DPP_TEST(CORO_EVENT_HANDLER, "coro: online event handler", tf_online | tf_coro);
268 DPP_TEST(CORO_API_CALLS, "coro: online api calls", tf_online | tf_coro);
269 DPP_TEST(CORO_MUMBO_JUMBO, "coro: online mumbo jumbo in event handler", tf_online | tf_coro | tf_extended);
270
271 DPP_TEST(SNOWFLAKE_STD_FORMAT, "snowflake: std::format support", tf_offline);
272
273 void coro_offline_tests();
274 void coro_online_tests(dpp::cluster *bot);
275
276 class test_cached_object_t : public dpp::managed {
277 public:
278 test_cached_object_t(dpp::snowflake _id) : dpp::managed(_id) { };
279 virtual ~test_cached_object_t() = default;
280 std::string foo;
281 };
282
283 /* How long the unit tests can run for */
284 const int64_t TEST_TIMEOUT = 60;
285
286 #define SAFE_GETENV(var) (getenv(var) && *(getenv(var)) ? getenv(var) : "0")
287
288 /* IDs of various channels and guilds used to test */
289 extern dpp::snowflake TEST_GUILD_ID;
290 extern dpp::snowflake TEST_TEXT_CHANNEL_ID;
291 extern dpp::snowflake TEST_VC_ID;
292 extern dpp::snowflake TEST_USER_ID;
293 extern dpp::snowflake TEST_EVENT_ID;
294
295 /* True if we skip tt_online tests */
296 extern bool offline;
297 /* True if we skip tt_extended tests */
298 extern bool extended;
299 #ifndef DPP_NO_CORO
300 inline constexpr bool coro = true;
301 #else
302 inline constexpr bool coro = false;
303 #endif
304
305 /**
306 * @brief Perform a test of a REST base API call with one parameter
307 */
308 #define singleparam_api_test(func_name, param, return_type, testname) \
309 set_test(testname, false); \
310 if (!offline) { \
311 bot.func_name (param, [&](const dpp::confirmation_callback_t &cc) { \
312 if (!cc.is_error()) { \
313 return_type g = std::get<return_type>(cc.value); \
314 if (g.id == param) { \
315 set_test(testname, true); \
316 } else { \
317 bot.log(dpp::ll_debug, cc.http_info.body); \
318 set_test(testname, false); \
319 } \
320 } else { \
321 bot.log(dpp::ll_debug, cc.http_info.body); \
322 set_test(testname, false); \
323 } \
324 }); \
325 }
326
327 /**
328 * @brief Perform a test of a REST base API call with one parameter
329 */
330 #define twoparam_api_test(func_name, param1, param2, return_type, testname) \
331 set_test(testname, false); \
332 if (!offline) { \
333 bot.func_name (param1, param2, [&](const dpp::confirmation_callback_t &cc) { \
334 if (!cc.is_error()) { \
335 return_type g = std::get<return_type>(cc.value); \
336 if (g.id > 0) { \
337 set_test(testname, true); \
338 } else { \
339 bot.log(dpp::ll_debug, cc.http_info.body); \
340 set_test(testname, false); \
341 } \
342 } else { \
343 bot.log(dpp::ll_debug, cc.http_info.body); \
344 set_test(testname, false); \
345 } \
346 }); \
347 }
348
349 /**
350 * @brief Perform a test of a REST base API call with one parameter that returns a list
351 */
352 #define singleparam_api_test_list(func_name, param, return_type, testname) \
353 set_test(testname, false); \
354 if (!offline) { \
355 bot.func_name (param, [&](const dpp::confirmation_callback_t &cc) { \
356 if (!cc.is_error()) { \
357 return_type g = std::get<return_type>(cc.value); \
358 if (g.size() > 0) { \
359 set_test(testname, true); \
360 } else { \
361 set_test(testname, false); \
362 bot.log(dpp::ll_debug, cc.http_info.body); \
363 } \
364 } else { \
365 set_test(testname, false); \
366 bot.log(dpp::ll_debug, cc.http_info.body); \
367 } \
368 }); \
369 }
370
371 /**
372 * @brief Perform a test of a REST base API call with one parameter that returns a list
373 */
374 #define multiparam_api_test_list(func_name, param, return_type, testname) \
375 set_test(testname, false); \
376 if (!offline) { \
377 bot.func_name (param, 0, 0, 1000, [&](const dpp::confirmation_callback_t &cc) { \
378 if (!cc.is_error()) { \
379 return_type g = std::get<return_type>(cc.value); \
380 if (g.size() > 0) { \
381 set_test(testname, true); \
382 } else { \
383 set_test(testname, false); \
384 bot.log(dpp::ll_debug, cc.http_info.body); \
385 } \
386 } else { \
387 set_test(testname, false); \
388 bot.log(dpp::ll_debug, cc.http_info.body); \
389 } \
390 }); \
391 }
392
393 /**
394 * @brief Perform a test of a REST base API call with two parameters
395 */
396 #define twoparam_api_test_list(func_name, param1, param2, return_type, testname) \
397 set_test(testname, false); \
398 if (!offline) { \
399 bot.func_name (param1, param2, [&](const dpp::confirmation_callback_t &cc) { \
400 if (!cc.is_error()) { \
401 return_type g = std::get<return_type>(cc.value); \
402 if (g.size() > 0) { \
403 set_test(testname, true); \
404 } else { \
405 bot.log(dpp::ll_debug, cc.http_info.body); \
406 set_test(testname, false); \
407 } \
408 } else { \
409 bot.log(dpp::ll_debug, cc.http_info.body); \
410 set_test(testname, false); \
411 } \
412 }); \
413 }
414
415
416 /**
417 * @brief Perform a test of a REST base API call with no parameters
418 */
419 #define noparam_api_test(func_name, return_type, testname) \
420 set_test(testname, false); \
421 if (!offline) { \
422 bot.func_name ([&](const dpp::confirmation_callback_t &cc) { \
423 if (!cc.is_error()) { \
424 return_type g = std::get<return_type>(cc.value); \
425 set_test(testname, true); \
426 } else { \
427 bot.log(dpp::ll_debug, cc.http_info.body); \
428 set_test(testname, false); \
429 } \
430 }); \
431 }
432
433 /**
434 * @brief Sets a test's status (legacy)
435 *
436 * @param test The test to set the status of
437 * @param success If set to true, sets success to true, if set to false and called
438 * once, sets executed to true, if called twice, also sets success to false.
439 * This means that before you run the test you should call this function once
440 * with success set to false, then if/wen the test completes call it again with true.
441 * If the test fails, call it a second time with false, or not at all.
442 */
443 void set_test(test_t &test, bool success = false);
444
445 /**
446 * @brief Sets a test's status
447 *
448 * @param test The test to set the status of
449 * @param status Status to set the test to
450 */
451 void set_status(test_t &test, test_status_t status, std::string_view message = {});
452
453 /**
454 * @brief Sets a test's status to ts_skipped
455 *
456 * @param test The test to set the status of
457 */
458 void skip_test(test_t &test);
459
460 /**
461 * @brief Sets a test's status to ts_started
462 *
463 * @param test The test to set the status of
464 */
465 void start_test(test_t &test);
466
467 /**
468 * @brief Check if a test is/should be skipped
469 *
470 * @return bool Whether the test is/should be skipped
471 */
472 bool is_skipped(const test_t &test);
473
474 /**
475 * @brief Prints a summary of all tests executed
476 * @param tests List of tests executed
477 *
478 * @return int Returns number of failed tests, for use as a return value from the main() function
479 */
480 int test_summary();
481
482
483 /**
484 * @brief Load test audio for the voice channel tests
485 *
486 * @return std::vector<uint8_t> data and size for test audio
487 */
488 std::vector<uint8_t> load_test_audio();
489
490 /**
491 * @brief Load bytes from file
492 *
493 * @return std::vector<std::byte> File data
494 */
495 std::vector<std::byte> load_data(const std::string& file);
496
497 /**
498 * @brief Get the token from the environment variable DPP_UNIT_TEST_TOKEN
499 *
500 * @return std::string token
501 * @note If the environment variable does not exist, this will exit the program.
502 */
503 std::string get_token();
504
505 /**
506 * @brief Wait for all tests to be completed or test program to time out
507 */
508 void wait_for_tests();
509
510 /**
511 * @brief Get the start time of tests
512 *
513 * @return double start time in fractional seconds
514 */
515 double get_start_time();
516
517 /**
518 * @brief Get the current execution time in seconds
519 *
520 * @return double fractional seconds
521 */
522 double get_time();
523
524 /**
525 * @brief A test version of the message collector for use in unit tests
526 */
527 class message_collector : public dpp::message_collector {
528 public:
529 message_collector(dpp::cluster* cl, uint64_t duration) : dpp::message_collector(cl, duration) { }
530
531 virtual void completed(const std::vector<dpp::message>& list) {
532 set_test(MSGCOLLECT, list.size() > 0);
533 }
534 };
535
536 /**
537 * @brief Convenience functor to get the snowflake of a certain type
538 */
539 struct user_project_id_t {
540 dpp::snowflake operator()(const dpp::user &user) const noexcept {
541 return user.id;
542 }
543
544 dpp::snowflake operator()(const dpp::guild_member &user) const noexcept {
545 return user.user_id;
546 }
547
548 dpp::snowflake operator()(dpp::snowflake user) const noexcept {
549 return user;
550 }
551
552 dpp::snowflake operator()(const dpp::thread_member &user) const noexcept {
553 return user.user_id;
554 }
555 };
556
557 /**
558 * @brief Convenience lambda to get the user snowflake of a certain user type
559 * @see user_project_id_t
560 */
561 inline constexpr user_project_id_t get_user_snowflake;
562
563 /**
564 * @brief Convenience lambda to check if a certain user is the owner of the test bot, mostly meant to be passed to standard algorithms
565 * @see get_user_snowflake
566 *
567 * @return bool whether the user is the test bot owner
568 */
569 inline constexpr auto is_owner = [](auto &&user) noexcept {
570 return get_user_snowflake(user) == TEST_USER_ID;
571 };
572
573 #define DPP_RUNTIME_CHECK(test, check, var) if (!check) { var = false; set_status(test, ts_failed, "check failed: " #check); }
574 #define DPP_COMPILETIME_CHECK(test, check, var) static_assert(check, #test ": " #check)
575
576 #ifndef DPP_STATIC_TEST
577 #define DPP_CHECK(test, check, var) DPP_RUNTIME_CHECK(test, check, var)
578 #else
579 #define DPP_CHECK(test, check, var) DPP_COMPILETIME_CHECK(test, check, var)
580 #endif
581
582 #define DPP_CHECK_CONSTRUCT_ASSIGN(test, type, var) do { \
583 DPP_CHECK(test, std::is_default_constructible_v<type>, var); \
584 DPP_CHECK(test, std::is_copy_constructible_v<type>, var); \
585 DPP_CHECK(test, std::is_move_constructible_v<type>, var); \
586 DPP_CHECK(test, std::is_copy_assignable_v<type>, var); \
587 DPP_CHECK(test, std::is_move_assignable_v<type>, var); \
588 } while(0)