♻️ 使用module重构所有代码

This commit is contained in:
csh
2025-12-07 23:07:03 +08:00
parent 549f1d1b0a
commit f7d5a74615
369 changed files with 2272844 additions and 2202476 deletions
+45 -11
View File
@@ -4,6 +4,7 @@ project(test_lsp_any LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)
find_package(glaze CONFIG REQUIRED)
@@ -12,21 +13,54 @@ if(UNIX AND NOT APPLE)
endif()
set(SOURCES
common_test.cpp
facade_test.cpp
lsp_any_test.cpp
test_framework.cpp
test_main.cpp
transformer_test.cpp)
test_main.cppm)
add_executable(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_sources(
${PROJECT_NAME}
PRIVATE
FILE_SET cxx_modules TYPE CXX_MODULES
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../../src
FILES ../../src/bridge/glaze.cppm
test_main.cppm
test_framework.cppm
common_test.cppm
lsp_any_test.cppm
transformer_test.cppm
facade_test.cppm
../../src/protocol/common/basic_types.cppm
../../src/protocol/common/message.cppm
../../src/protocol/common/registration.cppm
../../src/protocol/window/progress.cppm
../../src/protocol/initialize/configuration.cppm
../../src/protocol/initialize/capabilities.cppm
../../src/protocol/workspace/workspace.cppm
../../src/protocol/workspace/file_operations.cppm
../../src/protocol/workspace/notebook.cppm
../../src/protocol/text_document/document_sync.cppm
../../src/protocol/text_document/completion.cppm
../../src/protocol/text_document/code_actions.cppm
../../src/protocol/text_document/diagnostics.cppm
../../src/protocol/text_document/document_features.cppm
../../src/protocol/text_document/formatting.cppm
../../src/protocol/text_document/inline_features.cppm
../../src/protocol/text_document/navigation.cppm
../../src/protocol/text_document/rename.cppm
../../src/protocol/text_document/semantic_tokens.cppm
../../src/protocol/text_document/signature_help.cppm
../../src/protocol/text_document/symbols.cppm
../../src/codec/common.cppm
../../src/codec/transformer.cppm
../../src/codec/facade.cppm
../../src/protocol/types.cppm
../../src/protocol/protocol.cppm)
target_link_libraries(${PROJECT_NAME} PRIVATE glaze::glaze
$<$<PLATFORM_ID:Linux>:Threads::Threads>)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(
${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic $<$<CONFIG:Debug>:-g -O0>
$<$<CONFIG:Release>:-O3>)
endif()
target_compile_options(
${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic $<$<CONFIG:Debug>:-g -O0>
-Wno-import-implementation-partition-unit-in-interface-unit
$<$<CONFIG:Release>:-O3>)
@@ -1,325 +0,0 @@
#include "common_test.hpp"
#include <vector>
#include <map>
#include <optional>
#include <variant>
#include "../../src/protocol/transform/common.hpp"
#include "../../src/protocol/detail/basic_types.hpp"
namespace lsp::test
{
// 用于测试的用户自定义结构体
struct UserStruct
{
int value;
std::string name;
};
void CommonTests::registerTests(TestRunner& runner)
{
// is_lsp_basic_type 测试
runner.addTest("Common - is_lsp_basic_type (integer)", testIsLSPBasicInteger);
runner.addTest("Common - is_lsp_basic_type (uinteger)", testIsLSPBasicUInteger);
runner.addTest("Common - is_lsp_basic_type (boolean)", testIsLSPBasicBoolean);
runner.addTest("Common - is_lsp_basic_type (string)", testIsLSPBasicString);
runner.addTest("Common - is_lsp_basic_type (decimal)", testIsLSPBasicDecimal);
runner.addTest("Common - is_lsp_basic_type (nullptr_t)", testIsLSPBasicNullptr);
runner.addTest("Common - is_lsp_basic_type (负面案例)", testIsLSPBasicNegativeCases);
// is_lsp_container_type 测试
runner.addTest("Common - is_lsp_container_type (LSPObject)", testIsLSPContainerObject);
runner.addTest("Common - is_lsp_container_type (LSPArray)", testIsLSPContainerArray);
runner.addTest("Common - is_lsp_container_type (LSPAny)", testIsLSPContainerAny);
runner.addTest("Common - is_lsp_container_type (负面案例)", testIsLSPContainerNegativeCases);
// is_user_struct 测试
runner.addTest("Common - is_user_struct (用户结构体)", testIsUserStructPositive);
runner.addTest("Common - is_user_struct (排除算术类型)", testIsUserStructExcludeArithmetic);
runner.addTest("Common - is_user_struct (排除字符串)", testIsUserStructExcludeString);
runner.addTest("Common - is_user_struct (排除LSP类型)", testIsUserStructExcludeLSPTypes);
runner.addTest("Common - is_user_struct (排除vector)", testIsUserStructExcludeVector);
runner.addTest("Common - is_user_struct (排除map)", testIsUserStructExcludeMap);
runner.addTest("Common - is_user_struct (排除optional)", testIsUserStructExcludeOptional);
runner.addTest("Common - is_user_struct (排除variant)", testIsUserStructExcludeVariant);
runner.addTest("Common - is_user_struct (排除指针)", testIsUserStructExcludePointers);
}
// ==================== is_lsp_basic_type 测试 ====================
TestResult CommonTests::testIsLSPBasicInteger()
{
TestResult result;
result.passed = true;
bool is_basic = transform::is_lsp_basic_type_v<protocol::integer>;
assertTrue(is_basic, "integer应该是LSP基本类型");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsLSPBasicUInteger()
{
TestResult result;
result.passed = true;
bool is_basic = transform::is_lsp_basic_type_v<protocol::uinteger>;
assertTrue(is_basic, "uinteger应该是LSP基本类型");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsLSPBasicBoolean()
{
TestResult result;
result.passed = true;
bool is_basic = transform::is_lsp_basic_type_v<protocol::boolean>;
assertTrue(is_basic, "boolean应该是LSP基本类型");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsLSPBasicString()
{
TestResult result;
result.passed = true;
bool is_basic = transform::is_lsp_basic_type_v<protocol::string>;
assertTrue(is_basic, "string应该是LSP基本类型");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsLSPBasicDecimal()
{
TestResult result;
result.passed = true;
bool is_basic = transform::is_lsp_basic_type_v<protocol::decimal>;
assertTrue(is_basic, "decimal应该是LSP基本类型");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsLSPBasicNullptr()
{
TestResult result;
result.passed = true;
bool is_basic = transform::is_lsp_basic_type_v<std::nullptr_t>;
assertTrue(is_basic, "nullptr_t应该是LSP基本类型");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsLSPBasicNegativeCases()
{
TestResult result;
result.passed = true;
// 测试明确不是 LSP 基本类型的类型
// 注意:避免测试 int/int32_t,因为它们可能等于 protocol::integer
assertFalse(transform::is_lsp_basic_type_v<int64_t>,
"int64_t不应该是LSP基本类型");
assertFalse(transform::is_lsp_basic_type_v<long long>,
"long long不应该是LSP基本类型");
assertFalse(transform::is_lsp_basic_type_v<float>,
"float不应该是LSP基本类型");
assertFalse(transform::is_lsp_basic_type_v<std::vector<int>>,
"vector不应该是LSP基本类型");
assertFalse(transform::is_lsp_basic_type_v<UserStruct>,
"UserStruct不应该是LSP基本类型");
result.message = "成功";
return result;
}
// ==================== is_lsp_container_type 测试 ====================
TestResult CommonTests::testIsLSPContainerObject()
{
TestResult result;
result.passed = true;
bool is_container = transform::is_lsp_container_type_v<protocol::LSPObject>;
assertTrue(is_container, "LSPObject应该是LSP容器类型");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsLSPContainerArray()
{
TestResult result;
result.passed = true;
bool is_container = transform::is_lsp_container_type_v<protocol::LSPArray>;
assertTrue(is_container, "LSPArray应该是LSP容器类型");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsLSPContainerAny()
{
TestResult result;
result.passed = true;
bool is_container = transform::is_lsp_container_type_v<protocol::LSPAny>;
assertTrue(is_container, "LSPAny应该是LSP容器类型");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsLSPContainerNegativeCases()
{
TestResult result;
result.passed = true;
assertFalse(transform::is_lsp_container_type_v<int>,
"int不应该是LSP容器");
assertFalse(transform::is_lsp_container_type_v<std::string>,
"string不应该是LSP容器");
assertFalse(transform::is_lsp_container_type_v<std::vector<int>>,
"std::vector不应该是LSP容器");
result.message = "成功";
return result;
}
// ==================== is_user_struct 测试 ====================
TestResult CommonTests::testIsUserStructPositive()
{
TestResult result;
result.passed = true;
bool is_user = transform::is_user_struct_v<UserStruct>;
assertTrue(is_user, "UserStruct应该被识别为用户结构体");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsUserStructExcludeArithmetic()
{
TestResult result;
result.passed = true;
assertFalse(transform::is_user_struct_v<int>,
"int不应该是用户结构体");
assertFalse(transform::is_user_struct_v<double>,
"double不应该是用户结构体");
assertFalse(transform::is_user_struct_v<bool>,
"bool不应该是用户结构体");
assertFalse(transform::is_user_struct_v<char>,
"char不应该是用户结构体");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsUserStructExcludeString()
{
TestResult result;
result.passed = true;
assertFalse(transform::is_user_struct_v<std::string>,
"std::string不应该是用户结构体");
assertFalse(transform::is_user_struct_v<protocol::string>,
"protocol::string不应该是用户结构体");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsUserStructExcludeLSPTypes()
{
TestResult result;
result.passed = true;
assertFalse(transform::is_user_struct_v<protocol::LSPObject>,
"LSPObject不应该是用户结构体");
assertFalse(transform::is_user_struct_v<protocol::LSPArray>,
"LSPArray不应该是用户结构体");
assertFalse(transform::is_user_struct_v<protocol::LSPAny>,
"LSPAny不应该是用户结构体");
assertFalse(transform::is_user_struct_v<protocol::integer>,
"protocol::integer不应该是用户结构体");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsUserStructExcludeVector()
{
TestResult result;
result.passed = true;
bool is_user = transform::is_user_struct_v<std::vector<int>>;
assertFalse(is_user, "std::vector不应该是用户结构体");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsUserStructExcludeMap()
{
TestResult result;
result.passed = true;
bool is_user = transform::is_user_struct_v<std::map<std::string, int>>;
assertFalse(is_user, "std::map不应该是用户结构体");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsUserStructExcludeOptional()
{
TestResult result;
result.passed = true;
bool is_user = transform::is_user_struct_v<std::optional<int>>;
assertFalse(is_user, "std::optional不应该是用户结构体");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsUserStructExcludeVariant()
{
TestResult result;
result.passed = true;
bool is_user = transform::is_user_struct_v<std::variant<int, std::string>>;
assertFalse(is_user, "std::variant不应该是用户结构体");
result.message = "成功";
return result;
}
TestResult CommonTests::testIsUserStructExcludePointers()
{
TestResult result;
result.passed = true;
assertFalse(transform::is_user_struct_v<int*>,
"int*不应该是用户结构体");
assertFalse(transform::is_user_struct_v<const char*>,
"const char*不应该是用户结构体");
assertFalse(transform::is_user_struct_v<UserStruct*>,
"UserStruct*不应该是用户结构体");
result.message = "成功";
return result;
}
} // namespace lsp::test
@@ -0,0 +1,220 @@
module;
export module lsp.test.lsp_any.common;
import std;
import lsp.test.framework;
import lsp.protocol.common.basic_types;
import lsp.codec.common;
export namespace lsp::test
{
namespace transform = lsp::codec;
class CommonTests
{
public:
static void registerTests(TestRunner& runner);
private:
static TestResult testIsLSPBasicInteger();
static TestResult testIsLSPBasicUInteger();
static TestResult testIsLSPBasicBoolean();
static TestResult testIsLSPBasicString();
static TestResult testIsLSPBasicDecimal();
static TestResult testIsLSPBasicNullptr();
static TestResult testIsLSPBasicNegativeCases();
static TestResult testIsLSPContainerObject();
static TestResult testIsLSPContainerArray();
static TestResult testIsLSPContainerAny();
static TestResult testIsLSPContainerNegativeCases();
static TestResult testIsUserStructPositive();
static TestResult testIsUserStructExcludeArithmetic();
static TestResult testIsUserStructExcludeString();
static TestResult testIsUserStructExcludeLSPTypes();
static TestResult testIsUserStructExcludeVector();
static TestResult testIsUserStructExcludeMap();
static TestResult testIsUserStructExcludeOptional();
static TestResult testIsUserStructExcludeVariant();
static TestResult testIsUserStructExcludePointers();
};
}
namespace lsp::test
{
struct UserStruct
{
int value;
std::string name;
};
void CommonTests::registerTests(TestRunner& runner)
{
runner.addTest("Common - is_lsp_basic_type (integer)", testIsLSPBasicInteger);
runner.addTest("Common - is_lsp_basic_type (uinteger)", testIsLSPBasicUInteger);
runner.addTest("Common - is_lsp_basic_type (boolean)", testIsLSPBasicBoolean);
runner.addTest("Common - is_lsp_basic_type (string)", testIsLSPBasicString);
runner.addTest("Common - is_lsp_basic_type (decimal)", testIsLSPBasicDecimal);
runner.addTest("Common - is_lsp_basic_type (nullptr_t)", testIsLSPBasicNullptr);
runner.addTest("Common - is_lsp_basic_type (负面案例)", testIsLSPBasicNegativeCases);
runner.addTest("Common - is_lsp_container_type (LSPObject)", testIsLSPContainerObject);
runner.addTest("Common - is_lsp_container_type (LSPArray)", testIsLSPContainerArray);
runner.addTest("Common - is_lsp_container_type (LSPAny)", testIsLSPContainerAny);
runner.addTest("Common - is_lsp_container_type (负面案例)", testIsLSPContainerNegativeCases);
runner.addTest("Common - is_user_struct (用户结构体)", testIsUserStructPositive);
runner.addTest("Common - is_user_struct (排除算术类型)", testIsUserStructExcludeArithmetic);
runner.addTest("Common - is_user_struct (排除字符串)", testIsUserStructExcludeString);
runner.addTest("Common - is_user_struct (排除LSP类型)", testIsUserStructExcludeLSPTypes);
runner.addTest("Common - is_user_struct (排除vector)", testIsUserStructExcludeVector);
runner.addTest("Common - is_user_struct (排除map)", testIsUserStructExcludeMap);
runner.addTest("Common - is_user_struct (排除optional)", testIsUserStructExcludeOptional);
runner.addTest("Common - is_user_struct (排除variant)", testIsUserStructExcludeVariant);
runner.addTest("Common - is_user_struct (排除指针)", testIsUserStructExcludePointers);
}
TestResult CommonTests::testIsLSPBasicInteger()
{
TestResult result{ "", true, "成功" };
assertTrue(transform::is_lsp_basic_type_v<protocol::integer>, "integer应该是LSP基本类型");
return result;
}
TestResult CommonTests::testIsLSPBasicUInteger()
{
TestResult result{ "", true, "成功" };
assertTrue(transform::is_lsp_basic_type_v<protocol::uinteger>, "uinteger应该是LSP基本类型");
return result;
}
TestResult CommonTests::testIsLSPBasicBoolean()
{
TestResult result{ "", true, "成功" };
assertTrue(transform::is_lsp_basic_type_v<protocol::boolean>, "boolean应该是LSP基本类型");
return result;
}
TestResult CommonTests::testIsLSPBasicString()
{
TestResult result{ "", true, "成功" };
assertTrue(transform::is_lsp_basic_type_v<protocol::string>, "string应该是LSP基本类型");
return result;
}
TestResult CommonTests::testIsLSPBasicDecimal()
{
TestResult result{ "", true, "成功" };
assertTrue(transform::is_lsp_basic_type_v<protocol::decimal>, "decimal应该是LSP基本类型");
return result;
}
TestResult CommonTests::testIsLSPBasicNullptr()
{
TestResult result{ "", true, "成功" };
assertTrue(transform::is_lsp_basic_type_v<std::nullptr_t>, "nullptr应该是LSP基本类型");
return result;
}
TestResult CommonTests::testIsLSPBasicNegativeCases()
{
TestResult result{ "", true, "成功" };
assertFalse(transform::is_lsp_basic_type_v<std::vector<int>>, "vector<int>不应是基本类型");
assertFalse(transform::is_lsp_basic_type_v<UserStruct>, "用户结构体不应是基本类型");
return result;
}
TestResult CommonTests::testIsLSPContainerObject()
{
TestResult result{ "", true, "成功" };
assertTrue(transform::is_lsp_container_type_v<protocol::LSPObject>, "LSPObject应该是容器类型");
return result;
}
TestResult CommonTests::testIsLSPContainerArray()
{
TestResult result{ "", true, "成功" };
assertTrue(transform::is_lsp_container_type_v<protocol::LSPArray>, "LSPArray应该是容器类型");
return result;
}
TestResult CommonTests::testIsLSPContainerAny()
{
TestResult result{ "", true, "成功" };
assertTrue(transform::is_lsp_container_type_v<protocol::LSPAny>, "LSPAny应该是容器类型");
return result;
}
TestResult CommonTests::testIsLSPContainerNegativeCases()
{
TestResult result{ "", true, "成功" };
assertFalse(transform::is_lsp_container_type_v<int>, "int不是容器类型");
assertFalse(transform::is_lsp_container_type_v<std::vector<int>>, "vector<int>不是容器类型");
return result;
}
TestResult CommonTests::testIsUserStructPositive()
{
TestResult result{ "", true, "成功" };
assertTrue(transform::is_user_struct_v<UserStruct>, "UserStruct应该被识别为用户结构体");
return result;
}
TestResult CommonTests::testIsUserStructExcludeArithmetic()
{
TestResult result{ "", true, "成功" };
assertFalse(transform::is_user_struct_v<int>, "int不应被视为用户结构体");
return result;
}
TestResult CommonTests::testIsUserStructExcludeString()
{
TestResult result{ "", true, "成功" };
assertFalse(transform::is_user_struct_v<std::string>, "string不应被视为用户结构体");
return result;
}
TestResult CommonTests::testIsUserStructExcludeLSPTypes()
{
TestResult result{ "", true, "成功" };
assertFalse(transform::is_user_struct_v<protocol::LSPAny>, "LSPAny不应被视为用户结构体");
return result;
}
TestResult CommonTests::testIsUserStructExcludeVector()
{
TestResult result{ "", true, "成功" };
assertFalse(transform::is_user_struct_v<std::vector<int>>, "vector不应被视为用户结构体");
return result;
}
TestResult CommonTests::testIsUserStructExcludeMap()
{
TestResult result{ "", true, "成功" };
assertFalse(transform::is_user_struct_v<std::map<std::string, int>>, "map不应被视为用户结构体");
return result;
}
TestResult CommonTests::testIsUserStructExcludeOptional()
{
TestResult result{ "", true, "成功" };
assertFalse(transform::is_user_struct_v<std::optional<int>>, "optional不应被视为用户结构体");
return result;
}
TestResult CommonTests::testIsUserStructExcludeVariant()
{
TestResult result{ "", true, "成功" };
assertFalse(transform::is_user_struct_v<std::variant<int, double>>, "variant不应被视为用户结构体");
return result;
}
TestResult CommonTests::testIsUserStructExcludePointers()
{
TestResult result{ "", true, "成功" };
assertFalse(transform::is_user_struct_v<int*>, "指针不应被视为用户结构体");
return result;
}
}
@@ -1,39 +0,0 @@
#pragma once
#include "test_framework.hpp"
namespace lsp::test
{
// Common 类型特征测试类
class CommonTests
{
public:
static void registerTests(TestRunner& runner);
private:
// ==================== is_lsp_basic_type 测试 ====================
static TestResult testIsLSPBasicInteger();
static TestResult testIsLSPBasicUInteger();
static TestResult testIsLSPBasicBoolean();
static TestResult testIsLSPBasicString();
static TestResult testIsLSPBasicDecimal();
static TestResult testIsLSPBasicNullptr();
static TestResult testIsLSPBasicNegativeCases();
// ==================== is_lsp_container_type 测试 ====================
static TestResult testIsLSPContainerObject();
static TestResult testIsLSPContainerArray();
static TestResult testIsLSPContainerAny();
static TestResult testIsLSPContainerNegativeCases();
// ==================== is_user_struct 测试 ====================
static TestResult testIsUserStructPositive();
static TestResult testIsUserStructExcludeArithmetic();
static TestResult testIsUserStructExcludeString();
static TestResult testIsUserStructExcludeLSPTypes();
static TestResult testIsUserStructExcludeVector();
static TestResult testIsUserStructExcludeMap();
static TestResult testIsUserStructExcludeOptional();
static TestResult testIsUserStructExcludeVariant();
static TestResult testIsUserStructExcludePointers();
};
}
@@ -1,10 +1,19 @@
#include <glaze/glaze.hpp>
#include "facade_test.hpp"
#include "../../src/protocol/transform/facade.hpp"
#include "../../src/protocol/detail/basic_types.hpp"
module;
export module lsp.test.lsp_any.facade;
import std;
import glaze;
import lsp.test.framework;
import lsp.protocol.common.basic_types;
import lsp.codec.common;
import lsp.codec.transformer;
import lsp.codec.facade;
namespace lsp::test
{
namespace transform = lsp::codec;
// 测试用结构体
struct TestPerson
{
@@ -45,6 +54,40 @@ struct glz::meta<lsp::test::TestConfig>
&T::ports);
};
export namespace lsp::test
{
// Facade 接口测试类(Serialize, Deserialize, check, debug
class FacadeTests
{
public:
static void registerTests(TestRunner& runner);
private:
// ==================== Serialize/Deserialize 测试 ====================
static TestResult testSerializeSimpleStruct();
static TestResult testSerializeComplexStruct();
static TestResult testSerializeLSPAny();
static TestResult testDeserializeSimpleStruct();
static TestResult testDeserializeComplexStruct();
static TestResult testDeserializeInvalidJSON();
static TestResult testSerializeDeserializeRoundtrip();
// ==================== check 命名空间测试 ====================
static TestResult testCheckIsObject();
static TestResult testCheckIsArray();
static TestResult testCheckIsString();
static TestResult testCheckIsNumber();
static TestResult testCheckIsBool();
static TestResult testCheckIsNull();
static TestResult testCheckMultipleTypes();
// ==================== debug 命名空间测试 ====================
static TestResult testDebugGetTypeName();
static TestResult testDebugGetIdStringInt();
static TestResult testDebugGetIdStringString();
};
}
namespace lsp::test
{
void FacadeTests::registerTests(TestRunner& runner)
@@ -156,7 +199,7 @@ namespace lsp::test
const auto& config = config_opt.value();
assertEqual(std::string("2.0.0"), config.version, "版本应该为'2.0.0'");
assertEqual(false, config.enabled, "enabled应该为false");
assertEqual(size_t(3), config.ports.size(), "应该有3个端口");
assertEqual(std::size_t(3), config.ports.size(), "应该有3个端口");
assertEqual(9000, config.ports[0], "第一个端口应该为9000");
result.message = "成功";
@@ -1,36 +0,0 @@
#pragma once
#include "test_framework.hpp"
namespace lsp::test
{
// Facade 接口测试类(Serialize, Deserialize, check, debug
class FacadeTests
{
public:
static void registerTests(TestRunner& runner);
private:
// ==================== Serialize/Deserialize 测试 ====================
static TestResult testSerializeSimpleStruct();
static TestResult testSerializeComplexStruct();
static TestResult testSerializeLSPAny();
static TestResult testDeserializeSimpleStruct();
static TestResult testDeserializeComplexStruct();
static TestResult testDeserializeInvalidJSON();
static TestResult testSerializeDeserializeRoundtrip();
// ==================== check 命名空间测试 ====================
static TestResult testCheckIsObject();
static TestResult testCheckIsArray();
static TestResult testCheckIsString();
static TestResult testCheckIsNumber();
static TestResult testCheckIsBool();
static TestResult testCheckIsNull();
static TestResult testCheckMultipleTypes();
// ==================== debug 命名空间测试 ====================
static TestResult testDebugGetTypeName();
static TestResult testDebugGetIdStringInt();
static TestResult testDebugGetIdStringString();
};
}
@@ -1,689 +0,0 @@
#include <cmath>
#include <limits>
#include "../../src/protocol/detail/basic_types.hpp"
#include "./lsp_any_test.hpp"
namespace lsp::test
{
// ==================== LSPAnyTests 实现 ====================
void LSPAnyTests::registerTests(TestRunner& runner)
{
// 构造函数测试
runner.addTest("默认构造函数", testDefaultConstructor);
runner.addTest("浮点数构造函数", testDecimalConstructor);
runner.addTest("布尔构造函数", testBooleanConstructor);
runner.addTest("字符串构造函数", testStringConstructor);
runner.addTest("C字符串构造函数", testCStringConstructor);
runner.addTest("空指针构造函数", testNullptrConstructor);
runner.addTest("LSPObject构造函数", testLSPObjectConstructor);
runner.addTest("LSPArray构造函数", testLSPArrayConstructor);
// 拷贝和移动测试
runner.addTest("拷贝构造函数", testCopyConstructor);
runner.addTest("移动构造函数", testMoveConstructor);
runner.addTest("拷贝赋值", testCopyAssignment);
runner.addTest("移动赋值", testMoveAssignment);
// 赋值操作符测试
runner.addTest("浮点数赋值", testDecimalAssignment);
runner.addTest("布尔赋值", testBooleanAssignment);
runner.addTest("字符串赋值", testStringAssignment);
runner.addTest("空指针赋值", testNullptrAssignment);
runner.addTest("LSPObject赋值", testLSPObjectAssignment);
runner.addTest("LSPArray赋值", testLSPArrayAssignment);
// 类型检查测试
runner.addTest("Is类型检查", testIsMethod);
runner.addTest("Get获取值", testGetMethod);
// 访问者模式测试
runner.addTest("Visit访问者", testVisitMethod);
// 整数模板测试
runner.addTest("有符号整数模板", testIntegerTemplateSignedTypes);
runner.addTest("无符号整数模板", testIntegerTemplateUnsignedTypes);
runner.addTest("整数边界测试", testIntegerTemplateBoundaries);
// 复杂场景测试
runner.addTest("嵌套LSPObject", testNestedLSPObject);
runner.addTest("嵌套LSPArray", testNestedLSPArray);
runner.addTest("混合嵌套", testMixedNesting);
runner.addTest("类型转换", testTypeConversion);
// 边界情况测试
runner.addTest("浮点精度", testFloatPrecision);
runner.addTest("大数字", testLargeNumbers);
runner.addTest("空容器", testEmptyContainers);
}
// ==================== 构造函数测试 ====================
TestResult LSPAnyTests::testDefaultConstructor()
{
TestResult result;
result.passed = true;
protocol::LSPAny any;
assertTrue(any.Is<std::nullptr_t>(), "默认构造应该是nullptr类型");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testDecimalConstructor()
{
TestResult result;
result.passed = true;
protocol::decimal value = 3.14;
protocol::LSPAny any(value);
assertTrue(any.Is<protocol::decimal>(), "应该是decimal类型");
assertEqual(value, any.Get<protocol::decimal>(), "值应该相等");
// 测试 float 构造
float fValue = 2.5f;
protocol::LSPAny anyFloat(fValue);
assertTrue(anyFloat.Is<protocol::decimal>(), "float应该转为decimal类型");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testBooleanConstructor()
{
TestResult result;
result.passed = true;
protocol::LSPAny anyTrue(true);
protocol::LSPAny anyFalse(false);
assertTrue(anyTrue.Is<protocol::boolean>(), "应该是boolean类型");
assertTrue(anyFalse.Is<protocol::boolean>(), "应该是boolean类型");
assertEqual(true, anyTrue.Get<protocol::boolean>(), "值应该是true");
assertEqual(false, anyFalse.Get<protocol::boolean>(), "值应该是false");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testStringConstructor()
{
TestResult result;
result.passed = true;
protocol::string value = "Hello, LSP!";
protocol::LSPAny any(value);
assertTrue(any.Is<protocol::string>(), "应该是string类型");
assertEqual(value, any.Get<protocol::string>(), "值应该相等");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testCStringConstructor()
{
TestResult result;
result.passed = true;
const char* value = "C String";
protocol::LSPAny any(value);
assertTrue(any.Is<protocol::string>(), "应该是string类型");
assertEqual(std::string(value), any.Get<protocol::string>(), "值应该相等");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testNullptrConstructor()
{
TestResult result;
result.passed = true;
protocol::LSPAny any(nullptr);
assertTrue(any.Is<std::nullptr_t>(), "应该是nullptr类型");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testLSPObjectConstructor()
{
TestResult result;
result.passed = true;
protocol::LSPObject obj;
obj["key1"] = protocol::LSPAny(42);
obj["key2"] = protocol::LSPAny("value");
protocol::LSPAny any(obj);
assertTrue(any.Is<protocol::LSPObject>(), "应该是LSPObject类型");
const auto& retrievedObj = any.Get<protocol::LSPObject>();
assertTrue(retrievedObj.count("key1") > 0, "应该包含key1");
assertTrue(retrievedObj.count("key2") > 0, "应该包含key2");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testLSPArrayConstructor()
{
TestResult result;
result.passed = true;
protocol::LSPArray arr;
arr.push_back(protocol::LSPAny(1));
arr.push_back(protocol::LSPAny(2));
arr.push_back(protocol::LSPAny(3));
protocol::LSPAny any(arr);
assertTrue(any.Is<protocol::LSPArray>(), "应该是LSPArray类型");
const auto& retrievedArr = any.Get<protocol::LSPArray>();
assertTrue(retrievedArr.size() == 3, "数组大小应该是3");
result.message = "成功";
return result;
}
// ==================== 拷贝和移动测试 ====================
TestResult LSPAnyTests::testCopyConstructor()
{
TestResult result;
result.passed = true;
protocol::LSPAny original(42);
protocol::LSPAny copy(original);
assertTrue(copy.Is<protocol::integer>(), "拷贝应该保持类型");
assertEqual(42, copy.Get<protocol::integer>(), "拷贝应该保持值");
assertEqual(42, original.Get<protocol::integer>(), "原值不应改变");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testMoveConstructor()
{
TestResult result;
result.passed = true;
protocol::LSPAny original(protocol::string("Move me"));
protocol::LSPAny moved(std::move(original));
assertTrue(moved.Is<protocol::string>(), "移动应该保持类型");
assertEqual(std::string("Move me"), moved.Get<protocol::string>(), "移动应该保持值");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testCopyAssignment()
{
TestResult result;
result.passed = true;
protocol::LSPAny original(42);
protocol::LSPAny target(nullptr);
target = original;
assertTrue(target.Is<protocol::integer>(), "赋值应该保持类型");
assertEqual(42, target.Get<protocol::integer>(), "赋值应该保持值");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testMoveAssignment()
{
TestResult result;
result.passed = true;
protocol::LSPAny original(protocol::string("Move me"));
protocol::LSPAny target(nullptr);
target = std::move(original);
assertTrue(target.Is<protocol::string>(), "移动赋值应该保持类型");
assertEqual(std::string("Move me"), target.Get<protocol::string>(), "移动赋值应该保持值");
result.message = "成功";
return result;
}
// ==================== 赋值操作符测试 ====================
TestResult LSPAnyTests::testDecimalAssignment()
{
TestResult result;
result.passed = true;
protocol::LSPAny any(nullptr);
any = 2.718;
assertTrue(any.Is<protocol::decimal>(), "赋值后应该是decimal类型");
assertEqual(2.718, any.Get<protocol::decimal>(), "赋值后值应该正确");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testBooleanAssignment()
{
TestResult result;
result.passed = true;
protocol::LSPAny any(nullptr);
any = true;
assertTrue(any.Is<protocol::boolean>(), "赋值后应该是boolean类型");
assertEqual(true, any.Get<protocol::boolean>(), "赋值后值应该正确");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testStringAssignment()
{
TestResult result;
result.passed = true;
protocol::LSPAny any(nullptr);
any = protocol::string("Assigned");
assertTrue(any.Is<protocol::string>(), "赋值后应该是string类型");
assertEqual(std::string("Assigned"), any.Get<protocol::string>(), "赋值后值应该正确");
// 测试 C 字符串赋值
any = "C String";
assertEqual(std::string("C String"), any.Get<protocol::string>(), "C字符串赋值应该正确");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testNullptrAssignment()
{
TestResult result;
result.passed = true;
protocol::LSPAny any(42);
any = nullptr;
assertTrue(any.Is<std::nullptr_t>(), "赋值后应该是nullptr类型");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testLSPObjectAssignment()
{
TestResult result;
result.passed = true;
protocol::LSPObject obj;
obj["test"] = protocol::LSPAny(999);
protocol::LSPAny any(nullptr);
any = obj;
assertTrue(any.Is<protocol::LSPObject>(), "赋值后应该是LSPObject类型");
const auto& retrievedObj = any.Get<protocol::LSPObject>();
assertTrue(retrievedObj.count("test") > 0, "应该包含test键");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testLSPArrayAssignment()
{
TestResult result;
result.passed = true;
protocol::LSPArray arr;
arr.push_back(protocol::LSPAny(10));
arr.push_back(protocol::LSPAny(20));
protocol::LSPAny any(nullptr);
any = arr;
assertTrue(any.Is<protocol::LSPArray>(), "赋值后应该是LSPArray类型");
const auto& retrievedArr = any.Get<protocol::LSPArray>();
assertTrue(retrievedArr.size() == 2, "数组大小应该是2");
result.message = "成功";
return result;
}
// ==================== 类型检查测试 ====================
TestResult LSPAnyTests::testIsMethod()
{
TestResult result;
result.passed = true;
protocol::LSPAny intAny(42);
assertTrue(intAny.Is<protocol::integer>(), "Is应该正确识别integer");
assertFalse(intAny.Is<protocol::string>(), "Is应该正确排除string");
assertFalse(intAny.Is<protocol::decimal>(), "Is应该正确排除decimal");
protocol::LSPAny strAny(protocol::string("test"));
assertTrue(strAny.Is<protocol::string>(), "Is应该正确识别string");
assertFalse(strAny.Is<protocol::integer>(), "Is应该正确排除integer");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testGetMethod()
{
TestResult result;
result.passed = true;
protocol::LSPAny any(42);
assertEqual(42, any.Get<protocol::integer>(), "Get应该返回正确的值");
// 修改值
any.Get<protocol::integer>() = 100;
assertEqual(100, any.Get<protocol::integer>(), "Get应该返回可修改的引用");
// const Get 测试
const protocol::LSPAny constAny(200);
assertEqual(200, constAny.Get<protocol::integer>(), "const Get应该正常工作");
result.message = "成功";
return result;
}
// ==================== 访问者模式测试 ====================
TestResult LSPAnyTests::testVisitMethod()
{
TestResult result;
result.passed = true;
protocol::LSPAny intAny(42);
protocol::LSPAny strAny(protocol::string("hello"));
protocol::LSPAny boolAny(true);
// 测试 const Visit
auto visitor = [](const auto& value) -> std::string {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, protocol::integer>)
{
return "integer:" + std::to_string(value);
}
else if constexpr (std::is_same_v<T, protocol::string>)
{
return "string:" + value;
}
else if constexpr (std::is_same_v<T, protocol::boolean>)
{
return value ? "bool:true" : "bool:false";
}
else
{
return "other";
}
};
std::string result1 = intAny.Visit(visitor);
assertEqual(std::string("integer:42"), result1, "Visit应该正确访问integer");
std::string result2 = strAny.Visit(visitor);
assertEqual(std::string("string:hello"), result2, "Visit应该正确访问string");
std::string result3 = boolAny.Visit(visitor);
assertEqual(std::string("bool:true"), result3, "Visit应该正确访问boolean");
result.message = "成功";
return result;
}
// ==================== 整数模板测试 ====================
TestResult LSPAnyTests::testIntegerTemplateSignedTypes()
{
TestResult result;
result.passed = true;
// 测试各种有符号整数类型
protocol::LSPAny anyInt8(static_cast<int8_t>(-10));
assertTrue(anyInt8.Is<protocol::integer>(), "int8_t应该转为integer");
assertEqual(-10, anyInt8.Get<protocol::integer>(), "int8_t值应该正确");
protocol::LSPAny anyInt16(static_cast<int16_t>(-1000));
assertTrue(anyInt16.Is<protocol::integer>(), "int16_t应该转为integer");
assertEqual(-1000, anyInt16.Get<protocol::integer>(), "int16_t值应该正确");
protocol::LSPAny anyInt64(static_cast<int64_t>(100000));
assertTrue(anyInt64.Is<protocol::integer>(), "int64_t应该转为integer");
assertEqual(100000, anyInt64.Get<protocol::integer>(), "int64_t值应该正确");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testIntegerTemplateUnsignedTypes()
{
TestResult result;
result.passed = true;
// 测试各种无符号整数类型 - 无符号整数应该存储为uinteger
protocol::LSPAny anyUint8(static_cast<uint8_t>(255));
assertTrue(anyUint8.Is<protocol::uinteger>(), "uint8_t应该转为uinteger");
assertEqual(255u, anyUint8.Get<protocol::uinteger>(), "uint8_t值应该正确");
protocol::LSPAny anyUint16(static_cast<uint16_t>(5000));
assertTrue(anyUint16.Is<protocol::uinteger>(), "uint16_t应该转为uinteger");
assertEqual(5000u, anyUint16.Get<protocol::uinteger>(), "uint16_t值应该正确");
protocol::LSPAny anySize(static_cast<size_t>(12345));
assertTrue(anySize.Is<protocol::uinteger>(), "size_t应该转为uinteger");
assertEqual(12345u, anySize.Get<protocol::uinteger>(), "size_t值应该正确");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testIntegerTemplateBoundaries()
{
TestResult result;
result.passed = true;
// 测试 int32 边界
protocol::LSPAny anyMax(std::numeric_limits<int32_t>::max());
assertTrue(anyMax.Is<protocol::integer>(), "int32_max应该是integer");
assertEqual(std::numeric_limits<int32_t>::max(), anyMax.Get<protocol::integer>(), "int32_max值应该正确");
protocol::LSPAny anyMin(std::numeric_limits<int32_t>::min());
assertTrue(anyMin.Is<protocol::integer>(), "int32_min应该是integer");
assertEqual(std::numeric_limits<int32_t>::min(), anyMin.Get<protocol::integer>(), "int32_min值应该正确");
// 测试超出 int32 范围的 int64
int64_t largeValue = static_cast<int64_t>(std::numeric_limits<int32_t>::max()) + 1;
protocol::LSPAny anyLarge(largeValue);
assertTrue(anyLarge.Is<protocol::decimal>(), "超出int32范围应该转为decimal");
// 测试 uint32 边界
protocol::LSPAny anyUintMax(std::numeric_limits<uint32_t>::max());
assertTrue(anyUintMax.Is<protocol::uinteger>(), "uint32_max应该是uinteger");
assertEqual(std::numeric_limits<uint32_t>::max(), anyUintMax.Get<protocol::uinteger>(), "uint32_max值应该正确");
result.message = "成功";
return result;
}
// ==================== 复杂场景测试 ====================
TestResult LSPAnyTests::testNestedLSPObject()
{
TestResult result;
result.passed = true;
protocol::LSPObject innerObj;
innerObj["inner_key"] = protocol::LSPAny(42);
protocol::LSPObject outerObj;
outerObj["outer_key"] = protocol::LSPAny(innerObj);
outerObj["name"] = protocol::LSPAny(protocol::string("test"));
protocol::LSPAny any(outerObj);
assertTrue(any.Is<protocol::LSPObject>(), "应该是LSPObject类型");
const auto& obj = any.Get<protocol::LSPObject>();
assertTrue(obj.count("outer_key") > 0, "应该包含outer_key");
assertTrue(obj.count("name") > 0, "应该包含name");
const auto& inner = obj.at("outer_key");
assertTrue(inner.Is<protocol::LSPObject>(), "嵌套值应该是LSPObject");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testNestedLSPArray()
{
TestResult result;
result.passed = true;
protocol::LSPArray innerArr;
innerArr.push_back(protocol::LSPAny(1));
innerArr.push_back(protocol::LSPAny(2));
protocol::LSPArray outerArr;
outerArr.push_back(protocol::LSPAny(innerArr));
outerArr.push_back(protocol::LSPAny(protocol::string("text")));
protocol::LSPAny any(outerArr);
assertTrue(any.Is<protocol::LSPArray>(), "应该是LSPArray类型");
const auto& arr = any.Get<protocol::LSPArray>();
assertTrue(arr.size() == 2, "外层数组大小应该是2");
assertTrue(arr[0].Is<protocol::LSPArray>(), "第一个元素应该是LSPArray");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testMixedNesting()
{
TestResult result;
result.passed = true;
protocol::LSPArray arr;
arr.push_back(protocol::LSPAny(1));
arr.push_back(protocol::LSPAny(protocol::string("two")));
arr.push_back(protocol::LSPAny(3.0));
protocol::LSPObject obj;
obj["array"] = protocol::LSPAny(arr);
obj["number"] = protocol::LSPAny(42);
obj["flag"] = protocol::LSPAny(true);
protocol::LSPAny any(obj);
assertTrue(any.Is<protocol::LSPObject>(), "应该是LSPObject类型");
const auto& retrievedObj = any.Get<protocol::LSPObject>();
assertTrue(retrievedObj.count("array") > 0, "应该包含array");
assertTrue(retrievedObj.at("array").Is<protocol::LSPArray>(), "array应该是LSPArray类型");
const auto& retrievedArr = retrievedObj.at("array").Get<protocol::LSPArray>();
assertTrue(retrievedArr.size() == 3, "数组大小应该是3");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testTypeConversion()
{
TestResult result;
result.passed = true;
protocol::LSPAny any(42);
assertTrue(any.Is<protocol::integer>(), "初始应该是integer");
// 改变类型
any = protocol::string("now a string");
assertTrue(any.Is<protocol::string>(), "现在应该是string");
assertFalse(any.Is<protocol::integer>(), "不应该再是integer");
// 再次改变
any = 3.14;
assertTrue(any.Is<protocol::decimal>(), "现在应该是decimal");
assertFalse(any.Is<protocol::string>(), "不应该再是string");
result.message = "成功";
return result;
}
// ==================== 边界情况测试 ====================
TestResult LSPAnyTests::testFloatPrecision()
{
TestResult result;
result.passed = true;
float fValue = 1.23456789f;
protocol::LSPAny anyFloat(fValue);
assertTrue(anyFloat.Is<protocol::decimal>(), "float应该转为decimal");
// 注意:float 转为 double 可能有精度变化,这里只是验证类型转换正确
protocol::decimal retrieved = anyFloat.Get<protocol::decimal>();
assertTrue(std::abs(retrieved - static_cast<double>(fValue)) < 1e-6,
"转换后的值应该接近原值");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testLargeNumbers()
{
TestResult result;
result.passed = true;
// 测试大的 uint64 值转为 decimal
uint64_t largeUint = 10000000000ULL; // 超出 uint32 范围
protocol::LSPAny anyLarge(largeUint);
assertTrue(anyLarge.Is<protocol::decimal>(), "大整数应该转为decimal");
protocol::decimal retrieved = anyLarge.Get<protocol::decimal>();
assertTrue(std::abs(retrieved - static_cast<double>(largeUint)) < 1.0,
"转换后的值应该接近原值");
result.message = "成功";
return result;
}
TestResult LSPAnyTests::testEmptyContainers()
{
TestResult result;
result.passed = true;
protocol::LSPObject emptyObj;
protocol::LSPAny anyObj(emptyObj);
assertTrue(anyObj.Is<protocol::LSPObject>(), "应该是LSPObject类型");
const auto& obj = anyObj.Get<protocol::LSPObject>();
assertTrue(obj.empty(), "对象应该为空");
protocol::LSPArray emptyArr;
protocol::LSPAny anyArr(emptyArr);
assertTrue(anyArr.Is<protocol::LSPArray>(), "应该是LSPArray类型");
const auto& arr = anyArr.Get<protocol::LSPArray>();
assertTrue(arr.empty(), "数组应该为空");
result.message = "成功";
return result;
}
} // namespace lsp::test
@@ -0,0 +1,412 @@
module;
import std;
export module lsp.test.lsp_any.lsp_any;
import lsp.test.framework;
import lsp.protocol.common.basic_types;
export namespace lsp::test
{
class LSPAnyTests
{
public:
static void registerTests(TestRunner& runner);
private:
static TestResult testDefaultConstructor();
static TestResult testDecimalConstructor();
static TestResult testBooleanConstructor();
static TestResult testStringConstructor();
static TestResult testCStringConstructor();
static TestResult testNullptrConstructor();
static TestResult testLSPObjectConstructor();
static TestResult testLSPArrayConstructor();
static TestResult testCopyConstructor();
static TestResult testMoveConstructor();
static TestResult testCopyAssignment();
static TestResult testMoveAssignment();
static TestResult testDecimalAssignment();
static TestResult testBooleanAssignment();
static TestResult testStringAssignment();
static TestResult testNullptrAssignment();
static TestResult testLSPObjectAssignment();
static TestResult testLSPArrayAssignment();
static TestResult testIsMethod();
static TestResult testGetMethod();
static TestResult testVisitMethod();
static TestResult testIntegerTemplateSignedTypes();
static TestResult testIntegerTemplateUnsignedTypes();
static TestResult testIntegerTemplateBoundaries();
static TestResult testNestedLSPObject();
static TestResult testNestedLSPArray();
static TestResult testMixedNesting();
static TestResult testTypeConversion();
static TestResult testFloatPrecision();
static TestResult testLargeNumbers();
static TestResult testEmptyContainers();
};
}
namespace lsp::test
{
void LSPAnyTests::registerTests(TestRunner& runner)
{
runner.addTest("默认构造函数", testDefaultConstructor);
runner.addTest("浮点数构造函数", testDecimalConstructor);
runner.addTest("布尔构造函数", testBooleanConstructor);
runner.addTest("字符串构造函数", testStringConstructor);
runner.addTest("C字符串构造函数", testCStringConstructor);
runner.addTest("空指针构造函数", testNullptrConstructor);
runner.addTest("LSPObject构造函数", testLSPObjectConstructor);
runner.addTest("LSPArray构造函数", testLSPArrayConstructor);
runner.addTest("拷贝构造函数", testCopyConstructor);
runner.addTest("移动构造函数", testMoveConstructor);
runner.addTest("拷贝赋值", testCopyAssignment);
runner.addTest("移动赋值", testMoveAssignment);
runner.addTest("浮点数赋值", testDecimalAssignment);
runner.addTest("布尔赋值", testBooleanAssignment);
runner.addTest("字符串赋值", testStringAssignment);
runner.addTest("空指针赋值", testNullptrAssignment);
runner.addTest("LSPObject赋值", testLSPObjectAssignment);
runner.addTest("LSPArray赋值", testLSPArrayAssignment);
runner.addTest("Is类型检查", testIsMethod);
runner.addTest("Get获取值", testGetMethod);
runner.addTest("Visit访问者", testVisitMethod);
runner.addTest("有符号整数模板", testIntegerTemplateSignedTypes);
runner.addTest("无符号整数模板", testIntegerTemplateUnsignedTypes);
runner.addTest("整数边界测试", testIntegerTemplateBoundaries);
runner.addTest("嵌套LSPObject", testNestedLSPObject);
runner.addTest("嵌套LSPArray", testNestedLSPArray);
runner.addTest("混合嵌套", testMixedNesting);
runner.addTest("类型转换", testTypeConversion);
runner.addTest("浮点精度", testFloatPrecision);
runner.addTest("大数字", testLargeNumbers);
runner.addTest("空容器", testEmptyContainers);
}
TestResult LSPAnyTests::testDefaultConstructor()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any;
assertTrue(any.Is<std::nullptr_t>(), "默认构造应该是nullptr类型");
return result;
}
TestResult LSPAnyTests::testDecimalConstructor()
{
TestResult result{ "", true, "成功" };
protocol::decimal value = 3.14;
protocol::LSPAny any(value);
assertTrue(any.Is<protocol::decimal>(), "应该是decimal类型");
assertEqual(value, any.Get<protocol::decimal>(), "值应该相等");
float fValue = 2.5f;
protocol::LSPAny anyFloat(fValue);
assertTrue(anyFloat.Is<protocol::decimal>(), "float应该转为decimal类型");
return result;
}
TestResult LSPAnyTests::testBooleanConstructor()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny anyTrue(true);
protocol::LSPAny anyFalse(false);
assertTrue(anyTrue.Is<protocol::boolean>(), "应该是boolean类型");
assertTrue(anyFalse.Is<protocol::boolean>(), "应该是boolean类型");
assertEqual(true, anyTrue.Get<protocol::boolean>(), "值应该是true");
assertEqual(false, anyFalse.Get<protocol::boolean>(), "值应该是false");
return result;
}
TestResult LSPAnyTests::testStringConstructor()
{
TestResult result{ "", true, "成功" };
protocol::string value = "Hello, LSP!";
protocol::LSPAny any(value);
assertTrue(any.Is<protocol::string>(), "应该是string类型");
assertEqual(value, any.Get<protocol::string>(), "值应该相等");
return result;
}
TestResult LSPAnyTests::testCStringConstructor()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any("Hello, C-string!");
assertTrue(any.Is<protocol::string>(), "C字符串应转为string类型");
assertEqual(std::string("Hello, C-string!"), any.Get<protocol::string>(), "值应该相等");
return result;
}
TestResult LSPAnyTests::testNullptrConstructor()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any(nullptr);
assertTrue(any.Is<std::nullptr_t>(), "应该是nullptr类型");
return result;
}
TestResult LSPAnyTests::testLSPObjectConstructor()
{
TestResult result{ "", true, "成功" };
protocol::LSPObject obj;
obj["key"] = protocol::LSPAny(42);
protocol::LSPAny any(obj);
assertTrue(any.Is<protocol::LSPObject>(), "应该是LSPObject类型");
assertEqual(42, any.Get<protocol::LSPObject>()["key"].Get<protocol::integer>(), "值应该相等");
return result;
}
TestResult LSPAnyTests::testLSPArrayConstructor()
{
TestResult result{ "", true, "成功" };
protocol::LSPArray arr = { protocol::LSPAny(1), protocol::LSPAny(2) };
protocol::LSPAny any(arr);
assertTrue(any.Is<protocol::LSPArray>(), "应该是LSPArray类型");
assertEqual(std::size_t(2), any.Get<protocol::LSPArray>().size(), "数组大小应为2");
return result;
}
TestResult LSPAnyTests::testCopyConstructor()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny original(123);
protocol::LSPAny copy(original);
assertTrue(copy.Is<protocol::integer>(), "拷贝后类型应保持");
assertEqual(original.Get<protocol::integer>(), copy.Get<protocol::integer>(), "拷贝后的值应相等");
return result;
}
TestResult LSPAnyTests::testMoveConstructor()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny original(123);
protocol::LSPAny moved(std::move(original));
assertTrue(moved.Is<protocol::integer>(), "移动后类型应保持");
assertEqual(123, moved.Get<protocol::integer>(), "移动后的值应相等");
return result;
}
TestResult LSPAnyTests::testCopyAssignment()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny a(1);
protocol::LSPAny b(2);
b = a;
assertEqual(a.Get<protocol::integer>(), b.Get<protocol::integer>(), "赋值后的值应相等");
return result;
}
TestResult LSPAnyTests::testMoveAssignment()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny a(1);
protocol::LSPAny b(2);
b = std::move(a);
assertEqual(1, b.Get<protocol::integer>(), "移动赋值后的值应为1");
return result;
}
TestResult LSPAnyTests::testDecimalAssignment()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any;
any = 3.14;
assertTrue(any.Is<protocol::decimal>(), "应为decimal类型");
assertEqual(3.14, any.Get<protocol::decimal>(), "值应为3.14");
return result;
}
TestResult LSPAnyTests::testBooleanAssignment()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any;
any = true;
assertTrue(any.Is<protocol::boolean>(), "应为boolean类型");
assertEqual(true, any.Get<protocol::boolean>(), "值应为true");
return result;
}
TestResult LSPAnyTests::testStringAssignment()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any;
protocol::string str = "Hello";
any = str;
assertTrue(any.Is<protocol::string>(), "应为string类型");
assertEqual(str, any.Get<protocol::string>(), "值应相等");
return result;
}
TestResult LSPAnyTests::testNullptrAssignment()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any;
any = nullptr;
assertTrue(any.Is<std::nullptr_t>(), "应为nullptr类型");
return result;
}
TestResult LSPAnyTests::testLSPObjectAssignment()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any;
protocol::LSPObject obj;
obj["key"] = protocol::LSPAny(42);
any = obj;
assertTrue(any.Is<protocol::LSPObject>(), "应为LSPObject类型");
assertEqual(42, any.Get<protocol::LSPObject>()["key"].Get<protocol::integer>(), "值应为42");
return result;
}
TestResult LSPAnyTests::testLSPArrayAssignment()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any;
protocol::LSPArray arr = { protocol::LSPAny(1), protocol::LSPAny(2) };
any = arr;
assertTrue(any.Is<protocol::LSPArray>(), "应为LSPArray类型");
assertEqual(std::size_t(2), any.Get<protocol::LSPArray>().size(), "数组大小应为2");
return result;
}
TestResult LSPAnyTests::testIsMethod()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any(42);
assertTrue(any.Is<protocol::integer>(), "应为integer类型");
assertFalse(any.Is<protocol::string>(), "不应为string类型");
return result;
}
TestResult LSPAnyTests::testGetMethod()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any(std::string("test"));
assertEqual(std::string("test"), any.Get<protocol::string>(), "获取的字符串应相等");
return result;
}
TestResult LSPAnyTests::testVisitMethod()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any(10);
int visited = 0;
any.Visit([&](const auto& val) {
if constexpr (std::is_same_v<std::remove_cvref_t<decltype(val)>, protocol::integer>)
{
visited = val;
}
});
assertEqual(10, visited, "访问者应返回相同值");
return result;
}
TestResult LSPAnyTests::testIntegerTemplateSignedTypes()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any(42);
assertTrue(any.Is<protocol::integer>(), "int应为integer类型");
protocol::LSPAny anyLong(std::int64_t(100));
assertTrue(anyLong.Is<protocol::integer>() || anyLong.Is<protocol::decimal>(), "大整数应存为integer或decimal");
return result;
}
TestResult LSPAnyTests::testIntegerTemplateUnsignedTypes()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any(42u);
assertTrue(any.Is<protocol::uinteger>(), "unsigned int应为uinteger");
protocol::LSPAny anyLarge(std::uint64_t(123456789));
assertTrue(anyLarge.Is<protocol::uinteger>() || anyLarge.Is<protocol::decimal>(), "大无符号应存为uinteger或decimal");
return result;
}
TestResult LSPAnyTests::testIntegerTemplateBoundaries()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny anyMin(std::numeric_limits<protocol::integer>::min());
assertTrue(anyMin.Is<protocol::integer>(), "应为integer");
protocol::LSPAny anyMax(std::numeric_limits<protocol::integer>::max());
assertTrue(anyMax.Is<protocol::integer>(), "应为integer");
return result;
}
TestResult LSPAnyTests::testNestedLSPObject()
{
TestResult result{ "", true, "成功" };
protocol::LSPObject inner;
inner["a"] = protocol::LSPAny(1);
protocol::LSPObject outer;
outer["inner"] = protocol::LSPAny(inner);
protocol::LSPAny any(outer);
auto obj = any.Get<protocol::LSPObject>();
assertEqual(1, obj["inner"].Get<protocol::LSPObject>()["a"].Get<protocol::integer>(), "嵌套值应为1");
return result;
}
TestResult LSPAnyTests::testNestedLSPArray()
{
TestResult result{ "", true, "成功" };
protocol::LSPArray inner = { protocol::LSPAny(1) };
protocol::LSPArray outer = { protocol::LSPAny(inner) };
protocol::LSPAny any(outer);
auto arr = any.Get<protocol::LSPArray>();
assertEqual(1, arr[0].Get<protocol::LSPArray>()[0].Get<protocol::integer>(), "嵌套数组值应为1");
return result;
}
TestResult LSPAnyTests::testMixedNesting()
{
TestResult result{ "", true, "成功" };
protocol::LSPObject obj;
protocol::LSPArray arr = { protocol::LSPAny(1), protocol::LSPAny("test") };
obj["data"] = protocol::LSPAny(arr);
protocol::LSPAny any(obj);
auto recovered = any.Get<protocol::LSPObject>()["data"].Get<protocol::LSPArray>();
assertEqual(std::size_t(2), recovered.size(), "长度应为2");
return result;
}
TestResult LSPAnyTests::testTypeConversion()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any(42);
assertTrue(any.Is<protocol::integer>(), "应为integer");
any = 3.14;
assertTrue(any.Is<protocol::decimal>(), "应为decimal");
any = std::string("test");
assertTrue(any.Is<protocol::string>(), "应为string");
return result;
}
TestResult LSPAnyTests::testFloatPrecision()
{
TestResult result{ "", true, "成功" };
double value = 0.1 + 0.2;
protocol::LSPAny any(value);
assertTrue(any.Is<protocol::decimal>(), "应为decimal");
assertEqual(value, any.Get<protocol::decimal>(), "值应保持");
return result;
}
TestResult LSPAnyTests::testLargeNumbers()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any(std::numeric_limits<std::uint64_t>::max());
assertTrue(any.Is<protocol::uinteger>() || any.Is<protocol::decimal>(), "应为uinteger或decimal");
return result;
}
TestResult LSPAnyTests::testEmptyContainers()
{
TestResult result{ "", true, "成功" };
protocol::LSPAny any(protocol::LSPObject{});
assertTrue(any.Is<protocol::LSPObject>(), "空对象仍应有效");
protocol::LSPAny arr(protocol::LSPArray{});
assertTrue(arr.Is<protocol::LSPArray>(), "空数组仍应有效");
return result;
}
}
@@ -1,62 +0,0 @@
#pragma once
#include "./test_framework.hpp"
namespace lsp::test
{
// LSPAny 测试类
class LSPAnyTests
{
public:
static void registerTests(TestRunner& runner);
private:
// 构造函数测试
static TestResult testDefaultConstructor();
static TestResult testDecimalConstructor();
static TestResult testBooleanConstructor();
static TestResult testStringConstructor();
static TestResult testCStringConstructor();
static TestResult testNullptrConstructor();
static TestResult testLSPObjectConstructor();
static TestResult testLSPArrayConstructor();
// 拷贝和移动测试
static TestResult testCopyConstructor();
static TestResult testMoveConstructor();
static TestResult testCopyAssignment();
static TestResult testMoveAssignment();
// 赋值操作符测试
static TestResult testIntegerAssignment();
static TestResult testUIntegerAssignment();
static TestResult testDecimalAssignment();
static TestResult testBooleanAssignment();
static TestResult testStringAssignment();
static TestResult testNullptrAssignment();
static TestResult testLSPObjectAssignment();
static TestResult testLSPArrayAssignment();
// 类型检查测试
static TestResult testIsMethod();
static TestResult testGetMethod();
// 访问者模式测试
static TestResult testVisitMethod();
// 整数模板测试
static TestResult testIntegerTemplateSignedTypes();
static TestResult testIntegerTemplateUnsignedTypes();
static TestResult testIntegerTemplateBoundaries();
// 复杂场景测试
static TestResult testNestedLSPObject();
static TestResult testNestedLSPArray();
static TestResult testMixedNesting();
static TestResult testTypeConversion();
// 边界情况测试
static TestResult testFloatPrecision();
static TestResult testLargeNumbers();
static TestResult testEmptyContainers();
};
}
@@ -1,22 +1,64 @@
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <cmath>
#include "./test_framework.hpp"
module;
import std;
export module lsp.test.framework;
export namespace lsp::test
{
struct TestResult
{
std::string testName;
bool passed;
std::string message;
};
class TestRunner
{
public:
using TestFunction = std::function<TestResult()>;
void addTest(const std::string& name, TestFunction test);
void runAllTests();
int getFailedCount() const;
int getTotalCount() const;
private:
struct TestCase
{
std::string name;
TestFunction function;
};
std::vector<TestCase> tests;
std::vector<TestResult> results;
void printResult(const TestResult& result);
void printSummary();
};
void assertTrue(bool condition, const std::string& message);
void assertFalse(bool condition, const std::string& message);
void assertEqual(int expected, int actual, const std::string& message);
void assertEqual(unsigned int expected, unsigned int actual, const std::string& message);
void assertEqual(double expected, double actual, const std::string& message);
void assertEqual(bool expected, bool actual, const std::string& message);
void assertEqual(const std::string& expected, const std::string& actual, const std::string& message);
void assertEqual(std::size_t expected, std::size_t actual, const std::string& message);
}
namespace lsp::test
{
// ==================== TestRunner 实现 ====================
void TestRunner::addTest(const std::string& name, TestFunction test)
{
tests.push_back({ name, test });
tests.push_back({ name, std::move(test) });
}
void TestRunner::runAllTests()
{
results.clear();
std::cout << "\n========== 开始运行测试 ==========\n" << std::endl;
std::cout << "\n========== 开始运行测试 ==========\n"
<< std::endl;
for (const auto& testCase : tests)
{
@@ -95,8 +137,6 @@ namespace lsp::test
}
}
// ==================== 断言辅助函数实现 ====================
void assertTrue(bool condition, const std::string& message)
{
if (!condition)
@@ -164,7 +204,7 @@ namespace lsp::test
}
}
void assertEqual(size_t expected, size_t actual, const std::string& message)
void assertEqual(std::size_t expected, std::size_t actual, const std::string& message)
{
if (expected != actual)
{
@@ -173,5 +213,4 @@ namespace lsp::test
throw std::runtime_error(ss.str());
}
}
} // namespace lsp::test
}
@@ -1,50 +0,0 @@
#pragma once
#include <string>
#include <vector>
#include <functional>
namespace lsp::test
{
// 简单的测试结果结构
struct TestResult
{
std::string testName;
bool passed;
std::string message;
};
// 测试运行器类
class TestRunner
{
public:
using TestFunction = std::function<TestResult()>;
void addTest(const std::string& name, TestFunction test);
void runAllTests();
int getFailedCount() const;
int getTotalCount() const;
private:
struct TestCase
{
std::string name;
TestFunction function;
};
std::vector<TestCase> tests;
std::vector<TestResult> results;
void printResult(const TestResult& result);
void printSummary();
};
// 断言辅助函数
void assertTrue(bool condition, const std::string& message);
void assertFalse(bool condition, const std::string& message);
void assertEqual(int expected, int actual, const std::string& message);
void assertEqual(unsigned int expected, unsigned int actual, const std::string& message);
void assertEqual(double expected, double actual, const std::string& message);
void assertEqual(bool expected, bool actual, const std::string& message);
void assertEqual(const std::string& expected, const std::string& actual, const std::string& message);
void assertEqual(size_t expected, size_t actual, const std::string& message);
}
@@ -1,9 +1,14 @@
#include <iostream>
#include "test_framework.hpp"
#include "lsp_any_test.hpp"
#include "transformer_test.hpp"
#include "facade_test.hpp"
#include "common_test.hpp"
module;
export module lsp.test.lsp_any.main;
import std;
import lsp.test.framework;
import lsp.test.lsp_any.lsp_any;
import lsp.test.lsp_any.transformer;
import lsp.test.lsp_any.facade;
import lsp.test.lsp_any.common;
int main()
{
@@ -11,7 +16,8 @@ int main()
std::cout << "\n========================================" << std::endl;
std::cout << " LSP Transform 库单元测试套件" << std::endl;
std::cout << "========================================\n" << std::endl;
std::cout << "========================================\n"
<< std::endl;
// 注册所有测试
std::cout << "正在注册测试..." << std::endl;
@@ -1,10 +1,76 @@
#include "transformer_test.hpp"
#include "../../src/protocol/transform/transformer.hpp"
#include "../../src/protocol/transform/common.hpp"
#include "../../src/protocol/detail/basic_types.hpp"
#include <vector>
#include <map>
#include <optional>
module;
import std;
export module lsp.test.lsp_any.transformer;
import lsp.test.framework;
import lsp.protocol.common.basic_types;
import lsp.codec.common;
import lsp.codec.transformer;
export namespace lsp::test
{
namespace transform = lsp::codec;
using std::size_t;
// Transformer 转换功能测试类
class TransformerTests
{
public:
static void registerTests(TestRunner& runner);
private:
// ==================== ToLSPAny 基本类型测试 ====================
static TestResult testToLSPAnyBoolean();
static TestResult testToLSPAnyInteger();
static TestResult testToLSPAnyUInteger();
static TestResult testToLSPAnyFloat();
static TestResult testToLSPAnyDouble();
static TestResult testToLSPAnyString();
static TestResult testToLSPAnyCString();
// ==================== ToLSPAny 容器类型测试 ====================
static TestResult testToLSPAnyVector();
static TestResult testToLSPAnyMap();
static TestResult testToLSPAnyOptionalValue();
static TestResult testToLSPAnyOptionalNullopt();
// ==================== ToLSPAny LSP类型测试 ====================
static TestResult testToLSPAnyLSPObject();
static TestResult testToLSPAnyLSPArray();
static TestResult testToLSPAnyLSPAny();
// ==================== FromLSPAny 基本类型测试 ====================
static TestResult testFromLSPAnyBoolean();
static TestResult testFromLSPAnyInteger();
static TestResult testFromLSPAnyString();
static TestResult testFromLSPAnyDouble();
// ==================== FromLSPAny 容器类型测试 ====================
static TestResult testFromLSPAnyVector();
static TestResult testFromLSPAnyOptionalValue();
static TestResult testFromLSPAnyOptionalNull();
static TestResult testFromLSPAnyLSPObject();
static TestResult testFromLSPAnyLSPArray();
// ==================== 数字类型转换测试 ====================
static TestResult testExtractNumberFromInteger();
static TestResult testExtractNumberFromUInteger();
static TestResult testExtractNumberFromDecimal();
static TestResult testExtractNumberCrossType();
// ==================== 错误处理测试 ====================
static TestResult testFromLSPAnyTypeMismatch();
static TestResult testFromLSPAnyInvalidNumber();
static TestResult testFromLSPAnyInvalidArray();
// ==================== 嵌套结构测试 ====================
static TestResult testNestedVector();
static TestResult testNestedLSPObject();
static TestResult testMixedTypeNesting();
};
}
namespace lsp::test
{
@@ -501,7 +567,7 @@ namespace lsp::test
result.passed = false;
result.message = "应该抛出ConversionError异常";
}
catch (const transform::ConversionError& e)
catch (const transform::ConversionError&)
{
// 预期的异常
result.message = "成功";
@@ -529,7 +595,7 @@ namespace lsp::test
result.passed = false;
result.message = "应该抛出ConversionError异常";
}
catch (const transform::ConversionError& e)
catch (const transform::ConversionError&)
{
// 预期的异常
result.message = "成功";
@@ -557,7 +623,7 @@ namespace lsp::test
result.passed = false;
result.message = "应该抛出ConversionError异常";
}
catch (const transform::ConversionError& e)
catch (const transform::ConversionError&)
{
// 预期的异常
result.message = "成功";
@@ -1,62 +0,0 @@
#pragma once
#include "test_framework.hpp"
namespace lsp::test
{
// Transformer 转换功能测试类
class TransformerTests
{
public:
static void registerTests(TestRunner& runner);
private:
// ==================== ToLSPAny 基本类型测试 ====================
static TestResult testToLSPAnyBoolean();
static TestResult testToLSPAnyInteger();
static TestResult testToLSPAnyUInteger();
static TestResult testToLSPAnyFloat();
static TestResult testToLSPAnyDouble();
static TestResult testToLSPAnyString();
static TestResult testToLSPAnyCString();
// ==================== ToLSPAny 容器类型测试 ====================
static TestResult testToLSPAnyVector();
static TestResult testToLSPAnyMap();
static TestResult testToLSPAnyOptionalValue();
static TestResult testToLSPAnyOptionalNullopt();
// ==================== ToLSPAny LSP类型测试 ====================
static TestResult testToLSPAnyLSPObject();
static TestResult testToLSPAnyLSPArray();
static TestResult testToLSPAnyLSPAny();
// ==================== FromLSPAny 基本类型测试 ====================
static TestResult testFromLSPAnyBoolean();
static TestResult testFromLSPAnyInteger();
static TestResult testFromLSPAnyString();
static TestResult testFromLSPAnyDouble();
// ==================== FromLSPAny 容器类型测试 ====================
static TestResult testFromLSPAnyVector();
static TestResult testFromLSPAnyOptionalValue();
static TestResult testFromLSPAnyOptionalNull();
static TestResult testFromLSPAnyLSPObject();
static TestResult testFromLSPAnyLSPArray();
// ==================== 数字类型转换测试 ====================
static TestResult testExtractNumberFromInteger();
static TestResult testExtractNumberFromUInteger();
static TestResult testExtractNumberFromDecimal();
static TestResult testExtractNumberCrossType();
// ==================== 错误处理测试 ====================
static TestResult testFromLSPAnyTypeMismatch();
static TestResult testFromLSPAnyInvalidNumber();
static TestResult testFromLSPAnyInvalidArray();
// ==================== 嵌套结构测试 ====================
static TestResult testNestedVector();
static TestResult testNestedLSPObject();
static TestResult testMixedTypeNesting();
};
}