Skip to content

Commit 669089d

Browse files
committed
add failing test case for singleton generator with switch
1 parent 9b327b6 commit 669089d

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

test/coverlet.core.tests/Coverage/CoverageTest.Yield.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public void Yield_Single()
3030
CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path);
3131

3232
result.Document("Instrumentation.Yield.cs")
33+
.Method("System.Boolean Coverlet.Core.Samples.Tests.Yield/<One>d__0::MoveNext()")
3334
.AssertLinesCovered((9, 1))
3435
.ExpectedTotalNumberOfBranches(0);
3536
}
@@ -59,6 +60,7 @@ public void Yield_Multiple()
5960
CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path);
6061

6162
result.Document("Instrumentation.Yield.cs")
63+
.Method("System.Boolean Coverlet.Core.Samples.Tests.Yield/<Two>d__1::MoveNext()")
6264
.AssertLinesCovered((14, 1), (15, 1))
6365
.ExpectedTotalNumberOfBranches(0);
6466
}
@@ -67,5 +69,36 @@ public void Yield_Multiple()
6769
File.Delete(path);
6870
}
6971
}
72+
73+
[Fact]
74+
public void Yield_SingleWithSwitch()
75+
{
76+
string path = Path.GetTempFileName();
77+
try
78+
{
79+
FunctionExecutor.Run(async (string[] pathSerialize) =>
80+
{
81+
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<Yield>(instance =>
82+
{
83+
foreach (var _ in instance.OneWithSwitch(2)) ;
84+
85+
return Task.CompletedTask;
86+
}, persistPrepareResultToFile: pathSerialize[0]);
87+
88+
return 0;
89+
}, new string[] { path });
90+
91+
CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path);
92+
93+
result.Document("Instrumentation.Yield.cs")
94+
.Method("System.Boolean Coverlet.Core.Samples.Tests.Yield/<OneWithSwitch>d__2::MoveNext()")
95+
.AssertLinesCovered((30, 1), (31, 1), (37, 1))
96+
.ExpectedTotalNumberOfBranches(1);
97+
}
98+
finally
99+
{
100+
File.Delete(path);
101+
}
102+
}
70103
}
71104
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,36 @@ public static Document Document(this CoverageResult coverageResult, string docNa
7979
throw new XunitException($"Document not found '{docName}'");
8080
}
8181

82+
public static Document Method(this Document document, string methodName)
83+
{
84+
var methodDoc = new Document { Path = document.Path, Index = document.Index };
85+
86+
if (!document.Lines.Any() && !document.Branches.Any())
87+
{
88+
return methodDoc;
89+
}
90+
91+
if (document.Lines.Values.All(l => l.Method != methodName) && document.Branches.Values.All(l => l.Method != methodName))
92+
{
93+
var methods = document.Lines.Values.Select(l => $"'{l.Method}'")
94+
.Concat(document.Branches.Values.Select(b => $"'{b.Method}'"))
95+
.Distinct();
96+
throw new XunitException($"Method '{methodName}' not found. Methods in document: {string.Join(", ", methods)}");
97+
}
98+
99+
foreach (var line in document.Lines.Where(l => l.Value.Method == methodName))
100+
{
101+
methodDoc.Lines[line.Key] = line.Value;
102+
}
103+
104+
foreach (var branch in document.Branches.Where(b => b.Value.Method == methodName))
105+
{
106+
methodDoc.Branches[branch.Key] = branch.Value;
107+
}
108+
109+
return methodDoc;
110+
}
111+
82112
public static Document AssertBranchesCovered(this Document document, params (int line, int ordinal, int hits)[] lines)
83113
{
84114
return AssertBranchesCovered(document, BuildConfiguration.Debug | BuildConfiguration.Release, lines);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,27 @@ public System.Collections.Generic.IEnumerable<int> Two()
1414
yield return 1;
1515
yield return 2;
1616
}
17+
18+
public System.Collections.Generic.IEnumerable<int> OneWithSwitch(int n)
19+
{
20+
int result;
21+
switch (n)
22+
{
23+
case 0:
24+
result = 10;
25+
break;
26+
case 1:
27+
result = 11;
28+
break;
29+
case 2:
30+
result = 12;
31+
break;
32+
default:
33+
result = -1;
34+
break;
35+
}
36+
37+
yield return result;
38+
}
1739
}
1840
}

0 commit comments

Comments
 (0)