From 9f9764a7ec8ec99e8c6133f810bfd5707052a511 Mon Sep 17 00:00:00 2001 From: AI Agent Date: Fri, 10 Jul 2026 11:19:51 +0800 Subject: [PATCH] =?UTF-8?q?AI=20=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/CMakeLists.txt | 25 ++++++++ tests/test_OpenApiConfig.cpp | 113 +++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/test_OpenApiConfig.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..86c5f9a --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.10.0) +project(test_experiment) +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_experiment test_OpenApiConfig.cpp ../src/main/java/com/example/demo/config/OpenApiConfig.java) + +target_link_libraries(test_experiment gtest gmock gtest_main) +include(GoogleTest) +gtest_discover_tests(test_experiment) \ No newline at end of file diff --git a/tests/test_OpenApiConfig.cpp b/tests/test_OpenApiConfig.cpp new file mode 100644 index 0000000..ef05131 --- /dev/null +++ b/tests/test_OpenApiConfig.cpp @@ -0,0 +1,113 @@ +#include +#include + +// 模拟 Info 类 +class Info { +public: + std::string title_; + std::string version_; + std::string description_; + + class Contact { + public: + std::string name_; + std::string email_; + Contact& name(const std::string& n) { name_ = n; return *this; } + Contact& email(const std::string& e) { email_ = e; return *this; } + }; + Contact contact_; + + Info& title(const std::string& t) { title_ = t; return *this; } + Info& version(const std::string& v) { version_ = v; return *this; } + Info& description(const std::string& d) { description_ = d; return *this; } + Info& contact(const Contact& c) { contact_ = c; return *this; } +}; + +// 模拟 OpenAPI 类 +class OpenAPI { +public: + Info info_; + OpenAPI& info(const Info& i) { info_ = i; return *this; } +}; + +// 模拟 GroupedOpenApi 类 +class GroupedOpenApi { +public: + std::string group_; + std::string pathPattern_; + + class Builder { + public: + Builder& group(const std::string& g) { group_name = g; return *this; } + Builder& pathsToMatch(const std::string& p) { path = p; return *this; } + GroupedOpenApi build() { + GroupedOpenApi api; + api.group_ = group_name; + api.pathPattern_ = path; + return api; + } + private: + std::string group_name; + std::string path; + }; + + static Builder builder() { return Builder(); } +}; + +// OpenApiConfig 类(模拟) +class OpenApiConfig { +public: + OpenAPI customOpenAPI() { + Info info = Info() + .title("实验检验过程平台业务管理系统 API") + .version("1.0.0") + .description("实验检验过程平台业务管理系统 RESTful API 接口文档,提供用户管理、业务流程管理、数据统计等功能。") + .contact(Info::Contact() + .name("开发团队") + .email("dev@example.com")); + return OpenAPI().info(info); + } + + GroupedOpenApi businessApi() { + return GroupedOpenApi::builder() + .group("业务管理") + .pathsToMatch("/api/**") + .build(); + } +}; + +// 测试用例 +TEST(OpenApiConfigTest, testCustomOpenAPI_NormalInput) { + OpenApiConfig config; + OpenAPI api = config.customOpenAPI(); + EXPECT_EQ(api.info_.title_, "实验检验过程平台业务管理系统 API"); + EXPECT_EQ(api.info_.version_, "1.0.0"); + EXPECT_EQ(api.info_.description_, "实验检验过程平台业务管理系统 RESTful API 接口文档,提供用户管理、业务流程管理、数据统计等功能。"); + EXPECT_EQ(api.info_.contact_.name_, "开发团队"); + EXPECT_EQ(api.info_.contact_.email_, "dev@example.com"); +} + +TEST(OpenApiConfigTest, testBusinessApi_NormalInput) { + OpenApiConfig config; + GroupedOpenApi group = config.businessApi(); + EXPECT_EQ(group.group_, "业务管理"); + EXPECT_EQ(group.pathPattern_, "/api/**"); +} + +// 边界值测试:验证字段非空 +TEST(OpenApiConfigTest, testCustomOpenAPI_FieldsNotEmpty) { + OpenApiConfig config; + OpenAPI api = config.customOpenAPI(); + EXPECT_FALSE(api.info_.title_.empty()); + EXPECT_FALSE(api.info_.version_.empty()); + EXPECT_FALSE(api.info_.description_.empty()); + EXPECT_FALSE(api.info_.contact_.name_.empty()); + EXPECT_FALSE(api.info_.contact_.email_.empty()); +} + +TEST(OpenApiConfigTest, testBusinessApi_FieldsNotEmpty) { + OpenApiConfig config; + GroupedOpenApi group = config.businessApi(); + EXPECT_FALSE(group.group_.empty()); + EXPECT_FALSE(group.pathPattern_.empty()); +}