project/tests/TestOpenApiConfig.cpp

117 lines
4.2 KiB
C++
Raw Permalink Normal View History

2026-07-08 08:58:48 +00:00
#include <gtest/gtest.h>
#include <string>
#include <regex>
// 模拟 OpenAPI 相关类,避免外部头文件依赖
class License {
public:
std::string m_name;
std::string m_url;
License& name(const std::string& n) { m_name = n; return *this; }
License& url(const std::string& u) { m_url = u; return *this; }
std::string getName() const { return m_name; }
std::string getUrl() const { return m_url; }
};
class Contact {
public:
std::string m_name;
std::string m_email;
Contact& name(const std::string& n) { m_name = n; return *this; }
Contact& email(const std::string& e) { m_email = e; return *this; }
std::string getName() const { return m_name; }
std::string getEmail() const { return m_email; }
};
class Info {
public:
std::string m_title;
std::string m_version;
std::string m_description;
Contact m_contact;
License m_license;
Info& title(const std::string& t) { m_title = t; return *this; }
Info& version(const std::string& v) { m_version = v; return *this; }
Info& description(const std::string& d) { m_description = d; return *this; }
Info& contact(const Contact& c) { m_contact = c; return *this; }
Info& license(const License& l) { m_license = l; return *this; }
std::string getTitle() const { return m_title; }
std::string getVersion() const { return m_version; }
std::string getDescription() const { return m_description; }
Contact getContact() const { return m_contact; }
License getLicense() const { return m_license; }
};
class OpenAPI {
public:
Info m_info;
OpenAPI& info(const Info& i) { m_info = i; return *this; }
Info getInfo() const { return m_info; }
};
// 待测配置类
class OpenApiConfig {
public:
OpenAPI customOpenAPI() {
Contact contact;
contact.name("开发团队").email("dev@example.com");
License license;
license.name("Apache 2.0").url("https://www.apache.org/licenses/LICENSE-2.0");
Info info;
info.title("综合管理信息系统 API")
.version("1.0.0")
.description("综合管理信息系统后端接口文档,涵盖用户管理、角色管理、部门管理、"
"字典管理、参数配置、业务管理、统计分析、权限管理、审计日志及接口集成等模块。")
.contact(contact)
.license(license);
OpenAPI api;
api.info(info);
return api;
}
};
class OpenApiConfigTest : public ::testing::Test {
protected:
OpenApiConfig config;
};
// 正常输入测试:验证返回的 OpenAPI 对象各字段是否正确
TEST_F(OpenApiConfigTest, testCustomOpenAPI_ReturnsCorrectInfo) {
OpenAPI api = config.customOpenAPI();
const Info& info = api.getInfo();
EXPECT_EQ(info.getTitle(), "综合管理信息系统 API");
EXPECT_EQ(info.getVersion(), "1.0.0");
EXPECT_EQ(info.getDescription(),
"综合管理信息系统后端接口文档,涵盖用户管理、角色管理、部门管理、"
"字典管理、参数配置、业务管理、统计分析、权限管理、审计日志及接口集成等模块。");
const Contact& contact = info.getContact();
EXPECT_EQ(contact.getName(), "开发团队");
EXPECT_EQ(contact.getEmail(), "dev@example.com");
const License& license = info.getLicense();
EXPECT_EQ(license.getName(), "Apache 2.0");
EXPECT_EQ(license.getUrl(), "https://www.apache.org/licenses/LICENSE-2.0");
}
// 边界值测试:版本号格式必须符合语义化版本
TEST_F(OpenApiConfigTest, testCustomOpenAPI_VersionFormat) {
OpenAPI api = config.customOpenAPI();
std::string version = api.getInfo().getVersion();
std::regex versionPattern(R"(\d+\.\d+\.\d+)");
EXPECT_TRUE(std::regex_match(version, versionPattern));
}
// 边界值测试:描述信息非空
TEST_F(OpenApiConfigTest, testCustomOpenAPI_DescriptionNotNull) {
OpenAPI api = config.customOpenAPI();
EXPECT_FALSE(api.getInfo().getDescription().empty());
}
// 特殊场景测试:多次调用返回不同对象(每次都是新实例)
TEST_F(OpenApiConfigTest, testCustomOpenAPI_MultipleCallsReturnDifferentObjects) {
OpenAPI api1 = config.customOpenAPI();
OpenAPI api2 = config.customOpenAPI();
EXPECT_NE(&api1, &api2);
}