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(), "默认构造应该是nullptr类型"); return result; } TestResult LSPAnyTests::testDecimalConstructor() { TestResult result{ "", true, "成功" }; protocol::decimal value = 3.14; protocol::LSPAny any(value); assertTrue(any.Is(), "应该是decimal类型"); assertEqual(value, any.Get(), "值应该相等"); float fValue = 2.5f; protocol::LSPAny anyFloat(fValue); assertTrue(anyFloat.Is(), "float应该转为decimal类型"); return result; } TestResult LSPAnyTests::testBooleanConstructor() { TestResult result{ "", true, "成功" }; protocol::LSPAny anyTrue(true); protocol::LSPAny anyFalse(false); assertTrue(anyTrue.Is(), "应该是boolean类型"); assertTrue(anyFalse.Is(), "应该是boolean类型"); assertEqual(true, anyTrue.Get(), "值应该是true"); assertEqual(false, anyFalse.Get(), "值应该是false"); return result; } TestResult LSPAnyTests::testStringConstructor() { TestResult result{ "", true, "成功" }; protocol::string value = "Hello, LSP!"; protocol::LSPAny any(value); assertTrue(any.Is(), "应该是string类型"); assertEqual(value, any.Get(), "值应该相等"); return result; } TestResult LSPAnyTests::testCStringConstructor() { TestResult result{ "", true, "成功" }; protocol::LSPAny any("Hello, C-string!"); assertTrue(any.Is(), "C字符串应转为string类型"); assertEqual(std::string("Hello, C-string!"), any.Get(), "值应该相等"); return result; } TestResult LSPAnyTests::testNullptrConstructor() { TestResult result{ "", true, "成功" }; protocol::LSPAny any(nullptr); assertTrue(any.Is(), "应该是nullptr类型"); return result; } TestResult LSPAnyTests::testLSPObjectConstructor() { TestResult result{ "", true, "成功" }; protocol::LSPObject obj; obj["key"] = protocol::LSPAny(42); protocol::LSPAny any(obj); assertTrue(any.Is(), "应该是LSPObject类型"); assertEqual(42, any.Get()["key"].Get(), "值应该相等"); return result; } TestResult LSPAnyTests::testLSPArrayConstructor() { TestResult result{ "", true, "成功" }; protocol::LSPArray arr = { protocol::LSPAny(1), protocol::LSPAny(2) }; protocol::LSPAny any(arr); assertTrue(any.Is(), "应该是LSPArray类型"); assertEqual(std::size_t(2), any.Get().size(), "数组大小应为2"); return result; } TestResult LSPAnyTests::testCopyConstructor() { TestResult result{ "", true, "成功" }; protocol::LSPAny original(123); protocol::LSPAny copy(original); assertTrue(copy.Is(), "拷贝后类型应保持"); assertEqual(original.Get(), copy.Get(), "拷贝后的值应相等"); return result; } TestResult LSPAnyTests::testMoveConstructor() { TestResult result{ "", true, "成功" }; protocol::LSPAny original(123); protocol::LSPAny moved(std::move(original)); assertTrue(moved.Is(), "移动后类型应保持"); assertEqual(123, moved.Get(), "移动后的值应相等"); return result; } TestResult LSPAnyTests::testCopyAssignment() { TestResult result{ "", true, "成功" }; protocol::LSPAny a(1); protocol::LSPAny b(2); b = a; assertEqual(a.Get(), b.Get(), "赋值后的值应相等"); return result; } TestResult LSPAnyTests::testMoveAssignment() { TestResult result{ "", true, "成功" }; protocol::LSPAny a(1); protocol::LSPAny b(2); b = std::move(a); assertEqual(1, b.Get(), "移动赋值后的值应为1"); return result; } TestResult LSPAnyTests::testDecimalAssignment() { TestResult result{ "", true, "成功" }; protocol::LSPAny any; any = 3.14; assertTrue(any.Is(), "应为decimal类型"); assertEqual(3.14, any.Get(), "值应为3.14"); return result; } TestResult LSPAnyTests::testBooleanAssignment() { TestResult result{ "", true, "成功" }; protocol::LSPAny any; any = true; assertTrue(any.Is(), "应为boolean类型"); assertEqual(true, any.Get(), "值应为true"); return result; } TestResult LSPAnyTests::testStringAssignment() { TestResult result{ "", true, "成功" }; protocol::LSPAny any; protocol::string str = "Hello"; any = str; assertTrue(any.Is(), "应为string类型"); assertEqual(str, any.Get(), "值应相等"); return result; } TestResult LSPAnyTests::testNullptrAssignment() { TestResult result{ "", true, "成功" }; protocol::LSPAny any; any = nullptr; assertTrue(any.Is(), "应为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(), "应为LSPObject类型"); assertEqual(42, any.Get()["key"].Get(), "值应为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(), "应为LSPArray类型"); assertEqual(std::size_t(2), any.Get().size(), "数组大小应为2"); return result; } TestResult LSPAnyTests::testIsMethod() { TestResult result{ "", true, "成功" }; protocol::LSPAny any(42); assertTrue(any.Is(), "应为integer类型"); assertFalse(any.Is(), "不应为string类型"); return result; } TestResult LSPAnyTests::testGetMethod() { TestResult result{ "", true, "成功" }; protocol::LSPAny any(std::string("test")); assertEqual(std::string("test"), any.Get(), "获取的字符串应相等"); 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, protocol::integer>) { visited = val; } }); assertEqual(10, visited, "访问者应返回相同值"); return result; } TestResult LSPAnyTests::testIntegerTemplateSignedTypes() { TestResult result{ "", true, "成功" }; protocol::LSPAny any(42); assertTrue(any.Is(), "int应为integer类型"); protocol::LSPAny anyLong(std::int64_t(100)); assertTrue(anyLong.Is() || anyLong.Is(), "大整数应存为integer或decimal"); return result; } TestResult LSPAnyTests::testIntegerTemplateUnsignedTypes() { TestResult result{ "", true, "成功" }; protocol::LSPAny any(42u); assertTrue(any.Is(), "unsigned int应为uinteger"); protocol::LSPAny anyLarge(std::uint64_t(123456789)); assertTrue(anyLarge.Is() || anyLarge.Is(), "大无符号应存为uinteger或decimal"); return result; } TestResult LSPAnyTests::testIntegerTemplateBoundaries() { TestResult result{ "", true, "成功" }; protocol::LSPAny anyMin(std::numeric_limits::min()); assertTrue(anyMin.Is(), "应为integer"); protocol::LSPAny anyMax(std::numeric_limits::max()); assertTrue(anyMax.Is(), "应为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(); assertEqual(1, obj["inner"].Get()["a"].Get(), "嵌套值应为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(); assertEqual(1, arr[0].Get()[0].Get(), "嵌套数组值应为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()["data"].Get(); assertEqual(std::size_t(2), recovered.size(), "长度应为2"); return result; } TestResult LSPAnyTests::testTypeConversion() { TestResult result{ "", true, "成功" }; protocol::LSPAny any(42); assertTrue(any.Is(), "应为integer"); any = 3.14; assertTrue(any.Is(), "应为decimal"); any = std::string("test"); assertTrue(any.Is(), "应为string"); return result; } TestResult LSPAnyTests::testFloatPrecision() { TestResult result{ "", true, "成功" }; double value = 0.1 + 0.2; protocol::LSPAny any(value); assertTrue(any.Is(), "应为decimal"); assertEqual(value, any.Get(), "值应保持"); return result; } TestResult LSPAnyTests::testLargeNumbers() { TestResult result{ "", true, "成功" }; protocol::LSPAny any(std::numeric_limits::max()); assertTrue(any.Is() || any.Is(), "应为uinteger或decimal"); return result; } TestResult LSPAnyTests::testEmptyContainers() { TestResult result{ "", true, "成功" }; protocol::LSPAny any(protocol::LSPObject{}); assertTrue(any.Is(), "空对象仍应有效"); protocol::LSPAny arr(protocol::LSPArray{}); assertTrue(arr.Is(), "空数组仍应有效"); return result; } }