-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathCommandHandlerTests.cs
617 lines (489 loc) · 20.4 KB
/
CommandHandlerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.CommandLine.Binding;
using System.CommandLine.Builder;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
using System.CommandLine.Parsing;
using System.IO;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;
namespace System.CommandLine.Tests.Invocation
{
public class CommandHandlerTests
{
private readonly TestConsole _console = new();
[Fact]
public async Task Specific_invocation_behavior_can_be_specified_in_the_command()
{
var wasCalled = false;
var command = new Command("command");
command.Handler = CommandHandler.Create(() => wasCalled = true);
var parser = new Parser(command);
await parser.InvokeAsync("command", _console);
wasCalled.Should().BeTrue();
}
[Fact]
public async Task Method_parameters_on_the_invoked_method_are_bound_to_matching_option_names()
{
string boundName = default;
int boundAge = default;
void Execute(string name, int age)
{
boundName = name;
boundAge = age;
}
var command = new Command("command");
command.AddOption(new Option<string>("--name"));
command.AddOption(new Option<int>("--age"));
command.Handler = CommandHandler.Create<string, int>(Execute);
await command.InvokeAsync("command --age 425 --name Gandalf", _console);
boundName.Should().Be("Gandalf");
boundAge.Should().Be(425);
}
[Fact]
public async Task Method_parameters_on_the_invoked_method_are_bound_to_matching_option_aliases()
{
string boundName = default;
int boundAge = default;
void Execute(string n, int a)
{
boundName = n;
boundAge = a;
}
var command = new Command("command");
command.AddOption(new Option<string>("--name"));
command.AddOption(new Option<string>("--age"));
command.Handler = CommandHandler.Create<string, int>(Execute);
await command.InvokeAsync("command --age 425 --name Gandalf", _console);
boundName.Should().Be("Gandalf");
boundAge.Should().Be(425);
}
[Fact]
public async Task Method_parameters_on_the_invoked_method_can_be_bound_to_hyphenated_option_names()
{
string boundFirstName = default;
void Execute(string firstName)
{
boundFirstName = firstName;
}
var command = new Command("command")
{
new Option("--first-name", arity: ArgumentArity.ExactlyOne)
};
command.Handler = CommandHandler.Create<string>(Execute);
await command.InvokeAsync("command --first-name Gandalf", _console);
boundFirstName.Should().Be("Gandalf");
}
[Fact]
public async Task Method_parameters_on_the_invoked_method_can_be_bound_to_option_names_case_insensitively()
{
string boundName = default;
int boundAge = default;
void Execute(string name, int AGE)
{
boundName = name;
boundAge = AGE;
}
var command = new Command("command");
command.AddOption(new Option("--NAME", arity: ArgumentArity.ExactlyOne));
command.AddOption(new Option<int>("--age"));
command.Handler = CommandHandler.Create<string, int>(Execute);
await command.InvokeAsync("command --age 425 --NAME Gandalf", _console);
boundName.Should().Be("Gandalf");
boundAge.Should().Be(425);
}
[Fact]
public async Task Method_is_invoked_when_command_line_does_not_specify_matching_options()
{
string boundName = default;
int boundAge = default;
void Execute(string name, int age)
{
boundName = name;
boundAge = age;
}
var command = new Command("command")
{
new Option<string>("--name"),
new Option<int>("--age")
};
command.Handler = CommandHandler.Create<string, int>(Execute);
await command.InvokeAsync("command", _console);
boundName.Should().Be("");
boundAge.Should().Be(0);
}
[Fact]
public async Task Method_parameters_on_the_invoked_method_can_be_bound_to_option_names_by_alias()
{
string boundName = default;
int boundAge = default;
void Execute(string name, int age)
{
boundName = name;
boundAge = age;
}
var command = new Command("command")
{
new Option<string>(new[] { "-n", "--NAME" }),
new Option<int>(new[] { "-a", "--age" })
};
command.Handler = CommandHandler.Create<string, int>(Execute);
await command.InvokeAsync("command -a 425 -n Gandalf", _console);
boundName.Should().Be("Gandalf");
boundAge.Should().Be(425);
}
[Fact]
public async Task Method_parameters_on_the_invoked_lambda_are_bound_to_matching_option_names()
{
string boundName = default;
int boundAge = default;
var command = new Command("command")
{
new Option<string>("--name"),
new Option<int>("--age")
};
command.Handler = CommandHandler.Create<string, int>((name, age) =>
{
boundName = name;
boundAge = age;
});
await command.InvokeAsync("command --age 425 --name Gandalf", _console);
boundName.Should().Be("Gandalf");
boundAge.Should().Be(425);
}
[Fact]
public async Task Nullable_parameters_are_bound_to_correct_value_when_option_is_specified()
{
int? boundAge = default;
var command = new Command("command")
{
new Option<int?>("--age")
};
command.Handler = CommandHandler.Create<int?>(age =>
{
boundAge = age;
});
await command.InvokeAsync("command --age 425", _console);
boundAge.Should().Be(425);
}
[Fact]
public async Task Nullable_parameters_are_bound_to_null_when_option_is_not_specified()
{
var wasCalled = false;
int? boundAge = default;
var command = new Command("command")
{
new Option<int?>("--age")
};
command.Handler = CommandHandler.Create<int?>(age =>
{
wasCalled = true;
boundAge = age;
});
await command.InvokeAsync("command", _console);
wasCalled.Should().BeTrue();
boundAge.Should().BeNull();
}
[Fact]
public async Task Method_parameters_of_types_having_constructors_accepting_a_single_string_are_bound_using_handler_parameter_name()
{
DirectoryInfo boundDirectoryInfo = default;
var tempPath = Path.GetTempPath();
var command = new Command("command")
{
new Option<DirectoryInfo>("--dir")
};
command.Handler = CommandHandler.Create<DirectoryInfo>(dir =>
{
boundDirectoryInfo = dir;
});
await command.InvokeAsync($"command --dir \"{tempPath}\"", _console);
boundDirectoryInfo.FullName.Should().Be(tempPath);
}
[Fact]
public async Task Method_parameters_of_type_ParseResult_receive_the_current_ParseResult_instance()
{
ParseResult boundParseResult = default;
var option = new Option<int>("-x");
var command = new Command("command")
{
option
};
command.Handler = CommandHandler.Create<ParseResult>(result => { boundParseResult = result; });
await command.InvokeAsync("command -x 123", _console);
boundParseResult.ValueForOption(option).Should().Be(123);
}
[Fact]
public async Task Method_parameters_of_type_ParseResult_receive_the_current_BindingContext_instance()
{
BindingContext boundContext = default;
var option = new Option<int>("-x");
var command = new Command("command")
{
option
};
command.Handler = CommandHandler.Create<BindingContext>(context => { boundContext = context; });
await command.InvokeAsync("command -x 123", _console);
boundContext.ParseResult.ValueForOption(option).Should().Be(123);
}
[Fact]
public async Task Method_parameters_of_type_IConsole_receive_the_current_console_instance()
{
var command = new Command("command")
{
new Option<int>("-x")
};
command.Handler = CommandHandler.Create<IConsole>(console => { console.Out.Write("Hello!"); });
await command.InvokeAsync("command", _console);
_console.Out.ToString().Should().Be("Hello!");
}
[Fact]
public async Task Method_parameters_of_type_InvocationContext_receive_the_current_InvocationContext_instance()
{
InvocationContext boundContext = default;
var option = new Option<int>("-x");
var command = new Command("command")
{
option
};
command.Handler = CommandHandler.Create<InvocationContext>(context => { boundContext = context; });
await command.InvokeAsync("command -x 123", _console);
boundContext.ParseResult.ValueForOption(option).Should().Be(123);
}
private class ExecuteTestClass
{
public string boundName = default;
public int boundAge = default;
public void Execute(string name, int age)
{
boundName = name;
boundAge = age;
}
}
private delegate void ExecuteTestDelegate(string name, int age);
[Fact]
public async Task Method_parameters_on_the_invoked_member_method_are_bound_to_matching_option_names_by_delegate()
{
var testClass = new ExecuteTestClass();
var command = new Command("command")
{
new Option<string>("--name"),
new Option<int>("--age")
};
command.Handler = CommandHandler.Create((ExecuteTestDelegate)testClass.Execute);
await command.InvokeAsync("command --age 425 --name Gandalf", _console);
testClass.boundName.Should().Be("Gandalf");
testClass.boundAge.Should().Be(425);
}
[Fact]
public async Task Method_parameters_on_the_invoked_member_method_are_bound_to_matching_option_names_by_MethodInfo_with_target()
{
var testClass = new ExecuteTestClass();
var command = new Command("command")
{
new Option<string>("--name"),
new Option<int>("--age")
};
command.Handler = CommandHandler.Create(
testClass.GetType().GetMethod(nameof(ExecuteTestClass.Execute)),
testClass);
await command.InvokeAsync("command --age 425 --name Gandalf", _console);
testClass.boundName.Should().Be("Gandalf");
testClass.boundAge.Should().Be(425);
}
[Fact]
public async Task Method_parameters_on_the_invoked_method_are_bound_to_matching_argument_names()
{
string boundName = default;
int boundAge = default;
void Execute(string name, int age)
{
boundName = name;
boundAge = age;
}
var command = new Command("command");
command.AddArgument(new Argument<int>("age"));
command.AddArgument(new Argument<string>("name"));
command.Handler = CommandHandler.Create<string, int>(Execute);
await command.InvokeAsync("command 425 Gandalf", _console);
boundName.Should().Be("Gandalf");
boundAge.Should().Be(425);
}
[Fact]
public async Task Method_parameters_on_the_invoked_method_can_be_bound_to_hyphenated_argument_names()
{
string boundFirstName = default;
void Execute(string firstName)
{
boundFirstName = firstName;
}
var command = new Command("command")
{
new Argument<string>("first-name")
};
command.Handler = CommandHandler.Create<string>(Execute);
await command.InvokeAsync("command Gandalf", _console);
boundFirstName.Should().Be("Gandalf");
}
[Fact]
public async Task Method_parameters_on_the_invoked_method_can_be_bound_to_argument_names_case_insensitively()
{
string boundName = default;
int boundAge = default;
void Execute(string name, int AGE)
{
boundName = name;
boundAge = AGE;
}
var command = new Command("command");
command.AddArgument(new Argument<int>("AGE"));
command.AddArgument(new Argument<string>("Name"));
command.Handler = CommandHandler.Create<string, int>(Execute);
await command.InvokeAsync("command 425 Gandalf", _console);
boundName.Should().Be("Gandalf");
boundAge.Should().Be(425);
}
[Fact]
public async Task Method_parameters_on_the_invoked_method_are_bound_to_matching_argument_names_with_pipe_in()
{
string boundName = default;
int boundAge = default;
void Execute(string fullnameOrNickname, int age)
{
boundName = fullnameOrNickname;
boundAge = age;
}
var command = new Command("command");
command.AddArgument(new Argument<int>("age"));
command.AddArgument(new Argument<string>("fullname|nickname"));
command.Handler = CommandHandler.Create<string, int>(Execute);
await command.InvokeAsync("command 425 Gandalf", _console);
boundName.Should().Be("Gandalf");
boundAge.Should().Be(425);
}
[Theory]
[InlineData(typeof(ConcreteTestCommandHandler), 42)]
[InlineData(typeof(VirtualTestCommandHandler), 42)]
[InlineData(typeof(OverridenVirtualTestCommandHandler), 41)]
public async Task Method_invoked_is_matching_to_the_interface_implementation(Type type, int expectedResult)
{
var command = new Command("command");
command.Handler = CommandHandler.Create(type.GetMethod(nameof(ICommandHandler.InvokeAsync)));
var parser = new Parser(command);
int result = await parser.InvokeAsync("command", _console);
result.Should().Be(expectedResult);
}
public abstract class AbstractTestCommandHandler : ICommandHandler
{
public abstract Task<int> DoJobAsync();
public Task<int> InvokeAsync(InvocationContext context)
=> DoJobAsync();
}
public sealed class ConcreteTestCommandHandler : AbstractTestCommandHandler
{
public override Task<int> DoJobAsync()
=> Task.FromResult(42);
}
public class VirtualTestCommandHandler : ICommandHandler
{
public virtual Task<int> InvokeAsync(InvocationContext context)
=> Task.FromResult(42);
}
public class OverridenVirtualTestCommandHandler : VirtualTestCommandHandler
{
public override Task<int> InvokeAsync(InvocationContext context)
=> Task.FromResult(41);
}
[Fact]
public static void FromBindingContext_forwards_invocation_to_bound_handler_type()
{
var command = new RootCommand
{
Handler = CommandHandler.FromBindingContext<BindingContextResolvedCommandHandler>()
};
var parser = new CommandLineBuilder(command)
.ConfigureBindingContext(context => context.AddService<BindingContextResolvedCommandHandler>())
.Build();
var console = new TestConsole();
parser.Invoke(Array.Empty<string>(), console);
console.Out.ToString().Should().Be(typeof(BindingContextResolvedCommandHandler).FullName);
}
[Fact]
public static void FromBindingContext_returns_a_wrapper_type_instance()
{
ICommandHandler handler = CommandHandler.FromBindingContext<BindingContextResolvedCommandHandler>();
handler.Should().NotBeOfType<BindingContextResolvedCommandHandler>();
}
[Fact]
public static void Subsequent_call_to_configure_overrides_service_registration()
{
ICommandHandler invokedHandler = null;
BindingContextCommandHandlerAction action = (handler, Console) =>
{
invokedHandler = handler;
};
var parser = new CommandLineBuilder(new RootCommand
{
Handler = CommandHandler.FromBindingContext<IBindingContextCommandHandlerInterface>()
})
.ConfigureBindingContext(context => context.AddService(_ => action))
.ConfigureBindingContext(context => context.AddService<IBindingContextCommandHandlerInterface, BindingContextCommandHandler1>())
.ConfigureBindingContext(context => context.AddService<IBindingContextCommandHandlerInterface, BindingContextCommandHandler2>())
.Build();
parser.Invoke(Array.Empty<string>(), new TestConsole());
invokedHandler.Should().NotBeNull();
invokedHandler.Should().BeOfType<BindingContextCommandHandler2>();
}
public class BindingContextResolvedCommandHandler : ICommandHandler
{
public BindingContextResolvedCommandHandler(IConsole console)
{
Console = console;
}
public IConsole Console { get; }
public Task<int> InvokeAsync(InvocationContext context)
{
Console.Out.Write(GetType().FullName);
return Task.FromResult(0);
}
}
public interface IBindingContextCommandHandlerInterface : ICommandHandler
{
}
public class BindingContextCommandHandler1 : IBindingContextCommandHandlerInterface
{
private readonly BindingContextCommandHandlerAction invokeAction;
public BindingContextCommandHandler1(IConsole console,
BindingContextCommandHandlerAction invokeAction)
{
Console = console;
this.invokeAction = invokeAction;
}
public IConsole Console { get; }
public Task<int> InvokeAsync(InvocationContext context)
{
invokeAction(this, Console);
return Task.FromResult(0);
}
}
public class BindingContextCommandHandler2 : IBindingContextCommandHandlerInterface
{
private readonly BindingContextCommandHandlerAction invokeAction;
public BindingContextCommandHandler2(IConsole console,
BindingContextCommandHandlerAction invokeAction)
{
Console = console;
this.invokeAction = invokeAction;
}
public IConsole Console { get; }
public Task<int> InvokeAsync(InvocationContext context)
{
invokeAction(this, Console);
return Task.FromResult(0);
}
}
public delegate void BindingContextCommandHandlerAction(ICommandHandler handler, IConsole console);
}
}