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