-
Notifications
You must be signed in to change notification settings - Fork 4
feat(config): 添加YAML配置文件的JSON Schema校验功能 #211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1fac276
feat(config): 添加YAML配置文件的JSON Schema校验功能
GeWuYou 925b6ce
feat(config): 添加YAML配置序列化支持并完善测试依赖
GeWuYou e40703c
feat(config): 添加 YAML 配置文件 JSON Schema 校验器
GeWuYou 7473adb
feat(config): 添加YAML配置序列化器并更新集成测试
GeWuYou 12e54ce
feat(config): 添加YAML配置序列化和校验功能
GeWuYou 949904b
docs(Config): 更新YAML配置文本验证器文档
GeWuYou 774b69f
feat(config): 添加YAML配置文本校验器
GeWuYou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
GFramework.Game.Tests/Config/YamlConfigSchemaValidatorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| using System.IO; | ||
| using GFramework.Game.Config; | ||
|
|
||
| namespace GFramework.Game.Tests.Config; | ||
|
|
||
| /// <summary> | ||
| /// 验证内部 schema 解析器会输出稳定且可预期的运行时依赖元数据。 | ||
| /// </summary> | ||
| [TestFixture] | ||
| public sealed class YamlConfigSchemaValidatorTests | ||
| { | ||
| private string _rootPath = null!; | ||
|
|
||
| /// <summary> | ||
| /// 为每个测试准备独立临时目录。 | ||
| /// </summary> | ||
| [SetUp] | ||
| public void SetUp() | ||
| { | ||
| _rootPath = Path.Combine(Path.GetTempPath(), "GFramework.SchemaValidatorTests", Guid.NewGuid().ToString("N")); | ||
| Directory.CreateDirectory(_rootPath); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 清理测试临时目录。 | ||
| /// </summary> | ||
| [TearDown] | ||
| public void TearDown() | ||
| { | ||
| if (Directory.Exists(_rootPath)) | ||
| { | ||
| Directory.Delete(_rootPath, true); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 验证 schema 中声明的跨表引用名称会以序数排序形式输出, | ||
| /// 避免热重载依赖推导与测试快照受哈希集合枚举顺序影响。 | ||
| /// </summary> | ||
| [Test] | ||
| public void Load_Should_Return_Referenced_Table_Names_In_Ordinal_Sorted_Order() | ||
| { | ||
| var schemaPath = CreateSchemaFile( | ||
| "schemas/monster.schema.json", | ||
| """ | ||
| { | ||
| "type": "object", | ||
| "properties": { | ||
| "weaponId": { | ||
| "type": "string", | ||
| "x-gframework-ref-table": "weapon" | ||
| }, | ||
| "allies": { | ||
| "type": "array", | ||
| "items": { | ||
| "type": "integer", | ||
| "x-gframework-ref-table": "ally" | ||
| } | ||
| }, | ||
| "itemId": { | ||
| "type": "string", | ||
| "x-gframework-ref-table": "item" | ||
| } | ||
| } | ||
| } | ||
| """); | ||
|
|
||
| var schema = YamlConfigSchemaValidator.Load("monster", schemaPath); | ||
|
|
||
| Assert.That(schema.ReferencedTableNames, Is.EqualTo(new[] { "ally", "item", "weapon" })); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 在临时目录中创建 schema 文件。 | ||
| /// </summary> | ||
| /// <param name="relativePath">相对根目录的路径。</param> | ||
| /// <param name="content">文件内容。</param> | ||
| /// <returns>写入后的绝对路径。</returns> | ||
| private string CreateSchemaFile( | ||
| string relativePath, | ||
| string content) | ||
| { | ||
| var fullPath = Path.Combine(_rootPath, relativePath.Replace('/', Path.DirectorySeparatorChar)); | ||
| var directoryPath = Path.GetDirectoryName(fullPath); | ||
| if (!string.IsNullOrWhiteSpace(directoryPath)) | ||
| { | ||
| Directory.CreateDirectory(directoryPath); | ||
| } | ||
|
|
||
| File.WriteAllText(fullPath, content.Replace("\n", Environment.NewLine, StringComparison.Ordinal)); | ||
| return fullPath; | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
GFramework.Game.Tests/Config/YamlConfigTextSerializerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| using GFramework.Game.Config; | ||
|
|
||
| namespace GFramework.Game.Tests.Config; | ||
|
|
||
| /// <summary> | ||
| /// 验证公开 YAML 文本序列化入口的换行与参数契约。 | ||
| /// </summary> | ||
| [TestFixture] | ||
| public sealed class YamlConfigTextSerializerTests | ||
| { | ||
| /// <summary> | ||
| /// 验证序列化结果会稳定地以 LF 作为尾随换行, | ||
| /// 避免不同宿主平台的行尾约定影响生成内容。 | ||
| /// </summary> | ||
| [Test] | ||
| public void Serialize_Should_Use_Trailing_Lf_Newline() | ||
| { | ||
| var yaml = YamlConfigTextSerializer.Serialize(new MonsterYamlStub | ||
| { | ||
| Id = 1, | ||
| Name = "Slime" | ||
| }); | ||
|
|
||
| Assert.Multiple(() => | ||
| { | ||
| Assert.That(yaml, Does.Contain("id: 1")); | ||
| Assert.That(yaml, Does.Contain("name: Slime")); | ||
| Assert.That(yaml.EndsWith("\n", StringComparison.Ordinal), Is.True); | ||
| Assert.That(yaml.EndsWith("\r\n", StringComparison.Ordinal), Is.False); | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 验证空对象引用会继续通过参数异常暴露给调用方。 | ||
| /// </summary> | ||
| [Test] | ||
| public void Serialize_Should_Throw_When_Value_Is_Null() | ||
| { | ||
| var exception = Assert.Throws<ArgumentNullException>(() => | ||
| YamlConfigTextSerializer.Serialize<MonsterYamlStub>(null!)); | ||
|
|
||
| Assert.That(exception!.ParamName, Is.EqualTo("value")); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 用于 YAML 序列化测试的最小配置对象。 | ||
| /// </summary> | ||
| private sealed class MonsterYamlStub | ||
| { | ||
| /// <summary> | ||
| /// 获取或设置配置标识。 | ||
| /// </summary> | ||
| public int Id { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// 获取或设置配置名称。 | ||
| /// </summary> | ||
| public string Name { get; init; } = string.Empty; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.