Skip to content

Commit

Permalink
Update TgLongPoll with new method
Browse files Browse the repository at this point in the history
- Add add TEST entry to add_my_executable function
  • Loading branch information
Royna2544 committed Nov 18, 2024
1 parent e509332 commit 923edff
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 43 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/linux_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ jobs:
- name: Run Tests
run: |
./build/bin/TgBot++_test
cd build
ctest
3 changes: 2 additions & 1 deletion .github/workflows/macos_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ jobs:
- name: Run Tests
run: |
./build/bin/TgBot++_test
cd build
ctest
File renamed without changes.
3 changes: 2 additions & 1 deletion .github/workflows/windows_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ jobs:
- name: Run Tests
run: |
./build/bin/TgBot++_test.exe
cd build
ctest
20 changes: 14 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -234,14 +234,22 @@ 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 (TGBOT_EXE_TEST)
set(TEST_EXT test_)
endif()
if (NOT TGBOT_EXE_NO_PREFIX)
set(TGBOT_EXE_NAME ${PROJECT_NAME}_${TGBOT_EXE_NAME})
set(TGBOT_EXE_NAME ${PROJECT_NAME}_${TEST_EXT}${TGBOT_EXE_NAME})
endif()
if (TGBOT_EXE_OPTIONAL)
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})
Expand Down Expand Up @@ -376,7 +384,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
)
#####################################################################
Expand All @@ -385,7 +393,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)
Expand All @@ -408,4 +416,4 @@ set(CPACK_GENERATOR "ZIP;TGZ;NSIS")
set(CPACK_PACKAGE_INSTALL_DIRECTORY "TgBot C++ server ${CPACK_PACKAGE_VERSION}")

# Include CPack
include(CPack)
include(CPack)
11 changes: 6 additions & 5 deletions src/api/TgBotApiImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/command_modules/alive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions src/include/api/Providers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Providers {
Installable<ConfigManager> config{};
Installable<ThreadManager> threads{};

APPLE_INJECT(Providers(Random *random, ResourceProvider *resource,
APPLE_INJECT(Providers(RandomBase *random, ResourceProvider *resource,
DatabaseBase *database, CommandLine *cmd,
ConfigManager *configManager,
ThreadManager *thread)) {
Expand All @@ -40,4 +40,4 @@ class Providers {
this->config.instance = configManager;
this->threads.instance = thread;
}
};
};
2 changes: 1 addition & 1 deletion src/include/trivial_helpers/_tgbot.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ constexpr auto fmt::formatter<User::Ptr>::parse(ParseContext& ctx) {
template <typename FormatContext>
auto fmt::formatter<User::Ptr>::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);
}
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ getAllComponent(CommandLine cmd) {
return fruit::createComponent()
.bind<TgBotWebServerBase, TgBotWebServer>()
.bind<VFSOperations, RealFS>()
.bind<RandomBase, Random>()
.install(getDatabaseComponent)
.install(getTgBotApiImplComponent)
.install(getRegexHandlerComponent)
Expand Down
19 changes: 6 additions & 13 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
###################### Creates the test target ######################

add_my_executable(
NAME test
NAME main
SRCS
TestMain.cpp
AuthorizationTest.cpp
Expand All @@ -20,27 +20,22 @@ 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
NAME database
SRCS
TestMain.cpp
DatabaseBackendsTest.cpp
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
NAME socketdatahandler
SRCS
TestMain.cpp
SocketDataHandlerTest.cpp
Expand All @@ -49,7 +44,5 @@ add_my_executable(
GTest::gmock
TgBotPPImpl
fruit
OPTIONAL
TEST
)
# Add the test executable to CTest
add_test(NAME TestSuiteSocketDataHandler COMMAND TgBot++_test_socketdatahandler)
3 changes: 2 additions & 1 deletion tests/commands/AliveCmdTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class AliveCommandTest : public CommandTestBase {

TEST_F(AliveCommandTest, hasAliveMediaName) {
setCommandExtArgs();
const auto botUser = std::make_shared<User>();
auto botUser = std::make_shared<User>();
botUser->username = "ALALALASL";

// Would call two times for username, nickname
ON_CALL(*botApi, getBotUser_impl()).WillByDefault(Return(botUser));
Expand Down
4 changes: 2 additions & 2 deletions tests/commands/CommandModulesTest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ class CommandTestBase : public CommandModulesTest {
void setCommandExtArgs(
const std::initializer_list<std::string_view>& 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(
Expand Down
13 changes: 5 additions & 8 deletions tests/commands/DatabaseCmdTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@
#include <memory>

#include "CommandModulesTest.hpp"
#include "StringResLoader.hpp"
#include "api/TgBotApi.hpp"
#include "api/Utils.hpp"
#include "gmock/gmock.h"

namespace {
void verifyKeyboard(const TgBot::GenericReply::Ptr& reply) {
auto keyboardReply =
std::dynamic_pointer_cast<TgBot::ReplyKeyboardMarkup>(reply);
ASSERT_TRUE(keyboardReply);

EXPECT_TRUE(keyboardReply->resizeKeyboard);
EXPECT_TRUE(keyboardReply->oneTimeKeyboard);
EXPECT_TRUE(keyboardReply->selective);
EXPECT_FALSE(keyboardReply->isPersistent);
EXPECT_TRUE(keyboardReply->resizeKeyboard.value());
EXPECT_TRUE(keyboardReply->oneTimeKeyboard.value());
EXPECT_TRUE(keyboardReply->selective.value());
EXPECT_FALSE(keyboardReply->isPersistent.value());
}
} // namespace

Expand Down Expand Up @@ -119,4 +116,4 @@ TEST_F(DatabaseCommandTest, WithUserAddToBlackList) {
TEST_F(DatabaseCommandTest, WithUserRemoveFromBlackList) {
test_removeuser<DatabaseBase::ListType::BLACKLIST,
DatabaseBase::ListResult::OK, 1, 1>();
}
}

0 comments on commit 923edff

Please sign in to comment.