Skip to content

Commit 070f2b4

Browse files
Fix multi-line lambda coverage regression (#1060)
Fix multi-line lambda coverage regression
1 parent aaed9a5 commit 070f2b4

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

src/coverlet.core/Coverage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,8 @@ private void CalculateCoverage()
405405
{
406406
if (hitCandidate != hitCandidateToCompare && !hitCandidateToCompare.isBranch)
407407
{
408-
if (hitCandidateToCompare.start >= hitCandidate.start &&
409-
hitCandidateToCompare.end <= hitCandidate.end)
408+
if (hitCandidateToCompare.start > hitCandidate.start &&
409+
hitCandidateToCompare.end < hitCandidate.end)
410410
{
411411
for (int i = hitCandidateToCompare.start;
412412
i <= (hitCandidateToCompare.end == 0 ? hitCandidateToCompare.start : hitCandidateToCompare.end);

test/coverlet.core.tests/Coverage/CoverageTests.Lambda.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,40 @@ public void Lambda_Issue760()
102102
File.Delete(path);
103103
}
104104
}
105+
106+
[Fact]
107+
public void Issue_1056()
108+
{
109+
string path = Path.GetTempFileName();
110+
try
111+
{
112+
FunctionExecutor.Run(async (string[] pathSerialize) =>
113+
{
114+
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<Issue_1056>(instance =>
115+
{
116+
instance.T1();
117+
return Task.CompletedTask;
118+
},
119+
persistPrepareResultToFile: pathSerialize[0]);
120+
121+
return 0;
122+
}, new string[] { path });
123+
124+
TestInstrumentationHelper.GetCoverageResult(path)
125+
.Document("Instrumentation.Lambda.cs")
126+
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 110, 119)
127+
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 122, 124)
128+
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 127, 129)
129+
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 131, 131)
130+
.AssertLinesCovered(BuildConfiguration.Debug, (110, 1), (111, 2), (112, 2), (113, 2), (114, 2), (115, 2), (116, 2), (117, 2), (118, 2), (119, 1),
131+
(122, 2), (123, 2), (124, 2),
132+
(127, 2), (128, 2), (129, 2),
133+
(131, 4));
134+
}
135+
finally
136+
{
137+
File.Delete(path);
138+
}
139+
}
105140
}
106141
}

test/coverlet.core.tests/Coverage/InstrumenterHelper.Assertions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ public static Document AssertLinesCoveredAllBut(this Document document, BuildCon
246246
return document;
247247
}
248248

249+
public static Document AssertLinesCoveredFromTo(this Document document, int from, int to)
250+
{
251+
return AssertLinesCoveredFromTo(document, BuildConfiguration.Debug | BuildConfiguration.Release, from, to);
252+
}
253+
249254
public static Document AssertLinesCoveredFromTo(this Document document, BuildConfiguration configuration, int from, int to)
250255
{
251256
if (document is null)

test/coverlet.core.tests/Samples/Instrumentation.Lambda.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,31 @@ public async Task<int> Foreach()
103103
return sum;
104104
}
105105
}
106+
107+
public class Issue_1056
108+
{
109+
public void T1()
110+
{
111+
Do(x => WriteLine(x.GetType().Name));
112+
Do(x => WriteLine(x
113+
.GetType()
114+
.Name));
115+
Do2(x => x.GetType().Name.Length);
116+
Do2(x => x.GetType()
117+
.Name
118+
.Length);
119+
}
120+
121+
private static void Do(System.Action<object> action)
122+
{
123+
action(new object());
124+
}
125+
126+
private static object Do2(System.Func<object, object> func)
127+
{
128+
return func(new object());
129+
}
130+
131+
public void WriteLine(string str) { }
132+
}
106133
}

0 commit comments

Comments
 (0)