using System.IO; using GFramework.Game.Config; namespace GFramework.Game.Tests.Config; /// /// 验证内部 schema 解析器会输出稳定且可预期的运行时依赖元数据。 /// [TestFixture] public sealed class YamlConfigSchemaValidatorTests { private string _rootPath = null!; /// /// 为每个测试准备独立临时目录。 /// [SetUp] public void SetUp() { _rootPath = Path.Combine(Path.GetTempPath(), "GFramework.SchemaValidatorTests", Guid.NewGuid().ToString("N")); Directory.CreateDirectory(_rootPath); } /// /// 清理测试临时目录。 /// [TearDown] public void TearDown() { if (Directory.Exists(_rootPath)) { Directory.Delete(_rootPath, true); } } /// /// 验证 schema 中声明的跨表引用名称会以序数排序形式输出, /// 避免热重载依赖推导与测试快照受哈希集合枚举顺序影响。 /// [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" })); } /// /// 在临时目录中创建 schema 文件。 /// /// 相对根目录的路径。 /// 文件内容。 /// 写入后的绝对路径。 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; } }