[Proposal] Analyzer test readability/performance improvements #50342
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 suggest a couple of changes that can improve performance and readability of code analyzer tests (also submitted proposal to Roslyn SDK repo in #1226):
Use raw string literals for C# test source code in all unit tests for code analyzers.
Provide a helper method that creates
Test
instances. In most cases analyzer tests callVerifyCS.VerifyAnalyzerAsync
passing test C# code. But it's often needed to specify additional test settings, like the language version in the case when a unit test needs to use language features introduced in a specific C# version. Currently it's done with a code like this:Currently C# code specified for
TestCode
does not get syntax highlighting.What about providing a helper method like this:
Currently the code in a test might look like this:
With raw string literals and a helper method, this is what it will look like:
Using such a helper could have some benefits:
This will improve performance of tests because diagnostics are passed as
params ReadOnlySpan<DiagnosticResult>
instead ofparams DiagnosticResult[]
. Span-based parameter cannot be used in the async methodVerifyCS.VerifyAnalyzerAsync
.Test code gets syntax highlighting.
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'.
VS draws a nice vertical line for raw string literals, which also means that there's no extra whitespace before the code so that it does not have to start from the very left overflowing the test.
Some test classes have their own helper methods that create a
Test
that may lack[StringSyntax]
attribute for C# source code parameter (e.g. in Avoid reporting CA1859 for properties with setter more accessible than getter #50488). With a single helper that could be used instead, the attribute can be applied only in one place.Examples
I included some tests using this approach in #50339