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

Improve support for DiagnosticSuppressor #942

Closed
Flash0ver opened this issue Dec 27, 2021 · 2 comments
Closed

Improve support for DiagnosticSuppressor #942

Flash0ver opened this issue Dec 27, 2021 · 2 comments
Labels

Comments

@Flash0ver
Copy link

Flash0ver commented Dec 27, 2021

I just tried writing unit tests for a DiagnosticSuppressor via Microsoft.CodeAnalysis.Testing, but I have not succeeded.
Although DiagnosticSuppressor does derive from DiagnosticAnalyzer, and with that already inheriting support in the available infrastructure of Microsoft.CodeAnalysis.Testing, I was still unable to get it working.

My approach was to always add a custom DiagnosticAnalyzer which reports all diagnostics defined in the Assert-Phase of the test through a custom collection of DiagnosticResults.
After this Testing-Internal DiagnosticAnalyzer has reported all arranged diagnostics, the DiagnosticSuppressor under test could then suppress these, so that there are no ExpectedDiagnostics left.
But I couldn't really get the Markup-Syntax working, nor manually setting the Location/Span of the arranged diagnostic(s) to be suppressed during the Act-Phase, so that the Assert-Phase can expect zero ExpectedDiagnostics, or at least a set of ExpectedDiagnostics without the previously reported and suppressed DiagnosticResults. Maybe I got something wrong with the Location/TextSpan/Path: the DiagnosticSuppressor did receive the ReportedDiagnostics, but even after invoking ReportSuppression the test still failed with the message "Mismatch between number of diagnostics returned, expected "0" actual "1"".

Perhaps adding such a DiagnosticResult-Collection to the AnalyzerTest`1 or ProjectState (or another suitable type), which is iterated and diagnostics produced from, which are then subject to be passed to the DiagnosticSuppressor under test if supported, would be of great benefit for all authors of analyzers utilizing the might of Microsoft.CodeAnalysis.Testing.
I guess there is also a different perhaps more intuitive approach to be discussed.

@sharwell
Copy link
Member

I was expecting this to be fixed by #917. Have you had a chance to try this functionality out?

@Flash0ver
Copy link
Author

Thank you @sharwell for the hint. I tried it out and it worked perfectly.
Thank you @tom-englert for the implementation and the great example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants