Skip to content

Commit

Permalink
Post-review update: Add unit test for secondary locations
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource committed May 23, 2024
1 parent e80ab41 commit 4bf10b9
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Threading.Channels;
using SonarAnalyzer.AnalysisContext;

namespace SonarAnalyzer.Test.AnalysisContext;
Expand Down Expand Up @@ -50,4 +51,25 @@ public void Properties_ArePropagated()
sut.Options.Should().BeSameAs(options);
sut.Cancel.Should().Be(cancel);
}

[TestMethod]
public void ReportIssue_IgnoreSecondaryLocationOutsideCompilation()
{
Diagnostic lastDiagnostic = null;
var (tree, model) = TestHelper.CompileCS("using System;");
var secondaryTree = TestHelper.CompileCS("namespace Nothing;").Tree; // This tree is not in the analyzed compilation
var context = new SyntaxTreeAnalysisContext(tree, AnalysisScaffolding.CreateOptions(), x => lastDiagnostic = x, _ => true, default);
var sut = new SonarSyntaxTreeReportingContext(AnalysisScaffolding.CreateSonarAnalysisContext(), context, model.Compilation);
var rule = AnalysisScaffolding.CreateDescriptor("Sxxxx", DiagnosticDescriptorFactory.MainSourceScopeTag);

sut.ReportIssue(rule, tree.GetRoot(), [new SecondaryLocation(tree.GetRoot().GetLocation(), null)]);
lastDiagnostic.Should().NotBeNull();
lastDiagnostic.Id.Should().Be("Sxxxx");
lastDiagnostic.AdditionalLocations.Should().ContainSingle("This secondary location is in compilation");

sut.ReportIssue(rule, tree.GetRoot(), [new SecondaryLocation(secondaryTree.GetRoot().GetLocation(), null)]);
lastDiagnostic.Should().NotBeNull();
lastDiagnostic.Id.Should().Be("Sxxxx");
lastDiagnostic.AdditionalLocations.Should().BeEmpty("This secondary location is outside the compilation");
}
}

0 comments on commit 4bf10b9

Please sign in to comment.