|
5 | 5 | using System;
|
6 | 6 | using System.Collections.Generic;
|
7 | 7 | using System.Linq;
|
| 8 | +using System.Reflection; |
8 | 9 | using System.Threading;
|
9 | 10 | using System.Threading.Tasks;
|
10 | 11 | using CommunityToolkit.Mvvm.ComponentModel;
|
@@ -517,6 +518,42 @@ public void Test_RelayCommandAttribute_VerifyNoWarningsForNullableValues()
|
517 | 518 | model.TupleWithNullableElementsCommand.Execute((null, null, null, null));
|
518 | 519 | }
|
519 | 520 |
|
| 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 | + |
520 | 557 | #region Region
|
521 | 558 | public class Region
|
522 | 559 | {
|
@@ -906,4 +943,55 @@ private void TupleWithNullableElements((DateTime? date, string? message, bool? s
|
906 | 943 | {
|
907 | 944 | }
|
908 | 945 | }
|
| 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 | + } |
909 | 997 | }
|
0 commit comments