-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathParseDiagramTests.cs
More file actions
134 lines (110 loc) · 4.34 KB
/
ParseDiagramTests.cs
File metadata and controls
134 lines (110 loc) · 4.34 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
// 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.Parsing;
using System.IO;
using FluentAssertions;
using Xunit;
namespace System.CommandLine.Tests
{
public class ParseDiagramTests
{
[Fact]
public void Parse_result_diagram_helps_explain_parse_operation()
{
CliCommand command =
new CliCommand(
"the-command")
{
new CliOption<string>("-x"),
new CliOption<bool>("-y"),
new CliArgument<string[]>("arg")
};
var result = command.Parse("the-command -x one -y two three");
result.Diagram()
.Should()
.Be("[ the-command [ -x <one> ] [ -y ] <two> <three> ]");
}
[Fact]
public void Parse_result_diagram_displays_unmatched_tokens()
{
CliOption<string> option = new ("-x");
option.AcceptOnlyFromAmong("arg1", "arg2", "arg3");
var command = new CliCommand("command")
{
option
};
var result = command.Parse("command -x ar");
result.Diagram()
.Should()
.Be("[ command ![ -x <ar> ] ]");
}
// https://github.com/dotnet/command-line-api/issues/2392
[Fact]
public void Parse_diagram_with_option_argument_error_only_one_error_is_returned()
{
var command = new CliCommand("the-command")
{
new CliOption<int[]>("-x") { Arity = new ArgumentArity(2, 3)}
};
ParseResult parseResult = command.Parse("-x 1 -x 2 -x 3 -x 4");
_ = parseResult.Diagram();
parseResult.Errors
.Should()
.ContainSingle();
}
[Fact]
public void Parse_diagram_shows_type_conversion_errors()
{
var command = new CliRootCommand
{
new CliOption<int>("-f")
};
var result = command.Parse("-f not-an-int");
result.Diagram()
.Should()
.Be($"[ {CliRootCommand.ExecutableName} ![ -f <not-an-int> ] ]");
}
[Fact]
public void Parse_diagram_identifies_options_where_default_values_have_been_applied()
{
var rootCommand = new CliRootCommand
{
new CliOption<int>("--height", "-h") { DefaultValueFactory = _ => 10 },
new CliOption<int>("-w", "--width") { DefaultValueFactory = _ => 15 },
new CliOption<ConsoleColor>("--color", "-c") { DefaultValueFactory = _ => ConsoleColor.Cyan }
};
var result = rootCommand.Parse("-w 9000");
var diagram = result.Diagram();
diagram.Should()
.Be($"[ {CliRootCommand.ExecutableName} [ -w <9000> ] *[ --height <10> ] *[ --color <Cyan> ] ]");
}
[Fact]
public void Parse_diagram_indicates_which_tokens_were_applied_to_which_command_argument()
{
var command = new CliCommand("the-command")
{
new CliArgument<string>("first"),
new CliArgument<string> ("second"),
new CliArgument<string[]> ("third")
};
var result = command.Parse("one two three four five");
result.Diagram()
.Should()
.Be("[ the-command [ first <one> ] [ second <two> ] [ third <three> <four> <five> ] ]");
}
[Fact]
public void Parse_diagram_indicates_which_tokens_were_applied_to_which_command_argument_for_sequences_of_complex_types()
{
var command = new CliCommand("the-command")
{
new CliArgument<FileInfo> ("first"),
new CliArgument<FileInfo> ("second"),
new CliArgument<FileInfo[]> ("third")
};
var result = command.Parse("one two three four five");
result.Diagram()
.Should()
.Be("[ the-command [ first <one> ] [ second <two> ] [ third <three> <four> <five> ] ]");
}
}
}