diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..36821a3 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.10.0) +project(test_test_123) +include(FetchContent) +if (MSVC) + add_compile_options(/utf-8) +endif() +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip +) +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include) + +include(CTest) +enable_testing() + +add_executable(test_test_123 test_adapter.cpp ../src/adapter.cpp) + +target_link_libraries(test_test_123 gtest gmock gtest_main) +include(GoogleTest) +gtest_discover_tests(test_test_123) \ No newline at end of file diff --git a/tests/test_adapter.cpp b/tests/test_adapter.cpp new file mode 100644 index 0000000..3239a79 --- /dev/null +++ b/tests/test_adapter.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include "adapter.hpp" + +class StdoutCapture { +public: + StdoutCapture() { + old_buf_ = std::cout.rdbuf(); + std::cout.rdbuf(buffer_.rdbuf()); + } + ~StdoutCapture() { std::cout.rdbuf(old_buf_); } + std::string GetOutput() const { return buffer_.str(); } +private: + std::ostringstream buffer_; + std::streambuf* old_buf_; +}; + +class CommandLineAdapterTest : public ::testing::Test { +protected: + std::vector CreateArgv(const std::vector& args) { + std::vector argv; + for (const auto& arg : args) { + argv.push_back(const_cast(arg.c_str())); + } + return argv; + } +}; + +TEST_F(CommandLineAdapterTest, testCommandLineAdapterNormalInput) { + std::vector args = {"app", "--help"}; + auto argv = CreateArgv(args); + CommandLineAdapter adapter(static_cast(args.size()), argv.data()); + CommandLineArgs result = adapter.parse(); + EXPECT_TRUE(result.show_help); + EXPECT_FALSE(result.has_errors); +} + +TEST_F(CommandLineAdapterTest, testParseConfigMissingValueException) { + std::vector args = {"app", "--config"}; + auto argv = CreateArgv(args); + CommandLineAdapter adapter(static_cast(args.size()), argv.data()); + CommandLineArgs result = adapter.parse(); + EXPECT_TRUE(result.has_errors); + EXPECT_EQ(result.error_msg, "Missing value for --config"); +} + +TEST_F(CommandLineAdapterTest, testParseUnknownArgumentException) { + std::vector args = {"app", "--unknown"}; + auto argv = CreateArgv(args); + CommandLineAdapter adapter(static_cast(args.size()), argv.data()); + CommandLineArgs result = adapter.parse(); + EXPECT_TRUE(result.has_errors); + EXPECT_EQ(result.error_msg, "Unknown argument: --unknown"); +} + +TEST_F(CommandLineAdapterTest, testParseMultipleFlagsNormal) { + std::vector args = {"app", "--config", "cfg.ini", "--version"}; + auto argv = CreateArgv(args); + CommandLineAdapter adapter(static_cast(args.size()), argv.data()); + CommandLineArgs result = adapter.parse(); + EXPECT_EQ(result.config_path, "cfg.ini"); + EXPECT_TRUE(result.show_version); + EXPECT_FALSE(result.has_errors); +} + +TEST_F(CommandLineAdapterTest, testPrintHelpSpecialScenario) { + StdoutCapture capture; + std::vector args = {"app"}; + auto argv = CreateArgv(args); + CommandLineAdapter adapter(static_cast(args.size()), argv.data()); + adapter.printHelp(); + std::string output = capture.GetOutput(); + EXPECT_NE(output.find("ModularApp v1.0.0"), std::string::npos); + EXPECT_NE(output.find("--config"), std::string::npos); + EXPECT_NE(output.find("--help"), std::string::npos); +} + +TEST_F(CommandLineAdapterTest, testPrintVersionSpecialScenario) { + StdoutCapture capture; + std::vector args = {"app"}; + auto argv = CreateArgv(args); + CommandLineAdapter adapter(static_cast(args.size()), argv.data()); + adapter.printVersion(); + std::string output = capture.GetOutput(); + EXPECT_EQ(output, "ModularApp version 1.0.0\n"); +} \ No newline at end of file