Skip to content

Commit b861efc

Browse files
committed
Add unit tests for CanExecute on generated properties
1 parent 7e0bf75 commit b861efc

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

tests/CommunityToolkit.Mvvm.UnitTests/Test_ICommandAttribute.cs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Threading;
88
using System.Threading.Tasks;
9+
using CommunityToolkit.Mvvm.ComponentModel;
910
using CommunityToolkit.Mvvm.Input;
1011
using Microsoft.VisualStudio.TestTools.UnitTesting;
1112

@@ -76,6 +77,24 @@ public void Test_ICommandAttribute_CanExecute_NoParameters_Property()
7677
Assert.AreEqual(model.Counter, 1);
7778
}
7879

80+
[TestMethod]
81+
public void Test_ICommandAttribute_CanExecute_NoParameters_GeneratedProperty()
82+
{
83+
CanExecuteViewModel model = new();
84+
85+
model.SetGeneratedFlag(true);
86+
87+
model.IncrementCounter_NoParameters_GeneratedPropertyCommand.Execute(null);
88+
89+
Assert.AreEqual(model.Counter, 1);
90+
91+
model.SetGeneratedFlag(false);
92+
93+
model.IncrementCounter_NoParameters_GeneratedPropertyCommand.Execute(null);
94+
95+
Assert.AreEqual(model.Counter, 1);
96+
}
97+
7998
[TestMethod]
8099
public void Test_ICommandAttribute_CanExecute_WithParameter_Property()
81100
{
@@ -94,6 +113,24 @@ public void Test_ICommandAttribute_CanExecute_WithParameter_Property()
94113
Assert.AreEqual(model.Counter, 1);
95114
}
96115

116+
[TestMethod]
117+
public void Test_ICommandAttribute_CanExecute_WithParameter_GeneratedProperty()
118+
{
119+
CanExecuteViewModel model = new();
120+
121+
model.SetGeneratedFlag(true);
122+
123+
model.IncrementCounter_WithParameter_GeneratedPropertyCommand.Execute(null);
124+
125+
Assert.AreEqual(model.Counter, 1);
126+
127+
model.SetGeneratedFlag(false);
128+
129+
model.IncrementCounter_WithParameter_GeneratedPropertyCommand.Execute(null);
130+
131+
Assert.AreEqual(model.Counter, 1);
132+
}
133+
97134
[TestMethod]
98135
public void Test_ICommandAttribute_CanExecute_NoParameters_MethodWithNoParameters()
99136
{
@@ -384,12 +421,20 @@ private async Task AwaitForInputTaskAsync(Task task)
384421
}
385422
}
386423

387-
public sealed partial class CanExecuteViewModel
424+
public sealed partial class CanExecuteViewModel : ObservableObject
388425
{
389426
public int Counter { get; private set; }
390427

391428
public bool Flag { get; set; }
392429

430+
public void SetGeneratedFlag(bool flag)
431+
{
432+
GeneratedFlag = flag;
433+
}
434+
435+
[ObservableProperty]
436+
private bool generatedFlag;
437+
393438
private bool GetFlag1() => Flag;
394439

395440
private bool GetFlag2(User user) => user.Name == nameof(CanExecuteViewModel);
@@ -406,6 +451,18 @@ private void IncrementCounter_WithParameter_Property(User user)
406451
Counter++;
407452
}
408453

454+
[ICommand(CanExecute = nameof(GeneratedFlag))]
455+
private void IncrementCounter_NoParameters_GeneratedProperty()
456+
{
457+
Counter++;
458+
}
459+
460+
[ICommand(CanExecute = nameof(GeneratedFlag))]
461+
private void IncrementCounter_WithParameter_GeneratedProperty(User user)
462+
{
463+
Counter++;
464+
}
465+
409466
[ICommand(CanExecute = nameof(GetFlag1))]
410467
private void IncrementCounter_NoParameters_MethodWithNoParameters()
411468
{
@@ -440,6 +497,22 @@ private async Task IncrementCounter_Async_WithParameter_Property(User user)
440497
await Task.Delay(100);
441498
}
442499

500+
[ICommand(CanExecute = nameof(GeneratedFlag))]
501+
private async Task IncrementCounter_Async_NoParameters_GeneratedProperty()
502+
{
503+
Counter++;
504+
505+
await Task.Delay(100);
506+
}
507+
508+
[ICommand(CanExecute = nameof(GeneratedFlag))]
509+
private async Task IncrementCounter_Async_WithParameter_GeneratedProperty(User user)
510+
{
511+
Counter++;
512+
513+
await Task.Delay(100);
514+
}
515+
443516
[ICommand(CanExecute = nameof(GetFlag1))]
444517
private async Task IncrementCounter_Async_NoParameters_MethodWithNoParameters()
445518
{

0 commit comments

Comments
 (0)