|
| 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 | + |
| 23 | +#ifdef DPP_MODULES |
| 24 | +#include "test.h" |
| 25 | + |
| 26 | +import dpp; |
| 27 | + |
| 28 | +/** |
| 29 | + * @brief Test basic dpp types and functionality via module import |
| 30 | + */ |
| 31 | +void test_dpp_module_basic() { |
| 32 | + start_test(MODULE_IMPORT_BASIC); |
| 33 | + |
| 34 | + dpp::snowflake test_id = 825411104208977952ULL; |
| 35 | + time_t extracted_timestamp = test_id.get_creation_time(); |
| 36 | + if (extracted_timestamp != 1616978723) { |
| 37 | + set_status(MODULE_IMPORT_BASIC, ts_failed, "snowflake timestamp extraction"); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + // User object creation test |
| 42 | + dpp::user test_user; |
| 43 | + test_user.id = 987654321; |
| 44 | + test_user.username = "ModuleTestUser"; |
| 45 | + if (test_user.id != 987654321 || test_user.username != "ModuleTestUser") { |
| 46 | + set_status(MODULE_IMPORT_BASIC, ts_failed, "user object creation"); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // Testing ts_to_string |
| 51 | + std::string test_time = dpp::ts_to_string(1642611864); |
| 52 | + if (test_time != "2022-01-19T17:04:24Z") { |
| 53 | + set_status(MODULE_IMPORT_BASIC, ts_failed, "timestamp conversion"); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + // Message object creation test |
| 58 | + dpp::message test_msg; |
| 59 | + test_msg.content = "Test message from module"; |
| 60 | + test_msg.id = 111222333; |
| 61 | + |
| 62 | + auto is_valid_message = [](const dpp::message& msg, std::string_view s, int test_id) -> bool { |
| 63 | + return msg.content == s && msg.id == test_id; |
| 64 | + }; |
| 65 | + |
| 66 | + if (!is_valid_message(test_msg, "Test message from module")) { |
| 67 | + set_status(MODULE_IMPORT_BASIC, ts_failed, "message object creation"); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + // URL encoding test |
| 72 | + std::string encoded = dpp::utility::url_encode("test value"); |
| 73 | + if (encoded != "test%20value") { |
| 74 | + set_status(MODULE_IMPORT_BASIC, ts_failed, "URL encoding"); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // Markdown escaping test |
| 79 | + std::string markdown = "**bold** _italic_"; |
| 80 | + std::string escaped = dpp::utility::markdown_escape(markdown); |
| 81 | + if (escaped != "\\*\\*bold\\*\\* \\_italic\\_") { |
| 82 | + set_status(MODULE_IMPORT_BASIC, ts_failed, "markdown escaping"); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // Role comparison test |
| 87 | + dpp::role r1; |
| 88 | + dpp::role r2; |
| 89 | + r1.id = 100; |
| 90 | + r1.position = 1; |
| 91 | + r1.guild_id = 500; |
| 92 | + r2.id = 200; |
| 93 | + r2.position = 2; |
| 94 | + r2.guild_id = 500; |
| 95 | + |
| 96 | + auto role_comparison_1 = [](const dpp::role& r1, const dpp::role& r2) -> bool { |
| 97 | + return (r1 < r2); |
| 98 | + }; |
| 99 | + |
| 100 | + auto role_comparison_2 = [](const dpp::role& r1, const dpp::role& r2) -> bool { |
| 101 | + return !(r1 > r2); |
| 102 | + } |
| 103 | + |
| 104 | + if (!role_comparison_1(r1, r2) || !role_comparison_2(r1, r2)) { |
| 105 | + set_status(MODULE_IMPORT_BASIC, ts_failed, "role comparison"); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + set_status(MODULE_IMPORT_BASIC, ts_success); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * @brief Test dpp module coroutine support (if enabled) |
| 114 | + */ |
| 115 | +void test_dpp_module_coro() { |
| 116 | + start_test(MODULE_IMPORT_CORO); |
| 117 | + |
| 118 | +#ifndef DPP_NO_CORO |
| 119 | + dpp::promise<int> test_promise; |
| 120 | + test_promise.set_value(42); |
| 121 | + |
| 122 | + if constexpr (!requires { dpp::task<void>{}; }) { |
| 123 | + set_status(MODULE_IMPORT_CORO, ts_failed, "coroutine types not accessible"); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + set_status(MODULE_IMPORT_CORO, ts_success); |
| 128 | +#else |
| 129 | + set_status(MODULE_IMPORT_CORO, ts_skipped, "coroutines disabled"); |
| 130 | +#endif |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * @brief Main entry point for module tests |
| 135 | + */ |
| 136 | +void run_module_tests() { |
| 137 | + test_dpp_module_basic(); |
| 138 | + test_dpp_module_coro(); |
| 139 | +} |
| 140 | + |
| 141 | +#endif |
0 commit comments