9
9
using System . Threading . Tasks ;
10
10
using FluentAssertions ;
11
11
using Xunit ;
12
- using static System . CommandLine . Invocation . CommandHandlerGenerator ;
13
12
14
- #nullable enable
15
13
namespace System . CommandLine . Generator . Tests
16
14
{
17
15
public class CommandHandlerTests
@@ -32,54 +30,58 @@ void Execute(string fullnameOrNickname, IConsole console, int age)
32
30
boundAge = age ;
33
31
}
34
32
35
- var command = new Command ( "command" ) ;
36
33
var nameArgument = new Argument < string > ( ) ;
37
- command . AddArgument ( nameArgument ) ;
38
34
var ageOption = new Option < int > ( "--age" ) ;
39
- command . AddOption ( ageOption ) ;
40
35
41
- command . Handler = GeneratedHandler . Create < Action < string , IConsole , int > >
36
+ var command = new Command ( "command" )
37
+ {
38
+ nameArgument ,
39
+ ageOption
40
+ } ;
41
+
42
+ command . SetHandler < Action < string , IConsole , int > >
42
43
( Execute , nameArgument , ageOption ) ;
43
44
44
45
await command . InvokeAsync ( "command Gandalf --age 425" , _console ) ;
45
46
46
47
boundName . Should ( ) . Be ( "Gandalf" ) ;
47
48
boundAge . Should ( ) . Be ( 425 ) ;
48
49
boundConsole . Should ( ) . NotBeNull ( ) ;
49
- }
50
-
50
+ }
51
+
51
52
[ Fact ]
52
- public async Task Can_generate_handler_for_method_with_model ( )
53
+ public async Task Can_generate_handler_for_void_returning_delegate ( )
53
54
{
54
55
string ? boundName = default ;
55
56
int boundAge = default ;
56
57
IConsole ? boundConsole = null ;
57
58
58
- void Execute ( Character character , IConsole console )
59
- {
60
- boundName = character . FullName ;
61
- boundConsole = console ;
62
- boundAge = character . Age ;
63
- }
64
-
65
- var command = new Command ( "command" ) ;
66
- var nameOption = new Option < string > ( "--name" ) ;
67
- command . AddOption ( nameOption ) ;
59
+ var nameArgument = new Argument < string > ( ) ;
68
60
var ageOption = new Option < int > ( "--age" ) ;
69
- command . AddOption ( ageOption ) ;
70
61
71
- command . Handler = GeneratedHandler . Create < Action < Character , IConsole > >
72
- ( Execute , nameOption , ageOption ) ;
62
+ var command = new Command ( "command" )
63
+ {
64
+ nameArgument ,
65
+ ageOption
66
+ } ;
73
67
74
- await command . InvokeAsync ( "command --age 425 --name Gandalf" , _console ) ;
68
+ command . SetHandler < Action < string , IConsole , int > >
69
+ ( ( fullnameOrNickname , console , age ) =>
70
+ {
71
+ boundName = fullnameOrNickname ;
72
+ boundConsole = console ;
73
+ boundAge = age ;
74
+ } , nameArgument , ageOption ) ;
75
+
76
+ await command . InvokeAsync ( "command Gandalf --age 425" , _console ) ;
75
77
76
78
boundName . Should ( ) . Be ( "Gandalf" ) ;
77
79
boundAge . Should ( ) . Be ( 425 ) ;
78
80
boundConsole . Should ( ) . NotBeNull ( ) ;
79
81
}
80
82
81
83
[ Fact ]
82
- public async Task Can_generate_handler_for_method_with_model_property_binding ( )
84
+ public async Task Can_generate_handler_for_method_with_model ( )
83
85
{
84
86
string ? boundName = default ;
85
87
int boundAge = default ;
@@ -98,12 +100,7 @@ void Execute(Character character, IConsole console)
98
100
var ageOption = new Option < int > ( "--age" ) ;
99
101
command . AddOption ( ageOption ) ;
100
102
101
- command . Handler = GeneratedHandler . Create < Action < Character , IConsole > , Character >
102
- ( Execute , context => new Character
103
- {
104
- FullName = context . ParseResult . ValueForOption ( nameOption ) ,
105
- Age = context . ParseResult . ValueForOption ( ageOption ) ,
106
- } ) ;
103
+ command . SetHandler < Action < Character , IConsole > > ( Execute , nameOption , ageOption ) ;
107
104
108
105
await command . InvokeAsync ( "command --age 425 --name Gandalf" , _console ) ;
109
106
@@ -126,8 +123,7 @@ int Execute(int first, int second)
126
123
var secondArgument = new Argument < int > ( "second" ) ;
127
124
command . AddArgument ( secondArgument ) ;
128
125
129
- command . Handler = GeneratedHandler . Create < Func < int , int , int > >
130
- ( Execute , firstArgument , secondArgument ) ;
126
+ command . SetHandler < Func < int , int , int > > ( Execute , firstArgument , secondArgument ) ;
131
127
132
128
int result = await command . InvokeAsync ( "add 1 2" , _console ) ;
133
129
@@ -145,7 +141,7 @@ public async Task Can_generate_handler_with_well_know_parameters_types()
145
141
146
142
void Execute (
147
143
InvocationContext invocationContext ,
148
- IConsole console ,
144
+ IConsole console ,
149
145
ParseResult parseResult ,
150
146
IHelpBuilder helpBuilder ,
151
147
BindingContext bindingContext )
@@ -159,7 +155,7 @@ void Execute(
159
155
160
156
var command = new Command ( "command" ) ;
161
157
162
- command . Handler = GeneratedHandler . Create < Action < InvocationContext , IConsole , ParseResult , IHelpBuilder , BindingContext > > ( Execute ) ;
158
+ command . SetHandler < Action < InvocationContext , IConsole , ParseResult , IHelpBuilder , BindingContext > > ( Execute ) ;
163
159
164
160
await command . InvokeAsync ( "command" , _console ) ;
165
161
@@ -179,20 +175,22 @@ public async Task Can_generate_handler_for_async_method()
179
175
180
176
async Task ExecuteAsync ( string fullnameOrNickname , IConsole console , int age )
181
177
{
182
- //Just long enough to make sure the taks is be awaited
183
- await Task . Delay ( 100 ) ;
178
+ await Task . Yield ( ) ;
184
179
boundName = fullnameOrNickname ;
185
180
boundConsole = console ;
186
181
boundAge = age ;
187
182
}
188
183
189
- var command = new Command ( "command" ) ;
190
184
var nameArgument = new Argument < string > ( ) ;
191
- command . AddArgument ( nameArgument ) ;
192
185
var ageOption = new Option < int > ( "--age" ) ;
193
- command . AddOption ( ageOption ) ;
194
186
195
- command . Handler = GeneratedHandler . Create < Func < string , IConsole , int , Task > >
187
+ var command = new Command ( "command" )
188
+ {
189
+ nameArgument ,
190
+ ageOption
191
+ } ;
192
+
193
+ command . SetHandler < Func < string , IConsole , int , Task > >
196
194
( ExecuteAsync , nameArgument , ageOption ) ;
197
195
198
196
await command . InvokeAsync ( "command Gandalf --age 425" , _console ) ;
@@ -207,50 +205,52 @@ public async Task Can_generate_handler_for_async_task_of_int_returning_method()
207
205
{
208
206
async Task < int > Execute ( int first , int second )
209
207
{
210
- await Task . Delay ( 100 ) ;
208
+ await Task . Yield ( ) ;
211
209
return first + second ;
212
210
}
213
211
214
- var command = new Command ( "add" ) ;
215
212
var firstArgument = new Argument < int > ( "first" ) ;
216
- command . AddArgument ( firstArgument ) ;
217
213
var secondArgument = new Argument < int > ( "second" ) ;
218
- command . AddArgument ( secondArgument ) ;
214
+ var command = new Command ( "add" )
215
+ {
216
+ firstArgument ,
217
+ secondArgument
218
+ } ;
219
219
220
- command . Handler = GeneratedHandler . Create < Func < int , int , Task < int > > >
220
+ command . SetHandler < Func < int , int , Task < int > > >
221
221
( Execute , firstArgument , secondArgument ) ;
222
222
223
223
int result = await command . InvokeAsync ( "add 1 2" , _console ) ;
224
224
225
225
result . Should ( ) . Be ( 3 ) ;
226
- }
226
+ }
227
227
228
228
[ Fact ]
229
229
public async Task Can_generate_handler_for_multiple_commands_with_the_same_signature ( )
230
230
{
231
231
string firstValue = "" ;
232
+
232
233
void Execute1 ( string value )
233
234
{
234
235
firstValue = value ;
235
236
}
237
+
236
238
string secondValue = "" ;
239
+
237
240
void Execute2 ( string value )
238
241
{
239
242
secondValue = value ;
240
243
}
241
244
242
-
243
245
var command1 = new Command ( "first" ) ;
244
246
var argument1 = new Argument < string > ( "first-value" ) ;
245
247
command1 . AddArgument ( argument1 ) ;
246
- command1 . Handler = GeneratedHandler . Create < Action < string > >
247
- ( Execute1 , argument1 ) ;
248
+ command1 . SetHandler < Action < string > > ( Execute1 , argument1 ) ;
248
249
249
250
var command2 = new Command ( "second" ) ;
250
251
var argument2 = new Argument < string > ( "second-value" ) ;
251
252
command2 . AddArgument ( argument2 ) ;
252
- command2 . Handler = GeneratedHandler . Create < Action < string > >
253
- ( Execute2 , argument2 ) ;
253
+ command2 . SetHandler < Action < string > > ( Execute2 , argument2 ) ;
254
254
255
255
await command1 . InvokeAsync ( "first v1" , _console ) ;
256
256
await command2 . InvokeAsync ( "second v2" , _console ) ;
@@ -268,12 +268,11 @@ public Character(string? fullName, int age)
268
268
}
269
269
270
270
public Character ( )
271
- { }
271
+ {
272
+ }
272
273
273
274
public string ? FullName { get ; set ; }
274
275
public int Age { get ; set ; }
275
276
}
276
-
277
277
}
278
278
}
279
- #nullable restore
0 commit comments