[Proposal] Performance/usability improvements for analyzer tests #63684
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I would like to propose some improvements for code analyzer tests.
I also proposed this in Roslyn SDK repo: #1226
Currently
CSharpAnalyzerVerifier<TAnalyzer>.VerifyAnalyzerAsync
method is used to run a code analyzer test:Proposal
I believe that a factory method that creates a
CSharpAnalyzerTest<TAnalyzer, TVerifier>
could improve both usability and performance of tests. While this type comes from a package and cannot be modified, but it's possible to add such a method with the new extension members feature in C# 14:Using such a helper method could have some benefits:
It improves performance of tests because diagnostics are passed as
params ReadOnlySpan<DiagnosticResult>
that avoids array allocation. This is not possible with the asyncVerifyAnalyzerAsync
method.This allows to use additional helper methods that can be used to customize the test, to provide additional sources or test configuration if needed. For example, if each test needs some common code, test source code can be split into multiple 'files' and additional source can be provided using
WithSource
extension:This avoids code duplication as otherwise common code has to be included in each test. This can also keep the test source code clean so that it contains only the code necessary for a particular test without additional 'noise'.
Using
[StringSyntax("C#-test")]
attribute will ensure that test C# code gets syntax highlighting in Visual Studio for tests creatingCSharpAnalyzerTest
instance explicitly so that it can be customized (please see [Proposal] Analyzer test readability improvements #63642).This will avoid the need to create 'local' helper methods in analyzer test classes that need to customize a test in some way.
Examples
I included some tests using this approach in #63657