mirror of
https://github.com/chylex/.NET-Community-Toolkit.git
synced 2025-05-31 17:34:03 +02:00
Add more unit tests for synchronous exceptions
This commit is contained in:
parent
923d479bbf
commit
fdda4c6bb0
tests/CommunityToolkit.Mvvm.UnitTests
@ -321,6 +321,35 @@ private static async Task Test_AsyncRelayCommand_AllowConcurrentExecutions_TestL
|
||||
Assert.AreSame(args.Item2, EventArgs.Empty);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Test_AsyncRelayCommand_EnsureExceptionThrown_Synchronously()
|
||||
{
|
||||
Exception? executeException = null;
|
||||
|
||||
AsyncRelayCommand command = new(async () =>
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
|
||||
throw new Exception(nameof(Test_AsyncRelayCommand_EnsureExceptionThrown_Synchronously));
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
AsyncContext.Run(async () =>
|
||||
{
|
||||
command.Execute(null);
|
||||
|
||||
await Task.Delay(500);
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
executeException = e;
|
||||
}
|
||||
|
||||
Assert.AreEqual(nameof(Test_AsyncRelayCommand_EnsureExceptionThrown_Synchronously), executeException?.Message);
|
||||
}
|
||||
|
||||
// See https://github.com/CommunityToolkit/dotnet/pull/251
|
||||
[TestMethod]
|
||||
public async Task Test_AsyncRelayCommand_EnsureExceptionThrown()
|
||||
@ -384,6 +413,32 @@ async void TestCallback(Action throwAction, Action completeAction)
|
||||
Assert.IsTrue(success);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Test_AsyncRelayCommand_ThrowingTaskBubblesToUnobservedTaskException_Synchronously()
|
||||
{
|
||||
static async Task TestMethodAsync(Action action)
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
|
||||
action();
|
||||
}
|
||||
|
||||
async void TestCallback(Action throwAction, Action completeAction)
|
||||
{
|
||||
AsyncRelayCommand command = new(() => TestMethodAsync(throwAction), AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler);
|
||||
|
||||
command.Execute(null);
|
||||
|
||||
await Task.Delay(200);
|
||||
|
||||
completeAction();
|
||||
}
|
||||
|
||||
bool success = await TaskSchedulerTestHelper.IsExceptionBubbledUpToUnobservedTaskExceptionAsync(TestCallback);
|
||||
|
||||
Assert.IsTrue(success);
|
||||
}
|
||||
|
||||
// See https://github.com/CommunityToolkit/dotnet/issues/108
|
||||
[TestMethod]
|
||||
public void Test_AsyncRelayCommand_ExecuteDoesNotRaiseCanExecuteChanged()
|
||||
|
@ -242,6 +242,35 @@ private static async Task Test_AsyncRelayCommandOfT_AllowConcurrentExecutions_Te
|
||||
_ = Assert.ThrowsException<InvalidCastException>(() => command.Execute(new object()));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Test_AsyncRelayCommandOfT_EnsureExceptionThrown_Synchronously()
|
||||
{
|
||||
Exception? executeException = null;
|
||||
|
||||
AsyncRelayCommand<int> command = new(async delay =>
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
|
||||
throw new Exception(nameof(Test_AsyncRelayCommandOfT_EnsureExceptionThrown_Synchronously));
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
AsyncContext.Run(async () =>
|
||||
{
|
||||
command.Execute((object)42);
|
||||
|
||||
await Task.Delay(500);
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
executeException = e;
|
||||
}
|
||||
|
||||
Assert.AreEqual(nameof(Test_AsyncRelayCommandOfT_EnsureExceptionThrown_Synchronously), executeException?.Message);
|
||||
}
|
||||
|
||||
// See https://github.com/CommunityToolkit/dotnet/pull/251
|
||||
[TestMethod]
|
||||
public async Task Test_AsyncRelayCommandOfT_EnsureExceptionThrown()
|
||||
@ -249,7 +278,6 @@ public async Task Test_AsyncRelayCommandOfT_EnsureExceptionThrown()
|
||||
const int delay = 500;
|
||||
|
||||
Exception? executeException = null;
|
||||
Exception? executeTException = null;
|
||||
Exception? executeAsyncException = null;
|
||||
|
||||
AsyncRelayCommand<int> command = new(async delay =>
|
||||
@ -273,6 +301,27 @@ public async Task Test_AsyncRelayCommandOfT_EnsureExceptionThrown()
|
||||
executeException = e;
|
||||
}
|
||||
|
||||
executeAsyncException = await Assert.ThrowsExceptionAsync<Exception>(() => command.ExecuteAsync((object)delay));
|
||||
|
||||
Assert.AreEqual(nameof(Test_AsyncRelayCommandOfT_EnsureExceptionThrown), executeException?.Message);
|
||||
Assert.AreEqual(nameof(Test_AsyncRelayCommandOfT_EnsureExceptionThrown), executeAsyncException?.Message);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Test_AsyncRelayCommandOfT_EnsureExceptionThrown_GenericExecute()
|
||||
{
|
||||
const int delay = 500;
|
||||
|
||||
Exception? executeTException = null;
|
||||
Exception? executeAsyncException = null;
|
||||
|
||||
AsyncRelayCommand<int> command = new(async delay =>
|
||||
{
|
||||
await Task.Delay(delay);
|
||||
|
||||
throw new Exception(nameof(Test_AsyncRelayCommandOfT_EnsureExceptionThrown_GenericExecute));
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
AsyncContext.Run(async () =>
|
||||
@ -289,9 +338,8 @@ public async Task Test_AsyncRelayCommandOfT_EnsureExceptionThrown()
|
||||
|
||||
executeAsyncException = await Assert.ThrowsExceptionAsync<Exception>(() => command.ExecuteAsync(delay));
|
||||
|
||||
Assert.AreEqual(nameof(Test_AsyncRelayCommandOfT_EnsureExceptionThrown), executeException?.Message);
|
||||
Assert.AreEqual(nameof(Test_AsyncRelayCommandOfT_EnsureExceptionThrown), executeTException?.Message);
|
||||
Assert.AreEqual(nameof(Test_AsyncRelayCommandOfT_EnsureExceptionThrown), executeAsyncException?.Message);
|
||||
Assert.AreEqual(nameof(Test_AsyncRelayCommandOfT_EnsureExceptionThrown_GenericExecute), executeTException?.Message);
|
||||
Assert.AreEqual(nameof(Test_AsyncRelayCommandOfT_EnsureExceptionThrown_GenericExecute), executeAsyncException?.Message);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@ -320,6 +368,32 @@ async void TestCallback(Action throwAction, Action completeAction)
|
||||
Assert.IsTrue(success);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Test_AsyncRelayCommandOfT_ThrowingTaskBubblesToUnobservedTaskException_Synchronously()
|
||||
{
|
||||
static async Task TestMethodAsync(Action action)
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
|
||||
action();
|
||||
}
|
||||
|
||||
async void TestCallback(Action throwAction, Action completeAction)
|
||||
{
|
||||
AsyncRelayCommand<string> command = new(s => TestMethodAsync(throwAction), AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler);
|
||||
|
||||
command.Execute(null);
|
||||
|
||||
await Task.Delay(200);
|
||||
|
||||
completeAction();
|
||||
}
|
||||
|
||||
bool success = await TaskSchedulerTestHelper.IsExceptionBubbledUpToUnobservedTaskExceptionAsync(TestCallback);
|
||||
|
||||
Assert.IsTrue(success);
|
||||
}
|
||||
|
||||
public async Task Test_AsyncRelayCommand_ExecuteDoesNotRaiseCanExecuteChanged()
|
||||
{
|
||||
TaskCompletionSource<object?> tcs = new();
|
||||
|
Loading…
Reference in New Issue
Block a user