From 3ec34298578003945bb7468276d125cf1e99ccfa Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Thu, 9 Apr 2026 19:58:48 +0800
Subject: [PATCH] =?UTF-8?q?test(config):=20=E6=B7=BB=E5=8A=A0YAML=E9=85=8D?=
=?UTF-8?q?=E7=BD=AE=E5=8A=A0=E8=BD=BD=E5=99=A8=E5=8D=95=E5=85=83=E6=B5=8B?=
=?UTF-8?q?=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 验证YAML文件扫描和注册表写入功能
- 测试带schema校验的配置表注册选项对象支持
- 验证空配置表注册选项的异常处理
- 测试配置目录不存在时的错误抛出
- 验证配置表加载失败时注册表状态回滚
- 测试非法YAML文件的反序列化错误处理
- 验证schema校验对必填字段缺失的检查
- 测试schema校验对类型不匹配的检查
- 验证schema校验对枚举值限制的支持
- 测试数值范围约束的校验功能
- 验证数值exclusive min/max约束
- 测试multipleOf约束校验
- 验证大数值和科学计数法支持
- 测试字符串长度和正则模式约束
- 验证数组元素数量和唯一性约束
- 测试未知字段检测和错误处理
- 验证嵌套对象和数组的递归校验
- 测试跨表引用校验功能
---
.../Config/YamlConfigLoaderTests.cs | 44 +++++++++++++++++++
.../Config/YamlConfigSchemaValidator.cs | 10 ++---
2 files changed, 49 insertions(+), 5 deletions(-)
diff --git a/GFramework.Game.Tests/Config/YamlConfigLoaderTests.cs b/GFramework.Game.Tests/Config/YamlConfigLoaderTests.cs
index 29d57119..a8f45394 100644
--- a/GFramework.Game.Tests/Config/YamlConfigLoaderTests.cs
+++ b/GFramework.Game.Tests/Config/YamlConfigLoaderTests.cs
@@ -558,6 +558,50 @@ public class YamlConfigLoaderTests
});
}
+ ///
+ /// 验证大数值配合十进制步进时,会沿用 JS 工具侧的 multipleOf 容差策略。
+ ///
+ [Test]
+ public async Task LoadAsync_Should_Accept_Large_Decimal_Number_When_MultipleOf_Matches_Js_Tolerance()
+ {
+ CreateConfigFile(
+ "monster/slime.yaml",
+ """
+ id: 1
+ dropRate: 10000000.2
+ """);
+ CreateSchemaFile(
+ "schemas/monster.schema.json",
+ """
+ {
+ "type": "object",
+ "required": ["id", "dropRate"],
+ "properties": {
+ "id": { "type": "integer" },
+ "dropRate": {
+ "type": "number",
+ "multipleOf": 0.1
+ }
+ }
+ }
+ """);
+
+ var loader = new YamlConfigLoader(_rootPath)
+ .RegisterTable("monster", "monster", "schemas/monster.schema.json",
+ static config => config.Id);
+ var registry = new ConfigRegistry();
+
+ await loader.LoadAsync(registry);
+
+ var table = registry.GetTable("monster");
+
+ Assert.Multiple(() =>
+ {
+ Assert.That(table.Count, Is.EqualTo(1));
+ Assert.That(table.Get(1).DropRate, Is.EqualTo(10000000.2d));
+ });
+ }
+
///
/// 验证科学计数法数值会按 number 类型被运行时接受。
///
diff --git a/GFramework.Game/Config/YamlConfigSchemaValidator.cs b/GFramework.Game/Config/YamlConfigSchemaValidator.cs
index f7dc74c4..efeda8fa 100644
--- a/GFramework.Game/Config/YamlConfigSchemaValidator.cs
+++ b/GFramework.Game/Config/YamlConfigSchemaValidator.cs
@@ -1745,9 +1745,9 @@ internal static class YamlConfigSchemaValidator
///
/// 判断数值是否满足 multipleOf。
- /// 双精度浮点比较会保留一个与步进量级相关的微小容差,
- /// 以避免运行时与 JS 工具侧在 0.1 / 0.01 这类十进制步进上出现伪失败,
- /// 同时避免值越大就无限放宽合法余数范围。
+ /// 双精度浮点比较会在商空间保留一个与商量级相关的微小容差,
+ /// 以对齐 JS 工具侧对 0.1 / 0.01 这类十进制步进的判定方式,
+ /// 避免出现“编辑器通过、运行时拒绝”的跨环境漂移。
///
/// 当前值。
/// 步进约束。
@@ -1756,8 +1756,8 @@ internal static class YamlConfigSchemaValidator
{
var quotient = value / divisor;
var nearestInteger = Math.Round(quotient);
- var tolerance = 1e-9 * Math.Max(1d, Math.Abs(divisor));
- return Math.Abs(value - (nearestInteger * divisor)) <= tolerance;
+ var tolerance = 1e-9 * Math.Max(1d, Math.Abs(quotient));
+ return Math.Abs(quotient - nearestInteger) <= tolerance;
}
///