c# 代码分析器测试不工作,但测试是正确的
c# code analyzer test is not working, but test is coorrect
我正在使用 Microsoft 模板编写简单的代码分析器和修复程序。我想在 else 语句中修复嵌套的 if 语句。示例:
using System;
using System.Runtime.InteropServices;
namespace ExamApp
{
public class Program
{
static void Main()
{
if (Condition())
{
Action();
}
else
{
if (Condition())
{
Action();
}
}
}
static bool Condition()
{
return false;
}
static void Action() { }
}
}
应转换为
using System;
using System.Runtime.InteropServices;
namespace ExamApp
{
public class Program
{
static void Main()
{
if (Condition())
{
Action();
}
else if (Condition())
{
Action();
}
}
static bool Condition()
{
return false;
}
static void Action() { }
}
}
我 运行 Analyzer.Vsix 打开另一个 VS window,打开一些沙箱项目,从第一个示例编写代码。 VS 向我展示了警告在正确的位置,建议修复它并正确修复它。我决定编写这样的测试:
//Diagnostic and CodeFix both triggered and checked for
[TestMethod]
public async Task TestMethod2()
{
var test = @"
using System;
using System.Runtime.InteropServices;
namespace ExamApp
{
public class Program
{
static void Main()
{
if (Condition())
{
Action();
}
else
{
if (Condition())
{
Action();
}
}
}
static bool Condition()
{
return false;
}
static void Action() { }
}
}";
var fixtest = @"
using System;
using System.Runtime.InteropServices;
namespace ExamApp
{
public class Program
{
static void Main()
{
if (Condition())
{
Action();
}
else if (Condition())
{
Action();
}
}
static bool Condition()
{
return false;
}
static void Action() { }
}
}";
var expected = VerifyCS.Diagnostic("AnalyzerTemplate").WithSpan(17, 13, 17, 17).WithArguments("IfKeyword");
await VerifyCS.VerifyCodeFixAsync(test,expected, fixtest);
}
但是 smth 出错了,这是输出:
Assert.Fail failed. Context: Iterative code fix application
content of '/0/Test0.cs' did not match. Diff shown with expected as baseline:
using System;
using System.Runtime.InteropServices;
namespace ExamApp
{
public class Program
{
static void Main()
{
if (Condition())
{
Action();
}
else if (Condition())
{
Action();
}
}
static bool Condition()
{
return false;
}
static void Action() { }
}
}
at Microsoft.CodeAnalysis.Testing.Verifiers.MSTestVerifier.Fail(String message) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Testing.Verifiers.MSTest/MSTestVerifier.cs:line 78
at Microsoft.CodeAnalysis.Testing.IVerifierExtensions.EqualOrDiff(IVerifier verifier, String expected, String actual, String message) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/Extensions/IVerifierExtensions.cs:line 56
at Microsoft.CodeAnalysis.Testing.CodeFixTest`1.VerifyFixAsync(String language, ImmutableArray`1 analyzers, ImmutableArray`1 codeFixProviders, SolutionState oldState, SolutionState newState, Int32 numberOfIterations, Func`10 getFixedProject, IVerifier verifier, CancellationToken cancellationToken) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.CodeFix.Testing/CodeFixTest`1.cs:line 415
at Microsoft.CodeAnalysis.Testing.CodeFixTest`1.VerifyFixAsync(SolutionState testState, SolutionState fixedState, SolutionState batchFixedState, IVerifier verifier, CancellationToken cancellationToken) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.CodeFix.Testing/CodeFixTest`1.cs:line 372
at Microsoft.CodeAnalysis.Testing.CodeFixTest`1.RunImplAsync(CancellationToken cancellationToken) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.CodeFix.Testing/CodeFixTest`1.cs:line 248
at Microsoft.CodeAnalysis.Testing.AnalyzerTest`1.RunAsync(CancellationToken cancellationToken) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/AnalyzerTest`1.cs:line 180
at AnalyzerTemplate.Test.CSharpCodeFixVerifier`2.VerifyCodeFixAsync(String source, DiagnosticResult[] expected, String fixedSource) in C:\JetBrains\C#\AnalyzerTemplate-master\AnalyzerTemplate\AnalyzerTemplate.Test\Verifiers\CSharpCodeFixVerifier`2.cs:line 58
at AnalyzerTemplate.Test.CSharpCodeFixVerifier`2.VerifyCodeFixAsync(String source, DiagnosticResult expected, String fixedSource) in C:\JetBrains\C#\AnalyzerTemplate-master\AnalyzerTemplate\AnalyzerTemplate.Test\Verifiers\CSharpCodeFixVerifier`2.cs:line 46
at AnalyzerTemplate.Test.AnalyzerTemplateUnitTest.TestMethod2() in C:\JetBrains\C#\AnalyzerTemplate-master\AnalyzerTemplate\AnalyzerTemplate.Test\AnalyzerTemplateUnitTests.cs:line 89
at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.ThreadOperations.ExecuteWithAbortSafety(Action action)
我不明白,为什么没有差异(通常用 +/- 标记它们)以及如何使我的测试正常工作
差异将是行尾(\r\n
vs \n
)。
最近更新了测试库以显示 CRLF 与 LF 的差异。
您需要使用更新的测试库包,其中包含 this PR 中的修复程序。
我正在使用 Microsoft 模板编写简单的代码分析器和修复程序。我想在 else 语句中修复嵌套的 if 语句。示例:
using System;
using System.Runtime.InteropServices;
namespace ExamApp
{
public class Program
{
static void Main()
{
if (Condition())
{
Action();
}
else
{
if (Condition())
{
Action();
}
}
}
static bool Condition()
{
return false;
}
static void Action() { }
}
}
应转换为
using System;
using System.Runtime.InteropServices;
namespace ExamApp
{
public class Program
{
static void Main()
{
if (Condition())
{
Action();
}
else if (Condition())
{
Action();
}
}
static bool Condition()
{
return false;
}
static void Action() { }
}
}
我 运行 Analyzer.Vsix 打开另一个 VS window,打开一些沙箱项目,从第一个示例编写代码。 VS 向我展示了警告在正确的位置,建议修复它并正确修复它。我决定编写这样的测试:
//Diagnostic and CodeFix both triggered and checked for
[TestMethod]
public async Task TestMethod2()
{
var test = @"
using System;
using System.Runtime.InteropServices;
namespace ExamApp
{
public class Program
{
static void Main()
{
if (Condition())
{
Action();
}
else
{
if (Condition())
{
Action();
}
}
}
static bool Condition()
{
return false;
}
static void Action() { }
}
}";
var fixtest = @"
using System;
using System.Runtime.InteropServices;
namespace ExamApp
{
public class Program
{
static void Main()
{
if (Condition())
{
Action();
}
else if (Condition())
{
Action();
}
}
static bool Condition()
{
return false;
}
static void Action() { }
}
}";
var expected = VerifyCS.Diagnostic("AnalyzerTemplate").WithSpan(17, 13, 17, 17).WithArguments("IfKeyword");
await VerifyCS.VerifyCodeFixAsync(test,expected, fixtest);
}
但是 smth 出错了,这是输出:
Assert.Fail failed. Context: Iterative code fix application
content of '/0/Test0.cs' did not match. Diff shown with expected as baseline:
using System;
using System.Runtime.InteropServices;
namespace ExamApp
{
public class Program
{
static void Main()
{
if (Condition())
{
Action();
}
else if (Condition())
{
Action();
}
}
static bool Condition()
{
return false;
}
static void Action() { }
}
}
at Microsoft.CodeAnalysis.Testing.Verifiers.MSTestVerifier.Fail(String message) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Testing.Verifiers.MSTest/MSTestVerifier.cs:line 78
at Microsoft.CodeAnalysis.Testing.IVerifierExtensions.EqualOrDiff(IVerifier verifier, String expected, String actual, String message) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/Extensions/IVerifierExtensions.cs:line 56
at Microsoft.CodeAnalysis.Testing.CodeFixTest`1.VerifyFixAsync(String language, ImmutableArray`1 analyzers, ImmutableArray`1 codeFixProviders, SolutionState oldState, SolutionState newState, Int32 numberOfIterations, Func`10 getFixedProject, IVerifier verifier, CancellationToken cancellationToken) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.CodeFix.Testing/CodeFixTest`1.cs:line 415
at Microsoft.CodeAnalysis.Testing.CodeFixTest`1.VerifyFixAsync(SolutionState testState, SolutionState fixedState, SolutionState batchFixedState, IVerifier verifier, CancellationToken cancellationToken) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.CodeFix.Testing/CodeFixTest`1.cs:line 372
at Microsoft.CodeAnalysis.Testing.CodeFixTest`1.RunImplAsync(CancellationToken cancellationToken) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.CodeFix.Testing/CodeFixTest`1.cs:line 248
at Microsoft.CodeAnalysis.Testing.AnalyzerTest`1.RunAsync(CancellationToken cancellationToken) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/AnalyzerTest`1.cs:line 180
at AnalyzerTemplate.Test.CSharpCodeFixVerifier`2.VerifyCodeFixAsync(String source, DiagnosticResult[] expected, String fixedSource) in C:\JetBrains\C#\AnalyzerTemplate-master\AnalyzerTemplate\AnalyzerTemplate.Test\Verifiers\CSharpCodeFixVerifier`2.cs:line 58
at AnalyzerTemplate.Test.CSharpCodeFixVerifier`2.VerifyCodeFixAsync(String source, DiagnosticResult expected, String fixedSource) in C:\JetBrains\C#\AnalyzerTemplate-master\AnalyzerTemplate\AnalyzerTemplate.Test\Verifiers\CSharpCodeFixVerifier`2.cs:line 46
at AnalyzerTemplate.Test.AnalyzerTemplateUnitTest.TestMethod2() in C:\JetBrains\C#\AnalyzerTemplate-master\AnalyzerTemplate\AnalyzerTemplate.Test\AnalyzerTemplateUnitTests.cs:line 89
at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.ThreadOperations.ExecuteWithAbortSafety(Action action)
我不明白,为什么没有差异(通常用 +/- 标记它们)以及如何使我的测试正常工作
差异将是行尾(\r\n
vs \n
)。
最近更新了测试库以显示 CRLF 与 LF 的差异。
您需要使用更新的测试库包,其中包含 this PR 中的修复程序。