forked from dotnet/command-line-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgumentTests.cs
More file actions
773 lines (650 loc) · 25.9 KB
/
ArgumentTests.cs
File metadata and controls
773 lines (650 loc) · 25.9 KB
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
// 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.Collections.Generic;
using System.CommandLine.Parsing;
using System.CommandLine.Tests.Utility;
using System.IO;
using FluentAssertions;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace System.CommandLine.Tests
{
public class ArgumentTests : SymbolTests
{
[Fact]
public void By_default_there_is_no_default_value()
{
var argument = new Argument<string>();
argument.HasDefaultValue.Should().BeFalse();
}
[Fact]
public void When_default_value_is_set_to_null_then_HasDefaultValue_is_true()
{
var argument = new Argument<string>();
argument.SetDefaultValue(null);
argument.HasDefaultValue.Should().BeTrue();
}
[Fact]
public void When_default_value_factory_is_set_then_HasDefaultValue_is_true()
{
var argument = new Argument<string[]>();
argument.SetDefaultValueFactory(() => null);
argument.HasDefaultValue.Should().BeTrue();
}
[Fact]
public void When_there_is_no_default_value_then_GetDefaultValue_throws()
{
var argument = new Argument<string>("the-arg");
argument.Invoking(a => a.GetDefaultValue())
.Should()
.Throw<InvalidOperationException>()
.Which
.Message
.Should()
.Be("Argument \"the-arg\" does not have a default value");
}
public class CustomParsing
{
[Fact]
public void HasDefaultValue_can_be_set_to_true()
{
var argument = new Argument<FileSystemInfo>(result => null, true);
argument.HasDefaultValue
.Should()
.BeTrue();
}
[Fact]
public void HasDefaultValue_can_be_set_to_false()
{
var argument = new Argument<FileSystemInfo>(result => null, false);
argument.HasDefaultValue
.Should()
.BeFalse();
}
[Fact]
public void GetDefaultValue_returns_specified_value()
{
var argument = new Argument<string>(result => "the-default", isDefault: true);
argument.GetDefaultValue()
.Should()
.Be("the-default");
}
[Fact]
public void GetDefaultValue_returns_null_when_parse_delegate_returns_true_without_setting_a_value()
{
var argument = new Argument<string>(result => null, isDefault: true);
argument.GetDefaultValue()
.Should()
.BeNull();
}
[Fact]
public void GetDefaultValue_returns_null_when_parse_delegate_returns_true_and_sets_value_to_null()
{
var argument = new Argument<string>(result => null, isDefault: true);
argument.GetDefaultValue()
.Should()
.BeNull();
}
[Fact]
public void GetDefaultValue_can_return_null()
{
var argument = new Argument<string>(result => null, isDefault: true);
argument.GetDefaultValue()
.Should()
.BeNull();
}
[Fact]
public void Validation_failure_message_can_be_specified_when_parsing_tokens()
{
var argument = new Argument<FileSystemInfo>(result =>
{
result.ErrorMessage = "oops!";
return null;
});
argument.Parse("x")
.Errors
.Should()
.ContainSingle(e => e.SymbolResult.Symbol == argument)
.Which
.Message
.Should()
.Be("oops!");
}
[Fact]
public void Validation_failure_message_can_be_specified_when_evaluating_default_argument_value()
{
var argument = new Argument<FileSystemInfo>(result =>
{
result.ErrorMessage = "oops!";
return null;
}, true);
argument.Parse("")
.Errors
.Should()
.ContainSingle(e => e.SymbolResult.Symbol == argument)
.Which
.Message
.Should()
.Be("oops!");
}
[Fact]
public void Validation_failure_message_can_be_specified_when_evaluating_default_option_value()
{
var option = new Option<FileSystemInfo>(
"-x",
result =>
{
result.ErrorMessage = "oops!";
return null;
}, true);
option.Parse("")
.Errors
.Should()
.ContainSingle()
.Which
.Message
.Should()
.Be("oops!");
}
[Fact]
public void custom_parsing_of_scalar_value_from_an_argument_with_one_token()
{
var argument = new Argument<int>(result => int.Parse(result.Tokens.Single().Value));
argument.Parse("123")
.GetValueForArgument(argument)
.Should()
.Be(123);
}
[Fact]
public void custom_parsing_of_sequence_value_from_an_argument_with_one_token()
{
var argument = new Argument<IEnumerable<int>>(result => result.Tokens.Single().Value.Split(',').Select(int.Parse));
argument.Parse("1,2,3")
.GetValueForArgument(argument)
.Should()
.BeEquivalentTo(new[] { 1, 2, 3 });
}
[Fact]
public void custom_parsing_of_sequence_value_from_an_argument_with_multiple_tokens()
{
var argument = new Argument<IEnumerable<int>>(result =>
{
return result.Tokens.Select(t => int.Parse(t.Value)).ToArray();
});
argument.Parse("1 2 3")
.GetValueForArgument(argument)
.Should()
.BeEquivalentTo(new[] { 1, 2, 3 });
}
[Fact]
public void custom_parsing_of_scalar_value_from_an_argument_with_multiple_tokens()
{
var argument = new Argument<int>(result => result.Tokens.Select(t => int.Parse(t.Value)).Sum())
{
Arity = ArgumentArity.ZeroOrMore
};
argument.Parse("1 2 3")
.GetValueForArgument(argument)
.Should()
.Be(6);
}
[Fact]
public void Option_ArgumentResult_Parent_is_set_correctly_when_token_is_implicit()
{
ArgumentResult argumentResult = null;
var command = new Command("the-command")
{
new Option<string>(
"-x",
parseArgument: argResult =>
{
argumentResult = argResult;
return null;
}, isDefault: true)
};
command.Parse("");
argumentResult
.Parent
.Symbol
.Should()
.Be(command.Options.Single());
}
[Fact]
public void Option_ArgumentResult_parentage_to_root_symbol_is_set_correctly_when_token_is_implicit()
{
ArgumentResult argumentResult = null;
var command = new Command("the-command")
{
new Option<string>(
"-x",
parseArgument: argResult =>
{
argumentResult = argResult;
return null;
}, isDefault: true)
};
command.Parse("");
argumentResult
.Parent
.Parent
.Symbol
.Should()
.Be(command);
}
[Theory]
[InlineData("-x value-x -y value-y")]
[InlineData("-y value-y -x value-x")]
public void Symbol_can_be_found_without_explicitly_traversing_result_tree(string commandLine)
{
SymbolResult resultForOptionX = null;
var optionX = new Option<string>(
"-x",
parseArgument: _ => string.Empty);
var optionY = new Option<string>(
"-y",
parseArgument: argResult =>
{
resultForOptionX = argResult.FindResultFor(optionX);
return string.Empty;
});
var command = new Command("the-command")
{
optionX,
optionY,
};
command.Parse(commandLine);
resultForOptionX
.Should()
.BeOfType<OptionResult>()
.Which
.Option
.Should()
.BeSameAs(optionX);
}
[Fact]
public void Command_ArgumentResult_Parent_is_set_correctly_when_token_is_implicit()
{
ArgumentResult argumentResult = null;
var command = new Command("the-command")
{
new Argument<string>(
parse: argResult =>
{
argumentResult = argResult;
return null;
}, isDefault: true)
};
command.Parse("");
argumentResult
.Parent
.Symbol
.Should()
.Be(command);
}
[Fact]
public async Task Custom_argument_parser_is_only_called_once()
{
var callCount = 0;
var handlerWasCalled = false;
var option = new Option<int>("--value", result =>
{
callCount++;
return int.Parse(result.Tokens.Single().Value);
});
var command = new RootCommand();
command.SetHandler((int value) => handlerWasCalled = true, option);
command.AddOption(option);
await command.InvokeAsync("--value 42");
callCount.Should().Be(1);
handlerWasCalled.Should().BeTrue();
}
[Fact]
public void Default_value_and_custom_argument_parser_can_be_used_together()
{
var argument = new Argument<int>(_ => 789, true);
argument.SetDefaultValue(123);
var result = argument.Parse("");
result.GetValueForArgument(argument)
.Should()
.Be(123);
}
[Fact]
public void Multiple_command_arguments_can_have_custom_parse_delegates()
{
var root = new RootCommand
{
new Argument<FileInfo[]>("from", argumentResult =>
{
argumentResult.ErrorMessage = "nope";
return null;
}, true)
{
Arity = new ArgumentArity(0, 2)
},
new Argument<DirectoryInfo>("to", argumentResult =>
{
argumentResult.ErrorMessage = "UH UH";
return null;
}, true)
{
Arity = ArgumentArity.ExactlyOne
}
};
var result = root.Parse("a.txt b.txt /path/to/dir");
result.Errors
.Select(e => e.Message)
.Should()
.Contain("nope");
result.Errors
.Select(e => e.Message)
.Should()
.Contain("UH UH");
}
[Fact]
public void When_custom_conversion_fails_then_an_option_does_not_accept_further_arguments()
{
var command = new Command("the-command")
{
new Argument<string>(),
new Option<string>("-x", argResult =>
{
argResult.ErrorMessage = "nope";
return default;
})
};
var result = command.Parse("the-command -x nope yep");
result.CommandResult.Tokens.Count.Should().Be(1);
}
[Fact]
public void When_argument_cannot_be_parsed_as_the_specified_type_then_getting_value_throws()
{
var option = new Option<int>(new[] { "-o", "--one" }, argumentResult =>
{
if (int.TryParse(argumentResult.Tokens.Select(t => t.Value).Single(), out var value))
{
return value;
}
argumentResult.ErrorMessage = $"'{argumentResult.Tokens.Single().Value}' is not an integer";
return default;
});
var command = new Command("the-command")
{
option
};
var result = command.Parse("the-command -o not-an-int");
Action getValue = () =>
result.GetValueForOption(option);
getValue.Should()
.Throw<InvalidOperationException>()
.Which
.Message
.Should()
.Be("'not-an-int' is not an integer");
}
[Fact]
public void Parse_delegate_is_called_once_per_parse_operation()
{
var i = 0;
var command = new RootCommand
{
new Option<int>(
"-x",
result => ++i,
isDefault: true)
};
command.Parse("");
command.Parse("");
i.Should().Be(2);
}
[Theory] // https://github.com/dotnet/command-line-api/issues/1294
[InlineData("", "option-is-implicit")]
[InlineData("--bananas", "argument-is-implicit")]
[InlineData("--bananas argument-is-specified", "argument-is-specified")]
public void Parse_delegate_is_called_when_Option_Arity_allows_zero_tokens(string commandLine, string expectedValue)
{
var opt = new Option<string>(
"--bananas",
parseArgument: result =>
{
if (result.Tokens.Count == 0)
{
if (result.Parent is OptionResult { IsImplicit: true })
{
return "option-is-implicit";
}
return "argument-is-implicit";
}
else
{
return result.Tokens[0].Value;
}
}, isDefault: true)
{
Arity = ArgumentArity.ZeroOrOne
};
var rootCommand = new RootCommand
{
opt
};
rootCommand.Parse(commandLine).GetValueForOption(opt).Should().Be(expectedValue);
}
[Theory]
[InlineData("1 2 3 4 5 6 7 8")]
[InlineData("-o 999 1 2 3 4 5 6 7 8")]
[InlineData("1 2 3 -o 999 4 5 6 7 8")]
public void Custom_parser_can_pass_on_remaining_tokens(string commandLine)
{
var argument1 = new Argument<int[]>(
"one",
result =>
{
result.OnlyTake(3);
return new[]
{
int.Parse(result.Tokens[0].Value),
int.Parse(result.Tokens[1].Value),
int.Parse(result.Tokens[2].Value)
};
});
var argument2 = new Argument<int[]>(
"two",
result => result.Tokens.Select(t => t.Value).Select(int.Parse).ToArray());
var command = new RootCommand
{
argument1,
argument2,
new Option<int>("-o")
};
var parseResult = command.Parse(commandLine);
parseResult.FindResultFor(argument1)
.GetValueOrDefault()
.Should()
.BeEquivalentTo(new[] { 1, 2, 3 },
options => options.WithStrictOrdering());
parseResult.FindResultFor(argument2)
.GetValueOrDefault()
.Should()
.BeEquivalentTo(new[] { 4, 5, 6, 7, 8 },
options => options.WithStrictOrdering());
}
[Fact]
public void When_tokens_are_passed_on_by_custom_parser_on_last_argument_then_they_become_unmatched_tokens()
{
var argument1 = new Argument<int[]>(
"one",
result =>
{
result.OnlyTake(3);
return new[]
{
int.Parse(result.Tokens[0].Value),
int.Parse(result.Tokens[1].Value),
int.Parse(result.Tokens[2].Value)
};
});
var command = new RootCommand
{
argument1
};
var parseResult = command.Parse("1 2 3 4 5 6 7 8");
parseResult.UnmatchedTokens
.Should()
.BeEquivalentTo(new[] { "4", "5", "6", "7", "8" },
options => options.WithStrictOrdering());
}
[Fact]
public void When_custom_parser_passes_on_tokens_the_argument_result_tokens_reflect_the_change()
{
var argument1 = new Argument<int[]>(
"one",
result =>
{
result.OnlyTake(3);
return new[]
{
int.Parse(result.Tokens[0].Value),
int.Parse(result.Tokens[1].Value),
int.Parse(result.Tokens[2].Value)
};
});
var argument2 = new Argument<int[]>(
"two",
result => result.Tokens.Select(t => t.Value).Select(int.Parse).ToArray());
var command = new RootCommand
{
argument1,
argument2
};
var parseResult = command.Parse("1 2 3 4 5 6 7 8");
parseResult.FindResultFor(argument1)
.Tokens
.Select(t => t.Value)
.Should()
.BeEquivalentTo(new[] { "1", "2", "3" },
options => options.WithStrictOrdering());
parseResult.FindResultFor(argument2)
.Tokens
.Select(t => t.Value)
.Should()
.BeEquivalentTo(new[] { "4", "5", "6", "7", "8" },
options => options.WithStrictOrdering());
}
[Fact]
public void OnlyTake_throws_when_called_with_a_negative_value()
{
var argument = new Argument<int[]>(
"one",
result =>
{
result.OnlyTake(-1);
return null;
});
argument.Invoking(a => a.Parse("1 2 3"))
.Should()
.Throw<ArgumentOutOfRangeException>()
.Which
.Message
.Should()
.ContainAll("Value must be at least 1.", "Actual value was -1.");
}
[Fact]
public void OnlyTake_throws_when_called_twice()
{
var argument = new Argument<int[]>(
"one",
result =>
{
result.OnlyTake(1);
result.OnlyTake(1);
return null;
});
argument.Invoking(a => a.Parse("1 2 3"))
.Should()
.Throw<InvalidOperationException>()
.Which
.Message
.Should()
.Be("OnlyTake can only be called once.");
}
[Fact]
public void OnlyTake_can_pass_on_all_tokens_from_one_multiple_arity_argument_to_another()
{
var argument1 = new Argument<int[]>(result =>
{
result.OnlyTake(0);
return null;
});
var argument2 = new Argument<int[]>();
var command = new RootCommand
{
argument1,
argument2
};
var result = command.Parse("1 2 3");
result.GetValueForArgument(argument1).Should().BeEmpty();
result.GetValueForArgument(argument2).Should().BeEquivalentSequenceTo(1, 2, 3);
}
[Fact] // https://github.com/dotnet/command-line-api/issues/1759
public void OnlyTake_can_pass_on_all_tokens_from_a_single_arity_argument_to_another()
{
var scalar = new Argument<int?>(parse: ctx =>
{
ctx.OnlyTake(0);
return null;
});
Argument<int[]> multiple = new();
var command = new RootCommand
{
scalar,
multiple
};
var result = command.Parse("1 2 3");
result.GetValueForArgument(scalar).Should().BeNull();
result.GetValueForArgument(multiple).Should().BeEquivalentSequenceTo(1, 2, 3);
}
[Fact] //https://github.com/dotnet/command-line-api/issues/1779
public void OnlyTake_can_pass_on_all_tokens_from_a_single_arity_argument_to_another_that_also_passes_them_all_on()
{
var first = new Argument<string>(name: "first", parse: ctx =>
{
ctx.OnlyTake(0);
return null;
})
{
Arity = ArgumentArity.ZeroOrOne
};
var second = new Argument<string[]>(name: "second", parse: ctx =>
{
ctx.OnlyTake(0);
return null;
})
{
Arity = ArgumentArity.ZeroOrMore
};
var third = new Argument<string[]>(name: "third", parse: ctx =>
{
ctx.OnlyTake(3);
return new[] { "1", "2", "3" };
})
{
Arity = ArgumentArity.ZeroOrMore
};
var command = new RootCommand
{
first,
second,
third
};
var result = command.Parse("1 2 3");
result.GetValueForArgument(first).Should().BeNull();
result.GetValueForArgument(second).Should().BeEmpty();
result.GetValueForArgument(third).Should().BeEquivalentSequenceTo("1", "2", "3");
}
}
protected override Symbol CreateSymbol(string name)
{
return new Argument<string>(name);
}
}
}