Skip to content

Commit 923d479

Browse files
committed
Add unit tests for exception flow generator option
1 parent 0d7ec7a commit 923d479

File tree

2 files changed

+116
-8
lines changed

2 files changed

+116
-8
lines changed

tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests/Test_SourceGeneratorsDiagnostics.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public partial class SampleViewModel
213213
}
214214

215215
[TestMethod]
216-
public void InvalidICommandMethodSignatureError()
216+
public void InvalidRelayCommandMethodSignatureError()
217217
{
218218
string source = @"
219219
using CommunityToolkit.Mvvm.Input;
@@ -310,7 +310,7 @@ public partial class SampleViewModel : ObservableValidator
310310
}
311311

312312
[TestMethod]
313-
public void UnsupportedCSharpLanguageVersion_FromICommandGenerator()
313+
public void UnsupportedCSharpLanguageVersion_FromRelayCommandGenerator()
314314
{
315315
string source = @"
316316
using CommunityToolkit.Mvvm.Input;
@@ -535,7 +535,7 @@ private void GreetUser(string name)
535535
}
536536

537537
[TestMethod]
538-
public void InvalidICommandAllowConcurrentExecutionsSettings()
538+
public void InvalidRelayCommandAllowConcurrentExecutionsOption()
539539
{
540540
string source = @"
541541
using CommunityToolkit.Mvvm.Input;
@@ -555,7 +555,7 @@ private void GreetUser(User user)
555555
}
556556

557557
[TestMethod]
558-
public void InvalidICommandIncludeCancelCommandSettings_SynchronousMethod()
558+
public void InvalidRelayCommandIncludeCancelCommandSettings_SynchronousMethod()
559559
{
560560
string source = @"
561561
using CommunityToolkit.Mvvm.Input;
@@ -575,7 +575,7 @@ private void GreetUser(User user)
575575
}
576576

577577
[TestMethod]
578-
public void InvalidICommandIncludeCancelCommandSettings_AsynchronousMethodWithNoCancellationToken()
578+
public void InvalidRelayCommandIncludeCancelCommandSettings_AsynchronousMethodWithNoCancellationToken()
579579
{
580580
string source = @"
581581
using System.Threading.Tasks;
@@ -596,7 +596,7 @@ private async Task DoWorkAsync()
596596
}
597597

598598
[TestMethod]
599-
public void InvalidICommandIncludeCancelCommandSettings_AsynchronousMethodWithParameterAndNoCancellationToken()
599+
public void InvalidRelayCommandIncludeCancelCommandSettings_AsynchronousMethodWithParameterAndNoCancellationToken()
600600
{
601601
string source = @"
602602
using System.Threading.Tasks;
@@ -1081,7 +1081,7 @@ public partial class MyViewModel : ObservableObject
10811081
}
10821082

10831083
[TestMethod]
1084-
public void MultipleICommandMethodOverloads_WithOverloads()
1084+
public void MultipleRelayCommandMethodOverloads_WithOverloads()
10851085
{
10861086
string source = @"
10871087
using CommunityToolkit.Mvvm.Input;
@@ -1106,7 +1106,7 @@ private void GreetUser(object value)
11061106
}
11071107

11081108
[TestMethod]
1109-
public void MultipleICommandMethodOverloads_WithOverloadInBaseType()
1109+
public void MultipleRelayCommandMethodOverloads_WithOverloadInBaseType()
11101110
{
11111111
string source = @"
11121112
using CommunityToolkit.Mvvm.Input;
@@ -1390,6 +1390,26 @@ public partial class MyViewModel : MyBaseViewModel
13901390
VerifyGeneratedDiagnostics<ObservablePropertyGenerator>(source, "MVVMTK0030");
13911391
}
13921392

1393+
[TestMethod]
1394+
public void InvalidRelayCommandFlowExceptionsToTaskSchedulerOption()
1395+
{
1396+
string source = @"
1397+
using CommunityToolkit.Mvvm.Input;
1398+
1399+
namespace MyApp
1400+
{
1401+
public partial class SampleViewModel
1402+
{
1403+
[RelayCommand(FlowExceptionsToTaskScheduler = false)]
1404+
private void GreetUser(User user)
1405+
{
1406+
}
1407+
}
1408+
}";
1409+
1410+
VerifyGeneratedDiagnostics<RelayCommandGenerator>(source, "MVVMTK0031");
1411+
}
1412+
13931413
/// <summary>
13941414
/// Verifies the output of a source generator.
13951415
/// </summary>

tests/CommunityToolkit.Mvvm.UnitTests/Test_RelayCommandAttribute.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8+
using System.Reflection;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using CommunityToolkit.Mvvm.ComponentModel;
@@ -517,6 +518,42 @@ public void Test_RelayCommandAttribute_VerifyNoWarningsForNullableValues()
517518
model.TupleWithNullableElementsCommand.Execute((null, null, null, null));
518519
}
519520

521+
[TestMethod]
522+
public void Test_RelayCommandAttribute_VerifyOptions()
523+
{
524+
ModelWithCommandsWithCustomOptions model = new();
525+
526+
static void AssertOptions(IAsyncRelayCommand command, AsyncRelayCommandOptions options)
527+
{
528+
AsyncRelayCommandOptions commandOptions =
529+
(AsyncRelayCommandOptions)typeof(AsyncRelayCommand)
530+
.GetField("options", BindingFlags.Instance | BindingFlags.NonPublic)!
531+
.GetValue(command)!;
532+
533+
Assert.AreEqual(commandOptions, options);
534+
}
535+
536+
static void AssertOptionsOfT<T>(IAsyncRelayCommand<T> command, AsyncRelayCommandOptions options)
537+
{
538+
AsyncRelayCommandOptions commandOptions =
539+
(AsyncRelayCommandOptions)typeof(AsyncRelayCommand<T>)
540+
.GetField("options", BindingFlags.Instance | BindingFlags.NonPublic)!
541+
.GetValue(command)!;
542+
543+
Assert.AreEqual(commandOptions, options);
544+
}
545+
546+
AssertOptions(model.DefaultCommand, AsyncRelayCommandOptions.None);
547+
AssertOptions(model.AllowConcurrentExecutionsCommand, AsyncRelayCommandOptions.AllowConcurrentExecutions);
548+
AssertOptions(model.FlowExceptionsToTaskSchedulerCommand, AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler);
549+
AssertOptions(model.AllowConcurrentExecutionsAndFlowExceptionsToTaskSchedulerCommand, AsyncRelayCommandOptions.AllowConcurrentExecutions | AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler);
550+
551+
AssertOptionsOfT(model.OfTDefaultCommand, AsyncRelayCommandOptions.None);
552+
AssertOptionsOfT(model.OfTAndAllowConcurrentExecutionsCommand, AsyncRelayCommandOptions.AllowConcurrentExecutions);
553+
AssertOptionsOfT(model.OfTAndFlowExceptionsToTaskSchedulerCommand, AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler);
554+
AssertOptionsOfT(model.OfTAndAllowConcurrentExecutionsAndFlowExceptionsToTaskSchedulerCommand, AsyncRelayCommandOptions.AllowConcurrentExecutions | AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler);
555+
}
556+
520557
#region Region
521558
public class Region
522559
{
@@ -906,4 +943,55 @@ private void TupleWithNullableElements((DateTime? date, string? message, bool? s
906943
{
907944
}
908945
}
946+
947+
partial class ModelWithCommandsWithCustomOptions
948+
{
949+
[RelayCommand]
950+
private Task Default()
951+
{
952+
return Task.CompletedTask;
953+
}
954+
955+
[RelayCommand]
956+
private Task OfTDefault(string obj)
957+
{
958+
return Task.CompletedTask;
959+
}
960+
961+
[RelayCommand(AllowConcurrentExecutions = true)]
962+
private Task AllowConcurrentExecutions()
963+
{
964+
return Task.CompletedTask;
965+
}
966+
967+
[RelayCommand(AllowConcurrentExecutions = true)]
968+
private Task OfTAndAllowConcurrentExecutions(string obj)
969+
{
970+
return Task.CompletedTask;
971+
}
972+
973+
[RelayCommand(FlowExceptionsToTaskScheduler = true)]
974+
private Task FlowExceptionsToTaskScheduler()
975+
{
976+
return Task.CompletedTask;
977+
}
978+
979+
[RelayCommand(FlowExceptionsToTaskScheduler = true)]
980+
private Task OfTAndFlowExceptionsToTaskScheduler(string obj)
981+
{
982+
return Task.CompletedTask;
983+
}
984+
985+
[RelayCommand(AllowConcurrentExecutions = true, FlowExceptionsToTaskScheduler = true)]
986+
private Task AllowConcurrentExecutionsAndFlowExceptionsToTaskScheduler()
987+
{
988+
return Task.CompletedTask;
989+
}
990+
991+
[RelayCommand(AllowConcurrentExecutions = true, FlowExceptionsToTaskScheduler = true)]
992+
private Task OfTAndAllowConcurrentExecutionsAndFlowExceptionsToTaskScheduler(string obj)
993+
{
994+
return Task.CompletedTask;
995+
}
996+
}
909997
}

0 commit comments

Comments
 (0)