Skip to content

Commit

Permalink
Update UTs to reflect the latest changes from dotnet 8.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
costin-zaharia-sonarsource committed May 22, 2024
1 parent 3d24a9b commit 053a756
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -893,14 +893,14 @@ public void GetLineNumbers_Razor_FieldReference() =>
@page "/razor"
@using TestCases
<p>Current count: @currentCount</p> <!-- +1 -->
<p>Current count: @currentCount</p> <!-- Not counted -->
@currentCount <!-- +1 -->
@currentCount <!-- Not counted -->
@code {
private int currentCount = 0;
}
""", RazorFile, 4, 6);
""", RazorFile);

[TestMethod]
public void GetLineNumbers_Razor_MethodReferenceAndCall() =>
Expand All @@ -927,13 +927,13 @@ private string ShowAmount()
[TestMethod]
public void GetLineNumbers_Razor_PropertyReference() =>
AssertLineNumbersOfExecutableLinesRazor("""
@IncrementAmount <!-- +1 -->
@IncrementAmount <!-- Not counted -->
@code {
[Parameter]
public int IncrementAmount { get; set; } = 1;
}
""", RazorFile, 1);
""", RazorFile);

[TestMethod]
public void GetLineNumbers_Razor_Html() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ public void VerifyMetrics_Razor() =>
var metrics = messages.Single(x => x.FilePath.EndsWith(RazorFileName));

metrics.ClassCount.Should().Be(1);
metrics.CodeLine.Should().BeEquivalentTo(new[] { 3, 5, 8, 10, 13, 15, 16, 17, 19, 22, 23, 24, 26, 28, 29, 32, 33, 34, 36, 37, 39, 40, 43 });
metrics.CodeLine.Should().BeEquivalentTo(new[] { 2, 1, 3, 5, 8, 10, 13, 15, 16, 17, 19, 22, 23, 24, 26, 28, 29, 32, 33, 34, 36, 37, 39, 40, 43 });
metrics.CognitiveComplexity.Should().Be(3);
metrics.Complexity.Should().Be(4);
metrics.ExecutableLines.Should().BeEquivalentTo(new[] { 3, 5, 13, 15, 17, 24, 28, 29, 32, 36, 39, 43 });
metrics.ExecutableLines.Should().BeEquivalentTo(new[] { 13, 15, 28, 29, 32, 36 });
metrics.FunctionCount.Should().Be(1);
metrics.NoSonarComment.Should().BeEmpty();
metrics.NonBlankComment.Should().BeEquivalentTo(new[] { 7, 8, 10, 15, 21, 22, 23, 28, 29, 32, 33, 36, 37, 38 });
metrics.StatementCount.Should().Be(13);
metrics.StatementCount.Should().Be(6);
});

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ public void Verify_Razor() =>

orderedSymbols[1].FilePath.Should().EndWith("RazorComponent.razor"); // RazorComponent.razor
// https://github.com/SonarSource/sonar-dotnet/issues/8417
// Net8 SDK: Declaration (1,0) - (1,17) Reference (1,6) - (1,23) <- Overlapping
// before dotnet 8.0.5 SDK: Declaration (1,0) - (1,17) Reference (1,6) - (1,23) <- Overlapping
// Declaration of TSomeVeryLongName is placed starting at index 0 (ignoring "@typeparam ")
// Reference "where TSomeVeryLongName" is placed starting at index 6 (ignoring "@typeparam TSomeVeryLongName ")
VerifyReferences(orderedSymbols[1].Reference, 1, 1, 1);
orderedSymbols[1].Reference.Single().Reference.Single().Should().BeEquivalentTo(new TextRange { StartLine = 1, EndLine = 1, StartOffset = 6, EndOffset = 23 });
orderedSymbols[1].Reference.Single().Reference.Single().Should().BeEquivalentTo(new TextRange { StartLine = 1, EndLine = 1, StartOffset = 35, EndOffset = 52 });
});

[DataTestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@page "/razor" // FN: line of code, comment (location mapping issue)
@namespace RazorNamespace // FN: line of code, comment (location mapping issue)
<p>Current count: @currentCount</p> <!-- code line, FN: comment -->
@page "/razor"
@namespace RazorNamespace
<p>Current count: @currentCount</p> <!-- FN: comment -->

@currentCount <!-- code line, FN: comment -->
@currentCount <!-- FN: comment -->

@code { // FN: line of code (location mapping issue)
private int currentCount = 0; // code line
Expand All @@ -14,14 +14,14 @@

@for (var i = 0; i < 5; i++) // code line
{ <!-- code line, FN: comment -->
<text>Value: @i</text> <!-- code line, FN: comment -->
<text>Value: @i</text> <!-- FN: comment -->
<text>test</text>
} <!-- code line, FN: comment -->

@{ // FN: code line (location mapping issue)
void RenderName(string name) // code line
{ // code line
<p>Name: <strong>@name</strong></p> <!-- code line, FN: comment -->
<p>Name: <strong>@name</strong></p> <!-- FN: comment -->
<p>Name: <strong>Hardcoded</strong></p>
} <!-- code line, FN: comment -->

Expand All @@ -37,10 +37,10 @@
{ // code line
// comment
@:Name: @i // code line, FN: comment
} <!-- code line, FN: comment -->
} <!-- FN: comment,code line -->

<Component>
@currentCount <!-- code line, FN: comment -->
@currentCount <!-- FN: comment -->
</Component>

<!-- FN: comment -->
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,19 @@ namespace SonarAnalyzer.TestFramework.Test.Common;
public class SourceGeneratorProviderTest
{
private static AnalyzerFileReference RazorSourceGenerator =>
SourceGeneratorProvider.SourceGenerators.Single(x => x.FullPath.EndsWith("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll"));

SourceGeneratorProvider.SourceGenerators.Single(x => x.FullPath.EndsWith("Microsoft.CodeAnalysis.Razor.Compiler.dll"));
[TestMethod]
public void SourceGenerators_ContainsRazorSourceGenerator() =>
SourceGeneratorProvider.SourceGenerators.Should()
.Contain(x => x.FullPath.EndsWith(Path.Combine("Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll")));
.Contain(x => x.FullPath.EndsWith(Path.Combine("Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.dll")));

[TestMethod]
public void RazorSourceGenerator_ExistsLocally() =>
File.Exists(RazorSourceGenerator.FullPath).Should().BeTrue();

[TestMethod]
public void RazorSourceGenerator_LoadsCorrectAssembly() =>
RazorSourceGenerator.GetAssembly().GetName().Name.Should().Be("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators");
RazorSourceGenerator.GetAssembly().GetName().Name.Should().Be("Microsoft.CodeAnalysis.Razor.Compiler");

[TestMethod]
public void LatestSdkFolder_ReturnsAssemblyMajor()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<p>The solution to all problems is: @(RaiseHere(21))</p> @* wrong location *@
@* ^^^^^^^^^ *@
@* ^^ Secondary@-1 *@
@* ^^^^^^^^^ *@
@* ^^ Secondary@-1 *@

@code
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<p>Explicit expression: @(RaiseHere())</p> @* Noncompliant, wrong location*@
@* ^^^^^^^^^ *@
@* ^^^^^^^^^ *@

<p>Implicit expression: @RaiseHere()</p> @* Noncompliant, wrong location *@
@* ^^^^^^^^^ *@
@* ^^^^^^^^^ *@

<p>Multi-statement block: @{ var result = RaiseHere(); }</p>
@* ^^^^^^^^^ *@
Expand All @@ -17,15 +17,15 @@
@* ^^^^^^^^^ *@

<p>Lambda expression: @((Func<int>)(() => RaiseHere()))()</p> @* Noncompliant, wrong location *@
@* ^^^^^^^^^ *@
@* ^^^^^^^^^ *@

<p>Nested multi-statement block: @{ var result2 = RaiseHere(); var result3 = RaiseHere(); }</p>
@* ^^^^^^^^^ *@
@* ^^^^^^^^^@-1 *@

<p>Nested control structures: @if(RaiseHere() == 42) { <text>@(RaiseHere() + 1)</text> }</p> @* second issue has wrong location *@
@* ^^^^^^^^^ *@
@* ^^^^^^^^^@-1 *@
@* ^^^^^^^^^@-1 *@

@code
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace SonarAnalyzer.TestFramework.Common;
public static class SourceGeneratorProvider
{
private static readonly string RazorSourceGeneratorPath =
Path.Combine(LatestSdkFolder(), "Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll");
Path.Combine(LatestSdkFolder(), "Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.dll");

public static AnalyzerFileReference[] SourceGenerators { get; } =
[
Expand Down

0 comments on commit 053a756

Please sign in to comment.