Fix null sessions in HttpContextWrapper.Session
[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                                 { "p|indicator-style=", "append / indicator to directories",    v => {} },
390                                 { "color:",             "controls color info",                  v => {} },
391                                 { "color2:",            "set {color}",                          v => {} },
392                                 { "rk=",                "required key/value option",            (k, v) => {} },
393                                 { "rk2=",               "required {{foo}} {0:key}/{1:value} option",    (k, v) => {} },
394                                 { "ok:",                "optional key/value option",            (k, v) => {} },
395                                 { "long-desc",
396                                         "This has a really\nlong, multi-line description that also\ntests\n" +
397                                                 "the-builtin-supercalifragilisticexpialidicious-break-on-hyphen.  " + 
398                                                 "Also, a list:\n" +
399                                                 "  item 1\n" +
400                                                 "  item 2",
401                                         v => {} },
402                                 { "long-desc2",
403                                         "IWantThisDescriptionToBreakInsideAWordGeneratingAutoWordHyphenation. ",
404                                         v => {} },
405                                 { "long-desc3",
406                                         "OnlyOnePeriod.AndNoWhitespaceShouldBeSupportedEvenWithLongDescriptions",
407                                         v => {} },
408                                 { "long-desc4",
409                                         "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.",
410                                         v => {} },
411                                 { "long-desc5",
412                                         "Lots of spaces in the middle - . - . - . - . - . - . - . - and more until the end.",
413                                         v => {} },
414                                 "",
415                                 "==This is a really long category name which will involve line wrapping, just because...==",
416                                 { "o|out=",
417                                         "The {DIRECTORY} to place the generated files and directories.\n\n" +
418                                         "If not specified, defaults to\n`dirname FILE`/cache/`basename FILE .tree`.",
419                                         v => {} },
420                                 "",
421                                 "Category 3:",
422                                 { "h|?|help",           "show help text",                       v => {} },
423                                 { "version",            "output version information and exit",  v => {} },
424                                 { "<>", v => {} },
425                                 new TestArgumentSource (new[]{"@s1", "@s2"}, "Read Response File for More Options"),
426                         };
427
428                         StringWriter expected = new StringWriter ();
429                         expected.WriteLine ("");
430                         expected.WriteLine (":Category 1:");
431                         expected.WriteLine ("  -p, --indicator-style=VALUE");
432                         expected.WriteLine ("                             append / indicator to directories");
433                         expected.WriteLine ("      --color[=VALUE]        controls color info");
434                         expected.WriteLine ("      --color2[=color]       set color");
435                         expected.WriteLine ("      --rk=VALUE1:VALUE2     required key/value option");
436                         expected.WriteLine ("      --rk2=key:value        required {foo} key/value option");
437                         expected.WriteLine ("      --ok[=VALUE1:VALUE2]   optional key/value option");
438                         expected.WriteLine ("      --long-desc            This has a really");
439                         expected.WriteLine ("                               long, multi-line description that also");
440                         expected.WriteLine ("                               tests");
441                         expected.WriteLine ("                               the-builtin-supercalifragilisticexpialidicious-");
442                         expected.WriteLine ("                               break-on-hyphen.  Also, a list:");
443                         expected.WriteLine ("                                 item 1");
444                         expected.WriteLine ("                                 item 2");
445                         expected.WriteLine ("      --long-desc2           IWantThisDescriptionToBreakInsideAWordGeneratingAu-");
446                         expected.WriteLine ("                               toWordHyphenation.");
447                         expected.WriteLine ("      --long-desc3           OnlyOnePeriod.");
448                         expected.WriteLine ("                               AndNoWhitespaceShouldBeSupportedEvenWithLongDesc-");
449                         expected.WriteLine ("                               riptions");
450                         expected.WriteLine ("      --long-desc4           Lots of spaces in the middle 1 2 3 4 5 6 7 8 9 0 1");
451                         expected.WriteLine ("                               2 3 4 5 and more until the end.");
452                         expected.WriteLine ("      --long-desc5           Lots of spaces in the middle - . - . - . - . - . -");
453                         expected.WriteLine ("                               . - . - and more until the end.");
454                         expected.WriteLine ("");
455                         expected.WriteLine ("==This is a really long category name which will involve line wrapping, just");
456                         expected.WriteLine ("because...==");
457                         expected.WriteLine ("  -o, --out=DIRECTORY        The DIRECTORY to place the generated files and");
458                         expected.WriteLine ("                               directories.");
459                         expected.WriteLine ("                               ");
460                         expected.WriteLine ("                               If not specified, defaults to");
461                         expected.WriteLine ("                               `dirname FILE`/cache/`basename FILE .tree`.");
462                         expected.WriteLine ("");
463                         expected.WriteLine ("Category 3:");
464                         expected.WriteLine ("  -h, -?, --help             show help text");
465                         expected.WriteLine ("      --version              output version information and exit");
466                         expected.WriteLine ("  @s1, @s2                   Read Response File for More Options");
467
468                         StringWriter actual = new StringWriter ();
469                         p.WriteOptionDescriptions (actual);
470
471                         Assert.AreEqual (expected.ToString (), actual.ToString ());
472                 }
473
474                 [Test]
475                 public void OptionBundling ()
476                 {
477                         OptionBundling (_ ("-abcf", "foo", "bar"));
478                         OptionBundling (_ ("@s1", "foo", "bar"));
479                 }
480
481                 void OptionBundling (IEnumerable<string> args)
482                 {
483                         string a, b, c, f;
484                         a = b = c = f = null;
485                         var p = new OptionSet () {
486                                 { "a", v => a = "a" },
487                                 { "b", v => b = "b" },
488                                 { "c", v => c = "c" },
489                                 { "f=", v => f = v },
490                                 new TestArgumentSource (null, null) {
491                                         { "@s1", "-abcf" },
492                                 },
493                         };
494                         List<string> extra = p.Parse (args);
495                         Assert.AreEqual (extra.Count, 1);
496                         Assert.AreEqual (extra [0], "bar");
497                         Assert.AreEqual (a, "a");
498                         Assert.AreEqual (b, "b");
499                         Assert.AreEqual (c, "c");
500                         Assert.AreEqual (f, "foo");
501                 }
502
503                 [Test]
504                 public void HaltProcessing ()
505                 {
506                         var p = new OptionSet () {
507                                 { "a", v => {} },
508                                 { "b", v => {} },
509                                 new TestArgumentSource (null, null) {
510                                         { "@s1", "-a", "-b" },
511                                 },
512                         };
513                         List<string> e = p.Parse (_ ("-a", "-b", "--", "-a", "-b"));
514                         Assert.AreEqual (e.Count, 2);
515                         Assert.AreEqual (e [0], "-a");
516                         Assert.AreEqual (e [1], "-b");
517
518                         e = p.Parse (_ ("@s1", "--", "@s1"));
519                         Assert.AreEqual (e.Count, 1);
520                         Assert.AreEqual (e [0], "@s1");
521                 }
522
523                 [Test]
524                 public void KeyValueOptions ()
525                 {
526                         var a = new Dictionary<string, string> ();
527                         var b = new Dictionary<int, char> ();
528                         var p = new OptionSet () {
529                                 { "a=", (k,v) => a.Add (k, v) },
530                                 { "b=", (int k, char v) => b.Add (k, v) },
531                                 { "c:", (k, v) => {if (k != null) a.Add (k, v);} },
532                                 { "d={=>}{-->}", (k, v) => a.Add (k, v) },
533                                 { "e={}", (k, v) => a.Add (k, v) },
534                                 { "f=+/", (k, v) => a.Add (k, v) },
535                                 new TestArgumentSource (null, null) {
536                                         { "@s1", "-a", "A" },
537                                         { "@s2", @"C:\tmp", "-a" },
538                                         { "@s3", "C=D", @"-a=E=F:\tmp" },
539                                         { "@s4", "-a:G:H", "-aI=J" },
540                                         { "@s5", "-b", "1" },
541                                         { "@s6", "a", "-b" },
542                                         { "@s7", "2", "b" },
543                                         { "@s8", "-dA=>B", "-d" },
544                                         { "@s9", "C-->D", "-d:E" },
545                                         { "@s10", "F", "-d" },
546                                         { "@s11", "G", "H" },
547                                         { "@s12", "-dJ-->K" }
548                                 },
549                         };
550                         p.Parse (_("-a", "A", @"C:\tmp", "-a", "C=D", @"-a=E=F:\tmp", "-a:G:H", "-aI=J", "-b", "1", "a", "-b", "2", "b"));
551                         Action assert = () => {
552                                 AssertDictionary (a, 
553                                                 "A", @"C:\tmp", 
554                                                 "C", "D", 
555                                                 "E", @"F:\tmp", 
556                                                 "G", "H", 
557                                                 "I", "J");
558                                 AssertDictionary (b,
559                                                 "1", "a",
560                                                 "2", "b");
561                         };
562                         assert ();
563                         a.Clear ();
564                         b.Clear ();
565
566                         p.Parse (_("@s1", "@s2", "@s3", "@s4", "@s5", "@s6", "@s7"));
567                         assert ();
568                         a.Clear ();
569                         b.Clear ();
570
571                         p.Parse (_("-c"));
572                         Assert.AreEqual (a.Count, 0);
573                         p.Parse (_("-c", "a"));
574                         Assert.AreEqual (a.Count, 0);
575                         p.Parse (_("-ca"));
576                         AssertDictionary (a, "a", null);
577                         a.Clear ();
578                         p.Parse (_("-ca=b"));
579                         AssertDictionary (a, "a", "b");
580
581                         a.Clear ();
582                         p.Parse (_("-dA=>B", "-d", "C-->D", "-d:E", "F", "-d", "G", "H", "-dJ-->K"));
583                         assert = () => {
584                                 AssertDictionary (a,
585                                                 "A", "B",
586                                                 "C", "D", 
587                                                 "E", "F",
588                                                 "G", "H",
589                                                 "J", "K");
590                         };
591                         assert ();
592                         a.Clear ();
593
594                         p.Parse (_("@s8", "@s9", "@s10", "@s11", "@s12"));
595                         assert ();
596                         a.Clear ();
597
598                         p.Parse (_("-eA=B", "-eC=D", "-eE", "F", "-e:G", "H"));
599                         AssertDictionary (a,
600                                         "A=B", "-eC=D",
601                                         "E", "F", 
602                                         "G", "H");
603
604                         a.Clear ();
605                         p.Parse (_("-f1/2", "-f=3/4", "-f:5+6", "-f7", "8", "-f9=10", "-f11=12"));
606                         AssertDictionary (a,
607                                         "1", "2",
608                                         "3", "4",
609                                         "5", "6", 
610                                         "7", "8", 
611                                         "9=10", "-f11=12");
612                 }
613
614                 static void AssertDictionary<TKey, TValue> (Dictionary<TKey, TValue> dict, params string[] set)
615                 {
616                         TypeConverter k = TypeDescriptor.GetConverter (typeof (TKey));
617                         TypeConverter v = TypeDescriptor.GetConverter (typeof (TValue));
618
619                         Assert.AreEqual (dict.Count, set.Length / 2);
620                         for (int i = 0; i < set.Length; i += 2) {
621                                 TKey key = (TKey) k.ConvertFromString (set [i]);
622                                 Assert.AreEqual (dict.ContainsKey (key), true);
623                                 if (set [i+1] == null)
624                                         Assert.AreEqual (dict [key], default (TValue));
625                                 else
626                                         Assert.AreEqual (dict [key], (TValue) v.ConvertFromString (set [i+1]));
627                         }
628                 }
629
630                 class CustomOption : Option {
631                         Action<OptionValueCollection> action;
632
633                         public CustomOption (string p, string d, int c, Action<OptionValueCollection> a)
634                                 : base (p, d, c)
635                         {
636                                 this.action = a;
637                         }
638
639                         protected override void OnParseComplete (OptionContext c)
640                         {
641                                 action (c.OptionValues);
642                         }
643                 }
644
645                 [Test]
646                 public void CustomKeyValue ()
647                 {
648                         var a = new Dictionary<string, string> ();
649                         var b = new Dictionary<string, string[]> ();
650                         var p = new OptionSet () {
651                                 new CustomOption ("a==:", null, 2, v => a.Add (v [0], v [1])),
652                                 new CustomOption ("b==:", null, 3, v => b.Add (v [0], new string[]{v [1], v [2]})),
653                         };
654                         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"));
655                         Assert.AreEqual (a.Count, 6);
656                         Assert.AreEqual (a ["b"], @"C:\tmp");
657                         Assert.AreEqual (a ["d"], @"C:\e");
658                         Assert.AreEqual (a ["f"], @"C:\g");
659                         Assert.AreEqual (a ["h"], @"C:\i");
660                         Assert.AreEqual (a ["j"], @"C:\k");
661                         Assert.AreEqual (a ["l"], @"C:\m");
662
663                         Utils.AssertException (typeof(OptionException),
664                                         "Missing required value for option '-a'.",
665                                         p, v => {v.Parse (_("-a=b"));});
666
667                         p.Parse (_("-b", "a", "b", @"C:\tmp", @"-b:d:e:F:\tmp", @"-b=g=h:i:\tmp", @"-b:j=k:l:\tmp"));
668                         Assert.AreEqual (b.Count, 4);
669                         Assert.AreEqual (b ["a"][0], "b");
670                         Assert.AreEqual (b ["a"][1], @"C:\tmp");
671                         Assert.AreEqual (b ["d"][0], "e");
672                         Assert.AreEqual (b ["d"][1], @"F:\tmp");
673                         Assert.AreEqual (b ["g"][0], "h");
674                         Assert.AreEqual (b ["g"][1], @"i:\tmp");
675                         Assert.AreEqual (b ["j"][0], "k");
676                         Assert.AreEqual (b ["j"][1], @"l:\tmp");
677                 }
678
679                 [Test]
680                 public void Localization ()
681                 {
682                         var p = new OptionSet (f => "hello!") {
683                                 { "n=", (int v) => { } },
684                         };
685                         Utils.AssertException (typeof(OptionException), "hello!",
686                                         p, v => { v.Parse (_("-n=value")); });
687
688                         StringWriter expected = new StringWriter ();
689                         expected.WriteLine ("  -nhello!                   hello!");
690
691                         StringWriter actual = new StringWriter ();
692                         p.WriteOptionDescriptions (actual);
693
694                         Assert.AreEqual (actual.ToString (), expected.ToString ());
695                 }
696
697                 class CiOptionSet : OptionSet {
698                         protected override void InsertItem (int index, Option item)
699                         {
700                                 if (item.Prototype.ToLower () != item.Prototype)
701                                         throw new ArgumentException ("prototypes must be null!");
702                                 base.InsertItem (index, item);
703                         }
704
705                         protected override bool Parse (string option, OptionContext c)
706                         {
707                                 if (c.Option != null)
708                                         return base.Parse (option, c);
709                                 string f, n, s, v;
710                                 if (!GetOptionParts (option, out f, out n, out s, out v)) {
711                                         return base.Parse (option, c);
712                                 }
713                                 return base.Parse (f + n.ToLower () + (v != null && s != null ? s + v : ""), c);
714                         }
715
716                         public new Option GetOptionForName (string n)
717                         {
718                                 return base.GetOptionForName (n);
719                         }
720
721                         public void CheckOptionParts (string option, bool er, string ef, string en, string es, string ev)
722                         {
723                                 string f, n, s, v;
724                                 bool r = GetOptionParts (option, out f, out n, out s, out v);
725                                 Assert.AreEqual (r, er);
726                                 Assert.AreEqual (f, ef);
727                                 Assert.AreEqual (n, en);
728                                 Assert.AreEqual (s, es);
729                                 Assert.AreEqual (v, ev);
730                         }
731                 }
732
733                 [Test]
734                 public void DerivedType ()
735                 {
736                         bool help = false;
737                         var p = new CiOptionSet () {
738                                 { "h|help", v => help = v != null },
739                         };
740                         p.Parse (_("-H"));
741                         Assert.AreEqual (help, true);
742                         help = false;
743                         p.Parse (_("-HELP"));
744                         Assert.AreEqual (help, true);
745
746                         Assert.AreEqual (p.GetOptionForName ("h"), p [0]);
747                         Assert.AreEqual (p.GetOptionForName ("help"), p [0]);
748                         Assert.AreEqual (p.GetOptionForName ("invalid"), null);
749
750                         Utils.AssertException (typeof(ArgumentException), "prototypes must be null!",
751                                         p, v => { v.Add ("N|NUM=", (int n) => {}); });
752                         Utils.AssertException (typeof(ArgumentNullException),
753                                         "Argument cannot be null.\nParameter name: option",
754                                         p, v => { v.GetOptionForName (null); });
755                 }
756
757                 [Test]
758                 public void OptionParts ()
759                 {
760                         var p = new CiOptionSet ();
761                         p.CheckOptionParts ("A",        false,  null, null, null, null);
762                         p.CheckOptionParts ("A=B",      false,  null, null, null, null);
763                         p.CheckOptionParts ("-A=B",     true,   "-",  "A",  "=",  "B");
764                         p.CheckOptionParts ("-A:B",     true,   "-",  "A",  ":",  "B");
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=C",   true,   "-",  "A",  "=",  "B=C");
770                         p.CheckOptionParts ("-A:B=C",   true,   "-",  "A",  ":",  "B=C");
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 ("-AB=C",    true,   "-",  "AB", "=",  "C");
779                         p.CheckOptionParts ("-AB:C",    true,   "-",  "AB", ":",  "C");
780                 }
781
782                 class ContextCheckerOption : Option {
783                         string eName, eValue;
784                         int index;
785
786                         public ContextCheckerOption (string p, string d, string eName, string eValue, int index)
787                                 : base (p, d)
788                         {
789                                 this.eName  = eName;
790                                 this.eValue = eValue;
791                                 this.index  = index;
792                         }
793
794                         protected override void OnParseComplete (OptionContext c)
795                         {
796                                 Assert.AreEqual (c.OptionValues.Count, 1);
797                                 Assert.AreEqual (c.OptionValues [0], eValue);
798                                 Assert.AreEqual (c.OptionName, eName);
799                                 Assert.AreEqual (c.OptionIndex, index);
800                                 Assert.AreEqual (c.Option, this);
801                                 Assert.AreEqual (c.Option.Description, base.Description);
802                         }
803                 }
804
805                 [Test]
806                 public void OptionContext ()
807                 {
808                         var p = new OptionSet () {
809                                 new ContextCheckerOption ("a=", "a desc", "/a",   "a-val", 1),
810                                 new ContextCheckerOption ("b",  "b desc", "--b+", "--b+",  2),
811                                 new ContextCheckerOption ("c=", "c desc", "--c",  "C",     3),
812                                 new ContextCheckerOption ("d",  "d desc", "/d-",  null,    4),
813                         };
814                         Assert.AreEqual (p.Count, 4);
815                         p.Parse (_("/a", "a-val", "--b+", "--c=C", "/d-"));
816                 }
817
818                 [Test]
819                 public void DefaultHandler ()
820                 {
821                         var extra = new List<string> ();
822                         var p = new OptionSet () {
823                                 { "<>", v => extra.Add (v) },
824                         };
825                         var e = p.Parse (_("-a", "b", "--c=D", "E"));
826                         Assert.AreEqual (e.Count, 0);
827                         Assert.AreEqual (extra.Count, 4);
828                         Assert.AreEqual (extra [0], "-a");
829                         Assert.AreEqual (extra [1], "b");
830                         Assert.AreEqual (extra [2], "--c=D");
831                         Assert.AreEqual (extra [3], "E");
832                 }
833
834                 [Test]
835                 public void MixedDefaultHandler ()
836                 {
837                         var tests = new List<string> ();
838                         var p = new OptionSet () {
839                                 { "t|<>=", v => tests.Add (v) },
840                         };
841                         var e = p.Parse (_("-tA", "-t:B", "-t=C", "D", "--E=F"));
842                         Assert.AreEqual (e.Count, 0);
843                         Assert.AreEqual (tests.Count, 5);
844                         Assert.AreEqual (tests [0], "A");
845                         Assert.AreEqual (tests [1], "B");
846                         Assert.AreEqual (tests [2], "C");
847                         Assert.AreEqual (tests [3], "D");
848                         Assert.AreEqual (tests [4], "--E=F");
849                 }
850
851                 [Test]
852                 public void DefaultHandlerRuns ()
853                 {
854                         var formats = new Dictionary<string, List<string>> ();
855                         string format = "foo";
856                         var p = new OptionSet () {
857                                 { "f|format=", v => format = v },
858                                 { "<>", 
859                                         v => {
860                                                 List<string> f;
861                                                 if (!formats.TryGetValue (format, out f)) {
862                                                         f = new List<string> ();
863                                                         formats.Add (format, f);
864                                                 }
865                                                 f.Add (v);
866                                 } },
867                         };
868                         var e = p.Parse (_("a", "b", "-fbar", "c", "d", "--format=baz", "e", "f"));
869                         Assert.AreEqual (e.Count, 0);
870                         Assert.AreEqual (formats.Count, 3);
871                         Assert.AreEqual (formats ["foo"].Count, 2);
872                         Assert.AreEqual (formats ["foo"][0], "a");
873                         Assert.AreEqual (formats ["foo"][1], "b");
874                         Assert.AreEqual (formats ["bar"].Count, 2);
875                         Assert.AreEqual (formats ["bar"][0], "c");
876                         Assert.AreEqual (formats ["bar"][1], "d");
877                         Assert.AreEqual (formats ["baz"].Count, 2);
878                         Assert.AreEqual (formats ["baz"][0], "e");
879                         Assert.AreEqual (formats ["baz"][1], "f");
880                 }
881         }
882 }
883