using GFramework.Core.Abstractions.Environment; using GFramework.Core.Environment; namespace GFramework.Core.Tests.Environment; /// /// 测试环境相关的单元测试类,用于验证环境管理功能的正确性 /// [TestFixture] public class EnvironmentTests { /// /// 在每个测试方法执行前进行初始化设置 /// [SetUp] public void SetUp() { _environment = new TestEnvironment(); _environment.Initialize(); } private TestEnvironment _environment = null!; /// /// 验证默认环境的名称是否正确返回"Default" /// [Test] public void DefaultEnvironment_Name_Should_ReturnDefault() { var env = new DefaultEnvironment(); Assert.That(env.Name, Is.EqualTo("Default")); } /// /// 验证默认环境的初始化方法不会抛出异常 /// [Test] public void DefaultEnvironment_Initialize_Should_NotThrow() { var env = new DefaultEnvironment(); Assert.DoesNotThrow(() => env.Initialize()); } /// /// 验证当键存在时Get方法应该返回正确的值 /// [Test] public void Get_Should_Return_Value_When_Key_Exists() { _environment.Register("testKey", "testValue"); var result = _environment.Get("testKey"); Assert.That(result, Is.EqualTo("testValue")); } /// /// 验证当键不存在时Get方法应该返回null /// [Test] public void Get_Should_ReturnNull_When_Key_Not_Exists() { var result = _environment.Get("nonExistentKey"); Assert.That(result, Is.Null); } /// /// 验证当类型不匹配时Get方法应该返回null /// [Test] public void Get_Should_ReturnNull_When_Type_Does_Not_Match() { _environment.Register("testKey", "testValue"); var result = _environment.Get>("testKey"); Assert.That(result, Is.Null); } /// /// 验证当键存在时TryGet方法应该返回true并输出正确的值 /// [Test] public void TryGet_Should_ReturnTrue_And_Value_When_Key_Exists() { _environment.Register("testKey", "testValue"); var result = _environment.TryGet("testKey", out var value); Assert.That(result, Is.True); Assert.That(value, Is.EqualTo("testValue")); } /// /// 验证当键不存在时TryGet方法应该返回false且输出值为null /// [Test] public void TryGet_Should_ReturnFalse_When_Key_Not_Exists() { var result = _environment.TryGet("nonExistentKey", out var value); Assert.That(result, Is.False); Assert.That(value, Is.Null); } /// /// 验证当类型不匹配时TryGet方法应该返回false且输出值为null /// [Test] public void TryGet_Should_ReturnFalse_When_Type_Does_Not_Match() { _environment.Register("testKey", "testValue"); var result = _environment.TryGet>("testKey", out var value); Assert.That(result, Is.False); Assert.That(value, Is.Null); } /// /// 验证当键存在时GetRequired方法应该返回正确的值 /// [Test] public void GetRequired_Should_Return_Value_When_Key_Exists() { _environment.Register("testKey", "testValue"); var result = _environment.GetRequired("testKey"); Assert.That(result, Is.EqualTo("testValue")); } /// /// 验证当键不存在时GetRequired方法应该抛出InvalidOperationException异常 /// [Test] public void GetRequired_Should_ThrowInvalidOperationException_When_Key_Not_Exists() { Assert.Throws(() => _environment.GetRequired("nonExistentKey")); } /// /// 验证Register方法应该将值添加到字典中 /// [Test] public void Register_Should_Add_Value_To_Dictionary() { _environment.Register("newKey", "newValue"); var result = _environment.Get("newKey"); Assert.That(result, Is.EqualTo("newValue")); } /// /// 验证Register方法应该覆盖已存在的值 /// [Test] public void Register_Should_Overwrite_Existing_Value() { _environment.Register("testKey", "value1"); _environment.Register("testKey", "value2"); var result = _environment.Get("testKey"); Assert.That(result, Is.EqualTo("value2")); } /// /// 验证通过IEnvironment接口的Register方法应该能够添加值 /// [Test] public void IEnvironment_Register_Should_Add_Value() { IEnvironment env = _environment; env.Register("interfaceKey", "interfaceValue"); var result = env.Get("interfaceKey"); Assert.That(result, Is.EqualTo("interfaceValue")); } } /// /// 测试环境实现类,继承自EnvironmentBase /// public class TestEnvironment : EnvironmentBase { /// /// 获取环境名称 /// public override string Name { get; } = "TestEnvironment"; /// /// 注册键值对到环境中 /// /// 要注册的键 /// 要注册的值 public new void Register(string key, object value) { base.Register(key, value); } /// /// 初始化环境 /// public override void Initialize() { } }