diff --git a/src/Compat.cpp b/src/Compat.cpp index a1765b7..b5a5149 100644 --- a/src/Compat.cpp +++ b/src/Compat.cpp @@ -1,5 +1,8 @@ #include "Compat.h" +#include +#include + #ifndef WIN32 static struct termios old, current; @@ -20,6 +23,21 @@ void resetTermios(void) { tcsetattr(0, TCSANOW, &old); } +TEST_CASE("init and reset termios") { + struct termios original; + tcgetattr(0, &original); + SUBCASE("no echo") { + initTermios(false); + } + SUBCASE("yes echo") { + initTermios(true); + } + resetTermios(); + struct termios current; + tcgetattr(0, ¤t); + CHECK(std::memcmp(&original, ¤t, sizeof(struct termios)) == 0); +} + char getch_(int echo) { char ch; initTermios(echo); diff --git a/src/Http.cpp b/src/Http.cpp index 072a8ec..edc627e 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -142,6 +142,12 @@ std::string Http::Status::ToString(int Code) { } } +TEST_CASE("Http::Status::ToString") { + CHECK(Http::Status::ToString(200) == "OK"); + CHECK(Http::Status::ToString(696969) == "696969"); + CHECK(Http::Status::ToString(-1) == "Invalid Response Code"); +} + Http::Server::THttpServerInstance::THttpServerInstance() { Application::SetSubsystemStatus("HTTPServer", Application::Status::Starting); mThread = std::thread(&Http::Server::THttpServerInstance::operator(), this); diff --git a/src/LuaAPI.cpp b/src/LuaAPI.cpp index 7ac632a..2f3b7e5 100644 --- a/src/LuaAPI.cpp +++ b/src/LuaAPI.cpp @@ -102,6 +102,14 @@ void LuaAPI::Print(sol::variadic_args Args) { luaprint(ToPrint); } +TEST_CASE("LuaAPI::MP::GetServerVersion") { + const auto [ma, mi, pa] = LuaAPI::MP::GetServerVersion(); + const auto real = Application::ServerVersion(); + CHECK(ma == real.major); + CHECK(mi == real.minor); + CHECK(pa == real.patch); +} + static inline bool InternalTriggerClientEvent(int PlayerID, const std::string& EventName, const std::string& Data) { std::string Packet = "E:" + EventName + ":" + Data; if (PlayerID == -1) @@ -287,7 +295,7 @@ static std::pair FSWrapper(FnT Fn, ArgsT&&... Args) { std::pair LuaAPI::FS::CreateDirectory(const std::string& Path) { std::error_code errc; std::pair Result; - fs::create_directories(fs::relative(Path), errc); + fs::create_directories(Path, errc); Result.first = errc == std::error_code {}; if (!Result.first) { Result.second = errc.message(); @@ -295,6 +303,33 @@ std::pair LuaAPI::FS::CreateDirectory(const std::string& Path return Result; } +TEST_CASE("LuaAPI::FS::CreateDirectory") { + std::string TestDir = "beammp_test_dir"; + fs::remove_all(TestDir); + SUBCASE("Single level dir") { + const auto [Ok, Err] = LuaAPI::FS::CreateDirectory(TestDir); + CHECK(Ok); + CHECK(Err == ""); + CHECK(fs::exists(TestDir)); + } + SUBCASE("Multi level dir") { + const auto [Ok, Err] = LuaAPI::FS::CreateDirectory(TestDir + "/a/b/c"); + CHECK(Ok); + CHECK(Err == ""); + CHECK(fs::exists(TestDir + "/a/b/c")); + } + SUBCASE("Already exists") { + const auto [Ok, Err] = LuaAPI::FS::CreateDirectory(TestDir); + CHECK(Ok); + CHECK(Err == ""); + CHECK(fs::exists(TestDir)); + const auto [Ok2, Err2] = LuaAPI::FS::CreateDirectory(TestDir); + CHECK(Ok2); + CHECK(Err2 == ""); + } + fs::remove_all(TestDir); +} + std::pair LuaAPI::FS::Remove(const std::string& Path) { std::error_code errc; std::pair Result; @@ -306,10 +341,30 @@ std::pair LuaAPI::FS::Remove(const std::string& Path) { return Result; } +TEST_CASE("LuaAPI::FS::Remove") { + const std::string TestFileOrDir = "beammp_test_thing"; + SUBCASE("Remove existing directory") { + fs::create_directory(TestFileOrDir); + const auto [Ok, Err] = LuaAPI::FS::Remove(TestFileOrDir); + CHECK(Ok); + CHECK_EQ(Err, ""); + CHECK(!fs::exists(TestFileOrDir)); + } + SUBCASE("Remove non-existing directory") { + fs::remove_all(TestFileOrDir); + const auto [Ok, Err] = LuaAPI::FS::Remove(TestFileOrDir); + CHECK(Ok); + CHECK_EQ(Err, ""); + CHECK(!fs::exists(TestFileOrDir)); + } + // TODO: add tests for files + // TODO: add tests for files and folders without access permissions (failure) +} + std::pair LuaAPI::FS::Rename(const std::string& Path, const std::string& NewPath) { std::error_code errc; std::pair Result; - fs::rename(fs::relative(Path), fs::relative(NewPath), errc); + fs::rename(Path, NewPath, errc); Result.first = errc == std::error_code {}; if (!Result.first) { Result.second = errc.message(); @@ -317,10 +372,25 @@ std::pair LuaAPI::FS::Rename(const std::string& Path, const s return Result; } +TEST_CASE("LuaAPI::FS::Rename") { + const auto TestDir = "beammp_test_dir"; + const auto OtherTestDir = "beammp_test_dir_2"; + fs::remove_all(OtherTestDir); + fs::create_directory(TestDir); + const auto [Ok, Err] = LuaAPI::FS::Rename(TestDir, OtherTestDir); + CHECK(Ok); + CHECK_EQ(Err, ""); + CHECK(!fs::exists(TestDir)); + CHECK(fs::exists(OtherTestDir)); + + fs::remove_all(OtherTestDir); + fs::remove_all(TestDir); +} + std::pair LuaAPI::FS::Copy(const std::string& Path, const std::string& NewPath) { std::error_code errc; std::pair Result; - fs::copy(fs::relative(Path), fs::relative(NewPath), fs::copy_options::recursive, errc); + fs::copy(Path, NewPath, fs::copy_options::recursive, errc); Result.first = errc == std::error_code {}; if (!Result.first) { Result.second = errc.message(); @@ -328,8 +398,36 @@ std::pair LuaAPI::FS::Copy(const std::string& Path, const std return Result; } +TEST_CASE("LuaAPI::FS::Copy") { + const auto TestDir = "beammp_test_dir"; + const auto OtherTestDir = "beammp_test_dir_2"; + fs::remove_all(OtherTestDir); + fs::create_directory(TestDir); + const auto [Ok, Err] = LuaAPI::FS::Copy(TestDir, OtherTestDir); + CHECK(Ok); + CHECK_EQ(Err, ""); + CHECK(fs::exists(TestDir)); + CHECK(fs::exists(OtherTestDir)); + + fs::remove_all(OtherTestDir); + fs::remove_all(TestDir); +} + bool LuaAPI::FS::Exists(const std::string& Path) { - return fs::exists(fs::relative(Path)); + return fs::exists(Path); +} + +TEST_CASE("LuaAPI::FS::Exists") { + const auto TestDir = "beammp_test_dir"; + const auto OtherTestDir = "beammp_test_dir_2"; + fs::remove_all(OtherTestDir); + fs::create_directory(TestDir); + + CHECK(LuaAPI::FS::Exists(TestDir)); + CHECK(!LuaAPI::FS::Exists(OtherTestDir)); + + fs::remove_all(OtherTestDir); + fs::remove_all(TestDir); } std::string LuaAPI::FS::GetFilename(const std::string& Path) {