diff --git a/GFramework.Core.Tests/Architectures/RegistryInitializationHookBaseTests.cs b/GFramework.Core.Tests/Architectures/RegistryInitializationHookBaseTests.cs index b3a3975f..58f73afb 100644 --- a/GFramework.Core.Tests/Architectures/RegistryInitializationHookBaseTests.cs +++ b/GFramework.Core.Tests/Architectures/RegistryInitializationHookBaseTests.cs @@ -1,11 +1,5 @@ -using System.Reflection; -using GFramework.Core.Abstractions.Architectures; +using System; using GFramework.Core.Abstractions.Enums; -using GFramework.Core.Abstractions.Lifecycle; -using GFramework.Core.Abstractions.Model; -using GFramework.Core.Abstractions.Systems; -using GFramework.Core.Abstractions.Utility; -using GFramework.Core.Architectures; namespace GFramework.Core.Tests.Architectures; @@ -116,291 +110,3 @@ public class RegistryInitializationHookBaseTests Assert.That(registry.RegisteredConfigs.Count, Is.EqualTo(2)); } } - -/// -/// 测试用的注册表初始化钩子实现 -/// -public class TestRegistryInitializationHook : RegistryInitializationHookBase -{ - public TestRegistryInitializationHook( - IEnumerable configs, - ArchitecturePhase targetPhase = ArchitecturePhase.AfterSystemInit) - : base(configs, targetPhase) - { - } - - protected override void RegisterConfig(TestRegistry registry, string config) - { - registry.Register(config); - } -} - -/// -/// 测试用的注册表类 -/// -public class TestRegistry : IUtility -{ - public List RegisteredConfigs { get; } = new(); - - public void Register(string config) - { - RegisteredConfigs.Add(config); - } -} - -/// -/// 测试用的架构类(包含注册表) -/// -public class TestArchitectureWithRegistry : IArchitecture -{ - private readonly TestRegistry _registry; - - public TestArchitectureWithRegistry(TestRegistry registry) - { - _registry = registry; - Context = new TestArchitectureContextWithRegistry(registry); - } - - public Action? Configurator { get; } - - public IArchitectureContext Context { get; } - Action? IArchitecture.Configurator => Configurator; - - T IArchitecture.RegisterSystem(T system) - { - throw new NotSupportedException(); - } - - T IArchitecture.RegisterModel(T model) - { - throw new NotSupportedException(); - } - - T IArchitecture.RegisterUtility(T utility) - { - throw new NotSupportedException(); - } - - public void RegisterCqrsPipelineBehavior() where TBehavior : class - { - throw new NotSupportedException(); - } - - /// - /// 测试替身未实现显式程序集 CQRS 处理器接入入口。 - /// - /// 包含 CQRS 处理器或生成注册器的程序集。 - /// 该测试替身不参与 CQRS 程序集接入路径验证。 - public void RegisterCqrsHandlersFromAssembly(Assembly assembly) - { - throw new NotSupportedException(); - } - - /// - /// 测试替身未实现显式程序集 CQRS 处理器接入入口。 - /// - /// 要接入的程序集集合。 - /// 该测试替身不参与 CQRS 程序集接入路径验证。 - public void RegisterCqrsHandlersFromAssemblies(IEnumerable assemblies) - { - throw new NotSupportedException(); - } - - public IArchitectureModule InstallModule(IArchitectureModule module) - { - throw new NotSupportedException(); - } - - IArchitectureLifecycleHook IArchitecture.RegisterLifecycleHook(IArchitectureLifecycleHook hook) - { - throw new NotSupportedException(); - } - - Task IArchitecture.WaitUntilReadyAsync() - { - return WaitUntilReadyAsync(); - } - - public void RegisterUtility(Action? onCreated = default(Action?)) where T : class, IUtility - { - throw new NotSupportedException(); - } - - public void RegisterModel(Action? onCreated = default(Action?)) where T : class, IModel - { - throw new NotSupportedException(); - } - - public void RegisterSystem(Action? onCreated = default(Action?)) where T : class, ISystem - { - throw new NotSupportedException(); - } - - public void Initialize() - { - } - - public void Destroy() - { - throw new NotSupportedException(); - } - - Task IAsyncInitializable.InitializeAsync() - { - return InitializeAsync(); - } - - ValueTask IAsyncDestroyable.DestroyAsync() - { - return DestroyAsync(); - } - - public Task WaitUntilReadyAsync() - { - throw new NotSupportedException(); - } - - public void RegisterLifecycleHook(IArchitectureLifecycleHook hook) - { - } - - public Task InitializeAsync() - { - throw new NotSupportedException(); - } - - public ValueTask DestroyAsync() - { - throw new NotSupportedException(); - } -} - -/// -/// 测试用的架构上下文类(包含注册表) -/// -public class TestArchitectureContextWithRegistry : TestArchitectureContext -{ - private readonly TestRegistry _registry; - - public TestArchitectureContextWithRegistry(TestRegistry registry) - { - _registry = registry; - } - - public override TUtility? GetUtility() where TUtility : class - { - if (typeof(TUtility) == typeof(TestRegistry)) - { - return _registry as TUtility; - } - - return base.GetUtility(); - } -} - -/// -/// 测试用的架构类(不包含注册表) -/// -public class TestArchitectureWithoutRegistry : IArchitecture -{ - public TestArchitectureWithoutRegistry() - { - Context = new TestArchitectureContext(); - } - - public IArchitectureContext Context { get; } - public Action? Configurator { get; } - - T IArchitecture.RegisterSystem(T system) - { - throw new NotSupportedException(); - } - - T IArchitecture.RegisterModel(T model) - { - throw new NotSupportedException(); - } - - T IArchitecture.RegisterUtility(T utility) - { - throw new NotSupportedException(); - } - - public void RegisterCqrsPipelineBehavior() where TBehavior : class - { - throw new NotSupportedException(); - } - - /// - /// 测试替身未实现显式程序集 CQRS 处理器接入入口。 - /// - /// 包含 CQRS 处理器或生成注册器的程序集。 - /// 该测试替身不参与 CQRS 程序集接入路径验证。 - public void RegisterCqrsHandlersFromAssembly(Assembly assembly) - { - throw new NotSupportedException(); - } - - /// - /// 测试替身未实现显式程序集 CQRS 处理器接入入口。 - /// - /// 要接入的程序集集合。 - /// 该测试替身不参与 CQRS 程序集接入路径验证。 - public void RegisterCqrsHandlersFromAssemblies(IEnumerable assemblies) - { - throw new NotSupportedException(); - } - - public IArchitectureModule InstallModule(IArchitectureModule module) - { - throw new NotSupportedException(); - } - - IArchitectureLifecycleHook IArchitecture.RegisterLifecycleHook(IArchitectureLifecycleHook hook) - { - throw new NotSupportedException(); - } - - public Task WaitUntilReadyAsync() - { - throw new NotSupportedException(); - } - - public void RegisterUtility(Action? onCreated = default(Action?)) where T : class, IUtility - { - throw new NotSupportedException(); - } - - public void RegisterModel(Action? onCreated = default(Action?)) where T : class, IModel - { - throw new NotSupportedException(); - } - - public void RegisterSystem(Action? onCreated = default(Action?)) where T : class, ISystem - { - throw new NotSupportedException(); - } - - public void Initialize() - { - } - - public Task InitializeAsync() - { - throw new NotSupportedException(); - } - - public ValueTask DestroyAsync() - { - throw new NotSupportedException(); - } - - public void Destroy() - { - throw new NotSupportedException(); - } - - public void RegisterLifecycleHook(IArchitectureLifecycleHook hook) - { - } -} diff --git a/GFramework.Core.Tests/Architectures/TestArchitectureContextWithRegistry.cs b/GFramework.Core.Tests/Architectures/TestArchitectureContextWithRegistry.cs new file mode 100644 index 00000000..373bfe3e --- /dev/null +++ b/GFramework.Core.Tests/Architectures/TestArchitectureContextWithRegistry.cs @@ -0,0 +1,35 @@ +using GFramework.Core.Architectures; + +namespace GFramework.Core.Tests.Architectures; + +/// +/// 为 在架构上下文中暴露 的测试替身。 +/// +public class TestArchitectureContextWithRegistry : TestArchitectureContext +{ + private readonly TestRegistry _registry; + + /// + /// 使用给定测试注册表创建上下文测试替身。 + /// + /// 需要通过 返回的测试注册表。 + public TestArchitectureContextWithRegistry(TestRegistry registry) + { + _registry = registry; + } + + /// + /// 在请求 时返回测试注册表,其余类型回退到基类实现。 + /// + /// 请求的工具类型。 + /// 匹配时返回测试注册表,否则返回基类结果。 + public override TUtility GetUtility() + { + if (typeof(TUtility) == typeof(TestRegistry)) + { + return (TUtility)(object)_registry; + } + + return base.GetUtility(); + } +} diff --git a/GFramework.Core.Tests/Architectures/TestArchitectureWithRegistry.cs b/GFramework.Core.Tests/Architectures/TestArchitectureWithRegistry.cs new file mode 100644 index 00000000..cdb52e81 --- /dev/null +++ b/GFramework.Core.Tests/Architectures/TestArchitectureWithRegistry.cs @@ -0,0 +1,204 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Threading.Tasks; +using GFramework.Core.Abstractions.Architectures; +using GFramework.Core.Abstractions.Lifecycle; +using GFramework.Core.Abstractions.Model; +using GFramework.Core.Abstractions.Systems; +using GFramework.Core.Abstractions.Utility; + +namespace GFramework.Core.Tests.Architectures; + +/// +/// 为 提供已挂接 的架构测试替身。 +/// +public class TestArchitectureWithRegistry : IArchitecture +{ + /// + /// 使用给定测试注册表创建架构测试替身。 + /// + /// 要通过架构上下文暴露给钩子的测试注册表。 + public TestArchitectureWithRegistry(TestRegistry registry) + { + Context = new TestArchitectureContextWithRegistry(registry); + } + + /// + /// 获取测试替身公开的服务配置入口。 + /// 当前切片不验证服务配置流程,因此始终保持为空。 + /// + public Action? Configurator { get; } + + /// + /// 获取当前测试替身使用的架构上下文。 + /// + public IArchitectureContext Context { get; } + + Action? IArchitecture.Configurator => Configurator; + + T IArchitecture.RegisterSystem(T system) + { + throw new NotSupportedException(); + } + + T IArchitecture.RegisterModel(T model) + { + throw new NotSupportedException(); + } + + T IArchitecture.RegisterUtility(T utility) + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现 CQRS 管道行为注册。 + /// + /// 行为类型。 + /// 该测试替身不参与 CQRS 管道配置验证。 + public void RegisterCqrsPipelineBehavior() where TBehavior : class + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现显式程序集 CQRS 处理器接入入口。 + /// + /// 包含 CQRS 处理器或生成注册器的程序集。 + /// 该测试替身不参与 CQRS 程序集接入路径验证。 + public void RegisterCqrsHandlersFromAssembly(Assembly assembly) + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现显式程序集 CQRS 处理器接入入口。 + /// + /// 要接入的程序集集合。 + /// 该测试替身不参与 CQRS 程序集接入路径验证。 + public void RegisterCqrsHandlersFromAssemblies(IEnumerable assemblies) + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现模块安装流程。 + /// + /// 要安装的模块。 + /// 此方法始终抛出异常,不返回模块实例。 + /// 该测试替身不参与模块安装路径验证。 + public IArchitectureModule InstallModule(IArchitectureModule module) + { + throw new NotSupportedException(); + } + + IArchitectureLifecycleHook IArchitecture.RegisterLifecycleHook(IArchitectureLifecycleHook hook) + { + throw new NotSupportedException(); + } + + Task IArchitecture.WaitUntilReadyAsync() + { + return WaitUntilReadyAsync(); + } + + /// + /// 测试替身未实现工具延迟注册入口。 + /// + /// 工具类型。 + /// 工具创建后的回调。 + /// 该测试替身不参与工具注册路径验证。 + public void RegisterUtility(Action? onCreated = default) where T : class, IUtility + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现 Model 延迟注册入口。 + /// + /// Model 类型。 + /// Model 创建后的回调。 + /// 该测试替身不参与 Model 注册路径验证。 + public void RegisterModel(Action? onCreated = default) where T : class, IModel + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现 System 延迟注册入口。 + /// + /// System 类型。 + /// System 创建后的回调。 + /// 该测试替身不参与 System 注册路径验证。 + public void RegisterSystem(Action? onCreated = default) where T : class, ISystem + { + throw new NotSupportedException(); + } + + /// + /// 初始化测试替身。 + /// 该切片只需要上下文可用,因此初始化过程保持为空实现。 + /// + public void Initialize() + { + } + + /// + /// 测试替身未实现销毁路径。 + /// + /// 该测试替身不参与销毁路径验证。 + public void Destroy() + { + throw new NotSupportedException(); + } + + Task IAsyncInitializable.InitializeAsync() + { + return InitializeAsync(); + } + + ValueTask IAsyncDestroyable.DestroyAsync() + { + return DestroyAsync(); + } + + /// + /// 测试替身未实现就绪等待流程。 + /// + /// 此方法始终抛出异常,不返回等待任务。 + /// 该测试替身不参与就绪等待路径验证。 + public Task WaitUntilReadyAsync() + { + throw new NotSupportedException(); + } + + /// + /// 注册架构生命周期钩子。 + /// 当前切片不依赖生命周期钩子执行,因此保持空实现。 + /// + /// 要忽略的生命周期钩子。 + public void RegisterLifecycleHook(IArchitectureLifecycleHook hook) + { + } + + /// + /// 测试替身未实现异步初始化路径。 + /// + /// 此方法始终抛出异常,不返回初始化任务。 + /// 该测试替身不参与异步初始化验证。 + public Task InitializeAsync() + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现异步销毁路径。 + /// + /// 此方法始终抛出异常,不返回销毁任务。 + /// 该测试替身不参与异步销毁验证。 + public ValueTask DestroyAsync() + { + throw new NotSupportedException(); + } +} diff --git a/GFramework.Core.Tests/Architectures/TestArchitectureWithoutRegistry.cs b/GFramework.Core.Tests/Architectures/TestArchitectureWithoutRegistry.cs new file mode 100644 index 00000000..f4bd61ec --- /dev/null +++ b/GFramework.Core.Tests/Architectures/TestArchitectureWithoutRegistry.cs @@ -0,0 +1,187 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Threading.Tasks; +using GFramework.Core.Abstractions.Architectures; +using GFramework.Core.Abstractions.Lifecycle; +using GFramework.Core.Abstractions.Model; +using GFramework.Core.Abstractions.Systems; +using GFramework.Core.Abstractions.Utility; +using GFramework.Core.Architectures; + +namespace GFramework.Core.Tests.Architectures; + +/// +/// 为 提供不包含 的架构测试替身。 +/// +public class TestArchitectureWithoutRegistry : IArchitecture +{ + /// + /// 创建不包含测试注册表的架构替身。 + /// + public TestArchitectureWithoutRegistry() + { + Context = new TestArchitectureContext(); + } + + /// + /// 获取测试替身公开的服务配置入口。 + /// 当前切片不验证服务配置流程,因此始终保持为空。 + /// + public Action? Configurator { get; } + + /// + /// 获取当前测试替身使用的架构上下文。 + /// + public IArchitectureContext Context { get; } + + T IArchitecture.RegisterSystem(T system) + { + throw new NotSupportedException(); + } + + T IArchitecture.RegisterModel(T model) + { + throw new NotSupportedException(); + } + + T IArchitecture.RegisterUtility(T utility) + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现 CQRS 管道行为注册。 + /// + /// 行为类型。 + /// 该测试替身不参与 CQRS 管道配置验证。 + public void RegisterCqrsPipelineBehavior() where TBehavior : class + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现显式程序集 CQRS 处理器接入入口。 + /// + /// 包含 CQRS 处理器或生成注册器的程序集。 + /// 该测试替身不参与 CQRS 程序集接入路径验证。 + public void RegisterCqrsHandlersFromAssembly(Assembly assembly) + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现显式程序集 CQRS 处理器接入入口。 + /// + /// 要接入的程序集集合。 + /// 该测试替身不参与 CQRS 程序集接入路径验证。 + public void RegisterCqrsHandlersFromAssemblies(IEnumerable assemblies) + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现模块安装流程。 + /// + /// 要安装的模块。 + /// 此方法始终抛出异常,不返回模块实例。 + /// 该测试替身不参与模块安装路径验证。 + public IArchitectureModule InstallModule(IArchitectureModule module) + { + throw new NotSupportedException(); + } + + IArchitectureLifecycleHook IArchitecture.RegisterLifecycleHook(IArchitectureLifecycleHook hook) + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现就绪等待流程。 + /// + /// 此方法始终抛出异常,不返回等待任务。 + /// 该测试替身不参与就绪等待路径验证。 + public Task WaitUntilReadyAsync() + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现工具延迟注册入口。 + /// + /// 工具类型。 + /// 工具创建后的回调。 + /// 该测试替身不参与工具注册路径验证。 + public void RegisterUtility(Action? onCreated = default) where T : class, IUtility + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现 Model 延迟注册入口。 + /// + /// Model 类型。 + /// Model 创建后的回调。 + /// 该测试替身不参与 Model 注册路径验证。 + public void RegisterModel(Action? onCreated = default) where T : class, IModel + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现 System 延迟注册入口。 + /// + /// System 类型。 + /// System 创建后的回调。 + /// 该测试替身不参与 System 注册路径验证。 + public void RegisterSystem(Action? onCreated = default) where T : class, ISystem + { + throw new NotSupportedException(); + } + + /// + /// 初始化测试替身。 + /// 该切片只需要一个不含注册表的上下文,因此初始化过程保持为空实现。 + /// + public void Initialize() + { + } + + /// + /// 测试替身未实现异步初始化路径。 + /// + /// 此方法始终抛出异常,不返回初始化任务。 + /// 该测试替身不参与异步初始化验证。 + public Task InitializeAsync() + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现异步销毁路径。 + /// + /// 此方法始终抛出异常,不返回销毁任务。 + /// 该测试替身不参与异步销毁验证。 + public ValueTask DestroyAsync() + { + throw new NotSupportedException(); + } + + /// + /// 测试替身未实现销毁路径。 + /// + /// 该测试替身不参与销毁路径验证。 + public void Destroy() + { + throw new NotSupportedException(); + } + + /// + /// 注册架构生命周期钩子。 + /// 当前切片不依赖生命周期钩子执行,因此保持空实现。 + /// + /// 要忽略的生命周期钩子。 + public void RegisterLifecycleHook(IArchitectureLifecycleHook hook) + { + } +} diff --git a/GFramework.Core.Tests/Architectures/TestRegistry.cs b/GFramework.Core.Tests/Architectures/TestRegistry.cs new file mode 100644 index 00000000..4daf601b --- /dev/null +++ b/GFramework.Core.Tests/Architectures/TestRegistry.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using GFramework.Core.Abstractions.Utility; + +namespace GFramework.Core.Tests.Architectures; + +/// +/// 为 记录注册结果的测试注册表。 +/// +public class TestRegistry : IUtility +{ + private readonly List _registeredConfigs = []; + + /// + /// 获取已注册配置值的只读视图,避免将测试内部使用的列表实现暴露给调用方。 + /// + public IReadOnlyList RegisteredConfigs => _registeredConfigs; + + /// + /// 记录一次配置注册。 + /// + /// 要追加到测试结果中的配置值。 + public void Register(string config) + { + _registeredConfigs.Add(config); + } +} diff --git a/GFramework.Core.Tests/Architectures/TestRegistryInitializationHook.cs b/GFramework.Core.Tests/Architectures/TestRegistryInitializationHook.cs new file mode 100644 index 00000000..3241882d --- /dev/null +++ b/GFramework.Core.Tests/Architectures/TestRegistryInitializationHook.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using GFramework.Core.Abstractions.Enums; +using GFramework.Core.Architectures; + +namespace GFramework.Core.Tests.Architectures; + +/// +/// 为 提供的注册表初始化钩子测试替身。 +/// +public class TestRegistryInitializationHook : RegistryInitializationHookBase +{ + /// + /// 使用给定配置集合和目标阶段创建测试钩子。 + /// + /// 测试期间要注册到目标注册表的配置值。 + /// 触发注册行为的架构阶段。 + public TestRegistryInitializationHook( + IEnumerable configs, + ArchitecturePhase targetPhase = ArchitecturePhase.AfterSystemInit) + : base(configs, targetPhase) + { + } + + /// + /// 将当前配置值写入测试注册表。 + /// + /// 要接收配置值的测试注册表。 + /// 当前遍历到的配置值。 + protected override void RegisterConfig(TestRegistry registry, string config) + { + registry.Register(config); + } +}