From 5e0d633392872083804f63f830e2ae6fb9dea07c Mon Sep 17 00:00:00 2001 From: professor91 Date: Thu, 19 Oct 2023 00:57:38 +0530 Subject: [PATCH] moved utility tests --- src/unittest/test.cpp | 165 +------------------------------- src/unittest/test.h | 5 + src/unittest/utilities.cpp | 190 +++++++++++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+), 164 deletions(-) create mode 100644 src/unittest/utilities.cpp diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index eb9ed823b2..76d59f0298 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -21,49 +21,9 @@ ************************************************************************************/ #include "test.h" -#include -#include #include #include -/** - * @brief Type trait to check if a certain type has a build_json method - * - * @tparam T type to check for - */ -template > -struct has_build_json : std::false_type {}; - -template -struct has_build_json().build_json())>> : std::true_type {}; - -/** - * @brief Type trait to check if a certain type has a build_json method - * - * @tparam T type to check for - */ -template -constexpr bool has_build_json_v = has_build_json::value; - -/** - * @brief Type trait to check if a certain type has a fill_from_json method - * - * @tparam T type to check for - */ -template -struct has_fill_from_json : std::false_type {}; - -template -struct has_fill_from_json().fill_from_json(std::declval()))>> : std::true_type {}; - -/** - * @brief Type trait to check if a certain type has a fill_from_json method - * - * @tparam T type to check for - */ -template -constexpr bool has_fill_from_json_v = has_fill_from_json::value; - /* Unit tests go here */ int main(int argc, char *argv[]) { @@ -79,23 +39,6 @@ int main(int argc, char *argv[]) std::cout << "Running offline and " << (extended ? "extended" : "limited") << " online unit tests. Guild ID: " << TEST_GUILD_ID << " Text Channel ID: " << TEST_TEXT_CHANNEL_ID << " VC ID: " << TEST_VC_ID << " User ID: " << TEST_USER_ID << " Event ID: " << TEST_EVENT_ID << "\n"; } - std::string test_to_escape = "*** _This is a test_ ***\n```cpp\n\ -int main() {\n\ - /* Comment */\n\ - int answer = 42;\n\ - return answer; // ___\n\ -};\n\ -```\n\ -Markdown lol ||spoiler|| ~~strikethrough~~ `small *code* block`\n"; - - set_test(COMPARISON, false); - dpp::user u1; - dpp::user u2; - dpp::user u3; - u1.id = u2.id = 666; - u3.id = 777; - set_test(COMPARISON, u1 == u2 && u1 != u3); - dpp::cluster bot(token, dpp::i_all_intents); errors_test(); @@ -103,113 +46,7 @@ Markdown lol ||spoiler|| ~~strikethrough~~ `small *code* block`\n"; discord_objects_tests(); gateway_events_tests(token, bot); cache_tests(bot); - - set_test(MD_ESC_1, false); - set_test(MD_ESC_2, false); - std::string escaped1 = dpp::utility::markdown_escape(test_to_escape); - std::string escaped2 = dpp::utility::markdown_escape(test_to_escape, true); - set_test(MD_ESC_1, escaped1 == "\\*\\*\\* \\_This is a test\\_ \\*\\*\\*\n\ -```cpp\n\ -int main() {\n\ - /* Comment */\n\ - int answer = 42;\n\ - return answer; // ___\n\ -};\n\ -```\n\ -Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ `small *code* block`\n"); - set_test(MD_ESC_2, escaped2 == "\\*\\*\\* \\_This is a test\\_ \\*\\*\\*\n\ -\\`\\`\\`cpp\n\ -int main\\(\\) {\n\ - /\\* Comment \\*/\n\ - int answer = 42;\n\ - return answer; // \\_\\_\\_\n\ -};\n\ -\\`\\`\\`\n\ -Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* block\\`\n"); - - set_test(URLENC, false); - set_test(URLENC, dpp::utility::url_encode("ABC123_+\\|$*/AAA[]😄") == "ABC123_%2B%5C%7C%24%2A%2FAAA%5B%5D%F0%9F%98%84"); - - set_test(BASE64ENC, false); - set_test(BASE64ENC, - dpp::base64_encode(reinterpret_cast("a"), 1) == "YQ==" && - dpp::base64_encode(reinterpret_cast("bc"), 2) == "YmM=" && - dpp::base64_encode(reinterpret_cast("def"), 3) == "ZGVm" && - dpp::base64_encode(reinterpret_cast("ghij"), 4) == "Z2hpag==" && - dpp::base64_encode(reinterpret_cast("klmno"), 5) == "a2xtbm8=" && - dpp::base64_encode(reinterpret_cast("pqrstu"), 6) == "cHFyc3R1" && - dpp::base64_encode(reinterpret_cast("vwxyz12"), 7) == "dnd4eXoxMg==" - ); - - std::vector testaudio = load_test_audio(); - - set_test(READFILE, false); - std::string rf_test = dpp::utility::read_file(SHARED_OBJECT); - FILE* fp = fopen(SHARED_OBJECT, "rb"); - fseek(fp, 0, SEEK_END); - size_t off = (size_t)ftell(fp); - fclose(fp); - set_test(READFILE, off == rf_test.length()); - - set_test(TIMESTAMPTOSTRING, false); - set_test(TIMESTAMPTOSTRING, dpp::ts_to_string(1642611864) == "2022-01-19T17:04:24Z"); - - { // test dpp::json_interface - start_test(JSON_INTERFACE); - struct fillable : dpp::json_interface { - fillable &fill_from_json_impl(dpp::json *) { - return *this; - } - }; - struct buildable : dpp::json_interface { - json to_json_impl(bool = false) const { - return {}; - } - }; - struct fillable_and_buildable : dpp::json_interface { - fillable_and_buildable &fill_from_json_impl(dpp::json *) { - return *this; - } - - json to_json_impl(bool = false) const { - return {}; - } - }; - bool success = true; - - DPP_CHECK(JSON_INTERFACE, has_build_json_v>, success); - DPP_CHECK(JSON_INTERFACE, !has_fill_from_json_v>, success); - DPP_CHECK(JSON_INTERFACE, has_build_json_v, success); - DPP_CHECK(JSON_INTERFACE, !has_fill_from_json_v, success); - - DPP_CHECK(JSON_INTERFACE, !has_build_json_v>, success); - DPP_CHECK(JSON_INTERFACE, has_fill_from_json_v>, success); - DPP_CHECK(JSON_INTERFACE, !has_build_json_v, success); - DPP_CHECK(JSON_INTERFACE, has_fill_from_json_v, success); - - DPP_CHECK(JSON_INTERFACE, has_build_json_v>, success); - DPP_CHECK(JSON_INTERFACE, has_fill_from_json_v>, success); - DPP_CHECK(JSON_INTERFACE, has_build_json_v, success); - DPP_CHECK(JSON_INTERFACE, has_fill_from_json_v, success); - set_test(JSON_INTERFACE, success); - } - -#ifndef _WIN32 - set_test(TIMESTRINGTOTIMESTAMP, false); - json tj; - tj["t1"] = "2022-01-19T17:18:14.506000+00:00"; - tj["t2"] = "2022-01-19T17:18:14+00:00"; - uint32_t inTimestamp = 1642612694; - set_test(TIMESTRINGTOTIMESTAMP, (uint64_t)dpp::ts_not_null(&tj, "t1") == inTimestamp && (uint64_t)dpp::ts_not_null(&tj, "t2") == inTimestamp); -#else - set_test(TIMESTRINGTOTIMESTAMP, true); -#endif - - { - set_test(TS, false); - dpp::managed m(189759562910400512); - set_test(TS, ((uint64_t) m.get_creation_time()) == 1465312605); - } + utility_tests(); { coro_offline_tests(); diff --git a/src/unittest/test.h b/src/unittest/test.h index 745921f12e..343a0efb15 100644 --- a/src/unittest/test.h +++ b/src/unittest/test.h @@ -594,3 +594,8 @@ void gateway_events_tests(const std::string&, dpp::cluster&); * @brief Unit tests for Cache */ void cache_tests(dpp::cluster&); + +/** +* @brief Unit tests for library Utilities +*/ +void utility_tests(); diff --git a/src/unittest/utilities.cpp b/src/unittest/utilities.cpp new file mode 100644 index 0000000000..b0ff4af486 --- /dev/null +++ b/src/unittest/utilities.cpp @@ -0,0 +1,190 @@ +/************************************************************************************ + * + * D++, A Lightweight C++ library for Discord + * + * SPDX-License-Identifier: Apache-2.0 + * Copyright 2021 Craig Edwards and D++ contributors + * (https://github.com/brainboxdotcc/DPP/graphs/contributors) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ************************************************************************************/ +#include "test.h" + +#include + +/** + * @brief Type trait to check if a certain type has a build_json method + * + * @tparam T type to check for + */ +template > +struct has_build_json : std::false_type {}; + +template +struct has_build_json().build_json())>> : std::true_type {}; + +/** + * @brief Type trait to check if a certain type has a build_json method + * + * @tparam T type to check for + */ +template +constexpr bool has_build_json_v = has_build_json::value; + +/** + * @brief Type trait to check if a certain type has a fill_from_json method + * + * @tparam T type to check for + */ +template +struct has_fill_from_json : std::false_type {}; + +template +struct has_fill_from_json().fill_from_json(std::declval()))>> : std::true_type {}; + +/** + * @brief Type trait to check if a certain type has a fill_from_json method + * + * @tparam T type to check for + */ +template +constexpr bool has_fill_from_json_v = has_fill_from_json::value; + +/* Unit tests for library utilities */ +void utility_tests() { + // markdown escape tests + std::string test_to_escape = "*** _This is a test_ ***\n```cpp\n\ +int main() {\n\ + /* Comment */\n\ + int answer = 42;\n\ + return answer; // ___\n\ +};\n\ +```\n\ +Markdown lol ||spoiler|| ~~strikethrough~~ `small *code* block`\n"; + + set_test(MD_ESC_1, false); + set_test(MD_ESC_2, false); + std::string escaped1 = dpp::utility::markdown_escape(test_to_escape); + std::string escaped2 = dpp::utility::markdown_escape(test_to_escape, true); + set_test(MD_ESC_1, escaped1 == "\\*\\*\\* \\_This is a test\\_ \\*\\*\\*\n\ +```cpp\n\ +int main() {\n\ + /* Comment */\n\ + int answer = 42;\n\ + return answer; // ___\n\ +};\n\ +```\n\ +Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ `small *code* block`\n"); + set_test(MD_ESC_2, escaped2 == "\\*\\*\\* \\_This is a test\\_ \\*\\*\\*\n\ +\\`\\`\\`cpp\n\ +int main\\(\\) {\n\ + /\\* Comment \\*/\n\ + int answer = 42;\n\ + return answer; // \\_\\_\\_\n\ +};\n\ +\\`\\`\\`\n\ +Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* block\\`\n"); + + + set_test(COMPARISON, false); + dpp::user u1; + dpp::user u2; + dpp::user u3; + u1.id = u2.id = 666; + u3.id = 777; + set_test(COMPARISON, u1 == u2 && u1 != u3); + + // encoding tests + set_test(URLENC, false); + set_test(URLENC, dpp::utility::url_encode("ABC123_+\\|$*/AAA[]😄") == "ABC123_%2B%5C%7C%24%2A%2FAAA%5B%5D%F0%9F%98%84"); + + set_test(BASE64ENC, false); + set_test(BASE64ENC, + dpp::base64_encode(reinterpret_cast("a"), 1) == "YQ==" && + dpp::base64_encode(reinterpret_cast("bc"), 2) == "YmM=" && + dpp::base64_encode(reinterpret_cast("def"), 3) == "ZGVm" && + dpp::base64_encode(reinterpret_cast("ghij"), 4) == "Z2hpag==" && + dpp::base64_encode(reinterpret_cast("klmno"), 5) == "a2xtbm8=" && + dpp::base64_encode(reinterpret_cast("pqrstu"), 6) == "cHFyc3R1" && + dpp::base64_encode(reinterpret_cast("vwxyz12"), 7) == "dnd4eXoxMg==" + ); + + set_test(READFILE, false); + std::string rf_test = dpp::utility::read_file(SHARED_OBJECT); + FILE* fp = fopen(SHARED_OBJECT, "rb"); + fseek(fp, 0, SEEK_END); + size_t off = (size_t)ftell(fp); + fclose(fp); + set_test(READFILE, off == rf_test.length()); + + set_test(TIMESTAMPTOSTRING, false); + set_test(TIMESTAMPTOSTRING, dpp::ts_to_string(1642611864) == "2022-01-19T17:04:24Z"); + +#ifndef _WIN32 + set_test(TIMESTRINGTOTIMESTAMP, false); + json tj; + tj["t1"] = "2022-01-19T17:18:14.506000+00:00"; + tj["t2"] = "2022-01-19T17:18:14+00:00"; + uint32_t inTimestamp = 1642612694; + set_test(TIMESTRINGTOTIMESTAMP, (uint64_t)dpp::ts_not_null(&tj, "t1") == inTimestamp && (uint64_t)dpp::ts_not_null(&tj, "t2") == inTimestamp); +#else + set_test(TIMESTRINGTOTIMESTAMP, true); +#endif + + { + set_test(TS, false); + dpp::managed m(189759562910400512); + set_test(TS, ((uint64_t) m.get_creation_time()) == 1465312605); + } + + { // test dpp::json_interface + start_test(JSON_INTERFACE); + struct fillable : dpp::json_interface { + fillable &fill_from_json_impl(dpp::json *) { + return *this; + } + }; + struct buildable : dpp::json_interface { + json to_json_impl(bool = false) const { + return {}; + } + }; + struct fillable_and_buildable : dpp::json_interface { + fillable_and_buildable &fill_from_json_impl(dpp::json *) { + return *this; + } + + json to_json_impl(bool = false) const { + return {}; + } + }; + bool success = true; + + DPP_CHECK(JSON_INTERFACE, has_build_json_v>, success); + DPP_CHECK(JSON_INTERFACE, !has_fill_from_json_v>, success); + DPP_CHECK(JSON_INTERFACE, has_build_json_v, success); + DPP_CHECK(JSON_INTERFACE, !has_fill_from_json_v, success); + + DPP_CHECK(JSON_INTERFACE, !has_build_json_v>, success); + DPP_CHECK(JSON_INTERFACE, has_fill_from_json_v>, success); + DPP_CHECK(JSON_INTERFACE, !has_build_json_v, success); + DPP_CHECK(JSON_INTERFACE, has_fill_from_json_v, success); + + DPP_CHECK(JSON_INTERFACE, has_build_json_v>, success); + DPP_CHECK(JSON_INTERFACE, has_fill_from_json_v>, success); + DPP_CHECK(JSON_INTERFACE, has_build_json_v, success); + DPP_CHECK(JSON_INTERFACE, has_fill_from_json_v, success); + set_test(JSON_INTERFACE, success); + } +}