test(config): 添加YAML配置加载器dependentSchemas约束测试

- 验证触发字段出现但条件schema未满足时抛出异常
- 验证触发字段缺席时不会误触发dependentSchemas检查
- 验证触发字段出现且条件schema满足时正常加载对象
- 验证非对象dependentSchemas声明在解析阶段被拒绝
- 验证dependentSchemas触发字段必须在同级properties中显式声明
- 验证dependentSchemas只接受object-typed条件子schema
- 实现临时目录创建和清理避免文件堆积
- 提供完整的测试用例覆盖dependentSchemas各种场景
This commit is contained in:
GeWuYou 2026-04-17 10:15:04 +08:00
parent 01a815a518
commit 2486352341

View File

@ -252,6 +252,60 @@ public sealed class YamlConfigLoaderDependentSchemasTests
});
}
/// <summary>
/// 验证 dependentSchemas 的触发字段必须在同级 properties 中显式声明。
/// </summary>
[Test]
public void LoadAsync_Should_Throw_When_DependentSchemas_Trigger_Is_Not_Declared()
{
CreateConfigFile(
"monster/slime.yaml",
"""
id: 1
reward:
itemId: potion
""");
CreateSchemaFile(
"schemas/monster.schema.json",
"""
{
"type": "object",
"required": ["id", "reward"],
"properties": {
"id": { "type": "integer" },
"reward": {
"type": "object",
"properties": {
"itemCount": { "type": "integer" }
},
"dependentSchemas": {
"itemId": {
"type": "object",
"properties": {
"itemCount": { "type": "integer" }
}
}
}
}
}
}
""");
var loader = CreateMonsterRewardLoader();
var registry = CreateRegistry();
var exception = Assert.ThrowsAsync<ConfigLoadException>(async () => await loader.LoadAsync(registry));
Assert.Multiple(() =>
{
Assert.That(exception, Is.Not.Null);
Assert.That(exception!.Diagnostic.FailureKind, Is.EqualTo(ConfigLoadFailureKind.SchemaUnsupported));
Assert.That(exception.Diagnostic.DisplayPath, Is.EqualTo("reward"));
Assert.That(exception.Message, Does.Contain("dependentSchemas' for undeclared property 'itemId'"));
Assert.That(registry.Count, Is.EqualTo(0));
});
}
/// <summary>
/// 验证 dependentSchemas 只接受 object-typed 条件子 schema。
/// </summary>