From 5947d6e3a53ae9bd8a024f8a553ba7eed61f5fff Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Fri, 21 Jun 2024 07:30:06 -0700 Subject: [PATCH 1/3] Fix compliation errors in Testing README sample custom verifier --- src/Microsoft.CodeAnalysis.Testing/README.md | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.CodeAnalysis.Testing/README.md b/src/Microsoft.CodeAnalysis.Testing/README.md index 5fe7a3a4b..0d440aba1 100644 --- a/src/Microsoft.CodeAnalysis.Testing/README.md +++ b/src/Microsoft.CodeAnalysis.Testing/README.md @@ -202,7 +202,7 @@ use of "basic use cases" for test scenarios that would otherwise be considered a public static class CSharpAnalyzerVerifier where TAnalyzer : DiagnosticAnalyzer, new() { - public static DiagnosticResult Diagnostic(string diagnosticId = null) + public static DiagnosticResult Diagnostic(string diagnosticId) => CSharpAnalyzerVerifier.Diagnostic(diagnosticId); public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) @@ -217,7 +217,7 @@ public static class CSharpAnalyzerVerifier // Code fix tests support both analyzer and code fix testing. This test class is derived from the code fix test // to avoid the need to maintain duplicate copies of the customization work. - public class Test : CSharpCodeFixVerifier.Test + public class Test : CSharpCodeFixVerifier.Test { } } @@ -226,7 +226,7 @@ public static class CSharpCodeFixVerifier where TAnalyzer : DiagnosticAnalyzer, new() where TCodeFix : CodeFixProvider, new() { - public static DiagnosticResult Diagnostic(string diagnosticId = null) + public static DiagnosticResult Diagnostic(string diagnosticId) => CSharpCodeFixVerifier.Diagnostic(diagnosticId); public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) @@ -258,10 +258,8 @@ public static class CSharpCodeFixVerifier } public class Test : CSharpCodeFixTest - where TAnalyzer : DiagnosticAnalyzer, new() - where TCodeFix : CodeFixProvider, new() { - public CSharpCodeFixTest() + public Test() { // Custom initialization logic here } @@ -269,6 +267,4 @@ public static class CSharpCodeFixVerifier // Custom analyzers and/or code fix properties here } } - ``` - From bc422780c084199af41d5cbf6882aa6768fcceb6 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Fri, 21 Jun 2024 14:41:06 -0700 Subject: [PATCH 2/3] Add default overload for Diagnostic --- src/Microsoft.CodeAnalysis.Testing/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Microsoft.CodeAnalysis.Testing/README.md b/src/Microsoft.CodeAnalysis.Testing/README.md index 0d440aba1..8f583eaa7 100644 --- a/src/Microsoft.CodeAnalysis.Testing/README.md +++ b/src/Microsoft.CodeAnalysis.Testing/README.md @@ -202,6 +202,9 @@ use of "basic use cases" for test scenarios that would otherwise be considered a public static class CSharpAnalyzerVerifier where TAnalyzer : DiagnosticAnalyzer, new() { + public static DiagnosticResult Diagnostic() + => CSharpAnalyzerVerifier.Diagnostic(); + public static DiagnosticResult Diagnostic(string diagnosticId) => CSharpAnalyzerVerifier.Diagnostic(diagnosticId); From fa0788ac2abe927f7615490bda20a8768ccac87e Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Fri, 21 Jun 2024 15:02:16 -0700 Subject: [PATCH 3/3] Replace code snippet with link to analyzer template and instructions --- src/Microsoft.CodeAnalysis.Testing/README.md | 77 +------------------- 1 file changed, 4 insertions(+), 73 deletions(-) diff --git a/src/Microsoft.CodeAnalysis.Testing/README.md b/src/Microsoft.CodeAnalysis.Testing/README.md index 8f583eaa7..4aaf538f7 100644 --- a/src/Microsoft.CodeAnalysis.Testing/README.md +++ b/src/Microsoft.CodeAnalysis.Testing/README.md @@ -198,76 +198,7 @@ verifier and test types. For example, [Microsoft/vs-threading](https://github.co Additional Files and metadata references in most of its tests, so it uses custom verifier and test types to allow the use of "basic use cases" for test scenarios that would otherwise be considered advanced. -```csharp -public static class CSharpAnalyzerVerifier - where TAnalyzer : DiagnosticAnalyzer, new() -{ - public static DiagnosticResult Diagnostic() - => CSharpAnalyzerVerifier.Diagnostic(); - - public static DiagnosticResult Diagnostic(string diagnosticId) - => CSharpAnalyzerVerifier.Diagnostic(diagnosticId); - - public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) - => new DiagnosticResult(descriptor); - - public static Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected) - { - var test = new Test { TestCode = source }; - test.ExpectedDiagnostics.AddRange(expected); - return test.RunAsync(); - } - - // Code fix tests support both analyzer and code fix testing. This test class is derived from the code fix test - // to avoid the need to maintain duplicate copies of the customization work. - public class Test : CSharpCodeFixVerifier.Test - { - } -} - -public static class CSharpCodeFixVerifier - where TAnalyzer : DiagnosticAnalyzer, new() - where TCodeFix : CodeFixProvider, new() -{ - public static DiagnosticResult Diagnostic(string diagnosticId) - => CSharpCodeFixVerifier.Diagnostic(diagnosticId); - - public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) - => new DiagnosticResult(descriptor); - - public static Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected) - { - var test = new CSharpAnalyzerVerifier.Test { TestCode = source }; - test.ExpectedDiagnostics.AddRange(expected); - return test.RunAsync(); - } - - public static Task VerifyCodeFixAsync(string source, string fixedSource) - => VerifyCodeFixAsync(source, DiagnosticResult.EmptyDiagnosticResults, fixedSource); - - public static Task VerifyCodeFixAsync(string source, DiagnosticResult expected, string fixedSource) - => VerifyCodeFixAsync(source, new[] { expected }, fixedSource); - - public static Task VerifyCodeFixAsync(string source, DiagnosticResult[] expected, string fixedSource) - { - var test = new Test - { - TestCode = source, - FixedCode = fixedSource, - }; - - test.ExpectedDiagnostics.AddRange(expected); - return test.RunAsync(); - } - - public class Test : CSharpCodeFixTest - { - public Test() - { - // Custom initialization logic here - } - - // Custom analyzers and/or code fix properties here - } -} -``` +To create a custom verifier, add your test setup or configuration to the corresponding `Test` class provided by the +[analyzer template](../VisualStudio.Roslyn.SDK/Roslyn.SDK/ProjectTemplates/CSharp/Diagnostic/Test/Verifiers). See +https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/tutorials/how-to-write-csharp-analyzer-code-fix for +instructions on using the analyzer template.