6
6
using System . Linq ;
7
7
using System . Threading ;
8
8
using System . Threading . Tasks ;
9
+ using CommunityToolkit . Mvvm . ComponentModel ;
9
10
using CommunityToolkit . Mvvm . Input ;
10
11
using Microsoft . VisualStudio . TestTools . UnitTesting ;
11
12
@@ -76,6 +77,24 @@ public void Test_ICommandAttribute_CanExecute_NoParameters_Property()
76
77
Assert . AreEqual ( model . Counter , 1 ) ;
77
78
}
78
79
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
+
79
98
[ TestMethod ]
80
99
public void Test_ICommandAttribute_CanExecute_WithParameter_Property ( )
81
100
{
@@ -94,6 +113,24 @@ public void Test_ICommandAttribute_CanExecute_WithParameter_Property()
94
113
Assert . AreEqual ( model . Counter , 1 ) ;
95
114
}
96
115
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
+
97
134
[ TestMethod ]
98
135
public void Test_ICommandAttribute_CanExecute_NoParameters_MethodWithNoParameters ( )
99
136
{
@@ -384,12 +421,20 @@ private async Task AwaitForInputTaskAsync(Task task)
384
421
}
385
422
}
386
423
387
- public sealed partial class CanExecuteViewModel
424
+ public sealed partial class CanExecuteViewModel : ObservableObject
388
425
{
389
426
public int Counter { get ; private set ; }
390
427
391
428
public bool Flag { get ; set ; }
392
429
430
+ public void SetGeneratedFlag ( bool flag )
431
+ {
432
+ GeneratedFlag = flag ;
433
+ }
434
+
435
+ [ ObservableProperty ]
436
+ private bool generatedFlag ;
437
+
393
438
private bool GetFlag1 ( ) => Flag ;
394
439
395
440
private bool GetFlag2 ( User user ) => user . Name == nameof ( CanExecuteViewModel ) ;
@@ -406,6 +451,18 @@ private void IncrementCounter_WithParameter_Property(User user)
406
451
Counter ++ ;
407
452
}
408
453
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
+
409
466
[ ICommand ( CanExecute = nameof ( GetFlag1 ) ) ]
410
467
private void IncrementCounter_NoParameters_MethodWithNoParameters ( )
411
468
{
@@ -440,6 +497,22 @@ private async Task IncrementCounter_Async_WithParameter_Property(User user)
440
497
await Task . Delay ( 100 ) ;
441
498
}
442
499
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
+
443
516
[ ICommand ( CanExecute = nameof ( GetFlag1 ) ) ]
444
517
private async Task IncrementCounter_Async_NoParameters_MethodWithNoParameters ( )
445
518
{
0 commit comments