From 070f2b47ef616a62e7fedc89cfec02eac9560ca9 Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Sun, 17 Jan 2021 19:44:39 +0100 Subject: [PATCH] Fix multi-line lambda coverage regression (#1060) Fix multi-line lambda coverage regression --- src/coverlet.core/Coverage.cs | 4 +-- .../Coverage/CoverageTests.Lambda.cs | 35 +++++++++++++++++++ .../Coverage/InstrumenterHelper.Assertions.cs | 5 +++ .../Samples/Instrumentation.Lambda.cs | 27 ++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/coverlet.core/Coverage.cs b/src/coverlet.core/Coverage.cs index e2a74a1..cdee815 100644 --- a/src/coverlet.core/Coverage.cs +++ b/src/coverlet.core/Coverage.cs @@ -405,8 +405,8 @@ namespace Coverlet.Core { 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); diff --git a/test/coverlet.core.tests/Coverage/CoverageTests.Lambda.cs b/test/coverlet.core.tests/Coverage/CoverageTests.Lambda.cs index 6ef5191..37b3e76 100644 --- a/test/coverlet.core.tests/Coverage/CoverageTests.Lambda.cs +++ b/test/coverlet.core.tests/Coverage/CoverageTests.Lambda.cs @@ -102,5 +102,40 @@ namespace Coverlet.Core.Tests File.Delete(path); } } + + [Fact] + public void Issue_1056() + { + string path = Path.GetTempFileName(); + try + { + FunctionExecutor.Run(async (string[] pathSerialize) => + { + CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run(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); + } + } } } \ No newline at end of file diff --git a/test/coverlet.core.tests/Coverage/InstrumenterHelper.Assertions.cs b/test/coverlet.core.tests/Coverage/InstrumenterHelper.Assertions.cs index 92da3b4..07f3e2b 100644 --- a/test/coverlet.core.tests/Coverage/InstrumenterHelper.Assertions.cs +++ b/test/coverlet.core.tests/Coverage/InstrumenterHelper.Assertions.cs @@ -246,6 +246,11 @@ namespace Coverlet.Core.Tests 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) diff --git a/test/coverlet.core.tests/Samples/Instrumentation.Lambda.cs b/test/coverlet.core.tests/Samples/Instrumentation.Lambda.cs index b179b7e..dcf4ffe 100644 --- a/test/coverlet.core.tests/Samples/Instrumentation.Lambda.cs +++ b/test/coverlet.core.tests/Samples/Instrumentation.Lambda.cs @@ -103,4 +103,31 @@ namespace Coverlet.Core.Samples.Tests 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 action) + { + action(new object()); + } + + private static object Do2(System.Func func) + { + return func(new object()); + } + + public void WriteLine(string str) { } + } }