From c6a0e285760ec5cccd597763fd70e0a00e9445b6 Mon Sep 17 00:00:00 2001 From: James Date: Fri, 13 Sep 2024 12:29:26 +0700 Subject: [PATCH] add unit test --- engine/commands/engine_init_cmd.cc | 4 ++-- engine/test/components/test_url_parser.cc | 26 +++++++++++++++++++++++ engine/utils/url_parser.h | 4 ++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 engine/test/components/test_url_parser.cc diff --git a/engine/commands/engine_init_cmd.cc b/engine/commands/engine_init_cmd.cc index 16a1f2783..b0c633042 100644 --- a/engine/commands/engine_init_cmd.cc +++ b/engine/commands/engine_init_cmd.cc @@ -191,8 +191,8 @@ bool EngineInitCmd::Exec() const { std::string engine_path = file_manager_utils::GetCortexDataPath().string() + get_engine_path(engineName_); - archive_utils::ExtractArchive(finishedTask.items[0].localPath, - engine_path); + archive_utils::ExtractArchive( + finishedTask.items[0].localPath.string(), engine_path); try { std::filesystem::remove(finishedTask.items[0].localPath); diff --git a/engine/test/components/test_url_parser.cc b/engine/test/components/test_url_parser.cc new file mode 100644 index 000000000..375ce385d --- /dev/null +++ b/engine/test/components/test_url_parser.cc @@ -0,0 +1,26 @@ +#include "gtest/gtest.h" +#include "utils/url_parser.h" + +class UrlParserTestSuite : public ::testing::Test { + protected: + constexpr static auto kValidUrlWithOnlyPaths{"https://jan.ai/path1/path2"}; +}; + +TEST_F(UrlParserTestSuite, TestParseUrlCorrectly) { + auto url = url_parser::FromUrlString(kValidUrlWithOnlyPaths); + + EXPECT_EQ(url.host, "jan.ai"); + EXPECT_EQ(url.protocol, "https"); + EXPECT_EQ(url.pathParams.size(), 2); +} + +TEST_F(UrlParserTestSuite, ConstructUrlCorrectly) { + auto url = url_parser::Url{ + .protocol = "https", + .host = "jan.ai", + .pathParams = {"path1", "path2"}, + }; + auto url_str = url_parser::FromUrl(url); + + EXPECT_EQ(url_str, kValidUrlWithOnlyPaths); +} diff --git a/engine/utils/url_parser.h b/engine/utils/url_parser.h index b879fc95a..dba3ec0a5 100644 --- a/engine/utils/url_parser.h +++ b/engine/utils/url_parser.h @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "exceptions/malformed_url_exception.h" @@ -26,6 +27,9 @@ inline void SplitPathParams(const std::string& input, std::string token; std::istringstream tokenStream(input); while (std::getline(tokenStream, token, '/')) { + if (token.empty()) { + continue; + } pathList.push_back(token); } }