-
Notifications
You must be signed in to change notification settings - Fork 392
Skip branches in generated MoveNext()
for singleton iterators
#813
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6e1b51a
skip branches in generated `MoveNext()` for singleton iterators
bert2 e0757c8
use `All()` instead of `Any()`
bert2 ed10977
add integration tests
bert2 6d96cc0
fix tests and assert number of branches
bert2 9b327b6
remove guideline comments and dont skip tests on MacOS
bert2 669089d
add failing test case for singleton generator with switch
bert2 23607b7
use `Random` in switch to avoid it from being optimized away (just gu…
bert2 03620d9
Revert "use `Random` in switch to avoid it from being optimized away …
bert2 98605ca
also skip second branch if iterator contains a switch
bert2 c6e8f21
add comment explaining `skipFirstBranch` and `skipSecondBranch`
bert2 4104e80
add more integration test cases
bert2 d3c069f
check coverage of `switch(n)`
bert2 65ea9f3
check coverage of other lines in `Yield.Enumerable()`
bert2 93d5202
fix assertions for release builds
bert2 5b1471f
remove coverage assertions for release builds
bert2 e576020
refactor test a bit
MarcoRossignoli cf5d269
nit: style
MarcoRossignoli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
test/coverlet.core.tests/Coverage/CoverageTest.Yield.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
using Coverlet.Core.Samples.Tests; | ||
using Tmds.Utils; | ||
using Xunit; | ||
|
||
namespace Coverlet.Core.Tests | ||
{ | ||
public partial class CoverageTests : ExternalProcessExecutionTest | ||
{ | ||
[Fact] | ||
public void Yield_Single() | ||
{ | ||
string path = Path.GetTempFileName(); | ||
try | ||
{ | ||
FunctionExecutor.Run(async (string[] pathSerialize) => | ||
{ | ||
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<Yield>(instance => | ||
{ | ||
foreach (var _ in instance.One()) ; | ||
|
||
return Task.CompletedTask; | ||
}, persistPrepareResultToFile: pathSerialize[0]); | ||
|
||
return 0; | ||
}, new string[] { path }); | ||
|
||
CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); | ||
|
||
result.Document("Instrumentation.Yield.cs") | ||
.Method("System.Boolean Coverlet.Core.Samples.Tests.Yield/<One>d__0::MoveNext()") | ||
.AssertLinesCovered((9, 1)) | ||
.ExpectedTotalNumberOfBranches(0); | ||
} | ||
finally | ||
{ | ||
File.Delete(path); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Yield_Two() | ||
{ | ||
string path = Path.GetTempFileName(); | ||
try | ||
{ | ||
FunctionExecutor.Run(async (string[] pathSerialize) => | ||
{ | ||
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<Yield>(instance => | ||
{ | ||
foreach (var _ in instance.Two()) ; | ||
|
||
return Task.CompletedTask; | ||
}, persistPrepareResultToFile: pathSerialize[0]); | ||
return 0; | ||
}, new string[] { path }); | ||
|
||
CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); | ||
|
||
result.Document("Instrumentation.Yield.cs") | ||
.Method("System.Boolean Coverlet.Core.Samples.Tests.Yield/<Two>d__1::MoveNext()") | ||
.AssertLinesCovered((14, 1), (15, 1)) | ||
.ExpectedTotalNumberOfBranches(0); | ||
} | ||
finally | ||
{ | ||
File.Delete(path); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Yield_SingleWithSwitch() | ||
{ | ||
string path = Path.GetTempFileName(); | ||
try | ||
{ | ||
FunctionExecutor.Run(async (string[] pathSerialize) => | ||
{ | ||
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<Yield>(instance => | ||
{ | ||
foreach (var _ in instance.OneWithSwitch(2)) ; | ||
|
||
return Task.CompletedTask; | ||
}, persistPrepareResultToFile: pathSerialize[0]); | ||
|
||
return 0; | ||
}, new string[] { path }); | ||
|
||
CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); | ||
|
||
result.Document("Instrumentation.Yield.cs") | ||
.Method("System.Boolean Coverlet.Core.Samples.Tests.Yield/<OneWithSwitch>d__2::MoveNext()") | ||
.AssertLinesCovered(BuildConfiguration.Debug, (21, 1), (30, 1), (31, 1), (37, 1)) | ||
.ExpectedTotalNumberOfBranches(1); | ||
} | ||
finally | ||
{ | ||
File.Delete(path); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Yield_Three() | ||
{ | ||
string path = Path.GetTempFileName(); | ||
try | ||
{ | ||
FunctionExecutor.Run(async (string[] pathSerialize) => | ||
{ | ||
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<Yield>(instance => | ||
{ | ||
foreach (var _ in instance.Three()) ; | ||
|
||
return Task.CompletedTask; | ||
}, persistPrepareResultToFile: pathSerialize[0]); | ||
return 0; | ||
}, new string[] { path }); | ||
|
||
CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); | ||
|
||
result.Document("Instrumentation.Yield.cs") | ||
.Method("System.Boolean Coverlet.Core.Samples.Tests.Yield/<Three>d__3::MoveNext()") | ||
.AssertLinesCovered((42, 1), (43, 1), (44, 1)) | ||
.ExpectedTotalNumberOfBranches(0); | ||
} | ||
finally | ||
{ | ||
File.Delete(path); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Yield_Enumerable() | ||
{ | ||
string path = Path.GetTempFileName(); | ||
try | ||
{ | ||
FunctionExecutor.Run(async (string[] pathSerialize) => | ||
{ | ||
CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<Yield>(instance => | ||
{ | ||
foreach (var _ in instance.Enumerable(new[] { "one", "two", "three", "four" })) ; | ||
|
||
return Task.CompletedTask; | ||
}, persistPrepareResultToFile: pathSerialize[0]); | ||
return 0; | ||
}, new string[] { path }); | ||
|
||
CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); | ||
|
||
result.Document("Instrumentation.Yield.cs") | ||
.Method("System.Boolean Coverlet.Core.Samples.Tests.Yield/<Enumerable>d__4::MoveNext()") | ||
.AssertLinesCovered(BuildConfiguration.Debug, (48, 1), (49, 1), (50, 4), (51, 5), (52, 1), (54, 4), (55, 4), (56, 4), (57, 1)) | ||
.ExpectedTotalNumberOfBranches(1); | ||
MarcoRossignoli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
finally | ||
{ | ||
File.Delete(path); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Remember to use full name because adding new using directives change line numbers | ||
|
||
namespace Coverlet.Core.Samples.Tests | ||
{ | ||
public class Yield | ||
{ | ||
public System.Collections.Generic.IEnumerable<int> One() | ||
{ | ||
yield return 1; | ||
} | ||
|
||
public System.Collections.Generic.IEnumerable<int> Two() | ||
bert2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
yield return 1; | ||
yield return 2; | ||
} | ||
|
||
public System.Collections.Generic.IEnumerable<int> OneWithSwitch(int n) | ||
{ | ||
int result; | ||
switch (n) | ||
{ | ||
case 0: | ||
result = 10; | ||
break; | ||
case 1: | ||
result = 11; | ||
break; | ||
case 2: | ||
result = 12; | ||
break; | ||
default: | ||
result = -1; | ||
break; | ||
} | ||
|
||
yield return result; | ||
} | ||
|
||
public System.Collections.Generic.IEnumerable<int> Three() | ||
{ | ||
yield return 1; | ||
yield return 2; | ||
yield return 3; | ||
} | ||
|
||
public System.Collections.Generic.IEnumerable<string> Enumerable(System.Collections.Generic.IList<string> ls) | ||
{ | ||
foreach ( | ||
var item | ||
in | ||
ls | ||
) | ||
{ | ||
yield return item; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.