XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.XUnit

【DLL】提供方便快捷的测试,无需编写Main方法,可直接添加特性在类或方法上进行测试

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension.NetCore.XUnit

优化分析器与生成器,升级包版本至2.1.0

重构了 RegisterCodeFixesAsync 方法,提升代码简洁性;修正诊断文档链接及变量声明风格;生成器采用 C# 11 原始字符串字面量并补充全局 using,修正命名空间拼写;csproj 增加 LangVersion 支持最新特性,NuGet 包版本升级至 2.1.0。

c8b6990
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

5 个文件 +36 -29
Modified XFEExtension.NetCore.XUnit.Analyzer/CodeFix/XUnitCodeFixProvider.cs +10 -12
@@ -20,20 +20,18 @@ namespace XFEExtension.NetCore.XUnit.Analyzer.CodeFix
20 20
21 21 public override Task RegisterCodeFixesAsync(CodeFixContext context)
22 22 {
23 foreach (var diagnostic in context.Diagnostics)
23 foreach (var diagnostic in context.Diagnostics.Where(diagnostic => diagnostic.Id == XUnitCodeAnalyzer.SMTestID))
24 24 {
25 if (diagnostic.Id == XUnitCodeAnalyzer.SMTestID)
26 {
27 context.RegisterCodeFix(CodeAction.Create(title: "改用MTest特性",
28 createChangedDocument: c => ChangeSMTestToMTestAsync(context.Document, diagnostic, c),
29 equivalenceKey: "改用MTest特性"),
30 diagnostic: diagnostic);
31 context.RegisterCodeFix(CodeAction.Create(title: "将方法改为静态方法",
32 createChangedDocument: c => MakeMethodStaticAsync(context.Document, diagnostic, c),
33 equivalenceKey: "将方法改为静态方法"),
34 diagnostic: diagnostic);
35 }
25 context.RegisterCodeFix(CodeAction.Create(title: "改用MTest特性",
26 createChangedDocument: c => ChangeSMTestToMTestAsync(context.Document, diagnostic, c),
27 equivalenceKey: "改用MTest特性"),
28 diagnostic: diagnostic);
29 context.RegisterCodeFix(CodeAction.Create(title: "将方法改为静态方法",
30 createChangedDocument: c => MakeMethodStaticAsync(context.Document, diagnostic, c),
31 equivalenceKey: "将方法改为静态方法"),
32 diagnostic: diagnostic);
36 33 }
34
37 35 return Task.CompletedTask;
38 36 }
39 37
Modified XFEExtension.NetCore.XUnit.Analyzer/Diagnostics/XUnitCodeAnalyzer.cs +2 -3
@@ -18,7 +18,7 @@ namespace XFEExtension.NetCore.XUnit.Analyzer.Diagnostics
18 18 DiagnosticSeverity.Error,
19 19 true,
20 20 "SMTest特性不能用在静态方法之外的地方.",
21 "https://www.xfegzs.com/Docs/View/Errors%2FXUnit%2FXFE0001");
21 "https://docs.xfegzs.com/View/Errors%2FXUnit%2FXFE0001");
22 22
23 23 public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(SMTestError);
24 24
@@ -35,12 +35,11 @@ namespace XFEExtension.NetCore.XUnit.Analyzer.Diagnostics
35 35 foreach (var syntaxTree in syntaxTrees)
36 36 {
37 37 var root = syntaxTree.GetRoot();
38 var classes = root.DescendantNodes().OfType<ClassDeclarationSyntax>();
39 38 var methodDeclarations = root.DescendantNodes().OfType<MethodDeclarationSyntax>();
40 39 foreach (var methodDeclaration in methodDeclarations)
41 40 {
42 41 var modifiers = methodDeclaration.Modifiers;
43 bool isStatic = modifiers.Any(modifier => modifier.IsKind(SyntaxKind.StaticKeyword));
42 var isStatic = modifiers.Any(modifier => modifier.IsKind(SyntaxKind.StaticKeyword));
44 43 if (isStatic)
45 44 continue;
46 45 var methodName = methodDeclaration.Identifier.ValueText;
Modified XFEExtension.NetCore.XUnit.Analyzer/Generator/XUnitCodeGenerator.cs +17 -12
@@ -9,19 +9,24 @@ namespace XFEExtension.NetCore.XUnit.Analyzer.Generator
9 9 {
10 10 context.RegisterForPostInitialization(init =>
11 11 {
12 string globalUsingCode = @"global using global::XFEExtension.NetCore.XUnit;";
12 const string globalUsingCode = """
13 global using global::XFEExtension.NetCore.XUnit;
14 global using global::XFEExtension.NetCore.XUnit,Attributes;
15 """;
13 16 init.AddSource("GlobalUsing.g.cs", globalUsingCode);
14 string xUnitCode = $@"namespace XFEExtension.NetCore.XUnit
15 {{
16 public static class XUnitCode
17 {{
18 public async static Task Main(string[] args)
19 {{
20 await global::XFEExtension.NetCore.XUnit.XFECode.RunTest();
21 }}
22 }}
23 }}
24 ";
17 const string xUnitCode = """
18 namespace XFEExtension.NetCore.XUnit
19 {
20 public static class XUnitCode
21 {
22 public async static Task Main(string[] args)
23 {
24 await global::XFEExtension.NetCore.XUnit.XFECode.RunTest();
25 }
26 }
27 }
28
29 """;
25 30 init.AddSource($"XUnitCode.g.cs", xUnitCode);
26 31 });
27 32 }
Modified XFEExtension.NetCore.XUnit.Analyzer/XFEExtension.NetCore.XUnit.Analyzer.csproj +1 -0
@@ -2,6 +2,7 @@
2 2
3 3 <PropertyGroup>
4 4 <TargetFramework>netstandard2.0</TargetFramework>
5 <LangVersion>latest</LangVersion>
5 6 </PropertyGroup>
6 7
7 8 <PropertyGroup>
Modified XFEExtension.NetCore.XUnit/XFEExtension.NetCore.XUnit.csproj +6 -2
@@ -3,7 +3,6 @@
3 3 <PropertyGroup>
4 4 <TargetFramework>net8.0</TargetFramework>
5 5 <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
6 <PackageReleaseNotes>更新引用包</PackageReleaseNotes>
7 6 <Title>XUnit</Title>
8 7 <Authors>XFEstudio</Authors>
9 8 <Company>寰宇朽力网络科技有限公司</Company>
@@ -14,9 +13,14 @@
14 13 <PackageReadmeFile>README.md</PackageReadmeFile>
15 14 <RepositoryUrl>https://github.com/XFEstudio/XFEExtension.NetCore.XUnit</RepositoryUrl>
16 15 <PackageTags>XFE;Test;XUnit;Unit</PackageTags>
17 <Version>2.0.5</Version>
16 <Version>2.1.0</Version>
18 17 <PackageLicenseFile>LICENSE</PackageLicenseFile>
19 18 <PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
19 <PackageReleaseNotes>
20 优化分析器与生成器,升级包版本至2.1.0
21
22 重构了 RegisterCodeFixesAsync 方法,提升代码简洁性;修正诊断文档链接及变量声明风格;生成器采用 C# 11 原始字符串字面量并补充全局 using,修正命名空间拼写;csproj 增加 LangVersion 支持最新特性,NuGet 包版本升级至 2.1.0。
23 </PackageReleaseNotes>
20 24 </PropertyGroup>
21 25
22 26 <ItemGroup>