mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-08 09:34:30 +08:00
test(config): 添加YAML配置加载器单元测试
- 验证YAML文件扫描和注册表写入功能 - 测试带schema校验的配置表注册选项对象支持 - 验证空配置表注册选项的异常处理 - 测试配置目录不存在时的错误抛出 - 验证配置表加载失败时注册表状态回滚 - 测试非法YAML文件的反序列化错误处理 - 验证schema校验对必填字段缺失的检查 - 测试schema校验对类型不匹配的检查 - 验证schema校验对枚举值限制的支持 - 测试数值范围约束的校验功能 - 验证数值exclusive min/max约束 - 测试multipleOf约束校验 - 验证大数值和科学计数法支持 - 测试字符串长度和正则模式约束 - 验证数组元素数量和唯一性约束 - 测试未知字段检测和错误处理 - 验证嵌套对象和数组的递归校验 - 测试跨表引用校验功能
This commit is contained in:
parent
ccb6b5ad42
commit
3ec3429857
@ -558,6 +558,50 @@ public class YamlConfigLoaderTests
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证大数值配合十进制步进时,会沿用 JS 工具侧的 <c>multipleOf</c> 容差策略。
|
||||
/// </summary>
|
||||
[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<int, MonsterNumberConfigStub>("monster", "monster", "schemas/monster.schema.json",
|
||||
static config => config.Id);
|
||||
var registry = new ConfigRegistry();
|
||||
|
||||
await loader.LoadAsync(registry);
|
||||
|
||||
var table = registry.GetTable<int, MonsterNumberConfigStub>("monster");
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(table.Count, Is.EqualTo(1));
|
||||
Assert.That(table.Get(1).DropRate, Is.EqualTo(10000000.2d));
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证科学计数法数值会按 <c>number</c> 类型被运行时接受。
|
||||
/// </summary>
|
||||
|
||||
@ -1745,9 +1745,9 @@ internal static class YamlConfigSchemaValidator
|
||||
|
||||
/// <summary>
|
||||
/// 判断数值是否满足 <c>multipleOf</c>。
|
||||
/// 双精度浮点比较会保留一个与步进量级相关的微小容差,
|
||||
/// 以避免运行时与 JS 工具侧在 0.1 / 0.01 这类十进制步进上出现伪失败,
|
||||
/// 同时避免值越大就无限放宽合法余数范围。
|
||||
/// 双精度浮点比较会在商空间保留一个与商量级相关的微小容差,
|
||||
/// 以对齐 JS 工具侧对 0.1 / 0.01 这类十进制步进的判定方式,
|
||||
/// 避免出现“编辑器通过、运行时拒绝”的跨环境漂移。
|
||||
/// </summary>
|
||||
/// <param name="value">当前值。</param>
|
||||
/// <param name="divisor">步进约束。</param>
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user