Skip to content

Commit 01e78f5

Browse files
committed
Add unit tests for [RelayCommand] with nullable arguments
1 parent 6920dab commit 01e78f5

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tests/CommunityToolkit.Mvvm.UnitTests/Test_RelayCommandAttribute.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Reflection;
99
using System.Threading;
1010
using System.Threading.Tasks;
11+
using System.Windows.Input;
1112
using CommunityToolkit.Mvvm.ComponentModel;
1213
using CommunityToolkit.Mvvm.Input;
1314
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -554,6 +555,17 @@ static void AssertOptionsOfT<T>(IAsyncRelayCommand<T> command, AsyncRelayCommand
554555
AssertOptionsOfT(model.OfTAndAllowConcurrentExecutionsAndFlowExceptionsToTaskSchedulerCommand, AsyncRelayCommandOptions.AllowConcurrentExecutions | AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler);
555556
}
556557

558+
// See https://github.com/CommunityToolkit/dotnet/issues/294
559+
[TestMethod]
560+
public void Test_RelayCommandAttribute_CanExecuteWithNullabilityAnnotations()
561+
{
562+
ModelWithCommandWithNullableCanExecute model = new();
563+
564+
Assert.IsTrue(model.DoSomething1Command.CanExecute("Hello"));
565+
Assert.IsTrue(model.DoSomething2Command.CanExecute("Hello"));
566+
Assert.IsTrue(model.DoSomething3Command.CanExecute((0, "Hello")));
567+
}
568+
557569
#region Region
558570
public class Region
559571
{
@@ -994,4 +1006,37 @@ private Task OfTAndAllowConcurrentExecutionsAndFlowExceptionsToTaskScheduler(str
9941006
return Task.CompletedTask;
9951007
}
9961008
}
1009+
1010+
partial class ModelWithCommandWithNullableCanExecute
1011+
{
1012+
bool CanDoSomething1(string? parameter)
1013+
{
1014+
return !string.IsNullOrEmpty(parameter);
1015+
}
1016+
1017+
[RelayCommand(CanExecute = (nameof(CanDoSomething1)))]
1018+
private void DoSomething1(string? parameter)
1019+
{
1020+
}
1021+
1022+
bool CanDoSomething2(string parameter)
1023+
{
1024+
return !string.IsNullOrEmpty(parameter);
1025+
}
1026+
1027+
[RelayCommand(CanExecute = (nameof(CanDoSomething2)))]
1028+
private void DoSomething2(string? parameter)
1029+
{
1030+
}
1031+
1032+
bool CanDoSomething3((int A, string? B) parameter)
1033+
{
1034+
return !string.IsNullOrEmpty(parameter.B);
1035+
}
1036+
1037+
[RelayCommand(CanExecute = (nameof(CanDoSomething3)))]
1038+
private void DoSomething3((int A, string? B) parameter)
1039+
{
1040+
}
1041+
}
9971042
}

0 commit comments

Comments
 (0)