Skip to content

Commit

Permalink
Replace deprecated ReportIssue calls in UTs (#9422)
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-strecker-sonarsource authored Jun 14, 2024
1 parent 2cc6d48 commit eeeab09
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,32 +332,31 @@ class C
}
""");
var diagnosticDescriptor = new DiagnosticDescriptor("TEST", "Title", "{0}", "Category", DiagnosticSeverity.Warning, true, customTags: [scope]);
var location = Location.Create(snippet.SyntaxTree, TextSpan.FromBounds(0, 0));
var analyzer = new TestAnalyzerCS(diagnosticDescriptor, analysisContext =>
analysisContext.RegisterCompilationStartAction(compilationStartContext =>
compilationStartContext.RegisterSymbolStartAction(symbolStartContext =>
{
symbolStartContext.RegisterCodeBlockAction(codeBlockContext =>
codeBlockContext.ReportIssue(CreateDiagnostic("CodeBlock")));
codeBlockContext.ReportIssue(diagnosticDescriptor, location, "CodeBlock"));
symbolStartContext.RegisterCodeBlockStartAction<SyntaxKind>(codeBlockStartContext =>
{
codeBlockStartContext.RegisterNodeAction(nodeContext =>
nodeContext.ReportIssue(CreateDiagnostic("CodeBlockStart_Node")), SyntaxKind.InvocationExpression);
nodeContext.ReportIssue(diagnosticDescriptor, location, "CodeBlockStart_Node"), SyntaxKind.InvocationExpression);
codeBlockStartContext.RegisterCodeBlockEndAction(codeBlockEndContext =>
codeBlockEndContext.ReportIssue(CreateDiagnostic("CodeBlockStart_End")));
codeBlockEndContext.ReportIssue(diagnosticDescriptor, location, "CodeBlockStart_End"));
});
symbolStartContext.RegisterSymbolEndAction(symbolEndContext =>
symbolEndContext.ReportIssue(CSharpGeneratedCodeRecognizer.Instance, CreateDiagnostic("SymbolEnd")));
symbolEndContext.ReportIssue(CSharpGeneratedCodeRecognizer.Instance, diagnosticDescriptor, location, "SymbolEnd"));
symbolStartContext.RegisterSyntaxNodeAction(nodeContext =>
nodeContext.ReportIssue(CreateDiagnostic("Node")), SyntaxKind.InvocationExpression);
nodeContext.ReportIssue(diagnosticDescriptor, location, "Node"), SyntaxKind.InvocationExpression);
},
SymbolKind.NamedType)));
var compilation = snippet.Compilation.WithAnalyzers(ImmutableArray.Create<DiagnosticAnalyzer>(analyzer));
var compilation = snippet.Compilation.WithAnalyzers([analyzer]);
var diagnostics = await compilation.GetAllDiagnosticsAsync();
diagnostics.Should().HaveCount(expectedDiagnostics.Length);
// Ordering is only partially guaranteed and therefore we use BeEquivalentTo https://github.com/dotnet/roslyn/blob/main/docs/analyzers/Analyzer%20Actions%20Semantics.md
diagnostics.Select(x => x.GetMessage()).Should().BeEquivalentTo(expectedDiagnostics);

Diagnostic CreateDiagnostic(string message) => Diagnostic.Create(diagnosticDescriptor, Location.Create(snippet.SyntaxTree, TextSpan.FromBounds(0, 0)), message);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void Properties_ArePropagated()
public void RegistrationIsExecuted_SonarAnalysisContext_CS() =>
new VerifierBuilder().AddAnalyzer(() => new TestAnalyzerCS((context, g) =>
context.RegisterSemanticModelAction(g, c =>
c.ReportIssue(Diagnostic.Create(TestAnalyzer.Rule, c.Tree.GetRoot().GetFirstToken().GetLocation())))))
c.ReportIssue(TestAnalyzer.Rule, c.Tree.GetRoot().GetFirstToken()))))
.AddSnippet("""
using System; // Noncompliant
""")
Expand All @@ -58,7 +58,7 @@ public void RegistrationIsExecuted_SonarAnalysisContext_CS() =>
public void RegistrationIsExecuted_SonarAnalysisContext_VB() =>
new VerifierBuilder().AddAnalyzer(() => new TestAnalyzerVB((context, g) =>
context.RegisterSemanticModelAction(g, c =>
c.ReportIssue(Diagnostic.Create(TestAnalyzer.Rule, c.Tree.GetRoot().GetFirstToken().GetLocation())))))
c.ReportIssue(TestAnalyzer.Rule, c.Tree.GetRoot().GetFirstToken()))))
.AddSnippet("""
Imports System ' Noncompliant
""")
Expand All @@ -72,7 +72,7 @@ public void RegistrationIsExecuted_SonarCompilationStartAnalysisContext_CS() =>
{
if (c.Tree.GetRoot().GetFirstToken() is { RawKind: not (int)CS.SyntaxKind.None } token)
{
c.ReportIssue(Diagnostic.Create(TestAnalyzer.Rule, token.GetLocation()));
c.ReportIssue(TestAnalyzer.Rule, token);
}
}))))
.AddSnippet("""
Expand All @@ -88,7 +88,7 @@ public void RegistrationIsExecuted_SonarCompilationStartAnalysisContext_VB() =>
{
if (c.Tree.GetRoot().GetFirstToken() is { RawKind: not (int)VB.SyntaxKind.None } token)
{
c.ReportIssue(Diagnostic.Create(TestAnalyzer.Rule, token.GetLocation()));
c.ReportIssue(TestAnalyzer.Rule, token);
}
}))))
.AddSnippet("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void ReportIssue_TreeNotInCompilation_DoNotReport(bool reportOnCorrectTre
var sut = new SonarSyntaxNodeReportingContext(analysisContext, context);
try
{
sut.ReportIssue(Diagnostic.Create(rule, (reportOnCorrectTree ? nodeFromCorrectCompilation : nodeFromAnotherCompilation).GetLocation()));
sut.ReportIssue(rule, reportOnCorrectTree ? nodeFromCorrectCompilation : nodeFromAnotherCompilation);
}
catch (Exception ex) // Can't catch internal DebugAssertException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterNodeAction(TestGeneratedCodeRecognizer.Instance, c =>
{
// Duplicate issues from different analyzer versions, see https://github.com/SonarSource/sonar-dotnet/issues/1109
c.ReportIssue(Diagnostic.Create(rule, c.Context.Node.GetLocation()));
c.ReportIssue(Diagnostic.Create(rule, c.Context.Node.GetLocation()));
c.ReportIssue(rule, c.Context.Node);
c.ReportIssue(rule, c.Context.Node);
}, SyntaxKind.NamespaceDeclaration);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ public abstract class DummyAnalyzer<TSyntaxKind> : SonarDiagnosticAnalyzer where
protected abstract TSyntaxKind NumericLiteralExpression { get; }

protected sealed override void Initialize(SonarAnalysisContext context) =>
context.RegisterNodeAction(TestGeneratedCodeRecognizer.Instance, c => c.ReportIssue(Diagnostic.Create(rule, c.Node.GetLocation())), NumericLiteralExpression);
context.RegisterNodeAction(TestGeneratedCodeRecognizer.Instance, c => c.ReportIssue(rule, c.Node), NumericLiteralExpression);
}

0 comments on commit eeeab09

Please sign in to comment.