2 // CommandLineBuilderTest.cs
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2006 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using Microsoft.Build.Framework;
30 using Microsoft.Build.Utilities;
31 using NUnit.Framework;
33 namespace MonoTests.Microsoft.Build.Utilities {
35 internal class CLBTester : CommandLineBuilder {
37 public new bool IsQuotingRequired (string parameter)
39 return base.IsQuotingRequired (parameter);
42 public new void VerifyThrowNoEmbeddedDoubleQuotes (string switchName,
45 base.VerifyThrowNoEmbeddedDoubleQuotes (switchName, parameter);
51 public class CommandLineBuilderTest {
53 CommandLineBuilder clb;
58 public void SetUp () {
59 array = new string[] { "a", "b", "c"};
60 items = new TaskItem [] { new TaskItem ("a"), new TaskItem ("b")};
64 public void TestAppendFileNameIfNotNull1 ()
68 string itemSpec = "itemSpec";
70 item = new TaskItem ();
71 item.ItemSpec = itemSpec;
72 item.SetMetadata ("name", "value");
73 clb = new CommandLineBuilder ();
74 clb.AppendFileNameIfNotNull (item);
76 Assert.AreEqual (itemSpec, clb.ToString (), "A1");
80 public void TestAppendFileNameIfNotNull2 ()
83 string filename = "filename.txt";
85 clb = new CommandLineBuilder ();
87 clb.AppendFileNameIfNotNull (filename);
89 Assert.AreEqual (filename, clb.ToString (), "A1");
93 [ExpectedException (typeof (ArgumentNullException), "Parameter \"delimiter\" cannot be null.")]
94 public void TestAppendFileNamesIfNotNull1 ()
96 clb = new CommandLineBuilder ();
98 clb.AppendFileNamesIfNotNull (array, null);
102 public void TestAppendFileNamesIfNotNull2 ()
104 clb = new CommandLineBuilder ();
106 clb.AppendFileNamesIfNotNull (array, String.Empty);
108 Assert.AreEqual ("abc", clb.ToString (), "A1");
110 clb = new CommandLineBuilder ();
112 clb.AppendFileNamesIfNotNull (array, "\t");
114 Assert.AreEqual ("a\tb\tc", clb.ToString (), "A2");
116 clb.AppendFileNamesIfNotNull ((string[]) null, "sep");
118 Assert.AreEqual ("a\tb\tc", clb.ToString (), "A3");
122 [ExpectedException (typeof (ArgumentNullException), "Parameter \"delimiter\" cannot be null.")]
123 public void TestAppendFileNamesIfNotNull3 ()
125 clb = new CommandLineBuilder ();
127 clb.AppendFileNamesIfNotNull (items, null);
131 public void TestAppendFileNamesIfNotNull4 ()
133 clb = new CommandLineBuilder ();
135 clb.AppendFileNamesIfNotNull (items, String.Empty);
137 Assert.AreEqual ("ab", clb.ToString (), "A1");
139 clb.AppendFileNamesIfNotNull ((ITaskItem[]) null, "sep");
141 Assert.AreEqual ("ab", clb.ToString (), "A2");
145 public void TestAppendSwitch1 ()
147 string name = "/switch";
149 clb = new CommandLineBuilder ();
151 clb.AppendSwitch (name);
153 Assert.AreEqual (name, clb.ToString (), "A1");
157 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
158 public void TestAppendSwitch2 ()
160 clb = new CommandLineBuilder ();
162 clb.AppendSwitch (null);
166 public void TestAppendSwitch3 ()
168 clb = new CommandLineBuilder ();
170 clb.AppendSwitch (String.Empty);
174 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
175 public void TestAppendSwitchIfNotNull1 ()
177 clb = new CommandLineBuilder ();
179 clb.AppendSwitchIfNotNull (null, "parameter");
183 public void TestAppendSwitchIfNotNull2 ()
185 string name = "/switch:";
186 string parameter = "parameter";
188 clb = new CommandLineBuilder ();
190 clb.AppendSwitchIfNotNull (name, (string) null);
192 Assert.AreEqual (String.Empty, clb.ToString (), "A1");
194 clb.AppendSwitchIfNotNull (name, parameter);
196 Assert.AreEqual (name + parameter, clb.ToString (), "A2");
200 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
201 public void TestAppendSwitchIfNotNull3 ()
203 clb = new CommandLineBuilder ();
205 clb.AppendSwitchIfNotNull (null, items [0]);
209 public void TestAppendSwitchIfNotNull4 ()
211 string name = "/switch:";
213 clb = new CommandLineBuilder ();
215 clb.AppendSwitchIfNotNull (name, (ITaskItem) null);
217 Assert.AreEqual (String.Empty, clb.ToString (), "A1");
219 clb.AppendSwitchIfNotNull (name, items [0]);
221 Assert.AreEqual (name + items [0].ItemSpec, clb.ToString (), "A2");
225 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
226 public void TestAppendSwitchIfNotNull5 ()
228 clb = new CommandLineBuilder ();
230 clb.AppendSwitchIfNotNull (null, array, "delimiter");
234 [ExpectedException (typeof (ArgumentNullException), "Parameter \"delimiter\" cannot be null.")]
235 public void TestAppendSwitchIfNotNull6 ()
237 clb = new CommandLineBuilder ();
239 clb.AppendSwitchIfNotNull ("/switch", array, null);
243 public void TestAppendSwitchIfNotNull7 ()
245 clb = new CommandLineBuilder ();
247 clb.AppendSwitchIfNotNull ("/switch:", (string[]) null, ";");
249 Assert.AreEqual (String.Empty, clb.ToString (), "A1");
251 clb.AppendSwitchIfNotNull ("/switch:", array, ";");
253 Assert.AreEqual ("/switch:a;b;c", clb.ToString (), "A2");
257 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
258 public void TestAppendSwitchIfNotNull8 ()
260 clb = new CommandLineBuilder ();
262 clb.AppendSwitchIfNotNull (null, items, "delimiter");
266 [ExpectedException (typeof (ArgumentNullException), "Parameter \"delimiter\" cannot be null.")]
267 public void TestAppendSwitchIfNotNull9 ()
269 clb = new CommandLineBuilder ();
271 clb.AppendSwitchIfNotNull ("/switch", items, null);
275 public void TestAppendSwitchIfNotNull10 ()
277 clb = new CommandLineBuilder ();
279 clb.AppendSwitchIfNotNull ("/switch:", (ITaskItem[]) null, ";");
281 Assert.AreEqual (String.Empty, clb.ToString (), "A1");
283 clb.AppendSwitchIfNotNull ("/switch:", items, ";");
285 Assert.AreEqual ("/switch:a;b", clb.ToString (), "A2");
289 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
290 public void TestAppendSwitchUnquotedIfNotNull1 ()
292 clb = new CommandLineBuilder ();
294 clb.AppendSwitchUnquotedIfNotNull (null, "parameter");
298 public void TestAppendSwitchUnquotedIfNotNull2 ()
300 string name = "/switch:";
301 string parameter = "parameter";
303 clb = new CommandLineBuilder ();
305 clb.AppendSwitchUnquotedIfNotNull (name, (string) null);
307 Assert.AreEqual (String.Empty, clb.ToString (), "A1");
309 clb.AppendSwitchUnquotedIfNotNull (name, parameter);
311 Assert.AreEqual (name + parameter, clb.ToString (), "A2");
315 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
316 public void TestAppendSwitchUnquotedIfNotNull3 ()
318 clb = new CommandLineBuilder ();
320 clb.AppendSwitchUnquotedIfNotNull (null, items [0]);
324 public void TestAppendSwitchUnquotedIfNotNull4 ()
326 string name = "/switch:";
328 clb = new CommandLineBuilder ();
330 clb.AppendSwitchUnquotedIfNotNull (name, (ITaskItem) null);
332 Assert.AreEqual (String.Empty, clb.ToString (), "A1");
334 clb.AppendSwitchUnquotedIfNotNull (name, items [0]);
336 Assert.AreEqual (name + items [0].ItemSpec, clb.ToString (), "A2");
340 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
341 public void TestAppendSwitchUnquotedIfNotNull5 ()
343 clb = new CommandLineBuilder ();
345 clb.AppendSwitchUnquotedIfNotNull (null, array, "delimiter");
349 [ExpectedException (typeof (ArgumentNullException), "Parameter \"delimiter\" cannot be null.")]
350 public void TestAppendSwitchUnquotedIfNotNull6 ()
352 clb = new CommandLineBuilder ();
354 clb.AppendSwitchUnquotedIfNotNull ("/switch", array, null);
358 public void TestAppendSwitchUnquotedIfNotNull7 ()
360 clb = new CommandLineBuilder ();
362 clb.AppendSwitchUnquotedIfNotNull ("/switch:", (string[]) null, ";");
364 Assert.AreEqual (String.Empty, clb.ToString (), "A1");
366 clb.AppendSwitchUnquotedIfNotNull ("/switch:", array, ";");
368 Assert.AreEqual ("/switch:a;b;c", clb.ToString (), "A2");
372 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
373 public void TestAppendSwitchUnquotedIfNotNull8 ()
375 clb = new CommandLineBuilder ();
377 clb.AppendSwitchUnquotedIfNotNull (null, items, "delimiter");
381 [ExpectedException (typeof (ArgumentNullException), "Parameter \"delimiter\" cannot be null.")]
382 public void TestAppendSwitchUnquotedIfNotNull9 ()
384 clb = new CommandLineBuilder ();
386 clb.AppendSwitchUnquotedIfNotNull ("/switch", items, null);
390 public void TestAppendUnquotedSwitchIfNotNull10 ()
392 clb = new CommandLineBuilder ();
394 clb.AppendSwitchUnquotedIfNotNull ("/switch:", (ITaskItem[]) null, ";");
396 Assert.AreEqual (String.Empty, clb.ToString (), "A1");
398 clb.AppendSwitchUnquotedIfNotNull ("/switch:", items, ";");
400 Assert.AreEqual ("/switch:a;b", clb.ToString (), "A2");
404 public void TestIsQuotingRequired ()
406 CLBTester clbt = new CLBTester ();
408 Assert.AreEqual (false, clbt.IsQuotingRequired(""), "A1");
409 Assert.AreEqual (true, clbt.IsQuotingRequired(" "), "A2");
410 Assert.AreEqual (false, clbt.IsQuotingRequired("a"), "A3");
411 Assert.AreEqual (true, clbt.IsQuotingRequired("a a"), "A4");
412 Assert.AreEqual (true, clbt.IsQuotingRequired("\'\'"), "A5");
413 Assert.AreEqual (true, clbt.IsQuotingRequired("\' \'"), "A6");
414 Assert.AreEqual (true, clbt.IsQuotingRequired("\"\""), "A7");
415 Assert.AreEqual (true, clbt.IsQuotingRequired("\" \""), "A8");
416 Assert.AreEqual (true, clbt.IsQuotingRequired("\n\n"), "A9");
417 Assert.AreEqual (true, clbt.IsQuotingRequired("\n \n"), "A10");
418 Assert.AreEqual (true, clbt.IsQuotingRequired("\t\t"), "A11");
419 Assert.AreEqual (true, clbt.IsQuotingRequired("\t \t"), "A12");
423 public void TestVerifyThrowNoEmbeddedDoubleQuotes1 ()
425 CLBTester clbt = new CLBTester ();
427 clbt.VerifyThrowNoEmbeddedDoubleQuotes (null, null);
428 clbt.VerifyThrowNoEmbeddedDoubleQuotes ("", null);
429 clbt.VerifyThrowNoEmbeddedDoubleQuotes (null, "");
430 clbt.VerifyThrowNoEmbeddedDoubleQuotes (" ", "");
431 clbt.VerifyThrowNoEmbeddedDoubleQuotes ("", " ");
432 clbt.VerifyThrowNoEmbeddedDoubleQuotes ("\"\"", "");
433 clbt.VerifyThrowNoEmbeddedDoubleQuotes ("\'\'", "\'\'");
437 [Ignore ("Known bug")]
438 [ExpectedException (typeof (ArgumentException),
439 "Illegal quote passed to the command line switch named \"a\". The value was [\"\"].")]
440 public void TestVerifyThrowNoEmbeddedDoubleQuotes2 ()
442 CLBTester clbt = new CLBTester ();
444 clbt.VerifyThrowNoEmbeddedDoubleQuotes ("a", "\"\"");