// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace CommunityToolkit.Common.UnitTests.Extensions; [TestClass] public class Test_TaskExtensions { [TestMethod] public void Test_TaskExtensions_NonGeneric_CompletedTask() { _ = Task.CompletedTask.GetResultOrDefault(); } [TestMethod] public void Test_TaskExtensions_Generic_ValueType() { TaskCompletionSource tcs = new(); Assert.AreEqual(0, tcs.Task.GetResultOrDefault()); tcs.SetResult(42); Assert.AreEqual(42, tcs.Task.GetResultOrDefault()); } [TestMethod] public void Test_TaskExtensions_Generic_ReferenceType() { TaskCompletionSource tcs = new(); Assert.AreEqual(null, tcs.Task.GetResultOrDefault()); tcs.SetResult(nameof(Test_TaskExtensions_Generic_ReferenceType)); Assert.AreEqual(nameof(Test_TaskExtensions_Generic_ReferenceType), tcs.Task.GetResultOrDefault()); } [TestMethod] public void Test_TaskExtensions_ResultOrDefault() { TaskCompletionSource tcs = new(); Assert.AreEqual(null, ((Task)tcs.Task).GetResultOrDefault()); tcs.SetCanceled(); Assert.AreEqual(null, ((Task)tcs.Task).GetResultOrDefault()); tcs = new TaskCompletionSource(); tcs.SetException(new InvalidOperationException("Test")); Assert.AreEqual(null, ((Task)tcs.Task).GetResultOrDefault()); tcs = new TaskCompletionSource(); tcs.SetResult(42); Assert.AreEqual(42, ((Task)tcs.Task).GetResultOrDefault()); } [TestMethod] public void Test_TaskExtensions_ResultOrDefault_FromTaskCompleted() { Assert.AreEqual(null, Task.CompletedTask.GetResultOrDefault()); } [TestMethod] public async Task Test_TaskExtensions_ResultOrDefault_FromAsyncTaskMethodBuilder() { TaskCompletionSource? tcs = new(); Task taskFromBuilder = GetTaskFromAsyncMethodBuilder("Test", tcs); Assert.IsNull(((Task)taskFromBuilder).GetResultOrDefault()); Assert.IsNull(taskFromBuilder.GetResultOrDefault()); tcs.SetResult(null); _ = await taskFromBuilder; Assert.AreEqual(((Task)taskFromBuilder).GetResultOrDefault(), "Test"); Assert.AreEqual(taskFromBuilder.GetResultOrDefault(), "Test"); } [TestMethod] public void Test_TaskExtensions_ResultOrDefault_OfT_Int32() { TaskCompletionSource tcs = new(); Assert.AreEqual(0, tcs.Task.GetResultOrDefault()); tcs.SetCanceled(); Assert.AreEqual(0, tcs.Task.GetResultOrDefault()); tcs = new TaskCompletionSource(); tcs.SetException(new InvalidOperationException("Test")); Assert.AreEqual(0, tcs.Task.GetResultOrDefault()); tcs = new TaskCompletionSource(); tcs.SetResult(42); Assert.AreEqual(42, tcs.Task.GetResultOrDefault()); } [TestMethod] public void Test_TaskExtensions_ResultOrDefault_OfT_String() { TaskCompletionSource tcs = new(); Assert.AreEqual(null, tcs.Task.GetResultOrDefault()); tcs.SetCanceled(); Assert.AreEqual(null, tcs.Task.GetResultOrDefault()); tcs = new TaskCompletionSource(); tcs.SetException(new InvalidOperationException("Test")); Assert.AreEqual(null, tcs.Task.GetResultOrDefault()); tcs = new TaskCompletionSource(); tcs.SetResult("Hello world"); Assert.AreEqual("Hello world", tcs.Task.GetResultOrDefault()); } // Creates a Task of a given type which is actually an instance of // System.Runtime.CompilerServices.AsyncTaskMethodBuilder.AsyncStateMachineBox. // See https://source.dot.net/#System.Private.CoreLib/AsyncTaskMethodBuilderT.cs,f8f35fd356112b30. // This is needed to verify that the extension also works when the input Task is of a derived type. private static async Task GetTaskFromAsyncMethodBuilder(T? result, TaskCompletionSource tcs) { _ = await tcs.Task; return result; } }