Skip to content

Commit 8423fae

Browse files
committed
Fixing issue #907
First, I added a failing unit test, in two parts: * A new AsyncAwaitValueTaskStateMachine class in Samples.cs in coverlet.core.tests/Samples, in line with the existing AsyncAwaitStateMachine class, but returning a completed ValueTask instead of a completed Task. * A new test in CecilSymbolHelperTests.cs in coverlet.core.tests/Symbols that expects the "await" in that sample class not to have a branch in it. After it failed, I updated CecilSymbolHelper to include get_IsCompleted from System.Runtime.CompilerServices.ValueTaskAwaiter, as suggested by @a-jackson in issue #907. The test passed. One open question is whether it might be worth adding a ValueTask-based analogue for the Instrumentation.AsyncAwait.cs samples.
1 parent dd2237a commit 8423fae

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/coverlet.core/Symbols/CecilSymbolHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ instruction.Previous.Operand is MethodReference operand &&
172172
operand.Name == "get_IsCompleted" &&
173173
(
174174
operand.DeclaringType.FullName.StartsWith("System.Runtime.CompilerServices.TaskAwaiter") ||
175+
operand.DeclaringType.FullName.StartsWith("System.Runtime.CompilerServices.ValueTaskAwaiter") ||
175176
operand.DeclaringType.FullName.StartsWith("System.Runtime.CompilerServices.ConfiguredTaskAwaitable") ||
176177
operand.DeclaringType.FullName.StartsWith("System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable")
177178
)

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ async public Task AsyncAwait()
189189
}
190190
}
191191

192+
public class AsyncAwaitValueTaskStateMachine
193+
{
194+
async public ValueTask AsyncAwait()
195+
{
196+
await default(ValueTask);
197+
}
198+
}
199+
192200
[ExcludeFromCoverage]
193201
public class ClassExcludedByCoverletCodeCoverageAttr
194202
{

test/coverlet.core.tests/Symbols/CecilSymbolHelperTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,22 @@ public void GetBranchPoints_IgnoresBranchesIn_AsyncAwaitStateMachine()
294294
Assert.Empty(points);
295295
}
296296

297+
[Fact]
298+
public void GetBranchPoints_IgnoresBranchesIn_AsyncAwaitValueTaskStateMachine()
299+
{
300+
// arrange
301+
var nestedName = typeof(AsyncAwaitValueTaskStateMachine).GetNestedTypes(BindingFlags.NonPublic).First().Name;
302+
var type = _module.Types.FirstOrDefault(x => x.FullName == typeof(AsyncAwaitValueTaskStateMachine).FullName);
303+
var nestedType = type.NestedTypes.FirstOrDefault(x => x.FullName.EndsWith(nestedName));
304+
var method = nestedType.Methods.First(x => x.FullName.EndsWith("::MoveNext()"));
305+
306+
// act
307+
var points = _cecilSymbolHelper.GetBranchPoints(method);
308+
309+
// assert
310+
Assert.Empty(points);
311+
}
312+
297313
[Fact]
298314
public void GetBranchPoints_ExceptionFilter()
299315
{

0 commit comments

Comments
 (0)