|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +using Coverlet.Core.Samples.Tests; |
| 7 | +using Coverlet.Tests.Xunit.Extensions; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Coverlet.Core.Tests |
| 11 | +{ |
| 12 | + public partial class CoverageTests |
| 13 | + { |
| 14 | + [Fact] |
| 15 | + public void AsyncForeach() |
| 16 | + { |
| 17 | + string path = Path.GetTempFileName(); |
| 18 | + try |
| 19 | + { |
| 20 | + FunctionExecutor.Run(async (string[] pathSerialize) => |
| 21 | + { |
| 22 | + CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run<AsyncForeach>(instance => |
| 23 | + { |
| 24 | + int res = ((ValueTask<int>)instance.SumWithATwist(AsyncEnumerable.Range(1, 5))).GetAwaiter().GetResult(); |
| 25 | + res += ((ValueTask<int>)instance.Sum(AsyncEnumerable.Range(1, 3))).GetAwaiter().GetResult(); |
| 26 | + res += ((ValueTask<int>)instance.SumEmpty()).GetAwaiter().GetResult(); |
| 27 | + |
| 28 | + return Task.CompletedTask; |
| 29 | + }, persistPrepareResultToFile: pathSerialize[0]); |
| 30 | + return 0; |
| 31 | + }, new string[] { path }); |
| 32 | + |
| 33 | + TestInstrumentationHelper.GetCoverageResult(path) |
| 34 | + .Document("Instrumentation.AsyncForeach.cs") |
| 35 | + .AssertLinesCovered(BuildConfiguration.Debug, |
| 36 | + // SumWithATwist(IAsyncEnumerable<int>) |
| 37 | + // Apparently due to entering and exiting the async state machine, line 17 |
| 38 | + // (the top of an "await foreach" loop) is reached three times *plus* twice |
| 39 | + // per loop iteration. So, in this case, with five loop iterations, we end |
| 40 | + // up with 3 + 5 * 2 = 13 hits. |
| 41 | + (14, 1), (15, 1), (17, 13), (18, 5), (19, 5), (20, 5), (21, 5), (22, 5), |
| 42 | + (24, 0), (25, 0), (26, 0), (27, 5), (29, 1), (30, 1), |
| 43 | + // Sum(IAsyncEnumerable<int>) |
| 44 | + (34, 1), (35, 1), (37, 9), (38, 3), (39, 3), (40, 3), (42, 1), (43, 1), |
| 45 | + // SumEmpty() |
| 46 | + (47, 1), (48, 1), (50, 3), (51, 0), (52, 0), (53, 0), (55, 1), (56, 1) |
| 47 | + ) |
| 48 | + .AssertBranchesCovered(BuildConfiguration.Debug, |
| 49 | + // SumWithATwist(IAsyncEnumerable<int>) |
| 50 | + (17, 2, 1), (17, 3, 5), (19, 0, 5), (19, 1, 0), |
| 51 | + // Sum(IAsyncEnumerable<int>) |
| 52 | + (37, 0, 1), (37, 1, 3), |
| 53 | + // SumEmpty() |
| 54 | + // If we never entered the loop, that's a branch not taken, which is |
| 55 | + // what we want to see. |
| 56 | + (50, 0, 1), (50, 1, 0) |
| 57 | + ) |
| 58 | + .ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 4); |
| 59 | + } |
| 60 | + finally |
| 61 | + { |
| 62 | + File.Delete(path); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments