From 0f9e4446fef8601af605bf61081b2052f7ed2688 Mon Sep 17 00:00:00 2001 From: royna2544 Date: Mon, 18 Nov 2024 15:40:43 +0900 Subject: [PATCH] Update TgLongPoll with new method - Add add TEST entry to add_my_executable function --- .github/workflows/linux_test.yml | 3 ++- .github/workflows/macos_test.yml | 3 ++- .../{windows_msvc.yml => windows_msvc.yml.bak} | 0 .github/workflows/windows_test.yml | 3 ++- CMakeLists.txt | 13 +++++++++---- src/api/TgBotApiImpl.cpp | 11 ++++++----- src/command_modules/alive.cpp | 2 +- src/include/trivial_helpers/_tgbot.h | 2 +- src/third-party/tgbot-cpp | 2 +- tests/CMakeLists.txt | 15 ++++----------- tests/commands/AliveCmdTest.cpp | 3 ++- tests/commands/CommandModulesTest.hpp | 4 ++-- 12 files changed, 32 insertions(+), 29 deletions(-) rename .github/workflows/{windows_msvc.yml => windows_msvc.yml.bak} (100%) diff --git a/.github/workflows/linux_test.yml b/.github/workflows/linux_test.yml index f2acd49e..e829477c 100644 --- a/.github/workflows/linux_test.yml +++ b/.github/workflows/linux_test.yml @@ -69,4 +69,5 @@ jobs: - name: Run Tests run: | - ./build/bin/TgBot++_test + cd build + ctest diff --git a/.github/workflows/macos_test.yml b/.github/workflows/macos_test.yml index 8131d5e4..f12a3f32 100644 --- a/.github/workflows/macos_test.yml +++ b/.github/workflows/macos_test.yml @@ -35,5 +35,6 @@ jobs: - name: Run Tests run: | - ./build/bin/TgBot++_test + cd build + ctest diff --git a/.github/workflows/windows_msvc.yml b/.github/workflows/windows_msvc.yml.bak similarity index 100% rename from .github/workflows/windows_msvc.yml rename to .github/workflows/windows_msvc.yml.bak diff --git a/.github/workflows/windows_test.yml b/.github/workflows/windows_test.yml index ae86ebc4..90e0f7bd 100644 --- a/.github/workflows/windows_test.yml +++ b/.github/workflows/windows_test.yml @@ -64,4 +64,5 @@ jobs: - name: Run Tests run: | - ./build/bin/TgBot++_test.exe \ No newline at end of file + cd build + ctest \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index d7ab04a9..2dd37bd9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -223,7 +223,7 @@ endfunction(add_my_library) function(add_my_executable) cmake_parse_arguments(TGBOT_EXE - "NO_PREFIX;OPTIONAL" # Options + "NO_PREFIX;OPTIONAL;TEST" # Options "NAME" # One-value keywords "SRCS;LIBS;LIBS_WIN32" # Multiple-value keywords ${ARGN} @@ -234,6 +234,7 @@ function(add_my_executable) if (DIRTY_ABSEIL_FIX) list(APPEND TGBOT_EXE_SRCS ${CMAKE_SOURCE_DIR}/src/logging/log_message.cc) endif() + set(TGBOT_EXE_NAME_BAK ${TGBOT_EXE_NAME}) if (NOT TGBOT_EXE_NO_PREFIX) set(TGBOT_EXE_NAME ${PROJECT_NAME}_${TGBOT_EXE_NAME}) endif() @@ -241,7 +242,11 @@ function(add_my_executable) add_executable(${TGBOT_EXE_NAME} EXCLUDE_FROM_ALL ${TGBOT_EXE_SRCS}) else() add_executable(${TGBOT_EXE_NAME} ${TGBOT_EXE_SRCS}) - install(TARGETS ${TGBOT_EXE_NAME} DESTINATION bin) + if (TGBOT_EXE_TEST) + add_test(NAME TestSuite_${TGBOT_EXE_NAME_BAK} COMMAND ${TGBOT_EXE_NAME}) + else() + install(TARGETS ${TGBOT_EXE_NAME} DESTINATION bin) + endif() endif() add_sanitizers(${TGBOT_EXE_NAME}) target_link_libraries(${TGBOT_EXE_NAME} TgBotLogInit absl::log ${TGBOT_EXE_LIBS}) @@ -376,7 +381,7 @@ add_my_library( src/socket/interface/impl/backends/ServerBackend.cpp src/socket/interface/impl/backends/ServerBackend_${TARGET_VARIANT}.cpp LIBS TgBot TgBotUtils TgBotWeb TgBotDBImpl absl::status TgBotRandom TgBot_restartfmt_parser - TgBotPPImpl_shared_deps TgBotStringRes ${CMAKE_DL_LIBS} TgBotSocket JsonCpp::JsonCpp + TgBotPPImpl_shared_deps TgBotStringRes ${CMAKE_DL_LIBS} TgBotSocket JsonCpp::JsonCpp TgBotsighandler STATIC ) ##################################################################### @@ -385,7 +390,7 @@ add_my_library( add_my_executable( NAME main SRCS src/main.cpp - LIBS TgBotPPImpl TgBotDBLoading TgBotsighandler TgBot_restartfmt_parser fruit + LIBS TgBotPPImpl TgBotDBLoading TgBot_restartfmt_parser fruit ) if (UNIX) diff --git a/src/api/TgBotApiImpl.cpp b/src/api/TgBotApiImpl.cpp index 650045a6..89dbbaa0 100644 --- a/src/api/TgBotApiImpl.cpp +++ b/src/api/TgBotApiImpl.cpp @@ -201,15 +201,16 @@ void TgBotApiImpl::removeInlineQueryKeyboard(const std::string_view key) { } void TgBotApiImpl::startPoll() { - LOG(INFO) << "Bot username: " << getBotUser()->username.value_or("Unknown"); + LOG(INFO) << "Bot username: " << getBotUser()->username.value(); // Deleting webhook getApi().deleteWebhook(); - TgLongPoll longPoll(_bot, 100, 10, - {"message", "inline_query", "callback_query", - "my_chat_member", "chat_member", "chat_join_request"}); + auto* longPoll = _bot.createLongPoll( + 100, 10, + {"message", "inline_query", "callback_query", "my_chat_member", + "chat_member", "chat_join_request"}); while (!SignalHandler::isSignaled()) { - longPoll.start(); + longPoll->start(); } } diff --git a/src/command_modules/alive.cpp b/src/command_modules/alive.cpp index 86ce21bf..9f6fe1d1 100644 --- a/src/command_modules/alive.cpp +++ b/src/command_modules/alive.cpp @@ -35,7 +35,7 @@ DECLARE_COMMAND_HANDLER(alive) { {"_commitid_", data.commitid}, {"_commitmsg_", splitMsg.front()}, {"_botname_", api->getBotUser()->firstName}, - {"_botusername_", api->getBotUser()->username.value()} + {"_botusername_", api->getBotUser()->username.value_or("unknown")} }); }); const auto info = provider->database->queryMediaInfo("alive"); diff --git a/src/include/trivial_helpers/_tgbot.h b/src/include/trivial_helpers/_tgbot.h index e8889e53..eff7c83c 100644 --- a/src/include/trivial_helpers/_tgbot.h +++ b/src/include/trivial_helpers/_tgbot.h @@ -24,7 +24,7 @@ constexpr auto fmt::formatter::parse(ParseContext& ctx) { template auto fmt::formatter::format(const User::Ptr& user, FormatContext& ctx) const { - if (!user->lastName.has_value()) { + if (user->lastName.has_value()) { return fmt::format_to(ctx.out(), "{} {} (id: {})", user->firstName, user->lastName.value(), user->id); } diff --git a/src/third-party/tgbot-cpp b/src/third-party/tgbot-cpp index 8ca62261..4aea259b 160000 --- a/src/third-party/tgbot-cpp +++ b/src/third-party/tgbot-cpp @@ -1 +1 @@ -Subproject commit 8ca62261159fabf9b55981988e0465fe8b64e75d +Subproject commit 4aea259b32ecbd1ff28168a75c433168753aa0f4 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ced99cb8..a1db6794 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -20,12 +20,9 @@ add_my_executable( GTest::gtest GTest::gmock TgBotPPImpl fruit - OPTIONAL + TEST ) -# Add the test executable to CTest -add_test(NAME TestSuite COMMAND TgBot++_test) - add_my_executable( NAME test_database SRCS @@ -34,10 +31,8 @@ add_my_executable( LIBS GTest::gtest TgBotDBImpl - OPTIONAL + TEST ) -# Add the test executable to CTest -add_test(NAME TestSuiteDatabase COMMAND TgBot++_test_database) add_my_executable( NAME test_socketdatahandler @@ -49,7 +44,5 @@ add_my_executable( GTest::gmock TgBotPPImpl fruit - OPTIONAL -) -# Add the test executable to CTest -add_test(NAME TestSuiteSocketDataHandler COMMAND TgBot++_test_socketdatahandler) \ No newline at end of file + TEST +) \ No newline at end of file diff --git a/tests/commands/AliveCmdTest.cpp b/tests/commands/AliveCmdTest.cpp index 282d4ed1..bc15642f 100644 --- a/tests/commands/AliveCmdTest.cpp +++ b/tests/commands/AliveCmdTest.cpp @@ -12,7 +12,8 @@ class AliveCommandTest : public CommandTestBase { TEST_F(AliveCommandTest, hasAliveMediaName) { setCommandExtArgs(); - const auto botUser = std::make_shared(); + auto botUser = std::make_shared(); + botUser->username = "ALALALASL"; // Would call two times for username, nickname ON_CALL(*botApi, getBotUser_impl()).WillByDefault(Return(botUser)); diff --git a/tests/commands/CommandModulesTest.hpp b/tests/commands/CommandModulesTest.hpp index 33a2630f..4ef751d9 100644 --- a/tests/commands/CommandModulesTest.hpp +++ b/tests/commands/CommandModulesTest.hpp @@ -168,13 +168,13 @@ class CommandTestBase : public CommandModulesTest { void setCommandExtArgs( const std::initializer_list& command) { setCommandExtArgs(); - defaultProvidedMessage->text += + (*defaultProvidedMessage->text) += fmt::format("{}", fmt::join(command, " ")); } void setCommandExtArgs() { defaultProvidedMessage->text = "/" + name; defaultProvidedMessage->entities[0]->length = - defaultProvidedMessage->text.size(); + defaultProvidedMessage->text->size(); } void execute() { module->_module->function(