AI 自动生成测试用例

This commit is contained in:
linianlin 2026-05-16 13:16:04 +08:00
parent 04d4da3e17
commit 60d650d290
2 changed files with 114 additions and 0 deletions

25
tests/CMakeLists.txt Normal file
View File

@ -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)

89
tests/test_adapter.cpp Normal file
View File

@ -0,0 +1,89 @@
#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");
}