mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 19:03:29 +08:00
- 在测试代码中添加了完整的 LogAttribute 定义 - 添加了 ILogger 接口和相关实现类定义 - 包含 LoggerFactoryResolver 和 MockLoggerProvider 实现 - 补充了 MockLogger 类的具体实现 - 确保所有测试用例都有完整的基础依赖项定义
208 lines
8.1 KiB
C#
208 lines
8.1 KiB
C#
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 System;
|
|
|
|
namespace GFramework.SourceGenerators.Abstractions.enums
|
|
{
|
|
[AttributeUsage(AttributeTargets.Enum)]
|
|
public sealed class GenerateEnumExtensionsAttribute : Attribute
|
|
{
|
|
public bool GenerateIsMethods { get; set; } = true;
|
|
public bool GenerateIsInMethod { get; set; } = true;
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
using GFramework.SourceGenerators.Abstractions.enums;
|
|
|
|
[GenerateEnumExtensions]
|
|
public enum Status
|
|
{
|
|
Active,
|
|
Inactive,
|
|
Pending
|
|
}
|
|
}
|
|
""";
|
|
|
|
await GeneratorSnapshotTest<EnumExtensionsGenerator>.RunAsync(
|
|
source,
|
|
Path.Combine(
|
|
TestContext.CurrentContext.TestDirectory,
|
|
"enums",
|
|
"snapshots",
|
|
"EnumExtensionsGenerator",
|
|
"BasicEnum_IsMethods"));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Snapshot_BasicEnum_IsInMethod()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
|
|
namespace GFramework.SourceGenerators.Abstractions.enums
|
|
{
|
|
[AttributeUsage(AttributeTargets.Enum)]
|
|
public sealed class GenerateEnumExtensionsAttribute : Attribute
|
|
{
|
|
public bool GenerateIsMethods { get; set; } = true;
|
|
public bool GenerateIsInMethod { get; set; } = true;
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
using GFramework.SourceGenerators.Abstractions.enums;
|
|
|
|
[GenerateEnumExtensions]
|
|
public enum Status
|
|
{
|
|
Active,
|
|
Inactive
|
|
}
|
|
}
|
|
""";
|
|
|
|
await GeneratorSnapshotTest<EnumExtensionsGenerator>.RunAsync(
|
|
source,
|
|
Path.Combine(
|
|
TestContext.CurrentContext.TestDirectory,
|
|
"enums",
|
|
"snapshots",
|
|
"EnumExtensionsGenerator",
|
|
"BasicEnum_IsInMethod"));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Snapshot_EnumWithFlagValues()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
|
|
namespace GFramework.SourceGenerators.Abstractions.enums
|
|
{
|
|
[AttributeUsage(AttributeTargets.Enum)]
|
|
public sealed class GenerateEnumExtensionsAttribute : Attribute
|
|
{
|
|
public bool GenerateIsMethods { get; set; } = true;
|
|
public bool GenerateIsInMethod { get; set; } = true;
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
using GFramework.SourceGenerators.Abstractions.enums;
|
|
|
|
[GenerateEnumExtensions]
|
|
[Flags]
|
|
public enum Permissions
|
|
{
|
|
None = 0,
|
|
Read = 1,
|
|
Write = 2,
|
|
Execute = 4
|
|
}
|
|
}
|
|
""";
|
|
|
|
await GeneratorSnapshotTest<EnumExtensionsGenerator>.RunAsync(
|
|
source,
|
|
Path.Combine(
|
|
TestContext.CurrentContext.TestDirectory,
|
|
"enums",
|
|
"snapshots",
|
|
"EnumExtensionsGenerator",
|
|
"EnumWithFlagValues"));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Snapshot_DisableIsMethods()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
|
|
namespace GFramework.SourceGenerators.Abstractions.enums
|
|
{
|
|
[AttributeUsage(AttributeTargets.Enum)]
|
|
public sealed class GenerateEnumExtensionsAttribute : Attribute
|
|
{
|
|
public bool GenerateIsMethods { get; set; } = true;
|
|
public bool GenerateIsInMethod { get; set; } = true;
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
using GFramework.SourceGenerators.Abstractions.enums;
|
|
|
|
[GenerateEnumExtensions(GenerateIsMethods = false)]
|
|
public enum Status
|
|
{
|
|
Active,
|
|
Inactive
|
|
}
|
|
}
|
|
""";
|
|
|
|
await GeneratorSnapshotTest<EnumExtensionsGenerator>.RunAsync(
|
|
source,
|
|
Path.Combine(
|
|
TestContext.CurrentContext.TestDirectory,
|
|
"enums",
|
|
"snapshots",
|
|
"EnumExtensionsGenerator",
|
|
"DisableIsMethods"));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Snapshot_DisableIsInMethod()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
|
|
namespace GFramework.SourceGenerators.Abstractions.enums
|
|
{
|
|
[AttributeUsage(AttributeTargets.Enum)]
|
|
public sealed class GenerateEnumExtensionsAttribute : Attribute
|
|
{
|
|
public bool GenerateIsMethods { get; set; } = true;
|
|
public bool GenerateIsInMethod { get; set; } = true;
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
using GFramework.SourceGenerators.Abstractions.enums;
|
|
|
|
[GenerateEnumExtensions(GenerateIsInMethod = false)]
|
|
public enum Status
|
|
{
|
|
Active,
|
|
Inactive
|
|
}
|
|
}
|
|
""";
|
|
|
|
await GeneratorSnapshotTest<EnumExtensionsGenerator>.RunAsync(
|
|
source,
|
|
Path.Combine(
|
|
TestContext.CurrentContext.TestDirectory,
|
|
"enums",
|
|
"snapshots",
|
|
"EnumExtensionsGenerator",
|
|
"DisableIsInMethod"));
|
|
}
|
|
} |