Skip to content
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
7 changes: 7 additions & 0 deletions src/coverlet.core/Coverage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ public CoverageResult GetCoverageResult()
{
foreach (var @class in document.Value)
{
// We fix only lamda generated class
// https://github.com/dotnet/roslyn/blob/master/src/Compilers/CSharp/Portable/Symbols/Synthesized/GeneratedNameKind.cs#L18
if (!@class.Key.Contains("<>c"))
{
continue;
}

foreach (var method in @class.Value)
{
foreach (var branch in method.Value.Branches)
Expand Down
42 changes: 36 additions & 6 deletions test/coverlet.core.tests/Coverage/CoverageTests.Lambda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public void Lambda_Issue343()

TestInstrumentationHelper.GetCoverageResult(path)
.Document("Instrumentation.Lambda.cs")
.AssertLinesCoveredAllBut(BuildConfiguration.Debug, 23, 51)
.AssertLinesCoveredAllBut(BuildConfiguration.Debug, 24, 52)
.AssertBranchesCovered(BuildConfiguration.Debug,
// Expected branches
(22, 0, 0),
(22, 1, 1),
(50, 0, 0),
(50, 1, 1)
(23, 0, 0),
(23, 1, 1),
(51, 0, 0),
(51, 1, 1)
);
}
finally
Expand Down Expand Up @@ -64,13 +64,43 @@ public void AsyncAwait_Issue_730()

TestInstrumentationHelper.GetCoverageResult(path)
.Document("Instrumentation.Lambda.cs")
.AssertLinesCovered(BuildConfiguration.Debug, (72, 1), (73, 1), (74, 101), (75, 1), (76, 1))
.AssertLinesCovered(BuildConfiguration.Debug, (73, 1), (74, 1), (75, 101), (76, 1), (77, 1))
.ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 0);
}
finally
{
File.Delete(path);
}
}

[Fact]
public void Lambda_Issue760()
{
string path = Path.GetTempFileName();
try
{
FunctionExecutor.Run(async (string[] pathSerialize) =>
{
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<Issue_760>(instance =>
{
((Task)instance.If()).ConfigureAwait(false).GetAwaiter().GetResult();
((Task)instance.Foreach()).ConfigureAwait(false).GetAwaiter().GetResult();
return Task.CompletedTask;
},
persistPrepareResultToFile: pathSerialize[0]);

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

TestInstrumentationHelper.GetCoverageResult(path)
.Document("Instrumentation.Lambda.cs")
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 83, 92)
.AssertLinesCoveredFromTo(BuildConfiguration.Debug, 95, 104);
}
finally
{
File.Delete(path);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum BuildConfiguration

static class TestInstrumentationAssert
{
public static CoverageResult GenerateReport(this CoverageResult coverageResult, [CallerMemberName]string directory = "", bool show = false)
public static CoverageResult GenerateReport(this CoverageResult coverageResult, [CallerMemberName] string directory = "", bool show = false)
{
if (coverageResult is null)
{
Expand Down Expand Up @@ -246,6 +246,42 @@ public static Document AssertLinesCoveredAllBut(this Document document, BuildCon
return document;
}

public static Document AssertLinesCoveredFromTo(this Document document, BuildConfiguration configuration, int from, int to)
{
if (document is null)
{
throw new ArgumentNullException(nameof(document));
}

BuildConfiguration buildConfiguration = GetAssemblyBuildConfiguration();

if ((buildConfiguration & configuration) != buildConfiguration)
{
return document;
}

if (to < from)
{
throw new ArgumentException("to cannot be lower than from");
}

List<int> lines = new List<int>();
foreach (KeyValuePair<int, Line> line in document.Lines)
{
if (line.Value.Number >= from && line.Value.Number <= to && line.Value.Hits > 0)
{
lines.Add(line.Value.Number);
}
}

if (!lines.OrderBy(l => l).SequenceEqual(Enumerable.Range(from, to - from + 1)))
{
throw new XunitException($"Unexpected lines covered");
}

return document;
}

public static Document AssertLinesCovered(this Document document, BuildConfiguration configuration, params (int line, int hits)[] lines)
{
if (document is null)
Expand Down
28 changes: 28 additions & 0 deletions test/coverlet.core.tests/Samples/Instrumentation.Lambda.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Remember to use full name because adding new using directives change line numbers

using System.Linq;
using System.Threading.Tasks;

namespace Coverlet.Core.Samples.Tests
Expand Down Expand Up @@ -75,4 +76,31 @@ async public Task DoSomethingAsyncWithLinq(System.Collections.Generic.IEnumerabl
_ = System.Linq.Enumerable.ToArray(selected);
}
}

public class Issue_760
{
public async Task<int> If()
{
var numbers = (System.Collections.Generic.IEnumerable<int>)new[] { 1, 2, 3, 4, 5 };
var result = 0;
if (numbers.Select(i => i * 2).Count() == 5)
{
result = 1;
}
await Task.Delay(100);
return result;
}

public async Task<int> Foreach()
{
var numbers = (System.Collections.Generic.IEnumerable<int>)new[] { 1, 2, 3, 4, 5 };
var sum = 0;
foreach (var i in numbers.Select(n => n * 2))
{
sum += i;
}
await Task.Delay(100);
return sum;
}
}
}