Skip to content

Commit 471a807

Browse files
EvangelinkCopilot
andauthored
Guard Assert.IsNotNull against Task return-value regressions (#12306)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 04798c6 commit 471a807

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

‎test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.IsNull.cs‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,69 @@ public void IsNotNull_WhenNonNullNullableValueAndInterpolatedStringMessage_DoesN
9292
_ = obj.ToString(); // No potential NRE warning
9393
}
9494

95+
// Keep these statement calls in async methods: returning the checked Task would introduce CS4014.
96+
public async Task IsNotNull_WhenPendingTasks_DoesNotAwaitAndLearnsNotNull()
97+
{
98+
TaskCompletionSource<int> completion = new(TaskCreationOptions.RunContinuationsAsynchronously);
99+
Task? task = GetTask();
100+
Task<int>? resultTask = GetResultTask();
101+
102+
Assert.IsNotNull(task);
103+
Assert.IsNotNull(resultTask);
104+
task.IsCompleted.Should().BeFalse();
105+
resultTask.IsCompleted.Should().BeFalse();
106+
107+
completion.SetResult(42);
108+
await task;
109+
(await resultTask).Should().Be(42);
110+
111+
Task? GetTask() => completion.Task;
112+
113+
Task<int>? GetResultTask() => completion.Task;
114+
}
115+
116+
public async Task IsNotNull_WhenPendingTasksAndMessage_DoesNotAwaitAndLearnsNotNull()
117+
{
118+
TaskCompletionSource<int> completion = new(TaskCreationOptions.RunContinuationsAsynchronously);
119+
Task? task = GetTask();
120+
Task<int>? resultTask = GetResultTask();
121+
122+
Assert.IsNotNull(task, "pending task");
123+
Assert.IsNotNull(resultTask, "pending task");
124+
task.IsCompleted.Should().BeFalse();
125+
resultTask.IsCompleted.Should().BeFalse();
126+
127+
completion.SetResult(42);
128+
await task;
129+
(await resultTask).Should().Be(42);
130+
131+
Task? GetTask() => completion.Task;
132+
133+
Task<int>? GetResultTask() => completion.Task;
134+
}
135+
136+
public async Task IsNotNull_WhenPendingTasksAndInterpolatedStringMessage_DoesNotAwaitAndLearnsNotNull()
137+
{
138+
TaskCompletionSource<int> completion = new(TaskCreationOptions.RunContinuationsAsynchronously);
139+
Task? task = GetTask();
140+
Task<int>? resultTask = GetResultTask();
141+
DummyClassTrackingToStringCalls message = new();
142+
143+
Assert.IsNotNull(task, $"pending task {message}");
144+
Assert.IsNotNull(resultTask, $"pending task {message}");
145+
task.IsCompleted.Should().BeFalse();
146+
resultTask.IsCompleted.Should().BeFalse();
147+
message.WasToStringCalled.Should().BeFalse();
148+
149+
completion.SetResult(42);
150+
await task;
151+
(await resultTask).Should().Be(42);
152+
153+
Task? GetTask() => completion.Task;
154+
155+
Task<int>? GetResultTask() => completion.Task;
156+
}
157+
95158
public void IsNotNull_PassNull_ShouldFail()
96159
{
97160
Action action = () => Assert.IsNotNull(null);

0 commit comments

Comments
 (0)