Skip to content

Fix multi-line lambda coverage regression #1060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) { }
}
}