Skip to content

Commit b71b4c4

Browse files
committed
backup
1 parent 2ed7086 commit b71b4c4

File tree

9 files changed

+242
-66
lines changed

9 files changed

+242
-66
lines changed

FineCodeCoverageTests/Editor/DynamicCoverage/BlazorFileCodeSpanRangeService_Tests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,34 @@ public void Should_Return_Null_If_Cannot_Find_Syntax_Root_Of_Generated_Document(
4141
mockRazorGeneratedDocumentRootFinder.VerifyAll();
4242
}
4343

44+
[Test]
45+
public void Should_Return_Null_If_Generated_Document_Has_No_Code_Nodes()
46+
{
47+
var mockTextSnapshot = new Mock<ITextSnapshot>();
48+
var mockTextBuffer = new Mock<ITextBuffer>();
49+
mockTextSnapshot.SetupGet(textSnapshot => textSnapshot.TextBuffer).Returns(mockTextBuffer.Object);
50+
51+
var autoMoqer = new AutoMoqer();
52+
var mockCSharpCodeCoverageNodeVisitor = autoMoqer.GetMock<ICSharpCodeCoverageNodeVisitor>();
53+
mockCSharpCodeCoverageNodeVisitor.Setup(cSharpCodeCoverageNodeVisitor => cSharpCodeCoverageNodeVisitor.GetNodes(It.IsAny<SyntaxNode>()))
54+
.Returns(new List<SyntaxNode>());
55+
var razorGeneratedFilePathMatcher = autoMoqer.GetMock<IBlazorGeneratedFilePathMatcher>().Object;
56+
autoMoqer.SetInstance<IThreadHelper>(new TestThreadHelper());
57+
autoMoqer.GetMock<ITextInfoFactory>().Setup(t => t.GetFilePath(mockTextBuffer.Object)).Returns("path");
58+
59+
var rootNode = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration);
60+
var mockRazorGeneratedDocumentRootFinder = autoMoqer.GetMock<IBlazorGeneratedDocumentRootFinder>();
61+
mockRazorGeneratedDocumentRootFinder.Setup(
62+
razorGeneratedDocumentootFinder => razorGeneratedDocumentootFinder.FindSyntaxRootAsync(mockTextBuffer.Object, "path", razorGeneratedFilePathMatcher)
63+
).ReturnsAsync(rootNode);
64+
65+
var fileCodeSpanRanges = autoMoqer.Create<BlazorFileCodeSpanRangeService>().GetFileCodeSpanRanges(mockTextSnapshot.Object);
66+
67+
Assert.IsNull(fileCodeSpanRanges);
68+
69+
mockCSharpCodeCoverageNodeVisitor.VerifyAll();
70+
}
71+
4472
[TestCase(true)]
4573
[TestCase(false)]
4674
public void Should_Use_The_Generated_Coverage_Syntax_Nodes_Mapped_To_Razor_File_For_The_CodeSpanRange(

FineCodeCoverageTests/Editor/DynamicCoverage/ContainingCodeTrackedLinesBuilder_Tests.cs

Lines changed: 79 additions & 43 deletions
Large diffs are not rendered by default.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Collections.Generic;
2+
using AutoMoq;
3+
using FineCodeCoverage.Core.Utilities.VsThreading;
4+
using FineCodeCoverage.Editor.DynamicCoverage;
5+
using FineCodeCoverage.Editor.DynamicCoverage.ContentTypes.Roslyn;
6+
using FineCodeCoverage.Editor.Roslyn;
7+
using FineCodeCoverage.Options;
8+
using FineCodeCoverageTests.TestHelpers;
9+
using Microsoft.CodeAnalysis.Text;
10+
using Microsoft.VisualStudio.Text;
11+
using Moq;
12+
using NUnit.Framework;
13+
14+
namespace FineCodeCoverageTests.Editor.DynamicCoverage
15+
{
16+
internal class RoslynFileCodeSpanRangeService_Tests
17+
{
18+
[TestCase(EditorCoverageColouringMode.UseRoslynWhenTextChanges,true)]
19+
[TestCase(EditorCoverageColouringMode.DoNotUseRoslynWhenTextChanges, false)]
20+
public void Should_UseFileCodeSpanRangeServiceForChanges_When_Options_Not_DoNotUseRoslynWhenTextChanges(
21+
EditorCoverageColouringMode editorCoverageColouringMode,
22+
bool expectedUseFileCodeSpanRangeServiceForChanges
23+
)
24+
{
25+
var autoMoqer = new AutoMoqer();
26+
autoMoqer.SetInstance(new TestThreadHelper());
27+
var mockAppOptions = new Mock<IAppOptions>();
28+
mockAppOptions.SetupGet(appOptions => appOptions.EditorCoverageColouringMode).Returns(editorCoverageColouringMode);
29+
var mockAppOptionsProvider = autoMoqer.GetMock<IAppOptionsProvider>();
30+
mockAppOptionsProvider.Setup(appOptionsProvider => appOptionsProvider.Get()).Returns(mockAppOptions.Object);
31+
32+
var roslynFileCodeSpanRangeService = autoMoqer.Create<RoslynFileCodeSpanRangeService>();
33+
34+
Assert.That(roslynFileCodeSpanRangeService.UseFileCodeSpanRangeServiceForChanges, Is.EqualTo(expectedUseFileCodeSpanRangeServiceForChanges));
35+
}
36+
37+
[Test]
38+
public void Should_GetFileCodeSpanRanges_By_Converting_The_Coverage_TextSpans_Using_The_TextSnapshot()
39+
{
40+
var autoMoqer = new AutoMoqer();
41+
autoMoqer.SetInstance<IThreadHelper>(new TestThreadHelper());
42+
43+
var mockTextSnapshot = new Mock<ITextSnapshot>();
44+
mockTextSnapshot.Setup(textSnapshot => textSnapshot.GetLineNumberFromPosition(0)).Returns(1);
45+
mockTextSnapshot.Setup(textSnapshot => textSnapshot.GetLineNumberFromPosition(10)).Returns(2);
46+
var mockRoslynService = autoMoqer.GetMock<IRoslynService>();
47+
mockRoslynService.Setup(roslynService => roslynService.GetContainingCodeSpansAsync(mockTextSnapshot.Object))
48+
.ReturnsAsync(new List<TextSpan> { new TextSpan(0, 10) });
49+
50+
var roslynFileCodeSpanRangeService = autoMoqer.Create<RoslynFileCodeSpanRangeService>();
51+
var fileCodeSpanRanges = roslynFileCodeSpanRangeService.GetFileCodeSpanRanges(mockTextSnapshot.Object);
52+
53+
Assert.That(fileCodeSpanRanges, Is.EqualTo(new List<CodeSpanRange> { new CodeSpanRange(1, 2) }));
54+
}
55+
56+
[Test]
57+
public void Should_Return_Itself_As_FileCodeSpanRangeService()
58+
{
59+
var autoMoqer = new AutoMoqer();
60+
var roslynFileCodeSpanRangeService = autoMoqer.Create<RoslynFileCodeSpanRangeService>();
61+
62+
Assert.That(roslynFileCodeSpanRangeService.FileCodeSpanRangeService, Is.SameAs(roslynFileCodeSpanRangeService));
63+
}
64+
}
65+
}

FineCodeCoverageTests/FineCodeCoverageTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<Compile Include="Editor\DynamicCoverage\BlazorGeneratedFilePathMatcher_Tests.cs" />
7676
<Compile Include="Editor\DynamicCoverage\BufferLineCoverage_Tests.cs" />
7777
<Compile Include="Editor\DynamicCoverage\CPPCoverageContentType_Tests.cs" />
78+
<Compile Include="Editor\DynamicCoverage\RoslynFileCodeSpanRangeService_Tests.cs" />
7879
<Compile Include="Editor\DynamicCoverage\VBCoverageContentType_Tests.cs" />
7980
<Compile Include="Editor\DynamicCoverage\CSharpCoverageContentType_Tests.cs" />
8081
<Compile Include="Editor\DynamicCoverage\DynamicCoverageStore_Tests.cs" />

SharedProject/Editor/DynamicCoverage/ContentTypes/Blazor/BlazorFileCodeSpanRangeService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ public List<CodeSpanRange> GetFileCodeSpanRanges(ITextSnapshot snapshot)
4545
);
4646
if (generatedDocumentSyntaxRoot != null)
4747
{
48+
4849
List<SyntaxNode> nodes = this.cSharpCodeCoverageNodeVisitor.GetNodes(generatedDocumentSyntaxRoot);
50+
if(nodes.Count == 0)
51+
{
52+
return null; // sometimes the generated document has not been generated
53+
}
4954
return nodes.Select(node => new { Node = node, MappedLineSpan = this.syntaxNodeLocationMapper.Map(node) })
5055
.Where(a => a.MappedLineSpan.Path == filePath)
5156
.Select(a => new CodeSpanRange(

SharedProject/Editor/DynamicCoverage/TrackedLinesImpl/Construction/ContainingCodeTrackedLinesBuilder.cs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,22 @@ public ITrackedLines Create(List<ILine> lines, ITextSnapshot textSnapshot)
5555
{
5656
ICoverageContentType coverageContentType = this.GetCoverageContentType(textSnapshot);
5757
IFileCodeSpanRangeService fileCodeSpanRangeService = coverageContentType.FileCodeSpanRangeService;
58-
List<IContainingCodeTracker> containingCodeTrackers = this.CreateContainingCodeTrackers(
58+
(List<IContainingCodeTracker> containingCodeTrackers, bool usedFileCodeSpanRangeService) = this.CreateContainingCodeTrackers(
5959
lines, textSnapshot, fileCodeSpanRangeService, coverageContentType.CoverageOnlyFromFileCodeSpanRangeService);
60-
return containingCodeTrackers == null
60+
61+
IContainingCodeTrackerTrackedLines trackedLines = containingCodeTrackers == null
6162
? this.GetNonTrackingTrackedLines()
6263
: this.containingCodeTrackedLinesFactory.Create(
6364
containingCodeTrackers,
6465
this.GetNewCodeTrackerIfProvidesLineExcluder(coverageContentType.LineExcluder),
6566
this.GetFileCodeSpanRangeServiceForChanges(coverageContentType));
67+
68+
return new ContainingCodeTrackerTrackedLinesWithState(trackedLines, usedFileCodeSpanRangeService);
6669
}
6770

68-
private ITrackedLines GetNonTrackingTrackedLines() => this.containingCodeTrackedLinesFactory.Create(new List<IContainingCodeTracker>(), null, null);
71+
private IContainingCodeTrackerTrackedLines GetNonTrackingTrackedLines() => this.containingCodeTrackedLinesFactory.Create(new List<IContainingCodeTracker>(), null, null);
6972

70-
private List<IContainingCodeTracker> CreateContainingCodeTrackers(
73+
private (List<IContainingCodeTracker> containingCodeTrackers, bool usedFileCodeSpanRangeService) CreateContainingCodeTrackers(
7174
List<ILine> lines,
7275
ITextSnapshot textSnapshot,
7376
IFileCodeSpanRangeService fileCodeSpanRangeService,
@@ -76,20 +79,20 @@ bool coverageOnlyFromFileCodeSpanRangeService
7679
{
7780
if (this.AnyLinesOutsideTextSnapshot(lines, textSnapshot))
7881
{
79-
return null;
82+
return (null, false);
8083
}
8184

8285
if (fileCodeSpanRangeService != null)
8386
{
8487
List<CodeSpanRange> codeSpanRanges = fileCodeSpanRangeService.GetFileCodeSpanRanges(textSnapshot);
8588
if (codeSpanRanges != null)
8689
{
87-
return this.CreateContainingCodeTrackersFromCodeSpanRanges(
88-
lines, textSnapshot, codeSpanRanges, coverageOnlyFromFileCodeSpanRangeService);
90+
return (this.CreateContainingCodeTrackersFromCodeSpanRanges(
91+
lines, textSnapshot, codeSpanRanges, coverageOnlyFromFileCodeSpanRangeService), true);
8992
}
9093
}
9194

92-
return lines.Select(line => this.CreateSingleLineContainingCodeTracker(textSnapshot, line)).ToList();
95+
return (lines.Select(line => this.CreateSingleLineContainingCodeTracker(textSnapshot, line)).ToList(), false);
9396
}
9497

9598
private bool AnyLinesOutsideTextSnapshot(List<ILine> lines, ITextSnapshot textSnapshot)
@@ -204,7 +207,7 @@ void CreateOtherLine(int otherCodeLine)
204207

205208
#region Serialization
206209

207-
private ITrackedLines RecreateTrackedLines(
210+
private IContainingCodeTrackerTrackedLines RecreateTrackedLinesNoFileCodeSpanRangeService(
208211
List<SerializedContainingCodeTracker> serializedContainingCodeTrackers,
209212
ITextSnapshot currentSnapshot,
210213
ILineExcluder lineExcluder,
@@ -267,14 +270,14 @@ ITextSnapshot currentSnapshot
267270
return containingCodeTracker;
268271
}
269272

270-
private ITrackedLines RecreateTrackedLines(
273+
private IContainingCodeTrackerTrackedLines RecreateTrackedLinesFileCodeSpanRangeService(
271274
List<SerializedContainingCodeTracker> serializedContainingCodeTrackers,
272275
ITextSnapshot currentSnapshot,
273276
ICoverageContentType coverageContentType)
274277
{
275-
List<CodeSpanRange> codeSpanRanges = coverageContentType.FileCodeSpanRangeService.GetFileCodeSpanRanges(currentSnapshot);
276278
List<IContainingCodeTracker> containingCodeTrackers = this.RecreateContainingCodeTrackers(
277279
serializedContainingCodeTrackers, currentSnapshot);
280+
List<CodeSpanRange> codeSpanRanges = coverageContentType.FileCodeSpanRangeService.GetFileCodeSpanRanges(currentSnapshot);
278281
INewCodeTracker newCodeTracker = this.RecreateNewCodeTracker(
279282
serializedContainingCodeTrackers,
280283
currentSnapshot,
@@ -330,26 +333,29 @@ private IEnumerable<int> EveryLineInCodeSpanRanges(List<CodeSpanRange> newCodeCo
330333
public ITrackedLines Create(string serializedCoverage, ITextSnapshot currentSnapshot)
331334
{
332335
SerializedEditorDynamicCoverage serializedEditorDynamicCoverage = this.jsonConvertService.DeserializeObject<SerializedEditorDynamicCoverage>(serializedCoverage);
333-
334-
return this.TextUnchanged(serializedEditorDynamicCoverage, currentSnapshot)
336+
bool usedFileCodeSpanRangeService = serializedEditorDynamicCoverage.UsedFileCodeSpanRangeService;
337+
IContainingCodeTrackerTrackedLines trackedLines = this.TextUnchanged(serializedEditorDynamicCoverage, currentSnapshot)
335338
? this.RecreateTrackedLines(
336339
serializedEditorDynamicCoverage.SerializedContainingCodeTrackers,
337340
serializedEditorDynamicCoverage.NewCodeLineNumbers,
338-
currentSnapshot)
341+
currentSnapshot,
342+
usedFileCodeSpanRangeService
343+
)
339344
: this.GetNonTrackingTrackedLines();
345+
return new ContainingCodeTrackerTrackedLinesWithState(trackedLines, usedFileCodeSpanRangeService);
340346
}
341347

342-
private ITrackedLines RecreateTrackedLines(
348+
private IContainingCodeTrackerTrackedLines RecreateTrackedLines(
343349
List<SerializedContainingCodeTracker> serializedContainingCodeTrackers,
344350
List<int> newCodeLineNumbers,
345-
ITextSnapshot currentSnapshot
351+
ITextSnapshot currentSnapshot,
352+
bool usedFileCodeSpanRangeService
346353
)
347354
{
348355
ICoverageContentType coverageContentType = this.GetCoverageContentType(currentSnapshot);
349-
IFileCodeSpanRangeService fileCodeSpanRangeService = coverageContentType.FileCodeSpanRangeService;
350-
return fileCodeSpanRangeService == null
351-
? this.RecreateTrackedLines(serializedContainingCodeTrackers, currentSnapshot, coverageContentType.LineExcluder, newCodeLineNumbers)
352-
: this.RecreateTrackedLines(serializedContainingCodeTrackers, currentSnapshot, coverageContentType);
356+
return usedFileCodeSpanRangeService ?
357+
this.RecreateTrackedLinesFileCodeSpanRangeService(serializedContainingCodeTrackers, currentSnapshot, coverageContentType) :
358+
this.RecreateTrackedLinesNoFileCodeSpanRangeService(serializedContainingCodeTrackers, currentSnapshot, coverageContentType.LineExcluder, newCodeLineNumbers);
353359
}
354360

355361
private bool TextUnchanged(SerializedEditorDynamicCoverage serializedEditorDyamicCoverage, ITextSnapshot textSnapshot)
@@ -361,13 +367,20 @@ private bool TextUnchanged(SerializedEditorDynamicCoverage serializedEditorDyami
361367

362368
public string Serialize(ITrackedLines trackedLines, string text)
363369
{
364-
var containingCodeTrackerTrackedLines = trackedLines as IContainingCodeTrackerTrackedLines;
365-
List<SerializedContainingCodeTracker> serializedContainingCodeTrackers = this.GetSerializedContainingCodeTrackers(containingCodeTrackerTrackedLines);
370+
var containingCodeTrackerTrackedLinesWithState = trackedLines as ContainingCodeTrackerTrackedLinesWithState;
371+
List<SerializedContainingCodeTracker> serializedContainingCodeTrackers = this.GetSerializedContainingCodeTrackers(containingCodeTrackerTrackedLinesWithState);
372+
var newCodeLineNumbers = new List<int>();
373+
if (containingCodeTrackerTrackedLinesWithState.NewCodeTracker != null)
374+
{
375+
newCodeLineNumbers = containingCodeTrackerTrackedLinesWithState.NewCodeTracker.Lines.Select(l => l.Number).ToList();
376+
}
377+
366378
return this.jsonConvertService.SerializeObject(
367379
new SerializedEditorDynamicCoverage {
368380
SerializedContainingCodeTrackers = serializedContainingCodeTrackers,
369381
Text = text,
370-
NewCodeLineNumbers = containingCodeTrackerTrackedLines.NewCodeTracker?.Lines.Select(l => l.Number).ToList()
382+
NewCodeLineNumbers = newCodeLineNumbers,
383+
UsedFileCodeSpanRangeService = containingCodeTrackerTrackedLinesWithState.UsedFileCodeSpanRangeService
371384
});
372385
}
373386

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections.Generic;
2+
using System.Diagnostics.CodeAnalysis;
3+
using Microsoft.VisualStudio.Text;
4+
5+
namespace FineCodeCoverage.Editor.DynamicCoverage.TrackedLinesImpl.Construction
6+
{
7+
[ExcludeFromCodeCoverage]
8+
internal class ContainingCodeTrackerTrackedLinesWithState : IContainingCodeTrackerTrackedLines
9+
{
10+
public IContainingCodeTrackerTrackedLines Wrapped { get; }
11+
public ContainingCodeTrackerTrackedLinesWithState(IContainingCodeTrackerTrackedLines trackedLines, bool usedFileCodeSpanRangeService)
12+
{
13+
this.Wrapped = trackedLines;
14+
this.UsedFileCodeSpanRangeService = usedFileCodeSpanRangeService;
15+
}
16+
17+
public bool UsedFileCodeSpanRangeService { get; set; }
18+
public IReadOnlyList<IContainingCodeTracker> ContainingCodeTrackers => this.Wrapped.ContainingCodeTrackers;
19+
public INewCodeTracker NewCodeTracker => this.Wrapped.NewCodeTracker;
20+
21+
public IEnumerable<int> GetChangedLineNumbers(ITextSnapshot currentSnapshot, List<Span> newSpanChanges)
22+
=> this.Wrapped.GetChangedLineNumbers(currentSnapshot, newSpanChanges);
23+
public IEnumerable<IDynamicLine> GetLines(int startLineNumber, int endLineNumber)
24+
=> this.Wrapped.GetLines(startLineNumber, endLineNumber);
25+
}
26+
}

SharedProject/Editor/DynamicCoverage/TrackedLinesImpl/Construction/SerializedEditorDynamicCoverage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ internal class SerializedEditorDynamicCoverage
77
public List<SerializedContainingCodeTracker> SerializedContainingCodeTrackers { get; set; }
88
public string Text { get; set; }
99
public List<int> NewCodeLineNumbers { get; set; }
10+
public bool UsedFileCodeSpanRangeService { get; set; }
1011
}
1112
}

SharedProject/SharedProject.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
<Compile Include="$(MSBuildThisFileDirectory)Editor\DynamicCoverage\Management\DynamicCoverageType.cs" />
203203
<Compile Include="$(MSBuildThisFileDirectory)Editor\DynamicCoverage\Store\IDynamicCoverageStore.cs" />
204204
<Compile Include="$(MSBuildThisFileDirectory)Editor\DynamicCoverage\NotIncluded\INotIncludedLineFactory.cs" />
205+
<Compile Include="$(MSBuildThisFileDirectory)Editor\DynamicCoverage\TrackedLinesImpl\Construction\ContainingCodeTrackerTrackedLinesWithState.cs" />
205206
<Compile Include="$(MSBuildThisFileDirectory)Editor\DynamicCoverage\TrackedLinesImpl\Construction\ICoverageContentType.cs" />
206207
<Compile Include="$(MSBuildThisFileDirectory)Editor\DynamicCoverage\TrackedLinesImpl\Construction\SerializedContainingCodeTracker.cs" />
207208
<Compile Include="$(MSBuildThisFileDirectory)Editor\DynamicCoverage\TrackedLinesImpl\Construction\SerializedEditorDynamicCoverage.cs" />

0 commit comments

Comments
 (0)