Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions GFramework.Game.Tests/Config/YamlConfigLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,9 @@ public void LoadAsync_Should_Throw_When_String_Does_Not_Match_Pattern()
/// <param name="value">满足该 format 的 YAML 标量值。</param>
[TestCase("date", "2026-04-11")]
[TestCase("date-time", "2026-04-11T08:30:00Z")]
[TestCase("duration", "P2DT3H4M5.5S")]
[TestCase("email", "boss@example.com")]
[TestCase("time", "08:30:00Z")]
[TestCase("uri", "https://example.com/loot-table")]
[TestCase("uuid", "123e4567-e89b-12d3-a456-426614174000")]
public async Task LoadAsync_Should_Accept_Supported_String_Format(
Expand Down Expand Up @@ -891,7 +893,9 @@ public async Task LoadAsync_Should_Accept_Supported_String_Format(
/// <param name="value">不满足该 format 的 YAML 标量值。</param>
[TestCase("date", "2026-02-30")]
[TestCase("date-time", "2026-04-11T08:30:00")]
[TestCase("duration", "P1Y")]
[TestCase("email", "boss.example.com")]
[TestCase("time", "08:30:00")]
[TestCase("uri", "/loot-table")]
[TestCase("uuid", "123e4567e89b12d3a456426614174000")]
public void LoadAsync_Should_Throw_When_String_Does_Not_Match_Supported_Format(
Expand Down Expand Up @@ -986,6 +990,8 @@ public void LoadAsync_Should_Throw_When_String_Format_Is_Not_Supported()
Assert.That(exception.Diagnostic.RawValue, Is.EqualTo("ipv4"));
Assert.That(exception.Message, Does.Contain("unsupported string format"));
Assert.That(exception.Message, Does.Contain("date-time"));
Assert.That(exception.Message, Does.Contain("duration"));
Assert.That(exception.Message, Does.Contain("time"));
});
}

Expand Down Expand Up @@ -1079,6 +1085,103 @@ public void LoadAsync_Should_Throw_When_Format_Is_Not_A_String()
});
}

/// <summary>
/// 验证运行时会拒绝命中 <c>not</c> 子 schema 的标量值。
/// </summary>
[Test]
public void LoadAsync_Should_Throw_When_Value_Matches_Not_Schema()
{
CreateConfigFile(
"monster/slime.yaml",
"""
id: 1
name: Deprecated
hp: 10
""");
CreateSchemaFile(
"schemas/monster.schema.json",
"""
{
"type": "object",
"required": ["id", "name", "hp"],
"properties": {
"id": { "type": "integer" },
"name": {
"type": "string",
"not": {
"type": "string",
"const": "Deprecated"
}
},
"hp": { "type": "integer" }
}
}
""");

var loader = new YamlConfigLoader(_rootPath)
.RegisterTable<int, MonsterConfigStub>("monster", "monster", "schemas/monster.schema.json",
static config => config.Id);
var registry = new ConfigRegistry();

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.ConstraintViolation));
Assert.That(exception.Diagnostic.DisplayPath, Is.EqualTo("name"));
Assert.That(exception.Message, Does.Contain("must not match the 'not' schema"));
Assert.That(registry.Count, Is.EqualTo(0));
});
}
Comment thread
GeWuYou marked this conversation as resolved.
Outdated

/// <summary>
/// 验证 schema 将 <c>not</c> 声明为非对象值时,会在解析阶段被拒绝。
/// </summary>
[Test]
public void LoadAsync_Should_Throw_When_Not_Is_Not_An_Object()
{
CreateConfigFile(
"monster/slime.yaml",
"""
id: 1
name: Slime
hp: 10
""");
CreateSchemaFile(
"schemas/monster.schema.json",
"""
{
"type": "object",
"required": ["id", "name", "hp"],
"properties": {
"id": { "type": "integer" },
"name": {
"type": "string",
"not": "deprecated"
},
"hp": { "type": "integer" }
}
}
""");

var loader = new YamlConfigLoader(_rootPath)
.RegisterTable<int, MonsterConfigStub>("monster", "monster", "schemas/monster.schema.json",
static config => config.Id);
var registry = new ConfigRegistry();

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("name"));
Assert.That(exception.Message, Does.Contain("must declare 'not' as an object-valued schema"));
Assert.That(registry.Count, Is.EqualTo(0));
});
}

/// <summary>
/// 验证运行时 schema 校验与 JS 工具对反向引用模式保持一致。
/// </summary>
Expand Down
Loading
Loading