BadScript 2
Loading...
Searching...
No Matches
ParserResultExtensionsTests.cs
Go to the documentation of this file.
1// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2
3using System.Linq;
4using Xunit;
5using FluentAssertions;
7using System.Threading.Tasks;
8
10{
12 {
13 [Fact]
15 {
16 var expected = string.Empty;
17 Parser.Default.ParseArguments<Simple_Options>(new[] { "--stringvalue", "value" })
18 .WithParsed(opts => expected = opts.StringValue);
19
20 "value".Should().BeEquivalentTo(expected);
21 }
22
23 [Fact]
24 public static async Task Invoke_parsed_lambda_when_parsedAsync()
25 {
26 var expected = string.Empty;
27 await Parser.Default.ParseArguments<Simple_Options>(new[] { "--stringvalue", "value" })
28 .WithParsedAsync(opts => Task.Run(() => expected = opts.StringValue));
29
30 "value".Should().BeEquivalentTo(expected);
31 }
32
33 [Fact]
35 {
36 var expected = string.Empty;
38 new[] { "clone", "https://value.org/user/file.git" })
39 .WithParsed<Add_Verb>(opts => expected = "wrong1")
40 .WithParsed<Commit_Verb>(opts => expected = "wrong2")
41 .WithParsed<Clone_Verb>(opts => expected = opts.Urls.First());
42
43 "https://value.org/user/file.git".Should().BeEquivalentTo(expected);
44 }
45
46 [Fact]
48 {
49 var expected = string.Empty;
51 new[] { "clone", "https://value.org/user/file.git" });
52
53 await parsedArguments.WithParsedAsync<Add_Verb>(opts => Task.Run(() => expected = "wrong1"));
54 await parsedArguments.WithParsedAsync<Commit_Verb>(opts => Task.Run(() => expected = "wrong2"));
55 await parsedArguments.WithParsedAsync<Clone_Verb>(opts => Task.Run(() => expected = opts.Urls.First()));
56
57 "https://value.org/user/file.git".Should().BeEquivalentTo(expected);
58 }
59
60 [Fact]
62 {
63 var expected = "a default";
64 Parser.Default.ParseArguments<Simple_Options>(new[] { "-i", "aaa" })
65 .WithNotParsed(_ => expected = "changed");
66
67 "changed".Should().BeEquivalentTo(expected);
68 }
69
70 [Fact]
72 {
73 var expected = "a default";
74 await Parser.Default.ParseArguments<Simple_Options>(new[] { "-i", "aaa" })
75 .WithNotParsedAsync(_ => Task.Run(() => expected = "changed"));
76
77 "changed".Should().BeEquivalentTo(expected);
78 }
79
80 [Fact]
82 {
83 var expected = "a default";
84 Parser.Default.ParseArguments<Add_Verb, Commit_Verb, Clone_Verb>(new[] { "undefined", "-xyz" })
85 .WithParsed<Add_Verb>(opts => expected = "wrong1")
86 .WithParsed<Commit_Verb>(opts => expected = "wrong2")
87 .WithParsed<Clone_Verb>(opts => expected = "wrong3")
88 .WithNotParsed(_ => expected = "changed");
89
90 "changed".Should().BeEquivalentTo(expected);
91 }
92
93 [Fact]
95 {
96 var expected = "a default";
97 var parsedArguments = Parser.Default.ParseArguments<Add_Verb, Commit_Verb, Clone_Verb>(new[] { "undefined", "-xyz" });
98
99 await parsedArguments.WithParsedAsync<Add_Verb>(opts => Task.Run(() => expected = "wrong1"));
100 await parsedArguments.WithParsedAsync<Commit_Verb>(opts => Task.Run(() => expected = "wrong2"));
101 await parsedArguments.WithParsedAsync<Clone_Verb>(opts => Task.Run(() => expected = "wrong3"));
102 await parsedArguments.WithNotParsedAsync(_ => Task.Run(() => expected = "changed"));
103
104 "changed".Should().BeEquivalentTo(expected);
105 }
106
107 [Fact]
109 {
110 var expected = string.Empty;
111 Parser.Default.ParseArguments<Simple_Options>(new[] { "--stringvalue", "value" })
112 .WithParsed(opts => expected = opts.StringValue)
113 .WithNotParsed(_ => expected = "changed");
114
115 "value".Should().BeEquivalentTo(expected);
116 }
117
118 [Fact]
119 public static async Task Invoke_proper_lambda_when_parsedAsync()
120 {
121 var expected = string.Empty;
122 var parsedArguments = Parser.Default.ParseArguments<Simple_Options>(new[] { "--stringvalue", "value" });
123
124 await parsedArguments.WithParsedAsync(opts => Task.Run(() => expected = opts.StringValue));
125 await parsedArguments.WithNotParsedAsync(_ => Task.Run(() => expected = "changed"));
126
127 "value".Should().BeEquivalentTo(expected);
128 }
129
130 [Fact]
132 {
133 var expected = "a default";
134 Parser.Default.ParseArguments<Simple_Options>(new[] { "-i", "aaa" })
135 .WithParsed(opts => expected = opts.StringValue)
136 .WithNotParsed(_ => expected = "changed");
137
138 "changed".Should().BeEquivalentTo(expected);
139 }
140
141 [Fact]
143 {
144 var expected = "a default";
145 var parsedArguments = Parser.Default.ParseArguments<Simple_Options>(new[] { "-i", "aaa" });
146
147 await parsedArguments.WithParsedAsync(opts => Task.Run(() => expected = opts.StringValue));
148 await parsedArguments.WithNotParsedAsync(_ => Task.Run(() => expected = "changed"));
149
150 "changed".Should().BeEquivalentTo(expected);
151 }
152
153 [Fact]
155 {
156 var expected = Parser.Default.ParseArguments<Simple_Options>(new[] { "--stringvalue", "value" })
157 .MapResult(_ => 0, _ => -1);
158
159 0.Should().Be(expected);
160 }
161
162 [Fact]
164 {
166 new[] { "clone", "https://value.org/user/file.git" })
167 .MapResult(
168 (Add_Verb opts) => 0,
169 (Commit_Verb opts) => 1,
170 (Clone_Verb opts) => 2,
171 errs => 3);
172
173 2.Should().Be(expected);
174 }
175
176 [Fact]
178 {
179 var expected = Parser.Default.ParseArguments<Simple_Options>(new[] { "-i", "aaa" })
180 .MapResult(_ => 0, _ => -1);
181
182 (-1).Should().Be(expected);
183 }
184
185 [Fact]
187 {
189 new[] { "undefined", "-xyz" })
190 .MapResult(
191 (Add_Verb opts) => 0,
192 (Commit_Verb opts) => 1,
193 (Clone_Verb opts) => 2,
194 errs => 3);
195
196 3.Should().Be(expected);
197 }
198
199 [Fact]
201 {
202 var expected = string.Empty;
204 new[] { "derivedadd", "dummy.bin" })
205 .WithParsed<Add_Verb>(opts => expected = "wrong1")
206 .WithParsed<Commit_Verb>(opts => expected = "wrong2")
207 .WithParsed<Clone_Verb>(opts => expected = "wrong3")
208 .WithParsed<Base_Class_For_Verb>(opts => expected = opts.FileName);
209
210 "dummy.bin".Should().BeEquivalentTo(expected);
211 }
212
213 [Fact]
215 {
216 var expected = string.Empty;
218 new[] { "derivedadd", "dummy.bin" });
219
220 await parsedArguments.WithParsedAsync<Add_Verb>(opts => Task.Run(() => expected = "wrong1"));
221 await parsedArguments.WithParsedAsync<Commit_Verb>(opts => Task.Run(() => expected = "wrong2"));
222 await parsedArguments.WithParsedAsync<Clone_Verb>(opts => Task.Run(() => expected = "wrong3"));
223 await parsedArguments.WithParsedAsync<Base_Class_For_Verb>(opts => Task.Run(() => expected = opts.FileName));
224
225 "dummy.bin".Should().BeEquivalentTo(expected);
226 }
227
228 [Fact]
230 {
232 new[] { "derivedadd", "dummy.bin" })
233 .MapResult(
234 (Base_Class_For_Verb opts) => 1,
235 errs => 2);
236
237 1.Should().Be(expected);
238 }
239
240 [Fact]
242 {
244 new[] { "derivedadd", "dummy.bin" })
245 .MapResult(
246 (Add_Verb opts) => 0,
247 (Commit_Verb opts) => 1,
248 (Clone_Verb opts) => 2,
249 (Base_Class_For_Verb opts) => 4,
250 (Derived_Verb opts) => 3,
251 errs => 5);
252
253 4.Should().Be(expected);
254 }
255 }
256}
Provides methods to parse command line arguments.
Definition Parser.cs:21
ParserResult< object > ParseArguments(IEnumerable< string > args, params Type[] types)
Parses a string array of command line arguments for verb commands scenario, constructing the proper i...
Definition Parser.cs:216
static Parser Default
Gets the singleton instance created with basic defaults.
Definition Parser.cs:75