Skip to content

Commit

Permalink
Merge pull request #423 from tonyhallett/fix-blazor-editor-marks
Browse files Browse the repository at this point in the history
Fix blazor editor marks
  • Loading branch information
tonyhallett authored May 10, 2024
2 parents 2071f51 + 6d9a114 commit 7d401cf
Show file tree
Hide file tree
Showing 91 changed files with 3,018 additions and 1,268 deletions.
3 changes: 3 additions & 0 deletions FineCodeCoverage/FineCodeCoverage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@
<PackageReference Include="Newtonsoft.Json">
<Version>13.0.1</Version>
</PackageReference>
<PackageReference Include="NUnit">
<Version>3.13.1</Version>
</PackageReference>
<PackageReference Include="ReflectObject">
<Version>1.0.0</Version>
</PackageReference>
Expand Down
3 changes: 2 additions & 1 deletion FineCodeCoverageTests/AppOptionsProvider_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ internal void Should_Use_Deseralized_String_From_Store_For_AppOption_Property(Fu
{nameof(IAppOptions.ShowLineNewHighlighting),true },
{nameof(IAppOptions.ShowLineNotIncludedHighlighting),true },
{nameof(IAppOptions.UseEnterpriseFontsAndColors),true },
{nameof(IAppOptions.EditorCoverageColouringMode), EditorCoverageColouringMode.UseRoslynWhenTextChanges }
{nameof(IAppOptions.EditorCoverageColouringMode), EditorCoverageColouringMode.UseRoslynWhenTextChanges },
{nameof(IAppOptions.BlazorCoverageLinesFromGeneratedSource), true }
};
var mockJsonConvertService = autoMocker.GetMock<IJsonConvertService>();
mockJsonConvertService.Setup(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using FineCodeCoverage.Editor.DynamicCoverage.ContentTypes.Blazor;
using FineCodeCoverage.Options;
using Moq;
using NUnit.Framework;

namespace FineCodeCoverageTests.Editor.DynamicCoverage
{
internal class BlazorCoverageContentType_Tests
{
[TestCase("path.razor",false)]
[TestCase("path.cshtml", true)]
[TestCase("path.vbhtml", true)]
public void Should_Include_Razor_Component_Files(string filePath, bool expectedExclude)
{
Assert.That(new BlazorCoverageContentType(null, null).Exclude(filePath), Is.EqualTo(expectedExclude));
}

[Test]
public void Should_Line_Exclude_HtmlTags()
{
var lineExcluder = new BlazorCoverageContentType(null, null).LineExcluder;
Assert.True(lineExcluder.ExcludeIfNotCode("<"));
}

[Test]
public void Should_Line_Exclude_Directives()
{
var lineExcluder = new BlazorCoverageContentType(null, null).LineExcluder;
Assert.True(lineExcluder.ExcludeIfNotCode("@"));
}

[Test]
public void Should_Line_Exclude_Comments()
{
var lineExcluder = new BlazorCoverageContentType(null, null).LineExcluder;
Assert.True(lineExcluder.ExcludeIfNotCode("//"));
}

[Test]
public void Should_Line_Exclude_Compiler_Directives()
{
var lineExcluder = new BlazorCoverageContentType(null, null).LineExcluder;
Assert.True(lineExcluder.ExcludeIfNotCode("#"));
}

[Test]
public void Should_Not_UseFileCodeSpanRangeServiceForChanges()
{
Assert.False(new BlazorCoverageContentType(null, null).UseFileCodeSpanRangeServiceForChanges);
}

[TestCase(true)]
[TestCase(false)]
public void Should_CoverageFromFileCodeSpanRangeService_From_AppOptions(bool blazorCoverageLinesFromGeneratedSource)
{
var mockAppOptionsProvider = new Mock<IAppOptionsProvider>();
mockAppOptionsProvider.Setup(a => a.Get()).Returns(new AppOptions { BlazorCoverageLinesFromGeneratedSource = blazorCoverageLinesFromGeneratedSource });
Assert.That(new BlazorCoverageContentType(null, mockAppOptionsProvider.Object).CoverageOnlyFromFileCodeSpanRangeService, Is.EqualTo(blazorCoverageLinesFromGeneratedSource));
}

[Test]
public void Should_Use_BlazorFileCodeSpanRangeService()
{
var blazorFileCodeSpanRangeService = new Mock<IBlazorFileCodeSpanRangeService>().Object;
Assert.That(blazorFileCodeSpanRangeService, Is.SameAs(new BlazorCoverageContentType(blazorFileCodeSpanRangeService, null).FileCodeSpanRangeService));
}

[Test]
public void Should_Be_For_The_Razor_ContentType()
{
Assert.That("Razor", Is.EqualTo(new BlazorCoverageContentType(null, null).ContentTypeName));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System.Collections.Generic;
using System.Linq;
using AutoMoq;
using FineCodeCoverage.Core.Utilities.VsThreading;
using FineCodeCoverage.Editor.DynamicCoverage;
using FineCodeCoverage.Editor.DynamicCoverage.ContentTypes.Blazor;
using FineCodeCoverage.Editor.DynamicCoverage.Utilities;
using FineCodeCoverage.Editor.Roslyn;
using FineCodeCoverageTests.TestHelpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text;
using Moq;
using NUnit.Framework;

namespace FineCodeCoverageTests.Editor.DynamicCoverage
{
internal class BlazorFileCodeSpanRangeService_Tests
{
[Test]
public void Should_Return_Null_If_Cannot_Find_Syntax_Root_Of_Generated_Document()
{
var mockTextSnapshot = new Mock<ITextSnapshot>();
var mockTextBuffer = new Mock<ITextBuffer>();
mockTextSnapshot.SetupGet(textSnapshot => textSnapshot.TextBuffer).Returns(mockTextBuffer.Object);

var autoMoqer = new AutoMoqer();
var razorGeneratedFilePathMatcher = autoMoqer.GetMock<IBlazorGeneratedFilePathMatcher>().Object;
autoMoqer.SetInstance<IThreadHelper>(new TestThreadHelper());
autoMoqer.GetMock<ITextInfoFactory>().Setup(t => t.GetFilePath(mockTextBuffer.Object)).Returns("path");

var mockRazorGeneratedDocumentRootFinder = autoMoqer.GetMock<IBlazorGeneratedDocumentRootFinder>();
mockRazorGeneratedDocumentRootFinder.Setup(
razorGeneratedDocumentootFinder => razorGeneratedDocumentootFinder.FindSyntaxRootAsync(mockTextBuffer.Object, "path", razorGeneratedFilePathMatcher)
).ReturnsAsync((SyntaxNode)null);

var fileCodeSpanRanges = autoMoqer.Create<BlazorFileCodeSpanRangeService>().GetFileCodeSpanRanges(mockTextSnapshot.Object);

Assert.IsNull(fileCodeSpanRanges);
mockRazorGeneratedDocumentRootFinder.VerifyAll();
}

[Test]
public void Should_Return_Null_If_Generated_Document_Has_No_Code_Nodes()
{
var mockTextSnapshot = new Mock<ITextSnapshot>();
var mockTextBuffer = new Mock<ITextBuffer>();
mockTextSnapshot.SetupGet(textSnapshot => textSnapshot.TextBuffer).Returns(mockTextBuffer.Object);

var autoMoqer = new AutoMoqer();
var mockCSharpCodeCoverageNodeVisitor = autoMoqer.GetMock<ICSharpCodeCoverageNodeVisitor>();
mockCSharpCodeCoverageNodeVisitor.Setup(cSharpCodeCoverageNodeVisitor => cSharpCodeCoverageNodeVisitor.GetNodes(It.IsAny<SyntaxNode>()))
.Returns(new List<SyntaxNode>());
var razorGeneratedFilePathMatcher = autoMoqer.GetMock<IBlazorGeneratedFilePathMatcher>().Object;
autoMoqer.SetInstance<IThreadHelper>(new TestThreadHelper());
autoMoqer.GetMock<ITextInfoFactory>().Setup(t => t.GetFilePath(mockTextBuffer.Object)).Returns("path");

var rootNode = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration);
var mockRazorGeneratedDocumentRootFinder = autoMoqer.GetMock<IBlazorGeneratedDocumentRootFinder>();
mockRazorGeneratedDocumentRootFinder.Setup(
razorGeneratedDocumentootFinder => razorGeneratedDocumentootFinder.FindSyntaxRootAsync(mockTextBuffer.Object, "path", razorGeneratedFilePathMatcher)
).ReturnsAsync(rootNode);

var fileCodeSpanRanges = autoMoqer.Create<BlazorFileCodeSpanRangeService>().GetFileCodeSpanRanges(mockTextSnapshot.Object);

Assert.IsNull(fileCodeSpanRanges);

mockCSharpCodeCoverageNodeVisitor.VerifyAll();
}

[TestCase(true)]
[TestCase(false)]
public void Should_Use_The_Generated_Coverage_Syntax_Nodes_Mapped_To_Razor_File_For_The_CodeSpanRange(
bool firstMapsBack
)
{
var mockTextSnapshot = new Mock<ITextSnapshot>();
var mockTextBuffer = new Mock<ITextBuffer>();
mockTextSnapshot.SetupGet(textSnapshot => textSnapshot.TextBuffer).Returns(mockTextBuffer.Object);

var autoMoqer = new AutoMoqer();
var razorGeneratedFilePathMatcher = autoMoqer.GetMock<IBlazorGeneratedFilePathMatcher>().Object;
autoMoqer.SetInstance<IThreadHelper>(new TestThreadHelper());
autoMoqer.GetMock<ITextInfoFactory>().Setup(t => t.GetFilePath(mockTextBuffer.Object)).Returns("path");

var mockRazorGeneratedDocumentRootFinder = autoMoqer.GetMock<IBlazorGeneratedDocumentRootFinder>();
SyntaxNode rootSyntaxNode = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration);
SyntaxNode codeCoverageNode1 = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration);
SyntaxNode codeCoverageNode2 = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration);
mockRazorGeneratedDocumentRootFinder.Setup(
razorGeneratedDocumentootFinder => razorGeneratedDocumentootFinder.FindSyntaxRootAsync(mockTextBuffer.Object, "path", razorGeneratedFilePathMatcher)
).ReturnsAsync(rootSyntaxNode);

var mockCSharpCodeCoverageNodeVisitor = autoMoqer.GetMock<ICSharpCodeCoverageNodeVisitor>();
mockCSharpCodeCoverageNodeVisitor.Setup(cSharpCodeCoverageNodeVisitor => cSharpCodeCoverageNodeVisitor.GetNodes(rootSyntaxNode))
.Returns(new List<SyntaxNode> { codeCoverageNode1, codeCoverageNode2 });
var mockSyntaxNodeLocationMapper = autoMoqer.GetMock<ISyntaxNodeLocationMapper>();

var linePositionSpan1 = new LinePositionSpan(new LinePosition(1, 1), new LinePosition(2, 1));
var fileLinePositionSpan1 = new FileLinePositionSpan(firstMapsBack ? "path" : "",linePositionSpan1);
var linePositionSpan2 = new LinePositionSpan(new LinePosition(3, 1), new LinePosition(4, 1));
var fileLinePositionSpan2 = new FileLinePositionSpan(firstMapsBack ? "" : "path", linePositionSpan2);
var expectedCodeSpanRange = firstMapsBack ? new CodeSpanRange(1, 2) : new CodeSpanRange(3, 4);

mockSyntaxNodeLocationMapper.Setup(syntaxNodeLocationMapper => syntaxNodeLocationMapper.Map(codeCoverageNode1))
.Returns(fileLinePositionSpan1);
mockSyntaxNodeLocationMapper.Setup(syntaxNodeLocationMapper => syntaxNodeLocationMapper.Map(codeCoverageNode2))
.Returns(fileLinePositionSpan2);


var fileCodeSpanRanges = autoMoqer.Create<BlazorFileCodeSpanRangeService>().GetFileCodeSpanRanges(mockTextSnapshot.Object);
var fileCodeSpanRange = fileCodeSpanRanges.Single();

Assert.That(expectedCodeSpanRange, Is.EqualTo(fileCodeSpanRange));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using FineCodeCoverage.Editor.DynamicCoverage.ContentTypes.Blazor;
using NUnit.Framework;

namespace FineCodeCoverageTests.Editor.DynamicCoverage
{
internal class BlazorGeneratedFilePathMatcher_Tests
{
[TestCase("razorpath","razorpath.",true)]
[TestCase("razorpath", "razorpathx.", false)]
public void Should_Be_Generated_If_File_Path_Starts_With_Razor_Path_And_Dot(
string razorFilePath,
string generatedFilePath,
bool expectedIsGenerated
)
{
var isGenerated = new BlazorGeneratedFilePathMatcher().IsBlazorGeneratedFilePath(razorFilePath, generatedFilePath);

Assert.That(expectedIsGenerated, Is.EqualTo(isGenerated));
}
}
}
Loading

0 comments on commit 7d401cf

Please sign in to comment.