#include #include #include // 假设的端口详细信息结构体 struct PortDetail { int id; std::string unit; std::string rack; // 为简化测试,添加 operator== bool operator==(const PortDetail& other) const { return id == other.id && unit == other.unit && rack == other.rack; } }; // 假设的 OdfService 类 class OdfService { public: std::optional get_port_detail(int port_id) { // 模拟实现:只有 1 和 2 存在 if (port_id == 1) { return PortDetail{1, "Unit-A", "Rack-01"}; } else if (port_id == 2) { return PortDetail{2, "Unit-B", "Rack-02"}; } return std::nullopt; } }; class OdfServiceTest : public ::testing::Test { protected: OdfService service; }; // 正常输入测试:存在的端口 ID 返回正确的信息 TEST_F(OdfServiceTest, testGetPortDetailNormalCase) { auto result = service.get_port_detail(1); ASSERT_TRUE(result.has_value()); PortDetail expected{1, "Unit-A", "Rack-01"}; EXPECT_EQ(result.value(), expected); } // 边界值测试:端口 ID 为 0 TEST_F(OdfServiceTest, testGetPortDetailBoundaryZero) { auto result = service.get_port_detail(0); EXPECT_FALSE(result.has_value()); } // 边界值测试:端口 ID 为负数 TEST_F(OdfServiceTest, testGetPortDetailBoundaryNegative) { auto result = service.get_port_detail(-1); EXPECT_FALSE(result.has_value()); } // 异常输入测试:不存在的端口 ID TEST_F(OdfServiceTest, testGetPortDetailNonexistent) { auto result = service.get_port_detail(999); EXPECT_FALSE(result.has_value()); } // 特殊场景测试:验证不同端口返回不同单元和机架 TEST_F(OdfServiceTest, testGetPortDetailDifferentUnits) { auto result1 = service.get_port_detail(1); ASSERT_TRUE(result1.has_value()); EXPECT_EQ(result1.value().unit, "Unit-A"); EXPECT_EQ(result1.value().rack, "Rack-01"); auto result2 = service.get_port_detail(2); ASSERT_TRUE(result2.has_value()); EXPECT_EQ(result2.value().unit, "Unit-B"); EXPECT_EQ(result2.value().rack, "Rack-02"); }