Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / class / Mono.Options / Test / Mono.Options / OptionSetTest.cs
1 //
2 // OptionSetTest.cs
3 //
4 // Authors:
5 //  Jonathan Pryor <jpryor@novell.com>
6 //
7 // Copyright (C) 2008 Novell (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.ComponentModel;
33 using System.Globalization;
34 using System.IO;
35
36 #if NDESK_OPTIONS
37 using NDesk.Options;
38 #else
39 using Mono.Options;
40 #endif
41
42 using Cadenza.Collections.Tests;
43
44 using NUnit.Framework;
45
46 #if NDESK_OPTIONS
47 namespace Tests.NDesk.Options
48 #else
49 namespace Tests.Mono.Options
50 #endif
51 {
52         class FooConverter : TypeConverter {
53                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
54                 {
55                         if (sourceType == typeof (string))
56                                 return true;
57                         return base.CanConvertFrom (context, sourceType);
58                 }
59
60                 public override object ConvertFrom (ITypeDescriptorContext context,
61                                 CultureInfo culture, object value)
62                 {
63                         string v = value as string;
64                         if (v != null) {
65                                 switch (v) {
66                                         case "A": return Foo.A;
67                                         case "B": return Foo.B;
68                                 }
69                         }
70
71                         return base.ConvertFrom (context, culture, value);
72                 }
73         }
74
75         [TypeConverter (typeof(FooConverter))]
76         class Foo {
77                 public static readonly Foo A = new Foo ("A");
78                 public static readonly Foo B = new Foo ("B");
79                 string s;
80                 Foo (string s) { this.s = s; }
81                 public override string ToString () {return s;}
82         }
83
84         class TestArgumentSource : ArgumentSource, IEnumerable {
85                 string[] names;
86                 string desc;
87
88                 public TestArgumentSource (string[] names, string desc)
89                 {
90                         this.names  = names;
91                         this.desc   = desc;
92                 }
93
94                 Dictionary<string, string[]> args = new Dictionary<string, string[]>();
95
96                 public void Add (string key, params string[] values)
97                 {
98                         args.Add (key, values);
99                 }
100
101                 public override string[] GetNames ()
102                 {
103                         return names;
104                 }
105
106                 public override string Description {
107                         get {return desc;}
108                 }
109
110                 public override bool GetArguments (string value, out IEnumerable<string> replacement)
111                 {
112                         replacement = null;
113
114                         string[] values;
115                         if (args.TryGetValue (value, out values)) {
116                                 replacement = values;
117                                 return true;
118                         }
119
120                         return false;
121                 }
122
123
124                 IEnumerator IEnumerable.GetEnumerator ()
125                 {
126                         return args.GetEnumerator ();
127                 }
128         }
129
130         [TestFixture]
131         public class OptionSetTest : ListContract<Option> {
132
133                 protected override ICollection<Option> CreateCollection (IEnumerable<Option> values)
134                 {
135                         OptionSet set = new OptionSet();
136                         foreach (Option value in values)
137                                 set.Add (value);
138                         return set;
139                 }
140
141                 protected override Option CreateValueA ()
142                 {
143                         return new CustomOption ("A", null, 0, null);
144                 }
145
146                 protected override Option CreateValueB ()
147                 {
148                         return new CustomOption ("B", null, 0, null);
149                 }
150
151                 protected override Option CreateValueC ()
152                 {
153                         return new CustomOption ("C", null, 0, null);
154                 }
155
156                 static IEnumerable<string> _ (params string[] a)
157                 {
158                         return a;
159                 }
160
161                 [Test]
162                 public void BundledValues ()
163                 {
164                         BundledValues (_("-DNAME", "-D", "NAME2", "-Debug", "-L/foo", "-L", "/bar", "-EDNAME3"));
165                         BundledValues (_("@s1", "-D", "@s2", "-L/foo", "@s4"));
166                 }
167
168                 public void BundledValues (IEnumerable<string> args)
169                 {
170                         var defines = new List<string> ();
171                         var libs    = new List<string> ();
172                         bool debug  = false;
173                         var p = new OptionSet () {
174                                 { "D|define=",  v => defines.Add (v) },
175                                 { "L|library:", v => libs.Add (v) },
176                                 { "Debug",      v => debug = v != null },
177                                 { "E",          v => { /* ignore */ } },
178                                 new TestArgumentSource (null, null) {
179                                         { "@s1", "-DNAME" },
180                                         { "@s2", "NAME2", "@s3" },
181                                         { "@s3", "-Debug" },
182                                         { "@s4", "-L", "/bar", "-EDNAME3" },
183                                 },
184                         };
185                         p.Parse (args);
186                         Assert.AreEqual (defines.Count, 3);
187                         Assert.AreEqual (defines [0], "NAME");
188                         Assert.AreEqual (defines [1], "NAME2");
189                         Assert.AreEqual (defines [2], "NAME3");
190                         Assert.AreEqual (debug, true);
191                         Assert.AreEqual (libs.Count, 2);
192                         Assert.AreEqual (libs [0], "/foo");
193                         Assert.AreEqual (libs [1], null);
194
195                         Utils.AssertException (typeof(OptionException), 
196                                         "Cannot bundle unregistered option '-V'.",
197                                         p, v => { v.Parse (_("-EVALUENOTSUP")); });
198                 }
199
200                 [Test]
201                 public void RequiredValues ()
202                 {
203                         RequiredValues (_("a", "-a", "s", "-n=42", "n"));
204                         RequiredValues (_("@s1", "s", "@s2", "n"));
205                 }
206
207                 void RequiredValues (IEnumerable<string> args)
208                 {
209                         string a = null;
210                         int n = 0;
211                         OptionSet p = new OptionSet () {
212                                 { "a=", v => a = v },
213                                 { "n=", (int v) => n = v },
214                                 new TestArgumentSource (null, null) {
215                                         { "@s1", "a", "-a" },
216                                         { "@s2", "-n=42" },
217                                 },
218                         };
219                         List<string> extra = p.Parse (args);
220                         Assert.AreEqual (extra.Count, 2);
221                         Assert.AreEqual (extra [0], "a");
222                         Assert.AreEqual (extra [1], "n");
223                         Assert.AreEqual (a, "s");
224                         Assert.AreEqual (n, 42);
225
226                         extra = p.Parse (_("-a="));
227                         Assert.AreEqual (extra.Count, 0);
228                         Assert.AreEqual (a, "");
229                 }
230
231                 [Test]
232                 public void OptionalValues ()
233                 {
234                         string a = null;
235                         int? n = -1;
236                         Foo f = null;
237                         OptionSet p = new OptionSet () {
238                                 { "a:", v => a = v },
239                                 { "n:", (int? v) => n = v },
240                                 { "f:", (Foo v) => f = v },
241                         };
242                         p.Parse (_("-a=s"));
243                         Assert.AreEqual (a, "s");
244                         p.Parse (_("-a"));
245                         Assert.AreEqual (a, null);
246                         p.Parse (_("-a="));
247                         Assert.AreEqual (a, "");
248
249                         p.Parse (_("-f", "A"));
250                         Assert.AreEqual (f, null);
251                         p.Parse (_("-f"));
252                         Assert.AreEqual (f, null);
253                         p.Parse (_("-f=A"));
254                         Assert.AreEqual (f, Foo.A);
255                         f = null;
256                         p.Parse (_("-fA"));
257                         Assert.AreEqual (f, Foo.A);
258
259                         p.Parse (_("-n42"));
260                         Assert.AreEqual (n.Value, 42);
261                         p.Parse (_("-n", "42"));
262                         Assert.AreEqual (n.HasValue, false);
263                         p.Parse (_("-n=42"));
264                         Assert.AreEqual (n.Value, 42);
265                         p.Parse (_("-n"));
266                         Assert.AreEqual (n.HasValue, false);
267                         Utils.AssertException (typeof(OptionException),
268                                         "Could not convert string `' to type Int32 for option `-n'.",
269                                         p, v => { v.Parse (_("-n=")); });
270                 }
271
272                 [Test]
273                 public void BooleanValues ()
274                 {
275                         bool a = false;
276                         OptionSet p = new OptionSet () {
277                                 { "a", v => a = v != null },
278                         };
279                         p.Parse (_("-a"));
280                         Assert.AreEqual (a, true);
281                         p.Parse (_("-a+"));
282                         Assert.AreEqual (a, true);
283                         p.Parse (_("-a-"));
284                         Assert.AreEqual (a, false);
285                 }
286
287                 [Test]
288                 public void CombinationPlatter ()
289                 {
290                         CombinationPlatter (new string[]{"foo", "-v", "-a=42", "/b-",
291                                 "-a", "64", "bar", "--f", "B", "/h", "-?", "--help", "-v"});
292                         CombinationPlatter (_("@s1", "-a=42", "@s3", "-a", "64", "bar", "@s4"));
293                 }
294
295                 void CombinationPlatter (IEnumerable<string> args)
296                 {
297                         int a = -1, b = -1;
298                         string av = null, bv = null;
299                         Foo f = null;
300                         int help = 0;
301                         int verbose = 0;
302                         OptionSet p = new OptionSet () {
303                                 { "a=", v => { a = 1; av = v; } },
304                                 { "b", "desc", v => {b = 2; bv = v;} },
305                                 { "f=", (Foo v) => f = v },
306                                 { "v", v => { ++verbose; } },
307                                 { "h|?|help", (v) => { switch (v) {
308                                         case "h": help |= 0x1; break; 
309                                         case "?": help |= 0x2; break;
310                                         case "help": help |= 0x4; break;
311                                 } } },
312                                 new TestArgumentSource (null, null) {
313                                         { "@s1", "foo", "-v", "@s2" },
314                                         { "@s2" },
315                                         { "@s3", "/b-" },
316                                         { "@s4", "--f", "B", "/h", "-?", "--help", "-v" },
317                                 },
318                         };
319                         List<string> e = p.Parse (args);
320
321                         Assert.AreEqual (e.Count, 2);
322                         Assert.AreEqual (e[0], "foo");
323                         Assert.AreEqual (e[1], "bar");
324                         Assert.AreEqual (a, 1);
325                         Assert.AreEqual (av, "64");
326                         Assert.AreEqual (b, 2);
327                         Assert.AreEqual (bv, null);
328                         Assert.AreEqual (verbose, 2);
329                         Assert.AreEqual (help, 0x7);
330                         Assert.AreEqual (f, Foo.B);
331                 }
332
333                 [Test]
334                 public void Exceptions ()
335                 {
336                         string a = null;
337                         var p = new OptionSet () {
338                                 { "a=", v => a = v },
339                                 { "b",  v => { } },
340                                 { "c",  v => { } },
341                                 { "n=", (int v) => { } },
342                                 { "f=", (Foo v) => { } },
343                         };
344                         // missing argument
345                         Utils.AssertException (typeof(OptionException), 
346                                         "Missing required value for option '-a'.", 
347                                         p, v => { v.Parse (_("-a")); });
348                         // another named option while expecting one -- follow Getopt::Long
349                         Utils.AssertException (null, null,
350                                         p, v => { v.Parse (_("-a", "-a")); });
351                         Assert.AreEqual (a, "-a");
352                         // no exception when an unregistered named option follows.
353                         Utils.AssertException (null, null, 
354                                         p, v => { v.Parse (_("-a", "-b")); });
355                         Assert.AreEqual (a, "-b");
356                         Utils.AssertException (typeof(ArgumentNullException),
357                                         "Argument cannot be null.\nParameter name: option",
358                                         p, v => { v.Add ((Option) null); });
359                         Utils.AssertException (typeof(ArgumentNullException),
360                                         "Argument cannot be null.\nParameter name: header",
361                                         p, v => { v.Add ((string) null); });
362
363                         // bad type
364                         Utils.AssertException (typeof(OptionException),
365                                         "Could not convert string `value' to type Int32 for option `-n'.",
366                                         p, v => { v.Parse (_("-n", "value")); });
367                         Utils.AssertException (typeof(OptionException),
368                                         "Could not convert string `invalid' to type Foo for option `--f'.",
369                                         p, v => { v.Parse (_("--f", "invalid")); });
370
371                         // try to bundle with an option requiring a value
372                         Utils.AssertException (typeof(OptionException), 
373                                         "Cannot bundle unregistered option '-z'.", 
374                                         p, v => { v.Parse (_("-cz", "extra")); });
375
376                         Utils.AssertException (typeof(ArgumentNullException), 
377                                         "Argument cannot be null.\nParameter name: action",
378                                         p, v => { v.Add ("foo", (Action<string>) null); });
379                         Utils.AssertException (typeof(ArgumentException), 
380                                         "Cannot provide maxValueCount of 2 for OptionValueType.None.\nParameter name: maxValueCount",
381                                         p, v => { v.Add ("foo", (k, val) => {/* ignore */}); });
382                 }
383
384                 [Test]
385                 public void WriteOptionDescriptions ()
386                 {
387                         var p = new OptionSet () {
388                                 "\n:Category 1:",
389                                 { "hidden",             "hidden option, invisible in help",     v => {}, true },
390                                 { "hidden2=",           "hidden option, invisible in help",     (k, v) => {}, true },
391                                 { "p|indicator-style=", "append / indicator to directories",    v => {} },
392                                 { "color:",             "controls color info",                  v => {} },
393                                 { "color2:",            "set {color}",                          v => {} },
394                                 { "rk=",                "required key/value option",            (k, v) => {} },
395                                 { "rk2=",               "required {{foo}} {0:key}/{1:value} option",    (k, v) => {} },
396                                 { "ok:",                "optional key/value option",            (k, v) => {} },
397                                 { "long-desc",
398                                         "This has a really\nlong, multi-line description that also\ntests\n" +
399                                                 "the-builtin-supercalifragilisticexpialidicious-break-on-hyphen.  " + 
400                                                 "Also, a list:\n" +
401                                                 "  item 1\n" +
402                                                 "  item 2",
403                                         v => {} },
404                                 { "long-desc2",
405                                         "IWantThisDescriptionToBreakInsideAWordGeneratingAutoWordHyphenation. ",
406                                         v => {} },
407                                 { "long-desc3",
408                                         "OnlyOnePeriod.AndNoWhitespaceShouldBeSupportedEvenWithLongDescriptions",
409                                         v => {} },
410                                 { "long-desc4",
411                                         "Lots of spaces in the middle 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 and more until the end.",
412                                         v => {} },
413                                 { "long-desc5",
414                                         "Lots of spaces in the middle - . - . - . - . - . - . - . - and more until the end.",
415                                         v => {} },
416                                 "",
417                                 "==This is a really long category name which will involve line wrapping, just because...==",
418                                 { "o|out=",
419                                         "The {DIRECTORY} to place the generated files and directories.\n\n" +
420                                         "If not specified, defaults to\n`dirname FILE`/cache/`basename FILE .tree`.",
421                                         v => {} },
422                                 "",
423                                 "Category 3:",
424                                 { "h|?|help",           "show help text",                       v => {} },
425                                 { "version",            "output version information and exit",  v => {} },
426                                 { "<>", v => {} },
427                                 new TestArgumentSource (new[]{"@s1", "@s2"}, "Read Response File for More Options"),
428                         };
429
430                         StringWriter expected = new StringWriter ();
431                         expected.WriteLine ("");
432                         expected.WriteLine (":Category 1:");
433                         expected.WriteLine ("  -p, --indicator-style=VALUE");
434                         expected.WriteLine ("                             append / indicator to directories");
435                         expected.WriteLine ("      --color[=VALUE]        controls color info");
436                         expected.WriteLine ("      --color2[=color]       set color");
437                         expected.WriteLine ("      --rk=VALUE1:VALUE2     required key/value option");
438                         expected.WriteLine ("      --rk2=key:value        required {foo} key/value option");
439                         expected.WriteLine ("      --ok[=VALUE1:VALUE2]   optional key/value option");
440                         expected.WriteLine ("      --long-desc            This has a really");
441                         expected.WriteLine ("                               long, multi-line description that also");
442                         expected.WriteLine ("                               tests");
443                         expected.WriteLine ("                               the-builtin-supercalifragilisticexpialidicious-");
444                         expected.WriteLine ("                               break-on-hyphen.  Also, a list:");
445                         expected.WriteLine ("                                 item 1");
446                         expected.WriteLine ("                                 item 2");
447                         expected.WriteLine ("      --long-desc2           IWantThisDescriptionToBreakInsideAWordGeneratingAu-");
448                         expected.WriteLine ("                               toWordHyphenation.");
449                         expected.WriteLine ("      --long-desc3           OnlyOnePeriod.");
450                         expected.WriteLine ("                               AndNoWhitespaceShouldBeSupportedEvenWithLongDesc-");
451                         expected.WriteLine ("                               riptions");
452                         expected.WriteLine ("      --long-desc4           Lots of spaces in the middle 1 2 3 4 5 6 7 8 9 0 1");
453                         expected.WriteLine ("                               2 3 4 5 and more until the end.");
454                         expected.WriteLine ("      --long-desc5           Lots of spaces in the middle - . - . - . - . - . -");
455                         expected.WriteLine ("                               . - . - and more until the end.");
456                         expected.WriteLine ("");
457                         expected.WriteLine ("==This is a really long category name which will involve line wrapping, just");
458                         expected.WriteLine ("because...==");
459                         expected.WriteLine ("  -o, --out=DIRECTORY        The DIRECTORY to place the generated files and");
460                         expected.WriteLine ("                               directories.");
461                         expected.WriteLine ("                               ");
462                         expected.WriteLine ("                               If not specified, defaults to");
463                         expected.WriteLine ("                               `dirname FILE`/cache/`basename FILE .tree`.");
464                         expected.WriteLine ("");
465                         expected.WriteLine ("Category 3:");
466                         expected.WriteLine ("  -h, -?, --help             show help text");
467                         expected.WriteLine ("      --version              output version information and exit");
468                         expected.WriteLine ("  @s1, @s2                   Read Response File for More Options");
469
470                         StringWriter actual = new StringWriter ();
471                         p.WriteOptionDescriptions (actual);
472
473                         Assert.AreEqual (expected.ToString (), actual.ToString ());
474                 }
475
476                 [Test]
477                 public void OptionBundling ()
478                 {
479                         OptionBundling (_ ("-abcf", "foo", "bar"));
480                         OptionBundling (_ ("@s1", "foo", "bar"));
481                 }
482
483                 void OptionBundling (IEnumerable<string> args)
484                 {
485                         string a, b, c, f;
486                         a = b = c = f = null;
487                         var p = new OptionSet () {
488                                 { "a", v => a = "a" },
489                                 { "b", v => b = "b" },
490                                 { "c", v => c = "c" },
491                                 { "f=", v => f = v },
492                                 new TestArgumentSource (null, null) {
493                                         { "@s1", "-abcf" },
494                                 },
495                         };
496                         List<string> extra = p.Parse (args);
497                         Assert.AreEqual (extra.Count, 1);
498                         Assert.AreEqual (extra [0], "bar");
499                         Assert.AreEqual (a, "a");
500                         Assert.AreEqual (b, "b");
501                         Assert.AreEqual (c, "c");
502                         Assert.AreEqual (f, "foo");
503                 }
504
505                 [Test]
506                 public void HaltProcessing ()
507                 {
508                         var p = new OptionSet () {
509                                 { "a", v => {} },
510                                 { "b", v => {} },
511                                 new TestArgumentSource (null, null) {
512                                         { "@s1", "-a", "-b" },
513                                 },
514                         };
515                         List<string> e = p.Parse (_ ("-a", "-b", "--", "-a", "-b"));
516                         Assert.AreEqual (e.Count, 2);
517                         Assert.AreEqual (e [0], "-a");
518                         Assert.AreEqual (e [1], "-b");
519
520                         e = p.Parse (_ ("@s1", "--", "@s1"));
521                         Assert.AreEqual (e.Count, 1);
522                         Assert.AreEqual (e [0], "@s1");
523                 }
524
525                 [Test]
526                 public void KeyValueOptions ()
527                 {
528                         var a = new Dictionary<string, string> ();
529                         var b = new Dictionary<int, char> ();
530                         var p = new OptionSet () {
531                                 { "a=", (k,v) => a.Add (k, v) },
532                                 { "b=", (int k, char v) => b.Add (k, v) },
533                                 { "c:", (k, v) => {if (k != null) a.Add (k, v);} },
534                                 { "d={=>}{-->}", (k, v) => a.Add (k, v) },
535                                 { "e={}", (k, v) => a.Add (k, v) },
536                                 { "f=+/", (k, v) => a.Add (k, v) },
537                                 new TestArgumentSource (null, null) {
538                                         { "@s1", "-a", "A" },
539                                         { "@s2", @"C:\tmp", "-a" },
540                                         { "@s3", "C=D", @"-a=E=F:\tmp" },
541                                         { "@s4", "-a:G:H", "-aI=J" },
542                                         { "@s5", "-b", "1" },
543                                         { "@s6", "a", "-b" },
544                                         { "@s7", "2", "b" },
545                                         { "@s8", "-dA=>B", "-d" },
546                                         { "@s9", "C-->D", "-d:E" },
547                                         { "@s10", "F", "-d" },
548                                         { "@s11", "G", "H" },
549                                         { "@s12", "-dJ-->K" }
550                                 },
551                         };
552                         p.Parse (_("-a", "A", @"C:\tmp", "-a", "C=D", @"-a=E=F:\tmp", "-a:G:H", "-aI=J", "-b", "1", "a", "-b", "2", "b"));
553                         Action assert = () => {
554                                 AssertDictionary (a, 
555                                                 "A", @"C:\tmp", 
556                                                 "C", "D", 
557                                                 "E", @"F:\tmp", 
558                                                 "G", "H", 
559                                                 "I", "J");
560                                 AssertDictionary (b,
561                                                 "1", "a",
562                                                 "2", "b");
563                         };
564                         assert ();
565                         a.Clear ();
566                         b.Clear ();
567
568                         p.Parse (_("@s1", "@s2", "@s3", "@s4", "@s5", "@s6", "@s7"));
569                         assert ();
570                         a.Clear ();
571                         b.Clear ();
572
573                         p.Parse (_("-c"));
574                         Assert.AreEqual (a.Count, 0);
575                         p.Parse (_("-c", "a"));
576                         Assert.AreEqual (a.Count, 0);
577                         p.Parse (_("-ca"));
578                         AssertDictionary (a, "a", null);
579                         a.Clear ();
580                         p.Parse (_("-ca=b"));
581                         AssertDictionary (a, "a", "b");
582
583                         a.Clear ();
584                         p.Parse (_("-dA=>B", "-d", "C-->D", "-d:E", "F", "-d", "G", "H", "-dJ-->K"));
585                         assert = () => {
586                                 AssertDictionary (a,
587                                                 "A", "B",
588                                                 "C", "D", 
589                                                 "E", "F",
590                                                 "G", "H",
591                                                 "J", "K");
592                         };
593                         assert ();
594                         a.Clear ();
595
596                         p.Parse (_("@s8", "@s9", "@s10", "@s11", "@s12"));
597                         assert ();
598                         a.Clear ();
599
600                         p.Parse (_("-eA=B", "-eC=D", "-eE", "F", "-e:G", "H"));
601                         AssertDictionary (a,
602                                         "A=B", "-eC=D",
603                                         "E", "F", 
604                                         "G", "H");
605
606                         a.Clear ();
607                         p.Parse (_("-f1/2", "-f=3/4", "-f:5+6", "-f7", "8", "-f9=10", "-f11=12"));
608                         AssertDictionary (a,
609                                         "1", "2",
610                                         "3", "4",
611                                         "5", "6", 
612                                         "7", "8", 
613                                         "9=10", "-f11=12");
614                 }
615
616                 static void AssertDictionary<TKey, TValue> (Dictionary<TKey, TValue> dict, params string[] set)
617                 {
618                         TypeConverter k = TypeDescriptor.GetConverter (typeof (TKey));
619                         TypeConverter v = TypeDescriptor.GetConverter (typeof (TValue));
620
621                         Assert.AreEqual (dict.Count, set.Length / 2);
622                         for (int i = 0; i < set.Length; i += 2) {
623                                 TKey key = (TKey) k.ConvertFromString (set [i]);
624                                 Assert.AreEqual (dict.ContainsKey (key), true);
625                                 if (set [i+1] == null)
626                                         Assert.AreEqual (dict [key], default (TValue));
627                                 else
628                                         Assert.AreEqual (dict [key], (TValue) v.ConvertFromString (set [i+1]));
629                         }
630                 }
631
632                 class CustomOption : Option {
633                         Action<OptionValueCollection> action;
634
635                         public CustomOption (string p, string d, int c, Action<OptionValueCollection> a)
636                                 : base (p, d, c)
637                         {
638                                 this.action = a;
639                         }
640
641                         protected override void OnParseComplete (OptionContext c)
642                         {
643                                 action (c.OptionValues);
644                         }
645                 }
646
647                 [Test]
648                 public void CustomKeyValue ()
649                 {
650                         var a = new Dictionary<string, string> ();
651                         var b = new Dictionary<string, string[]> ();
652                         var p = new OptionSet () {
653                                 new CustomOption ("a==:", null, 2, v => a.Add (v [0], v [1])),
654                                 new CustomOption ("b==:", null, 3, v => b.Add (v [0], new string[]{v [1], v [2]})),
655                         };
656                         p.Parse (_(@"-a=b=C:\tmp", "-a=d", @"C:\e", @"-a:f=C:\g", @"-a:h:C:\i", "-a", @"j=C:\k", "-a", @"l:C:\m"));
657                         Assert.AreEqual (a.Count, 6);
658                         Assert.AreEqual (a ["b"], @"C:\tmp");
659                         Assert.AreEqual (a ["d"], @"C:\e");
660                         Assert.AreEqual (a ["f"], @"C:\g");
661                         Assert.AreEqual (a ["h"], @"C:\i");
662                         Assert.AreEqual (a ["j"], @"C:\k");
663                         Assert.AreEqual (a ["l"], @"C:\m");
664
665                         Utils.AssertException (typeof(OptionException),
666                                         "Missing required value for option '-a'.",
667                                         p, v => {v.Parse (_("-a=b"));});
668
669                         p.Parse (_("-b", "a", "b", @"C:\tmp", @"-b:d:e:F:\tmp", @"-b=g=h:i:\tmp", @"-b:j=k:l:\tmp"));
670                         Assert.AreEqual (b.Count, 4);
671                         Assert.AreEqual (b ["a"][0], "b");
672                         Assert.AreEqual (b ["a"][1], @"C:\tmp");
673                         Assert.AreEqual (b ["d"][0], "e");
674                         Assert.AreEqual (b ["d"][1], @"F:\tmp");
675                         Assert.AreEqual (b ["g"][0], "h");
676                         Assert.AreEqual (b ["g"][1], @"i:\tmp");
677                         Assert.AreEqual (b ["j"][0], "k");
678                         Assert.AreEqual (b ["j"][1], @"l:\tmp");
679                 }
680
681                 [Test]
682                 public void Localization ()
683                 {
684                         var p = new OptionSet (f => "hello!") {
685                                 { "n=", (int v) => { } },
686                         };
687                         Utils.AssertException (typeof(OptionException), "hello!",
688                                         p, v => { v.Parse (_("-n=value")); });
689
690                         StringWriter expected = new StringWriter ();
691                         expected.WriteLine ("  -nhello!                   hello!");
692
693                         StringWriter actual = new StringWriter ();
694                         p.WriteOptionDescriptions (actual);
695
696                         Assert.AreEqual (actual.ToString (), expected.ToString ());
697                 }
698
699                 class CiOptionSet : OptionSet {
700                         protected override void InsertItem (int index, Option item)
701                         {
702                                 if (item.Prototype.ToLower () != item.Prototype)
703                                         throw new ArgumentException ("prototypes must be null!");
704                                 base.InsertItem (index, item);
705                         }
706
707                         protected override bool Parse (string option, OptionContext c)
708                         {
709                                 if (c.Option != null)
710                                         return base.Parse (option, c);
711                                 string f, n, s, v;
712                                 if (!GetOptionParts (option, out f, out n, out s, out v)) {
713                                         return base.Parse (option, c);
714                                 }
715                                 return base.Parse (f + n.ToLower () + (v != null && s != null ? s + v : ""), c);
716                         }
717
718                         public new Option GetOptionForName (string n)
719                         {
720                                 return base.GetOptionForName (n);
721                         }
722
723                         public void CheckOptionParts (string option, bool er, string ef, string en, string es, string ev)
724                         {
725                                 string f, n, s, v;
726                                 bool r = GetOptionParts (option, out f, out n, out s, out v);
727                                 Assert.AreEqual (r, er);
728                                 Assert.AreEqual (f, ef);
729                                 Assert.AreEqual (n, en);
730                                 Assert.AreEqual (s, es);
731                                 Assert.AreEqual (v, ev);
732                         }
733                 }
734
735                 [Test]
736                 public void DerivedType ()
737                 {
738                         bool help = false;
739                         var p = new CiOptionSet () {
740                                 { "h|help", v => help = v != null },
741                         };
742                         p.Parse (_("-H"));
743                         Assert.AreEqual (help, true);
744                         help = false;
745                         p.Parse (_("-HELP"));
746                         Assert.AreEqual (help, true);
747
748                         Assert.AreEqual (p.GetOptionForName ("h"), p [0]);
749                         Assert.AreEqual (p.GetOptionForName ("help"), p [0]);
750                         Assert.AreEqual (p.GetOptionForName ("invalid"), null);
751
752                         Utils.AssertException (typeof(ArgumentException), "prototypes must be null!",
753                                         p, v => { v.Add ("N|NUM=", (int n) => {}); });
754                         Utils.AssertException (typeof(ArgumentNullException),
755                                         "Argument cannot be null.\nParameter name: option",
756                                         p, v => { v.GetOptionForName (null); });
757                 }
758
759                 [Test]
760                 public void OptionParts ()
761                 {
762                         var p = new CiOptionSet ();
763                         p.CheckOptionParts ("A",        false,  null, null, null, null);
764                         p.CheckOptionParts ("A=B",      false,  null, null, null, null);
765                         p.CheckOptionParts ("-A=B",     true,   "-",  "A",  "=",  "B");
766                         p.CheckOptionParts ("-A:B",     true,   "-",  "A",  ":",  "B");
767                         p.CheckOptionParts ("--A=B",    true,   "--", "A",  "=",  "B");
768                         p.CheckOptionParts ("--A:B",    true,   "--", "A",  ":",  "B");
769                         p.CheckOptionParts ("/A=B",     true,   "/",  "A",  "=",  "B");
770                         p.CheckOptionParts ("/A:B",     true,   "/",  "A",  ":",  "B");
771                         p.CheckOptionParts ("-A=B=C",   true,   "-",  "A",  "=",  "B=C");
772                         p.CheckOptionParts ("-A:B=C",   true,   "-",  "A",  ":",  "B=C");
773                         p.CheckOptionParts ("-A:B:C",   true,   "-",  "A",  ":",  "B:C");
774                         p.CheckOptionParts ("--A=B=C",  true,   "--", "A",  "=",  "B=C");
775                         p.CheckOptionParts ("--A:B=C",  true,   "--", "A",  ":",  "B=C");
776                         p.CheckOptionParts ("--A:B:C",  true,   "--", "A",  ":",  "B:C");
777                         p.CheckOptionParts ("/A=B=C",   true,   "/",  "A",  "=",  "B=C");
778                         p.CheckOptionParts ("/A:B=C",   true,   "/",  "A",  ":",  "B=C");
779                         p.CheckOptionParts ("/A:B:C",   true,   "/",  "A",  ":",  "B:C");
780                         p.CheckOptionParts ("-AB=C",    true,   "-",  "AB", "=",  "C");
781                         p.CheckOptionParts ("-AB:C",    true,   "-",  "AB", ":",  "C");
782                 }
783
784                 class ContextCheckerOption : Option {
785                         string eName, eValue;
786                         int index;
787
788                         public ContextCheckerOption (string p, string d, string eName, string eValue, int index)
789                                 : base (p, d)
790                         {
791                                 this.eName  = eName;
792                                 this.eValue = eValue;
793                                 this.index  = index;
794                         }
795
796                         protected override void OnParseComplete (OptionContext c)
797                         {
798                                 Assert.AreEqual (c.OptionValues.Count, 1);
799                                 Assert.AreEqual (c.OptionValues [0], eValue);
800                                 Assert.AreEqual (c.OptionName, eName);
801                                 Assert.AreEqual (c.OptionIndex, index);
802                                 Assert.AreEqual (c.Option, this);
803                                 Assert.AreEqual (c.Option.Description, base.Description);
804                         }
805                 }
806
807                 [Test]
808                 public void OptionContext ()
809                 {
810                         var p = new OptionSet () {
811                                 new ContextCheckerOption ("a=", "a desc", "/a",   "a-val", 1),
812                                 new ContextCheckerOption ("b",  "b desc", "--b+", "--b+",  2),
813                                 new ContextCheckerOption ("c=", "c desc", "--c",  "C",     3),
814                                 new ContextCheckerOption ("d",  "d desc", "/d-",  null,    4),
815                         };
816                         Assert.AreEqual (p.Count, 4);
817                         p.Parse (_("/a", "a-val", "--b+", "--c=C", "/d-"));
818                 }
819
820                 [Test]
821                 public void DefaultHandler ()
822                 {
823                         var extra = new List<string> ();
824                         var p = new OptionSet () {
825                                 { "<>", v => extra.Add (v) },
826                         };
827                         var e = p.Parse (_("-a", "b", "--c=D", "E"));
828                         Assert.AreEqual (e.Count, 0);
829                         Assert.AreEqual (extra.Count, 4);
830                         Assert.AreEqual (extra [0], "-a");
831                         Assert.AreEqual (extra [1], "b");
832                         Assert.AreEqual (extra [2], "--c=D");
833                         Assert.AreEqual (extra [3], "E");
834                 }
835
836                 [Test]
837                 public void MixedDefaultHandler ()
838                 {
839                         var tests = new List<string> ();
840                         var p = new OptionSet () {
841                                 { "t|<>=", v => tests.Add (v) },
842                         };
843                         var e = p.Parse (_("-tA", "-t:B", "-t=C", "D", "--E=F"));
844                         Assert.AreEqual (e.Count, 0);
845                         Assert.AreEqual (tests.Count, 5);
846                         Assert.AreEqual (tests [0], "A");
847                         Assert.AreEqual (tests [1], "B");
848                         Assert.AreEqual (tests [2], "C");
849                         Assert.AreEqual (tests [3], "D");
850                         Assert.AreEqual (tests [4], "--E=F");
851                 }
852
853                 [Test]
854                 public void DefaultHandlerRuns ()
855                 {
856                         var formats = new Dictionary<string, List<string>> ();
857                         string format = "foo";
858                         var p = new OptionSet () {
859                                 { "f|format=", v => format = v },
860                                 { "<>", 
861                                         v => {
862                                                 List<string> f;
863                                                 if (!formats.TryGetValue (format, out f)) {
864                                                         f = new List<string> ();
865                                                         formats.Add (format, f);
866                                                 }
867                                                 f.Add (v);
868                                 } },
869                         };
870                         var e = p.Parse (_("a", "b", "-fbar", "c", "d", "--format=baz", "e", "f"));
871                         Assert.AreEqual (e.Count, 0);
872                         Assert.AreEqual (formats.Count, 3);
873                         Assert.AreEqual (formats ["foo"].Count, 2);
874                         Assert.AreEqual (formats ["foo"][0], "a");
875                         Assert.AreEqual (formats ["foo"][1], "b");
876                         Assert.AreEqual (formats ["bar"].Count, 2);
877                         Assert.AreEqual (formats ["bar"][0], "c");
878                         Assert.AreEqual (formats ["bar"][1], "d");
879                         Assert.AreEqual (formats ["baz"].Count, 2);
880                         Assert.AreEqual (formats ["baz"][0], "e");
881                         Assert.AreEqual (formats ["baz"][1], "f");
882                 }
883         }
884 }
885