1 // 2 // OptionTest.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 31 #if NDESK_OPTIONS 32 using NDesk.Options; 33 #else 34 using Mono.Options; 35 #endif 36 37 using NUnit.Framework; 38 39 #if NDESK_OPTIONS 40 namespace Tests.NDesk.Options 41 #else 42 namespace MonoTests.Mono.Options 43 #endif 44 { 45 class DefaultOption : Option { DefaultOption(string prototypes, string description)46 public DefaultOption (string prototypes, string description) 47 : base (prototypes, description) 48 { 49 } 50 DefaultOption(string prototypes, string description, int c)51 public DefaultOption (string prototypes, string description, int c) 52 : base (prototypes, description, c) 53 { 54 } 55 OnParseComplete(OptionContext c)56 protected override void OnParseComplete (OptionContext c) 57 { 58 throw new NotImplementedException (); 59 } 60 } 61 62 [TestFixture] 63 public class OptionTest { 64 [Test] Exceptions()65 public void Exceptions () 66 { 67 object p = null; 68 Utils.AssertException (typeof(ArgumentNullException), 69 $"Value cannot be null.{Environment.NewLine}Parameter name: prototype", 70 p, v => { new DefaultOption (null, null); }); 71 Utils.AssertException (typeof(ArgumentException), 72 $"Cannot be the empty string.{Environment.NewLine}Parameter name: prototype", 73 p, v => { new DefaultOption ("", null); }); 74 Utils.AssertException (typeof(ArgumentException), 75 $"Empty option names are not supported.{Environment.NewLine}Parameter name: prototype", 76 p, v => { new DefaultOption ("a|b||c=", null); }); 77 Utils.AssertException (typeof(ArgumentException), 78 $"Conflicting option types: '=' vs. ':'.{Environment.NewLine}Parameter name: prototype", 79 p, v => { new DefaultOption ("a=|b:", null); }); 80 Utils.AssertException (typeof(ArgumentException), 81 $"The default option handler '<>' cannot require values.{Environment.NewLine}Parameter name: prototype", 82 p, v => { new DefaultOption ("<>=", null); }); 83 Utils.AssertException (typeof(ArgumentException), 84 $"The default option handler '<>' cannot require values.{Environment.NewLine}Parameter name: prototype", 85 p, v => { new DefaultOption ("<>:", null); }); 86 Utils.AssertException (null, null, 87 p, v => { new DefaultOption ("t|<>=", null, 1); }); 88 Utils.AssertException (typeof(ArgumentException), 89 $"The default option handler '<>' cannot require values.{Environment.NewLine}Parameter name: prototype", 90 p, v => { new DefaultOption ("t|<>=", null, 2); }); 91 Utils.AssertException (null, null, 92 p, v => { new DefaultOption ("a|b=", null, 2); }); 93 Utils.AssertException (typeof(ArgumentOutOfRangeException), 94 $"Specified argument was out of the range of valid values.{Environment.NewLine}Parameter name: maxValueCount", 95 p, v => { new DefaultOption ("a", null, -1); }); 96 Utils.AssertException (typeof(ArgumentException), 97 "Cannot provide maxValueCount of 0 for OptionValueType.Required or " + 98 $"OptionValueType.Optional.{Environment.NewLine}Parameter name: maxValueCount", 99 p, v => { new DefaultOption ("a=", null, 0); }); 100 Utils.AssertException (typeof(ArgumentException), 101 "Ill-formed name/value separator found in \"a={\"." + Environment.NewLine + "Parameter name: prototype", 102 p, v => { new DefaultOption ("a={", null); }); 103 Utils.AssertException (typeof(ArgumentException), 104 "Ill-formed name/value separator found in \"a=}\"." + Environment.NewLine + "Parameter name: prototype", 105 p, v => { new DefaultOption ("a=}", null); }); 106 Utils.AssertException (typeof(ArgumentException), 107 "Ill-formed name/value separator found in \"a={{}}\"." + Environment.NewLine + "Parameter name: prototype", 108 p, v => { new DefaultOption ("a={{}}", null); }); 109 Utils.AssertException (typeof(ArgumentException), 110 "Ill-formed name/value separator found in \"a={}}\"." + Environment.NewLine + "Parameter name: prototype", 111 p, v => { new DefaultOption ("a={}}", null); }); 112 Utils.AssertException (typeof(ArgumentException), 113 "Ill-formed name/value separator found in \"a={}{\"." + Environment.NewLine + "Parameter name: prototype", 114 p, v => { new DefaultOption ("a={}{", null); }); 115 Utils.AssertException (typeof(ArgumentException), 116 $"Cannot provide key/value separators for Options taking 1 value(s).{Environment.NewLine}Parameter name: prototype", 117 p, v => { new DefaultOption ("a==", null); }); 118 Utils.AssertException (typeof(ArgumentException), 119 $"Cannot provide key/value separators for Options taking 1 value(s).{Environment.NewLine}Parameter name: prototype", 120 p, v => { new DefaultOption ("a={}", null); }); 121 Utils.AssertException (typeof(ArgumentException), 122 $"Cannot provide key/value separators for Options taking 1 value(s).{Environment.NewLine}Parameter name: prototype", 123 p, v => { new DefaultOption ("a=+-*/", null); }); 124 Utils.AssertException (null, null, 125 p, v => { new DefaultOption ("a", null, 0); }); 126 Utils.AssertException (null, null, 127 p, v => { new DefaultOption ("a", null, 0); }); 128 Utils.AssertException (null, null, 129 p, v => { 130 var d = new DefaultOption ("a", null); 131 Assert.AreEqual (d.GetValueSeparators ().Length, 0); 132 }); 133 Utils.AssertException (null, null, 134 p, v => { 135 var d = new DefaultOption ("a=", null, 1); 136 string[] s = d.GetValueSeparators (); 137 Assert.AreEqual (s.Length, 0); 138 }); 139 Utils.AssertException (null, null, 140 p, v => { 141 var d = new DefaultOption ("a=", null, 2); 142 string[] s = d.GetValueSeparators (); 143 Assert.AreEqual (s.Length, 2); 144 Assert.AreEqual (s [0], ":"); 145 Assert.AreEqual (s [1], "="); 146 }); 147 Utils.AssertException (null, null, 148 p, v => { 149 var d = new DefaultOption ("a={}", null, 2); 150 string[] s = d.GetValueSeparators (); 151 Assert.AreEqual (s.Length, 0); 152 }); 153 Utils.AssertException (null, null, 154 p, v => { 155 var d = new DefaultOption ("a={-->}{=>}", null, 2); 156 string[] s = d.GetValueSeparators (); 157 Assert.AreEqual (s.Length, 2); 158 Assert.AreEqual (s [0], "-->"); 159 Assert.AreEqual (s [1], "=>"); 160 }); 161 Utils.AssertException (null, null, 162 p, v => { 163 var d = new DefaultOption ("a=+-*/", null, 2); 164 string[] s = d.GetValueSeparators (); 165 Assert.AreEqual (s.Length, 4); 166 Assert.AreEqual (s [0], "+"); 167 Assert.AreEqual (s [1], "-"); 168 Assert.AreEqual (s [2], "*"); 169 Assert.AreEqual (s [3], "/"); 170 }); 171 } 172 } 173 } 174 175