Skip to content

Commit

Permalink
Fix multi-line lambda coverage regression (#1060)
Browse files Browse the repository at this point in the history
Fix multi-line lambda coverage regression
  • Loading branch information
MarcoRossignoli authored Jan 17, 2021
1 parent aaed9a5 commit 070f2b4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/coverlet.core/Coverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ private void CalculateCoverage()
{
if (hitCandidate != hitCandidateToCompare && !hitCandidateToCompare.isBranch)
{
if (hitCandidateToCompare.start >= hitCandidate.start &&
hitCandidateToCompare.end <= hitCandidate.end)
if (hitCandidateToCompare.start > hitCandidate.start &&
hitCandidateToCompare.end < hitCandidate.end)
{
for (int i = hitCandidateToCompare.start;
i <= (hitCandidateToCompare.end == 0 ? hitCandidateToCompare.start : hitCandidateToCompare.end);
Expand Down
35 changes: 35 additions & 0 deletions test/coverlet.core.tests/Coverage/CoverageTests.Lambda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,40 @@ public void Lambda_Issue760()
File.Delete(path);
}
}

[Fact]
public void Issue_1056()
{
string path = Path.GetTempFileName();
try
{
FunctionExecutor.Run(async (string[] pathSerialize) =>
{
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<Issue_1056>(instance =>
{
instance.T1();
return Task.CompletedTask;
},
persistPrepareResultToFile: pathSerialize[0]);

return 0;
}, new string[] { path });

TestInstrumentationHelper.GetCoverageResult(path)
.Document("Instrumentation.Lambda.cs")
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 110, 119)
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 122, 124)
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 127, 129)
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 131, 131)
.AssertLinesCovered(BuildConfiguration.Debug, (110, 1), (111, 2), (112, 2), (113, 2), (114, 2), (115, 2), (116, 2), (117, 2), (118, 2), (119, 1),
(122, 2), (123, 2), (124, 2),
(127, 2), (128, 2), (129, 2),
(131, 4));
}
finally
{
File.Delete(path);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ public static Document AssertLinesCoveredAllBut(this Document document, BuildCon
return document;
}

public static Document AssertLinesCoveredFromTo(this Document document, int from, int to)
{
return AssertLinesCoveredFromTo(document, BuildConfiguration.Debug | BuildConfiguration.Release, from, to);
}

public static Document AssertLinesCoveredFromTo(this Document document, BuildConfiguration configuration, int from, int to)
{
if (document is null)
Expand Down
27 changes: 27 additions & 0 deletions test/coverlet.core.tests/Samples/Instrumentation.Lambda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,31 @@ public async Task<int> Foreach()
return sum;
}
}

public class Issue_1056
{
public void T1()
{
Do(x => WriteLine(x.GetType().Name));
Do(x => WriteLine(x
.GetType()
.Name));
Do2(x => x.GetType().Name.Length);
Do2(x => x.GetType()
.Name
.Length);
}

private static void Do(System.Action<object> action)
{
action(new object());
}

private static object Do2(System.Func<object, object> func)
{
return func(new object());
}

public void WriteLine(string str) { }
}
}

0 comments on commit 070f2b4

Please sign in to comment.