mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-04-02 20:09:00 +08:00
- 实现了 ConfigRegistry 配置注册表,支持按名称注册和类型安全查询 - 创建了 InMemoryConfigTable 内存配置表,提供基于字典的只读配置存储 - 定义了 IConfigLoader、IConfigRegistry 和 IConfigTable 接口契约 - 添加了完整的单元测试验证配置表的注册、查询和类型检查功能 - 在项目文件中添加了新的代码文件夹结构 - 实现了配置表的覆盖策略以支持开发期热重载需求
72 lines
2.1 KiB
C#
72 lines
2.1 KiB
C#
using GFramework.Game.Config;
|
|
|
|
namespace GFramework.Game.Tests.Config;
|
|
|
|
/// <summary>
|
|
/// 验证内存配置表的基础只读查询行为。
|
|
/// </summary>
|
|
[TestFixture]
|
|
public class InMemoryConfigTableTests
|
|
{
|
|
/// <summary>
|
|
/// 验证已存在主键可以被正确查询。
|
|
/// </summary>
|
|
[Test]
|
|
public void Get_Should_Return_Config_When_Key_Exists()
|
|
{
|
|
var table = new InMemoryConfigTable<int, MonsterConfigStub>(
|
|
new[]
|
|
{
|
|
new MonsterConfigStub(1, "Slime"),
|
|
new MonsterConfigStub(2, "Goblin")
|
|
},
|
|
static config => config.Id);
|
|
|
|
var result = table.Get(2);
|
|
|
|
Assert.That(result.Name, Is.EqualTo("Goblin"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 验证重复主键会在加载期被拒绝,避免运行期覆盖旧值。
|
|
/// </summary>
|
|
[Test]
|
|
public void Constructor_Should_Throw_When_Duplicate_Key_Is_Detected()
|
|
{
|
|
Assert.Throws<InvalidOperationException>(() =>
|
|
new InMemoryConfigTable<int, MonsterConfigStub>(
|
|
new[]
|
|
{
|
|
new MonsterConfigStub(1, "Slime"),
|
|
new MonsterConfigStub(1, "Goblin")
|
|
},
|
|
static config => config.Id));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 验证 All 返回的集合包含完整快照。
|
|
/// </summary>
|
|
[Test]
|
|
public void All_Should_Return_All_Configs()
|
|
{
|
|
var table = new InMemoryConfigTable<int, MonsterConfigStub>(
|
|
new[]
|
|
{
|
|
new MonsterConfigStub(1, "Slime"),
|
|
new MonsterConfigStub(2, "Goblin")
|
|
},
|
|
static config => config.Id);
|
|
|
|
var all = table.All();
|
|
|
|
Assert.That(all, Has.Count.EqualTo(2));
|
|
Assert.That(all.Select(static config => config.Name), Is.EquivalentTo(new[] { "Slime", "Goblin" }));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用于配置表测试的最小配置类型。
|
|
/// </summary>
|
|
/// <param name="Id">配置主键。</param>
|
|
/// <param name="Name">配置名称。</param>
|
|
private sealed record MonsterConfigStub(int Id, string Name);
|
|
} |