From 5d11666fd8de98165047edd6bef15fcc6305d393 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Thu, 15 Jan 2026 14:36:30 +0800 Subject: [PATCH] =?UTF-8?q?test(core):=20=E6=B7=BB=E5=8A=A0=E6=A0=B8?= =?UTF-8?q?=E5=BF=83=E7=BB=84=E4=BB=B6=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 为 ContextAware 功能添加全面的单元测试覆盖 - 增加对枚举扩展生成器的快照测试验证 - 实现环境管理器的完整测试用例集 - 添加事件总线功能的核心测试验证 - 为游戏上下文管理添加架构测试 - 扩展注销列表扩展方法的测试覆盖 - 增加注销机制的全面单元测试验证 - [skip ci] --- .../architecture/GameContextTests.cs | 198 ++++++++++++++++++ .../environment/EnvironmentTests.cs | 156 ++++++++++++++ GFramework.Core.Tests/events/EventBusTests.cs | 61 ++++++ .../events/UnRegisterTests.cs | 89 ++++++++ .../UnRegisterListExtensionTests.cs | 99 +++++++++ .../rule/ContextAwareTests.cs | 76 +++++++ .../EnumExtensionsGeneratorSnapshotTests.cs | 149 +++++++++++++ .../logging/LoggerGeneratorSnapshotTests.cs | 159 ++++++++++++++ 8 files changed, 987 insertions(+) create mode 100644 GFramework.Core.Tests/architecture/GameContextTests.cs create mode 100644 GFramework.Core.Tests/environment/EnvironmentTests.cs create mode 100644 GFramework.Core.Tests/events/EventBusTests.cs create mode 100644 GFramework.Core.Tests/events/UnRegisterTests.cs create mode 100644 GFramework.Core.Tests/extensions/UnRegisterListExtensionTests.cs create mode 100644 GFramework.Core.Tests/rule/ContextAwareTests.cs create mode 100644 GFramework.SourceGenerators.Tests/enums/EnumExtensionsGeneratorSnapshotTests.cs create mode 100644 GFramework.SourceGenerators.Tests/logging/LoggerGeneratorSnapshotTests.cs diff --git a/GFramework.Core.Tests/architecture/GameContextTests.cs b/GFramework.Core.Tests/architecture/GameContextTests.cs new file mode 100644 index 0000000..fb89558 --- /dev/null +++ b/GFramework.Core.Tests/architecture/GameContextTests.cs @@ -0,0 +1,198 @@ +using GFramework.Core.Abstractions.architecture; +using GFramework.Core.Abstractions.command; +using GFramework.Core.Abstractions.environment; +using GFramework.Core.Abstractions.events; +using GFramework.Core.Abstractions.ioc; +using GFramework.Core.Abstractions.model; +using GFramework.Core.Abstractions.query; +using GFramework.Core.Abstractions.system; +using GFramework.Core.Abstractions.utility; +using GFramework.Core.architecture; +using GFramework.Core.command; +using GFramework.Core.environment; +using GFramework.Core.events; +using GFramework.Core.ioc; +using GFramework.Core.query; +using NUnit.Framework; + +namespace GFramework.Core.Tests.architecture; + +[TestFixture] +public class GameContextTests +{ + [SetUp] + public void SetUp() + { + GameContext.Clear(); + } + + [TearDown] + public void TearDown() + { + GameContext.Clear(); + } + + [Test] + public void ArchitectureReadOnlyDictionary_Should_Return_Empty_At_Start() + { + var dict = GameContext.ArchitectureReadOnlyDictionary; + + Assert.That(dict.Count, Is.EqualTo(0)); + } + + [Test] + public void Bind_Should_Add_Context_To_Dictionary() + { + var context = new TestArchitectureContext(); + + GameContext.Bind(typeof(TestArchitecture), context); + + Assert.That(GameContext.ArchitectureReadOnlyDictionary.Count, Is.EqualTo(1)); + } + + [Test] + public void Bind_WithDuplicateType_Should_ThrowInvalidOperationException() + { + var context1 = new TestArchitectureContext(); + var context2 = new TestArchitectureContext(); + + GameContext.Bind(typeof(TestArchitecture), context1); + + Assert.Throws(() => + GameContext.Bind(typeof(TestArchitecture), context2)); + } + + [Test] + public void GetByType_Should_Return_Correct_Context() + { + var context = new TestArchitectureContext(); + GameContext.Bind(typeof(TestArchitecture), context); + + var result = GameContext.GetByType(typeof(TestArchitecture)); + + Assert.That(result, Is.SameAs(context)); + } + + [Test] + public void GetByType_Should_Throw_When_Not_Found() + { + Assert.Throws(() => + GameContext.GetByType(typeof(TestArchitecture))); + } + + [Test] + public void GetGeneric_Should_Return_Correct_Context() + { + var context = new TestArchitectureContext(); + GameContext.Bind(typeof(TestArchitectureContext), context); + + var result = GameContext.Get(); + + Assert.That(result, Is.SameAs(context)); + } + + [Test] + public void TryGet_Should_ReturnTrue_When_Found() + { + var context = new TestArchitectureContext(); + GameContext.Bind(typeof(TestArchitectureContext), context); + + var result = GameContext.TryGet(out TestArchitectureContext? foundContext); + + Assert.That(result, Is.True); + Assert.That(foundContext, Is.SameAs(context)); + } + + [Test] + public void TryGet_Should_ReturnFalse_When_Not_Found() + { + var result = GameContext.TryGet(out TestArchitectureContext? foundContext); + + Assert.That(result, Is.False); + Assert.That(foundContext, Is.Null); + } + + [Test] + public void GetFirstArchitectureContext_Should_Return_When_Exists() + { + var context = new TestArchitectureContext(); + GameContext.Bind(typeof(TestArchitecture), context); + + var result = GameContext.GetFirstArchitectureContext(); + + Assert.That(result, Is.SameAs(context)); + } + + [Test] + public void GetFirstArchitectureContext_Should_Throw_When_Empty() + { + Assert.Throws(() => + GameContext.GetFirstArchitectureContext()); + } + + [Test] + public void Unbind_Should_Remove_Context() + { + var context = new TestArchitectureContext(); + GameContext.Bind(typeof(TestArchitecture), context); + + GameContext.Unbind(typeof(TestArchitecture)); + + Assert.That(GameContext.ArchitectureReadOnlyDictionary.Count, Is.EqualTo(0)); + } + + [Test] + public void Clear_Should_Remove_All_Contexts() + { + GameContext.Bind(typeof(TestArchitecture), new TestArchitectureContext()); + GameContext.Bind(typeof(TestArchitectureContext), new TestArchitectureContext()); + + GameContext.Clear(); + + Assert.That(GameContext.ArchitectureReadOnlyDictionary.Count, Is.EqualTo(0)); + } +} + +public class TestArchitecture : Architecture +{ + protected override void Init() + { + } +} + +public class TestArchitectureContext : IArchitectureContext +{ + private readonly IocContainer _container = new(); + + public IIocContainer Container => _container; + public IEventBus EventBus => new EventBus(); + public ICommandBus CommandBus => new CommandBus(); + public IQueryBus QueryBus => new QueryBus(); + public IEnvironment Environment => new DefaultEnvironment(); + + public TModel? GetModel() where TModel : class, IModel => _container.Get(); + public TSystem? GetSystem() where TSystem : class, ISystem => _container.Get(); + public TUtility? GetUtility() where TUtility : class, IUtility => _container.Get(); + + public void SendEvent() where TEvent : new() + { + } + + public void SendEvent(TEvent e) where TEvent : class + { + } + + public IUnRegister RegisterEvent(Action handler) => new DefaultUnRegister(() => { }); + + public void UnRegisterEvent(Action onEvent) + { + } + + public void SendCommand(ICommand command) + { + } + + public TResult SendCommand(ICommand command) => default!; + public TResult SendQuery(IQuery query) => default!; + public IEnvironment GetEnvironment() => Environment; +} \ No newline at end of file diff --git a/GFramework.Core.Tests/environment/EnvironmentTests.cs b/GFramework.Core.Tests/environment/EnvironmentTests.cs new file mode 100644 index 0000000..e77c619 --- /dev/null +++ b/GFramework.Core.Tests/environment/EnvironmentTests.cs @@ -0,0 +1,156 @@ +using GFramework.Core.Abstractions.environment; +using GFramework.Core.environment; +using NUnit.Framework; + +namespace GFramework.Core.Tests.environment; + +[TestFixture] +public class EnvironmentTests +{ + [SetUp] + public void SetUp() + { + _environment = new TestEnvironment(); + _environment.Initialize(); + } + + private TestEnvironment _environment = null!; + + [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()); + } + + [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")); + } + + [Test] + public void Get_Should_ReturnNull_When_Key_Not_Exists() + { + var result = _environment.Get("nonExistentKey"); + + Assert.That(result, Is.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); + } + + [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")); + } + + [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); + } + + [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); + } + + [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")); + } + + [Test] + public void GetRequired_Should_ThrowInvalidOperationException_When_Key_Not_Exists() + { + Assert.Throws(() => + _environment.GetRequired("nonExistentKey")); + } + + [Test] + public void Register_Should_Add_Value_To_Dictionary() + { + _environment.Register("newKey", "newValue"); + + var result = _environment.Get("newKey"); + + Assert.That(result, Is.EqualTo("newValue")); + } + + [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")); + } + + [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")); + } +} + +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() + { + } +} \ No newline at end of file diff --git a/GFramework.Core.Tests/events/EventBusTests.cs b/GFramework.Core.Tests/events/EventBusTests.cs new file mode 100644 index 0000000..608d1bb --- /dev/null +++ b/GFramework.Core.Tests/events/EventBusTests.cs @@ -0,0 +1,61 @@ +using GFramework.Core.events; +using NUnit.Framework; + +namespace GFramework.Core.Tests.events; + +[TestFixture] +public class EventBusTests +{ + [SetUp] + public void SetUp() + { + _eventBus = new EventBus(); + } + + private EventBus _eventBus = null!; + + [Test] + public void Register_Should_Add_Handler() + { + var called = false; + _eventBus.Register(@event => { called = true; }); + + _eventBus.Send(); + + Assert.That(called, Is.True); + } + + [Test] + public void UnRegister_Should_Remove_Handler() + { + var count = 0; + + Action handler = @event => { count++; }; + _eventBus.Register(handler); + _eventBus.Send(); + Assert.That(count, Is.EqualTo(1)); + + _eventBus.UnRegister(handler); + _eventBus.Send(); + Assert.That(count, Is.EqualTo(1)); + } + + [Test] + public void SendEvent_Should_Invoke_All_Handlers() + { + var count1 = 0; + var count2 = 0; + + _eventBus.Register(@event => { count1++; }); + _eventBus.Register(@event => { count2++; }); + + _eventBus.Send(); + + Assert.That(count1, Is.EqualTo(1)); + Assert.That(count2, Is.EqualTo(1)); + } +} + +public class EventBusTestsEvent +{ +} \ No newline at end of file diff --git a/GFramework.Core.Tests/events/UnRegisterTests.cs b/GFramework.Core.Tests/events/UnRegisterTests.cs new file mode 100644 index 0000000..8a5ed78 --- /dev/null +++ b/GFramework.Core.Tests/events/UnRegisterTests.cs @@ -0,0 +1,89 @@ +using GFramework.Core.events; +using GFramework.Core.property; +using NUnit.Framework; + +namespace GFramework.Core.Tests.events; + +[TestFixture] +public class UnRegisterTests +{ + [Test] + public void DefaultUnRegister_Should_InvokeCallback_When_UnRegisterCalled() + { + var invoked = false; + var unRegister = new DefaultUnRegister(() => { invoked = true; }); + + unRegister.UnRegister(); + + Assert.That(invoked, Is.True); + } + + [Test] + public void DefaultUnRegister_Should_ClearCallback_After_UnRegister() + { + var callCount = 0; + var unRegister = new DefaultUnRegister(() => { callCount++; }); + + unRegister.UnRegister(); + unRegister.UnRegister(); + + Assert.That(callCount, Is.EqualTo(1)); + } + + [Test] + public void DefaultUnRegister_WithNullCallback_Should_NotThrow() + { + var unRegister = new DefaultUnRegister(null!); + + Assert.DoesNotThrow(() => unRegister.UnRegister()); + } + + [Test] + public void BindablePropertyUnRegister_Should_UnRegister_From_Property() + { + var property = new BindableProperty(0); + var callCount = 0; + + Action handler = _ => { callCount++; }; + property.Register(handler); + + var unRegister = new BindablePropertyUnRegister(property, handler); + unRegister.UnRegister(); + + property.Value = 42; + + Assert.That(callCount, Is.EqualTo(0)); + } + + [Test] + public void BindablePropertyUnRegister_Should_Clear_References() + { + var property = new BindableProperty(0); + + Action handler = _ => { }; + var unRegister = new BindablePropertyUnRegister(property, handler); + + unRegister.UnRegister(); + + Assert.That(unRegister.BindableProperty, Is.Null); + Assert.That(unRegister.OnValueChanged, Is.Null); + } + + [Test] + public void BindablePropertyUnRegister_WithNull_Property_Should_NotThrow() + { + Action handler = _ => { }; + var unRegister = new BindablePropertyUnRegister(null!, handler); + + Assert.DoesNotThrow(() => unRegister.UnRegister()); + } + + [Test] + public void BindablePropertyUnRegister_WithNull_Handler_Should_NotThrow() + { + var property = new BindableProperty(0); + var unRegister = new BindablePropertyUnRegister(property, null!); + + Assert.DoesNotThrow(() => unRegister.UnRegister()); + } +} \ No newline at end of file diff --git a/GFramework.Core.Tests/extensions/UnRegisterListExtensionTests.cs b/GFramework.Core.Tests/extensions/UnRegisterListExtensionTests.cs new file mode 100644 index 0000000..73fea1c --- /dev/null +++ b/GFramework.Core.Tests/extensions/UnRegisterListExtensionTests.cs @@ -0,0 +1,99 @@ +using GFramework.Core.Abstractions.events; +using GFramework.Core.events; +using GFramework.Core.extensions; +using NUnit.Framework; + +namespace GFramework.Core.Tests.extensions; + +[TestFixture] +public class UnRegisterListExtensionTests +{ + [SetUp] + public void SetUp() + { + _unRegisterList = new TestUnRegisterList(); + } + + private TestUnRegisterList _unRegisterList = null!; + + [Test] + public void AddToUnregisterList_Should_Add_To_List() + { + var unRegister = new DefaultUnRegister(() => { }); + + unRegister.AddToUnregisterList(_unRegisterList); + + Assert.That(_unRegisterList.UnregisterList.Count, Is.EqualTo(1)); + } + + [Test] + public void AddToUnregisterList_Should_Add_Multiple_Elements() + { + var unRegister1 = new DefaultUnRegister(() => { }); + var unRegister2 = new DefaultUnRegister(() => { }); + var unRegister3 = new DefaultUnRegister(() => { }); + + unRegister1.AddToUnregisterList(_unRegisterList); + unRegister2.AddToUnregisterList(_unRegisterList); + unRegister3.AddToUnregisterList(_unRegisterList); + + Assert.That(_unRegisterList.UnregisterList.Count, Is.EqualTo(3)); + } + + [Test] + public void UnRegisterAll_Should_UnRegister_All_Elements() + { + var invoked1 = false; + var invoked2 = false; + var invoked3 = false; + + var unRegister1 = new DefaultUnRegister(() => { invoked1 = true; }); + var unRegister2 = new DefaultUnRegister(() => { invoked2 = true; }); + var unRegister3 = new DefaultUnRegister(() => { invoked3 = true; }); + + unRegister1.AddToUnregisterList(_unRegisterList); + unRegister2.AddToUnregisterList(_unRegisterList); + unRegister3.AddToUnregisterList(_unRegisterList); + + _unRegisterList.UnRegisterAll(); + + Assert.That(invoked1, Is.True); + Assert.That(invoked2, Is.True); + Assert.That(invoked3, Is.True); + } + + [Test] + public void UnRegisterAll_Should_Clear_List() + { + var unRegister = new DefaultUnRegister(() => { }); + unRegister.AddToUnregisterList(_unRegisterList); + + _unRegisterList.UnRegisterAll(); + + Assert.That(_unRegisterList.UnregisterList.Count, Is.EqualTo(0)); + } + + [Test] + public void UnRegisterAll_Should_Not_Throw_When_Empty() + { + Assert.DoesNotThrow(() => _unRegisterList.UnRegisterAll()); + } + + [Test] + public void UnRegisterAll_Should_Invoke_Once_Per_Element() + { + var callCount = 0; + var unRegister = new DefaultUnRegister(() => { callCount++; }); + + unRegister.AddToUnregisterList(_unRegisterList); + + _unRegisterList.UnRegisterAll(); + + Assert.That(callCount, Is.EqualTo(1)); + } +} + +public class TestUnRegisterList : IUnRegisterList +{ + public IList UnregisterList { get; } = new List(); +} \ No newline at end of file diff --git a/GFramework.Core.Tests/rule/ContextAwareTests.cs b/GFramework.Core.Tests/rule/ContextAwareTests.cs new file mode 100644 index 0000000..d58e4b0 --- /dev/null +++ b/GFramework.Core.Tests/rule/ContextAwareTests.cs @@ -0,0 +1,76 @@ +using GFramework.Core.Abstractions.architecture; +using GFramework.Core.Abstractions.rule; +using GFramework.Core.rule; +using GFramework.Core.Tests.architecture; +using NUnit.Framework; + +namespace GFramework.Core.Tests.rule; + +[TestFixture] +public class ContextAwareTests +{ + [SetUp] + public void SetUp() + { + _contextAware = new TestContextAware(); + _mockContext = new TestArchitectureContext(); + } + + private TestContextAware _contextAware = null!; + private TestArchitectureContext _mockContext = null!; + + [Test] + public void SetContext_Should_Set_Context_Property() + { + IContextAware aware = _contextAware; + aware.SetContext(_mockContext); + + Assert.That(_contextAware.PublicContext, Is.SameAs(_mockContext)); + } + + [Test] + public void SetContext_Should_Call_OnContextReady() + { + IContextAware aware = _contextAware; + aware.SetContext(_mockContext); + + Assert.That(_contextAware.OnContextReadyCalled, Is.True); + } + + [Test] + public void GetContext_Should_Return_Set_Context() + { + IContextAware aware = _contextAware; + aware.SetContext(_mockContext); + + var result = aware.GetContext(); + + Assert.That(result, Is.SameAs(_mockContext)); + } + + [Test] + public void GetContext_Should_Return_FirstArchitectureContext_When_Not_Set() + { + // Arrange - 暂时不调用 SetContext,让 Context 为 null + IContextAware aware = _contextAware; + + // Act - 当 Context 为 null 时,应该返回第一个 Architecture Context + // 由于测试环境中没有实际的 Architecture Context,这里只测试调用不会抛出异常 + // 在实际使用中,当 Context 为 null 时会调用 GameContext.GetFirstArchitectureContext() + + // Assert - 验证在没有设置 Context 时的行为 + // 注意:由于测试环境中可能没有 Architecture Context,这里我们只测试不抛出异常 + Assert.DoesNotThrow(() => aware.GetContext()); + } +} + +public class TestContextAware : ContextAwareBase +{ + public IArchitectureContext? PublicContext => Context; + public bool OnContextReadyCalled { get; private set; } + + protected override void OnContextReady() + { + OnContextReadyCalled = true; + } +} \ No newline at end of file diff --git a/GFramework.SourceGenerators.Tests/enums/EnumExtensionsGeneratorSnapshotTests.cs b/GFramework.SourceGenerators.Tests/enums/EnumExtensionsGeneratorSnapshotTests.cs new file mode 100644 index 0000000..62ffab8 --- /dev/null +++ b/GFramework.SourceGenerators.Tests/enums/EnumExtensionsGeneratorSnapshotTests.cs @@ -0,0 +1,149 @@ +using GFramework.SourceGenerators.enums; +using GFramework.SourceGenerators.Tests.core; +using NUnit.Framework; + +namespace GFramework.SourceGenerators.Tests.enums; + +[TestFixture] +public class EnumExtensionsGeneratorSnapshotTests +{ + [Test] + public async Task Snapshot_BasicEnum_IsMethods() + { + const string source = """ + using GFramework.SourceGenerators.Abstractions.enums; + + namespace TestApp + { + [GenerateEnumExtensions] + public enum Status + { + Active, + Inactive, + Pending + } + } + """; + + await GeneratorSnapshotTest.RunAsync( + source, + Path.Combine( + TestContext.CurrentContext.TestDirectory, + "enums", + "snapshots", + "EnumExtensionsGenerator", + "BasicEnum_IsMethods")); + } + + [Test] + public async Task Snapshot_BasicEnum_IsInMethod() + { + const string source = """ + using GFramework.SourceGenerators.Abstractions.enums; + + namespace TestApp + { + [GenerateEnumExtensions] + public enum Status + { + Active, + Inactive + } + } + """; + + await GeneratorSnapshotTest.RunAsync( + source, + Path.Combine( + TestContext.CurrentContext.TestDirectory, + "enums", + "snapshots", + "EnumExtensionsGenerator", + "BasicEnum_IsInMethod")); + } + + [Test] + public async Task Snapshot_EnumWithFlagValues() + { + const string source = """ + using GFramework.SourceGenerators.Abstractions.enums; + using System; + + namespace TestApp + { + [GenerateEnumExtensions] + [Flags] + public enum Permissions + { + None = 0, + Read = 1, + Write = 2, + Execute = 4 + } + } + """; + + await GeneratorSnapshotTest.RunAsync( + source, + Path.Combine( + TestContext.CurrentContext.TestDirectory, + "enums", + "snapshots", + "EnumExtensionsGenerator", + "EnumWithFlagValues")); + } + + [Test] + public async Task Snapshot_DisableIsMethods() + { + const string source = """ + using GFramework.SourceGenerators.Abstractions.enums; + + namespace TestApp + { + [GenerateEnumExtensions(GenerateIsMethods = false)] + public enum Status + { + Active, + Inactive + } + } + """; + + await GeneratorSnapshotTest.RunAsync( + source, + Path.Combine( + TestContext.CurrentContext.TestDirectory, + "enums", + "snapshots", + "EnumExtensionsGenerator", + "DisableIsMethods")); + } + + [Test] + public async Task Snapshot_DisableIsInMethod() + { + const string source = """ + using GFramework.SourceGenerators.Abstractions.enums; + + namespace TestApp + { + [GenerateEnumExtensions(GenerateIsInMethod = false)] + public enum Status + { + Active, + Inactive + } + } + """; + + await GeneratorSnapshotTest.RunAsync( + source, + Path.Combine( + TestContext.CurrentContext.TestDirectory, + "enums", + "snapshots", + "EnumExtensionsGenerator", + "DisableIsInMethod")); + } +} \ No newline at end of file diff --git a/GFramework.SourceGenerators.Tests/logging/LoggerGeneratorSnapshotTests.cs b/GFramework.SourceGenerators.Tests/logging/LoggerGeneratorSnapshotTests.cs new file mode 100644 index 0000000..7bc3af3 --- /dev/null +++ b/GFramework.SourceGenerators.Tests/logging/LoggerGeneratorSnapshotTests.cs @@ -0,0 +1,159 @@ +using GFramework.SourceGenerators.logging; +using GFramework.SourceGenerators.Tests.core; +using NUnit.Framework; + +namespace GFramework.SourceGenerators.Tests.logging; + +[TestFixture] +public class LoggerGeneratorSnapshotTests +{ + [Test] + public async Task Snapshot_DefaultConfiguration_Class() + { + const string source = """ + using GFramework.SourceGenerators.Abstractions.logging; + + namespace TestApp + { + [Log] + public partial class MyService + { + } + } + """; + + await GeneratorSnapshotTest.RunAsync( + source, + Path.Combine( + TestContext.CurrentContext.TestDirectory, + "logging", + "snapshots", + "LoggerGenerator", + "DefaultConfiguration_Class")); + } + + [Test] + public async Task Snapshot_CustomName_Class() + { + const string source = """ + using GFramework.SourceGenerators.Abstractions.logging; + + namespace TestApp + { + [Log(Name = "CustomLogger")] + public partial class MyService + { + } + } + """; + + await GeneratorSnapshotTest.RunAsync( + source, + Path.Combine( + TestContext.CurrentContext.TestDirectory, + "logging", + "snapshots", + "LoggerGenerator", + "CustomName_Class")); + } + + [Test] + public async Task Snapshot_CustomFieldName_Class() + { + const string source = """ + using GFramework.SourceGenerators.Abstractions.logging; + + namespace TestApp + { + [Log(FieldName = "MyLogger")] + public partial class MyService + { + } + } + """; + + await GeneratorSnapshotTest.RunAsync( + source, + Path.Combine( + TestContext.CurrentContext.TestDirectory, + "logging", + "snapshots", + "LoggerGenerator", + "CustomFieldName_Class")); + } + + [Test] + public async Task Snapshot_InstanceField_Class() + { + const string source = """ + using GFramework.SourceGenerators.Abstractions.logging; + + namespace TestApp + { + [Log(IsStatic = false)] + public partial class MyService + { + } + } + """; + + await GeneratorSnapshotTest.RunAsync( + source, + Path.Combine( + TestContext.CurrentContext.TestDirectory, + "logging", + "snapshots", + "LoggerGenerator", + "InstanceField_Class")); + } + + [Test] + public async Task Snapshot_PublicField_Class() + { + const string source = """ + using GFramework.SourceGenerators.Abstractions.logging; + + namespace TestApp + { + [Log(AccessModifier = "public")] + public partial class MyService + { + } + } + """; + + await GeneratorSnapshotTest.RunAsync( + source, + Path.Combine( + TestContext.CurrentContext.TestDirectory, + "logging", + "snapshots", + "LoggerGenerator", + "PublicField_Class")); + } + + [Test] + public async Task Snapshot_GenericClass() + { + const string source = """ + using GFramework.SourceGenerators.Abstractions.logging; + + namespace TestApp + { + [Log] + public partial class MyService + { + } + } + """; + + await GeneratorSnapshotTest.RunAsync( + source, + Path.Combine( + TestContext.CurrentContext.TestDirectory, + "logging", + "snapshots", + "LoggerGenerator", + "GenericClass")); + } +} \ No newline at end of file