test_123/tests/test_adapter.cpp

89 lines
3.1 KiB
C++
Raw Normal View History

2026-05-16 05:16:04 +00:00
#include <gtest/gtest.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#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<char*> CreateArgv(const std::vector<std::string>& args) {
std::vector<char*> argv;
for (const auto& arg : args) {
argv.push_back(const_cast<char*>(arg.c_str()));
}
return argv;
}
};
TEST_F(CommandLineAdapterTest, testCommandLineAdapterNormalInput) {
std::vector<std::string> args = {"app", "--help"};
auto argv = CreateArgv(args);
CommandLineAdapter adapter(static_cast<int>(args.size()), argv.data());
CommandLineArgs result = adapter.parse();
EXPECT_TRUE(result.show_help);
EXPECT_FALSE(result.has_errors);
}
TEST_F(CommandLineAdapterTest, testParseConfigMissingValueException) {
std::vector<std::string> args = {"app", "--config"};
auto argv = CreateArgv(args);
CommandLineAdapter adapter(static_cast<int>(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<std::string> args = {"app", "--unknown"};
auto argv = CreateArgv(args);
CommandLineAdapter adapter(static_cast<int>(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<std::string> args = {"app", "--config", "cfg.ini", "--version"};
auto argv = CreateArgv(args);
CommandLineAdapter adapter(static_cast<int>(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<std::string> args = {"app"};
auto argv = CreateArgv(args);
CommandLineAdapter adapter(static_cast<int>(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<std::string> args = {"app"};
auto argv = CreateArgv(args);
CommandLineAdapter adapter(static_cast<int>(args.size()), argv.data());
adapter.printVersion();
std::string output = capture.GetOutput();
EXPECT_EQ(output, "ModularApp version 1.0.0\n");
}