From f06f31c2a0b7a8b0740a377da2d668145de362c6 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 26 May 2022 16:58:13 +0200 Subject: [PATCH] add moar tests!!! --- CMakeLists.txt | 1 + include/TLuaEngine.h | 2 +- src/TConsole.cpp | 26 ++++++++++++++++++++++++++ src/TLuaEngine.cpp | 7 +++++++ src/main.cpp | 8 ++++---- test/test_main.cpp | 2 +- 6 files changed, 40 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 26401e2..928c6bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,6 +62,7 @@ elseif (UNIX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -static-libstdc++ -static-libgcc") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -fno-builtin") + option(SANITIZE "Turns on thread and UB sanitizers" OFF) if (SANITIZE) message(STATUS "sanitize is ON") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined,thread") diff --git a/include/TLuaEngine.h b/include/TLuaEngine.h index 32e5bbf..706ecbf 100644 --- a/include/TLuaEngine.h +++ b/include/TLuaEngine.h @@ -73,7 +73,7 @@ private: std::unordered_map mFileTimes; }; -class TLuaEngine : IThreaded { +class TLuaEngine : IThreaded, public std::enable_shared_from_this { public: enum CallStrategy : int { BestEffort, diff --git a/src/TConsole.cpp b/src/TConsole.cpp index 8bc98c6..c26f957 100644 --- a/src/TConsole.cpp +++ b/src/TConsole.cpp @@ -14,6 +14,17 @@ static inline bool StringStartsWith(const std::string& What, const std::string& return What.size() >= StartsWith.size() && What.substr(0, StartsWith.size()) == StartsWith; } +TEST_CASE("StringStartsWith") { + CHECK(StringStartsWith("Hello, World", "Hello")); + CHECK(StringStartsWith("Hello, World", "H")); + CHECK(StringStartsWith("Hello, World", "")); + CHECK(!StringStartsWith("Hello, World", "ello")); + CHECK(!StringStartsWith("Hello, World", "World")); + CHECK(StringStartsWith("", "")); + CHECK(!StringStartsWith("", "hello")); +} + +// Trims leading and trailing spaces, newlines, tabs, etc. static inline std::string TrimString(std::string S) { S.erase(S.begin(), std::find_if(S.begin(), S.end(), [](unsigned char ch) { return !std::isspace(ch); @@ -25,6 +36,21 @@ static inline std::string TrimString(std::string S) { return S; } +TEST_CASE("TrimString") { + CHECK(TrimString("hel lo") == "hel lo"); + CHECK(TrimString(" hel lo") == "hel lo"); + CHECK(TrimString(" hel lo ") == "hel lo"); + CHECK(TrimString("hel lo ") == "hel lo"); + CHECK(TrimString(" hel lo") == "hel lo"); + CHECK(TrimString("hel lo ") == "hel lo"); + CHECK(TrimString(" hel lo ") == "hel lo"); + CHECK(TrimString("\t\thel\nlo\n\n") == "hel\nlo"); + CHECK(TrimString("\n\thel\tlo\n\t") == "hel\tlo"); + CHECK(TrimString(" ") == ""); + CHECK(TrimString(" \t\n\r ") == ""); + CHECK(TrimString("") == ""); +} + std::string GetDate() { std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); time_t tt = std::chrono::system_clock::to_time_t(now); diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index e5885ec..db0f825 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -38,6 +38,12 @@ TLuaEngine::TLuaEngine() Start(); } +TEST_CASE("TLuaEngine ctor & dtor") { + Application::Settings.Resource = "beammp_server_test_resources"; + TLuaEngine engine; + Application::GracefullyShutdown(); +} + void TLuaEngine::operator()() { RegisterThread("LuaEngine"); Application::SetSubsystemStatus("LuaEngine", Application::Status::Good); @@ -269,6 +275,7 @@ std::shared_ptr TLuaEngine::EnqueueFunctionCall(TLuaStateId StateID, } void TLuaEngine::CollectAndInitPlugins() { + fs::create_directories(mResourceServerPath); for (const auto& Dir : fs::directory_iterator(mResourceServerPath)) { auto Path = Dir.path(); Path = fs::relative(Path); diff --git a/src/main.cpp b/src/main.cpp index 49c0eb7..74dbbfd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -133,9 +133,9 @@ int BeamMPServerMain(MainArguments Arguments) { TServer Server(Arguments.List); TConfig Config(ConfigPath); - TLuaEngine LuaEngine; - LuaEngine.SetServer(&Server); - Application::Console().InitializeLuaConsole(LuaEngine); + auto LuaEngine = std::make_shared(); + LuaEngine->SetServer(&Server); + Application::Console().InitializeLuaConsole(*LuaEngine); if (Config.Failed()) { beammp_info("Closing in 10 seconds"); @@ -155,7 +155,7 @@ int BeamMPServerMain(MainArguments Arguments) { TPPSMonitor PPSMonitor(Server); THeartbeatThread Heartbeat(ResourceManager, Server); TNetwork Network(Server, PPSMonitor, ResourceManager); - LuaEngine.SetNetwork(&Network); + LuaEngine->SetNetwork(&Network); PPSMonitor.SetNetwork(Network); Application::CheckForUpdates(); diff --git a/test/test_main.cpp b/test/test_main.cpp index 1aba0a1..01af393 100644 --- a/test/test_main.cpp +++ b/test/test_main.cpp @@ -6,7 +6,7 @@ int main(int argc, char** argv) { doctest::Context context; - Application::InitializeConsole(); + // Application::InitializeConsole(); context.applyCommandLine(argc, argv);