拆出mcp

This commit is contained in:
2026-08-28 10:11:40 +08:00
parent 5c2be8eef0
commit 81d3d50966
32 changed files with 1179 additions and 238 deletions
+72
View File
@@ -0,0 +1,72 @@
#include <mcp/core/Render_Types.hpp>
#include <gtest/gtest.h>
#include <string>
#include <vector>
namespace aethera::mcp::tests {
enum struct Sample_Mode : std::uint8_t {
first,
second
};
struct Nested_Value {
double gain{}; /* 测试嵌套数值。 */
Sample_Mode mode{Sample_Mode::first}; /* 测试枚举描述。 */
bool operator==(const Nested_Value&) const = default;
};
struct Request_Value {
std::string plot; /* 测试字段名反射。 */
std::vector<Nested_Value> values; /* 测试递归容器。 */
bool operator==(const Request_Value&) const = default;
};
struct Semantic_Value {
int value{}; /* 自定义类型值。 */
bool operator==(const Semantic_Value&) const = default;
};
struct Semantic_Value_Type : Aggregate_Type<Semantic_Value> {
[[nodiscard]] static constexpr std::string_view editor() noexcept {
return "semantic-value";
}
};
/* 自定义类型把精确重载直接放在 struct 定义之后;PFR 通用分派无需修改。 */
[[nodiscard]] auto match_protocol_type(
Type_Tag<Semantic_Value>) -> Semantic_Value_Type;
TEST(Protocol_Type, Pfr_Field_Names_Drive_Recursive_Schema) {
const auto schema = describe_protocol_type<Request_Value>();
ASSERT_EQ(schema.at("type"), "object");
ASSERT_TRUE(schema.at("properties").contains("plot"));
ASSERT_TRUE(schema.at("properties").contains("values"));
const auto& nested = schema.at("properties").at("values").at("items");
EXPECT_TRUE(nested.at("properties").contains("gain"));
EXPECT_EQ(nested.at("properties").at("mode").at("enum").size(), 2);
}
TEST(Protocol_Type, Pfr_Codec_Round_Trips_Nested_Values) {
const Request_Value input{"plot-3d", {{1.5, Sample_Mode::second}}};
const auto encoded = encode_protocol_value(input);
Request_Value decoded{};
decode_protocol_value(decoded, encoded);
EXPECT_EQ(decoded, input);
}
TEST(Protocol_Type, Exact_Matcher_Overload_Wins_Without_If_Dispatch) {
EXPECT_EQ(protocol_editor<Semantic_Value>(), "semantic-value");
EXPECT_EQ(encode_protocol_value(Semantic_Value{7}).at("value"), 7);
}
TEST(Protocol_Type, Non_Aggregate_Render_Type_Has_Explicit_Matcher) {
render_2d::Color_Map color_map;
color_map.stops = {Color::red_color(), Color::green_color()};
const auto encoded = encode_protocol_value(color_map);
render_2d::Color_Map decoded;
decode_protocol_value(decoded, encoded);
EXPECT_EQ(decoded, color_map);
}
}