Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compliation errors in Testing README sample custom verifier #1166

Merged
merged 3 commits into from
Jul 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 4 additions & 74 deletions src/Microsoft.CodeAnalysis.Testing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,77 +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<TAnalyzer>
where TAnalyzer : DiagnosticAnalyzer, new()
{
public static DiagnosticResult Diagnostic(string diagnosticId = null)
=> CSharpAnalyzerVerifier<TAnalyzer, DefaultVerifier>.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<TAnalyzer, EmptyCodeFixProvider>
{
}
}

public static class CSharpCodeFixVerifier<TAnalyzer, TCodeFix>
where TAnalyzer : DiagnosticAnalyzer, new()
where TCodeFix : CodeFixProvider, new()
{
public static DiagnosticResult Diagnostic(string diagnosticId = null)
=> CSharpCodeFixVerifier<TAnalyzer, TCodeFix, DefaultVerifier>.Diagnostic(diagnosticId);

public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor)
=> new DiagnosticResult(descriptor);

public static Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected)
{
var test = new CSharpAnalyzerVerifier<TAnalyzer>.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<TAnalyzer, TCodeFix, DefaultVerifier>
where TAnalyzer : DiagnosticAnalyzer, new()
where TCodeFix : CodeFixProvider, new()
{
public CSharpCodeFixTest()
{
// 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.