forked from commandlineparser/commandline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnParserExtensionsTests.cs
More file actions
442 lines (407 loc) · 20.2 KB
/
Copy pathUnParserExtensionsTests.cs
File metadata and controls
442 lines (407 loc) · 20.2 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
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;
using FluentAssertions;
using CommandLine.Tests.Fakes;
#if !SKIP_FSHARP
using Microsoft.FSharp.Core;
#endif
namespace CommandLine.Tests.Unit
{
public class UnParserExtensionsTests
{
[Theory]
[MemberData(nameof(UnParseData))]
public static void UnParsing_instance_returns_command_line(Simple_Options options, string result)
{
new Parser()
.FormatCommandLine(options)
.Should().BeEquivalentTo(result);
}
[Theory]
[MemberData(nameof(UnParseData))]
public static void UnParsing_instance_with_splitArgs_returns_same_option_class(Simple_Options options, string result)
{
new Parser()
.FormatCommandLineArgs(options)
.Should().BeEquivalentTo(result.SplitArgs());
}
[Theory]
[MemberData(nameof(UnParseFileDirectoryData))]
public static void UnParsing_instance_returns_command_line_for_file_directory_paths(Options_With_FileDirectoryInfo options, string result)
{
new Parser()
.FormatCommandLine(options)
.Should().BeEquivalentTo(result);
}
[Theory]
[MemberData(nameof(UnParseFileDirectoryData))]
public static void UnParsing_instance_by_splitArgs_returns_command_line_for_file_directory_paths(Options_With_FileDirectoryInfo options, string result)
{
new Parser()
.FormatCommandLineArgs(options)
.Should().BeEquivalentTo(result.SplitArgs());
}
[Theory]
[MemberData(nameof(UnParseDataVerbs))]
public static void UnParsing_instance_returns_command_line_for_verbs(Add_Verb verb, string result)
{
new Parser()
.FormatCommandLine(verb)
.Should().BeEquivalentTo(result);
}
[Theory]
[MemberData(nameof(UnParseDataVerbs))]
public static void UnParsing_instance_to_splitArgs_returns_command_line_for_verbs(Add_Verb verb, string result)
{
new Parser()
.FormatCommandLineArgs(verb)
.Should().BeEquivalentTo(result.SplitArgs());
}
[Theory]
[MemberData(nameof(UnParseDataImmutable))]
public static void UnParsing_immutable_instance_returns_command_line(Immutable_Simple_Options options, string result)
{
new Parser()
.FormatCommandLine(options)
.Should().BeEquivalentTo(result);
}
[Theory]
[MemberData(nameof(UnParseDataHidden))]
public static void Unparsing_hidden_option_returns_command_line(Hidden_Option options, bool showHidden, string result)
{
new Parser()
.FormatCommandLine(options, config => config.ShowHidden = showHidden)
.Should().BeEquivalentTo(result);
}
#if !SKIP_FSHARP
[Theory]
[MemberData(nameof(UnParseDataFSharpOption))]
public static void UnParsing_instance_with_fsharp_option_returns_command_line(Options_With_FSharpOption options, string result)
{
new Parser()
.FormatCommandLine(options)
.Should().BeEquivalentTo(result);
}
#endif
[Fact]
public static void UnParsing_instance_with_group_switches_returns_command_line_with_switches_grouped()
{
var options = new Options_With_Switches { InputFile = "input.bin", HumanReadable = true, IgnoreWarnings = true };
new Parser()
.FormatCommandLine(options, config => config.GroupSwitches = true)
.Should().BeEquivalentTo("-hi --input input.bin");
}
[Fact]
public static void UnParsing_instance_with_equal_token_returns_command_line_with_long_option_using_equal_sign()
{
var options = new Simple_Options { BoolValue = true, IntSequence = new[] { 1, 2, 3 }, StringValue = "nospaces", LongValue = 123456789 };
new Parser()
.FormatCommandLine(options, config => config.UseEqualToken = true)
.Should().BeEquivalentTo("-i 1 2 3 --stringvalue=nospaces -x 123456789");
}
[Fact]
public static void UnParsing_instance_with_dash_in_value_and_dashdash_enabled_returns_command_line_with_value_prefixed_with_dash_dash()
{
var options = new Simple_Options_With_Values { StringSequence = new List<string> { "-something", "with", "dash" } };
new Parser((setting) => setting.EnableDashDash = true)
.FormatCommandLine(options)
.Should().BeEquivalentTo("-- -something with dash");
}
[Fact]
public static void UnParsing_instance_with_no_values_and_dashdash_enabled_returns_command_line_without_dash_dash()
{
var options = new Simple_Options_With_Values();
new Parser((setting) => setting.EnableDashDash = true)
.FormatCommandLine(options)
.Should().BeEquivalentTo("");
}
[Fact]
public static void UnParsing_instance_with_dash_in_value_and_dashdash_disabled_returns_command_line_with_value()
{
var options = new Simple_Options_With_Values { StringSequence = new List<string> { "-something", "with", "dash" } };
new Parser()
.FormatCommandLine(options)
.Should().BeEquivalentTo("-something with dash");
}
#region Issue 579
[Fact]
public static void UnParsing_instance_with_TimeSpan_returns_the_value_unquoted_in_command_line()
{
var options = new Options_With_TimeSpan { Duration = TimeSpan.FromMinutes(1) };
new Parser()
.FormatCommandLine(options)
.Should().Be("--duration 00:01:00");
}
#endregion
#region PR 550
[Fact]
public static void UnParsing_instance_with_default_values_when_skip_default_is_false()
{
var options = new Options_With_Defaults { P2 = "xyz", P1 = 99, P3 = 88, P4 = Shapes.Square };
new Parser()
.FormatCommandLine(options)
.Should().BeEquivalentTo("--p1 99 --p2 xyz --p3 88 --p4 Square");
}
[Theory]
[InlineData(true, "--p2 xyz")]
[InlineData(false, "--p1 99 --p2 xyz --p3 88 --p4 Square")]
public static void UnParsing_instance_with_default_values_when_skip_default_is_true(bool skipDefault, string expected)
{
var options = new Options_With_Defaults { P2 = "xyz", P1 = 99, P3 = 88, P4 = Shapes.Square };
new Parser()
.FormatCommandLine(options, x => x.SkipDefault = skipDefault)
.Should().BeEquivalentTo(expected);
}
[Theory]
[InlineData(true, "--p2 xyz")]
[InlineData(false, "--p1 99 --p2 xyz --p3 88 --p4 Square")]
public static void UnParsing_instance_with_nullable_default_values_when_skip_default_is_true(bool skipDefault, string expected)
{
var options = new Nuulable_Options_With_Defaults { P2 = "xyz", P1 = 99, P3 = 88, P4 = Shapes.Square };
new Parser()
.FormatCommandLine(options, x => x.SkipDefault = skipDefault)
.Should().BeEquivalentTo(expected);
}
[Fact]
public static void UnParsing_instance_with_datetime()
{
var date = new DateTime(2019, 5, 1);
var options = new Options_Date { Start = date };
var result = new Parser()
.FormatCommandLine(options)
.Should().MatchRegex("--start\\s\".+\"");
}
[Fact]
public static void UnParsing_instance_with_datetime_nullable()
{
var date = new DateTime(2019, 5, 1);
var options = new Options_Date_Nullable { Start = date };
var result = new Parser()
.FormatCommandLine(options)
.Should().MatchRegex("--start\\s\".+\"");
}
[Fact]
public static void UnParsing_instance_with_datetime_offset()
{
DateTimeOffset date = new DateTime(2019, 5, 1);
var options = new Options_DateTimeOffset { Start = date };
var result = new Parser()
.FormatCommandLine(options)
.Should().MatchRegex("--start\\s\".+\"");
}
[Fact]
public static void UnParsing_instance_with_timespan()
{
var ts = new TimeSpan(1,2,3);
var options = new Options_TimeSpan { Start = ts };
var result = new Parser()
.FormatCommandLine(options)
.Should().BeEquivalentTo("--start 01:02:03"); //changed for issue 579
}
[Theory]
[InlineData(false, 0, "")] //default behaviour based on type
[InlineData(false, 1, "-v 1")] //default skip=false
[InlineData(false, 2, "-v 2")]
[InlineData(true, 1, "")] //default skip=true
public static void UnParsing_instance_with_int(bool skipDefault, int value, string expected)
{
var options = new Option_Int { VerboseLevel = value };
var result = new Parser()
.FormatCommandLine(options, x => x.SkipDefault = skipDefault)
.Should().BeEquivalentTo(expected);
}
[Theory]
[InlineData(false, 0, "-v 0")]
[InlineData(false, 1, "-v 1")] //default
[InlineData(false, 2, "-v 2")]
[InlineData(false, null, "")]
[InlineData(true, 1, "")] //default
public static void UnParsing_instance_with_int_nullable(bool skipDefault, int? value, string expected)
{
var options = new Option_Int_Nullable { VerboseLevel = value };
var result = new Parser()
.FormatCommandLine(options, x => x.SkipDefault = skipDefault)
.Should().BeEquivalentTo(expected);
}
[Theory]
[InlineData(false, false, 0, "")]
[InlineData(false, false, 1, "-v")] // default but not skipped
[InlineData(false, false, 2, "-v -v")]
[InlineData(false, true, 2, "-vv")]
[InlineData(false, false, 3, "-v -v -v")]
[InlineData(false, true, 3, "-vvv")]
[InlineData(true, false, 1, "")] // default, skipped
public static void UnParsing_instance_with_flag_counter(bool skipDefault, bool groupSwitches, int value, string expected)
{
var options = new Option_FlagCounter { VerboseLevel = value };
var result = new Parser()
.FormatCommandLine(options, x => { x.SkipDefault = skipDefault; x.GroupSwitches = groupSwitches; })
.Should().BeEquivalentTo(expected);
}
[Theory]
[InlineData(Shapes.Circle, "--shape Circle")]
[InlineData(Shapes.Square, "--shape Square")]
[InlineData(null, "")]
public static void UnParsing_instance_with_nullable_enum(Shapes? shape, string expected)
{
var options = new Option_Nullable_Enum { Shape = shape };
var result = new Parser()
.FormatCommandLine(options)
.Should().BeEquivalentTo(expected);
}
[Theory]
[InlineData(true, "-v True")]
[InlineData(false, "-v False")]
[InlineData(null, "")]
public static void UnParsing_instance_with_nullable_bool(bool? flag, string expected)
{
var options = new Option_Nullable_Bool { Verbose = flag };
var result = new Parser()
.FormatCommandLine(options)
.Should().BeEquivalentTo(expected);
}
#region SplitArgs
[Theory]
[InlineData("--shape Circle", new[] { "--shape","Circle" })]
[InlineData(" --shape Circle ", new[] { "--shape", "Circle" })]
[InlineData("-a --shape Circle", new[] {"-a", "--shape", "Circle" })]
[InlineData("-a --shape Circle -- -x1 -x2", new[] { "-a", "--shape", "Circle","--","-x1","-x2" })]
[InlineData("--name \"name with space and quote\" -x1", new[] { "--name", "name with space and quote","-x1" })]
public static void Split_arguments(string command, string[] expectedArgs)
{
var args = command.SplitArgs();
args.Should().BeEquivalentTo(expectedArgs);
}
[Theory]
[InlineData("--shape Circle", new[] { "--shape", "Circle" })]
[InlineData(" --shape Circle ", new[] { "--shape", "Circle" })]
[InlineData("-a --shape Circle", new[] { "-a", "--shape", "Circle" })]
[InlineData("-a --shape Circle -- -x1 -x2", new[] { "-a", "--shape", "Circle", "--", "-x1", "-x2" })]
[InlineData("--name \"name with space and quote\" -x1", new[] { "--name", "\"name with space and quote\"", "-x1" })]
public static void Split_arguments_with_keep_quote(string command, string[] expectedArgs)
{
var args = command.SplitArgs(true);
args.Should().BeEquivalentTo(expectedArgs);
}
#endregion
class Option_Int_Nullable
{
[Option('v', Default = 1)]
public int? VerboseLevel { get; set; }
}
class Option_Int
{
[Option('v', Default = 1)]
public int VerboseLevel { get; set; }
}
class Option_FlagCounter
{
[Option('v', Default = 1, FlagCounter=true)]
public int VerboseLevel { get; set; }
}
class Option_Nullable_Bool
{
[Option('v')]
public bool? Verbose { get; set; }
}
class Option_Nullable_Enum
{
[Option]
public Shapes? Shape { get; set; }
}
class Options_Date
{
[Option]
public DateTime Start { get; set; }
}
class Options_Date_Nullable
{
[Option]
public DateTime? Start { get; set; }
}
class Options_TimeSpan
{
[Option]
public TimeSpan Start { get; set; }
}
class Options_DateTimeOffset
{
[Option]
public DateTimeOffset Start { get; set; }
}
#endregion
public static IEnumerable<object[]> UnParseData
{
get
{
yield return new object[] { new Simple_Options(), "" };
yield return new object[] { new Simple_Options { BoolValue = true }, "-x" };
yield return new object[] { new Simple_Options { IntSequence = new[] { 1, 2, 3 } }, "-i 1 2 3" };
yield return new object[] { new Simple_Options { StringValue = "nospaces" }, "--stringvalue nospaces" };
yield return new object[] { new Simple_Options { StringValue = " with spaces " }, "--stringvalue \" with spaces \"" };
yield return new object[] { new Simple_Options { StringValue = "with\"quote" }, "--stringvalue \"with\\\"quote\"" };
yield return new object[] { new Simple_Options { StringValue = "with \"quotes\" spaced" }, "--stringvalue \"with \\\"quotes\\\" spaced\"" };
yield return new object[] { new Simple_Options { LongValue = 123456789 }, "123456789" };
yield return new object[] { new Simple_Options { BoolValue = true, IntSequence = new[] { 1, 2, 3 }, StringValue = "nospaces", LongValue = 123456789 }, "-i 1 2 3 --stringvalue nospaces -x 123456789" };
yield return new object[] { new Simple_Options { BoolValue = true, IntSequence = new[] { 1, 2, 3 }, StringValue = "with \"quotes\" spaced", LongValue = 123456789 }, "-i 1 2 3 --stringvalue \"with \\\"quotes\\\" spaced\" -x 123456789" };
}
}
public static IEnumerable<object[]> UnParseFileDirectoryData
{
get
{
yield return new object[] { new Options_With_FileDirectoryInfo(), "" };
yield return new object[] { new Options_With_FileDirectoryInfo { FilePath = new FileInfo(@"C:\my path\with spaces\file with spaces.txt"), DirectoryPath = new DirectoryInfo(@"C:\my path\with spaces\"), StringPath = @"C:\my path\with spaces\file with spaces.txt" }, @"--directoryPath ""C:\my path\with spaces\"" --filePath ""C:\my path\with spaces\file with spaces.txt"" --stringPath ""C:\my path\with spaces\file with spaces.txt""" };
}
}
public static IEnumerable<object[]> UnParseDataVerbs
{
get
{
yield return new object[] { new Add_Verb(), "add" };
yield return new object[] { new Add_Verb { Patch = true, FileName = "mysource.cs" }, "add --patch mysource.cs" };
yield return new object[] { new Add_Verb { Force = true, FileName = "mysource.fs" }, "add --force mysource.fs" };
}
}
public static IEnumerable<object[]> UnParseDataImmutable
{
get
{
yield return new object[] { new Immutable_Simple_Options("", Enumerable.Empty<int>(), default(bool), default(long)), "" };
yield return new object[] { new Immutable_Simple_Options("", Enumerable.Empty<int>(), true, default(long)), "-x" };
yield return new object[] { new Immutable_Simple_Options("", new[] { 1, 2, 3 }, default(bool), default(long)), "-i 1 2 3" };
yield return new object[] { new Immutable_Simple_Options("nospaces", Enumerable.Empty<int>(), default(bool), default(long)), "--stringvalue nospaces" };
yield return new object[] { new Immutable_Simple_Options(" with spaces ", Enumerable.Empty<int>(), default(bool), default(long)), "--stringvalue \" with spaces \"" };
yield return new object[] { new Immutable_Simple_Options("with\"quote", Enumerable.Empty<int>(), default(bool), default(long)), "--stringvalue \"with\\\"quote\"" };
yield return new object[] { new Immutable_Simple_Options("with \"quotes\" spaced", Enumerable.Empty<int>(), default(bool), default(long)), "--stringvalue \"with \\\"quotes\\\" spaced\"" };
yield return new object[] { new Immutable_Simple_Options("", Enumerable.Empty<int>(), default(bool), 123456789), "123456789" };
yield return new object[] { new Immutable_Simple_Options("nospaces", new[] { 1, 2, 3 }, true, 123456789), "-i 1 2 3 --stringvalue nospaces -x 123456789" };
yield return new object[] { new Immutable_Simple_Options("with \"quotes\" spaced", new[] { 1, 2, 3 }, true, 123456789), "-i 1 2 3 --stringvalue \"with \\\"quotes\\\" spaced\" -x 123456789" };
}
}
public static IEnumerable<object[]> UnParseDataHidden
{
get
{
yield return new object[] { new Hidden_Option { HiddenOption = "hidden" }, true, "--hiddenOption hidden" };
yield return new object[] { new Hidden_Option { HiddenOption = "hidden" }, false, "" };
}
}
#if !SKIP_FSHARP
public static IEnumerable<object[]> UnParseDataFSharpOption
{
get
{
yield return new object[] { new Options_With_FSharpOption(), "" };
yield return new object[] { new Options_With_FSharpOption { FileName = FSharpOption<string>.Some("myfile.bin") }, "--filename myfile.bin" };
yield return new object[] { new Options_With_FSharpOption { Offset = FSharpOption<int>.Some(123456789) }, "123456789" };
yield return new object[] { new Options_With_FSharpOption { FileName = FSharpOption<string>.Some("myfile.bin"), Offset = FSharpOption<int>.Some(123456789) }, "--filename myfile.bin 123456789" };
}
}
#endif
}
}