dc035f7bc69e57e08f8b1d2ebf9b6f205f130cee
[mono.git] / mcs / class / Microsoft.Build.Utilities / Test / Microsoft.Build.Utilities / CommandLineBuilderTest.cs
1 //
2 // CommandLineBuilderTest.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2006 Marek Sieradzki
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 using System;
29 using Microsoft.Build.Framework;
30 using Microsoft.Build.Utilities;
31 using NUnit.Framework;
32
33 namespace MonoTests.Microsoft.Build.Utilities {
34
35         internal class CLBTester : CommandLineBuilder {
36
37                 public new bool IsQuotingRequired (string parameter)
38                 {
39                         return base.IsQuotingRequired (parameter);
40                 }
41
42                 public new void VerifyThrowNoEmbeddedDoubleQuotes (string switchName,
43                                                 string parameter)
44                 {
45                         base.VerifyThrowNoEmbeddedDoubleQuotes (switchName, parameter);
46                 }
47         }
48
49
50         [TestFixture]
51         public class CommandLineBuilderTest {
52
53                 CommandLineBuilder clb;
54                 string[] array;
55                 ITaskItem[] items;
56                 
57                 [SetUp]
58                 public void SetUp () {
59                         array = new string[] { "a", "b", "c"};
60                         items = new TaskItem [] { new TaskItem ("a"), new TaskItem ("b")};
61                 }
62                 
63                 [Test]
64                 public void TestAppendFileNameIfNotNull1 ()
65                 {
66                         
67                         ITaskItem item;
68                         string itemSpec = "itemSpec";
69                 
70                         item = new TaskItem ();
71                         item.ItemSpec = itemSpec;
72                         item.SetMetadata ("name", "value");
73                         clb = new CommandLineBuilder ();
74                         clb.AppendFileNameIfNotNull (item);
75                         
76                         Assert.AreEqual (itemSpec, clb.ToString (), "A1");
77                 }
78                 
79                 [Test]
80                 public void TestAppendFileNameIfNotNull2 ()
81                 {
82                         
83                         string filename = "filename.txt";
84                         
85                         clb = new CommandLineBuilder ();
86                         
87                         clb.AppendFileNameIfNotNull (filename);
88                         
89                         Assert.AreEqual (filename, clb.ToString (), "A1");
90                 }
91                 
92                 [Test]
93                 [ExpectedException (typeof (ArgumentNullException), "Parameter \"delimiter\" cannot be null.")]
94                 public void TestAppendFileNamesIfNotNull1 ()
95                 {
96                         clb = new CommandLineBuilder ();
97                         
98                         clb.AppendFileNamesIfNotNull (array, null);
99                 }
100                 
101                 [Test]
102                 public void TestAppendFileNamesIfNotNull2 ()
103                 {
104                         clb = new CommandLineBuilder ();
105                         
106                         clb.AppendFileNamesIfNotNull (array, String.Empty);
107                         
108                         Assert.AreEqual ("abc", clb.ToString (), "A1");
109                         
110                         clb = new CommandLineBuilder ();
111                         
112                         clb.AppendFileNamesIfNotNull (array, "\t");
113                         
114                         Assert.AreEqual ("a\tb\tc", clb.ToString (), "A2");
115                         
116                         clb.AppendFileNamesIfNotNull ((string[]) null, "sep");
117                         
118                         Assert.AreEqual ("a\tb\tc", clb.ToString (), "A3");
119                 }
120                 
121                 [Test]
122                 [ExpectedException (typeof (ArgumentNullException), "Parameter \"delimiter\" cannot be null.")]
123                 public void TestAppendFileNamesIfNotNull3 ()
124                 {
125                         clb = new CommandLineBuilder ();
126                         
127                         clb.AppendFileNamesIfNotNull (items, null);
128                 }
129                 
130                 [Test]
131                 public void TestAppendFileNamesIfNotNull4 ()
132                 {
133                         clb = new CommandLineBuilder ();
134                         
135                         clb.AppendFileNamesIfNotNull (items, String.Empty);
136                         
137                         Assert.AreEqual ("ab", clb.ToString (), "A1");
138                         
139                         clb.AppendFileNamesIfNotNull ((ITaskItem[]) null, "sep");
140                         
141                         Assert.AreEqual ("ab", clb.ToString (), "A2");
142                 }
143                 
144                 [Test]
145                 public void TestAppendSwitch1 ()
146                 {
147                         string name = "/switch";
148                         
149                         clb = new CommandLineBuilder ();
150                         
151                         clb.AppendSwitch (name);
152                         
153                         Assert.AreEqual (name, clb.ToString (), "A1");
154                 }
155                 
156                 [Test]
157                 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
158                 public void TestAppendSwitch2 ()
159                 {
160                         clb = new CommandLineBuilder ();
161                         
162                         clb.AppendSwitch (null);
163                 }
164                 
165                 [Test]
166                 public void TestAppendSwitch3 ()
167                 {
168                         clb = new CommandLineBuilder ();
169                         
170                         clb.AppendSwitch (String.Empty);
171                 }
172                 
173                 [Test]
174                 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
175                 public void TestAppendSwitchIfNotNull1 ()
176                 {
177                         clb = new CommandLineBuilder ();
178                         
179                         clb.AppendSwitchIfNotNull (null, "parameter");
180                 }
181                 
182                 [Test]
183                 public void TestAppendSwitchIfNotNull2 ()
184                 {
185                         string name = "/switch:";
186                         string parameter = "parameter";
187                         
188                         clb = new CommandLineBuilder ();
189                         
190                         clb.AppendSwitchIfNotNull (name, (string) null);
191                         
192                         Assert.AreEqual (String.Empty, clb.ToString (), "A1");
193                         
194                         clb.AppendSwitchIfNotNull (name, parameter);
195                         
196                         Assert.AreEqual (name + parameter, clb.ToString (), "A2");
197                 }
198                 
199                 [Test]
200                 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
201                 public void TestAppendSwitchIfNotNull3 ()
202                 {
203                         clb = new CommandLineBuilder ();
204                         
205                         clb.AppendSwitchIfNotNull (null, items [0]);
206                 }
207                 
208                 [Test]
209                 public void TestAppendSwitchIfNotNull4 ()
210                 {
211                         string name = "/switch:";
212                         
213                         clb = new CommandLineBuilder ();
214                         
215                         clb.AppendSwitchIfNotNull (name, (ITaskItem) null);
216                         
217                         Assert.AreEqual (String.Empty, clb.ToString (), "A1");
218                         
219                         clb.AppendSwitchIfNotNull (name, items [0]);
220                         
221                         Assert.AreEqual (name + items [0].ItemSpec, clb.ToString (), "A2");
222                 }
223                 
224                 [Test]
225                 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
226                 public void TestAppendSwitchIfNotNull5 ()
227                 {
228                         clb = new CommandLineBuilder ();
229                         
230                         clb.AppendSwitchIfNotNull (null, array, "delimiter");
231                 }
232                 
233                 [Test]
234                 [ExpectedException (typeof (ArgumentNullException), "Parameter \"delimiter\" cannot be null.")]
235                 public void TestAppendSwitchIfNotNull6 ()
236                 {
237                         clb = new CommandLineBuilder ();
238                         
239                         clb.AppendSwitchIfNotNull ("/switch", array, null);
240                 }
241                 
242                 [Test]
243                 public void TestAppendSwitchIfNotNull7 ()
244                 {
245                         clb = new CommandLineBuilder ();
246                         
247                         clb.AppendSwitchIfNotNull ("/switch:", (string[]) null, ";");
248                         
249                         Assert.AreEqual (String.Empty, clb.ToString (), "A1");
250                         
251                         clb.AppendSwitchIfNotNull ("/switch:", array, ";");
252                         
253                         Assert.AreEqual ("/switch:a;b;c", clb.ToString (), "A2");
254                 }
255
256                 [Test]
257                 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
258                 public void TestAppendSwitchIfNotNull8 ()
259                 {
260                         clb = new CommandLineBuilder ();
261                         
262                         clb.AppendSwitchIfNotNull (null, items, "delimiter");
263                 }
264                 
265                 [Test]
266                 [ExpectedException (typeof (ArgumentNullException), "Parameter \"delimiter\" cannot be null.")]
267                 public void TestAppendSwitchIfNotNull9 ()
268                 {
269                         clb = new CommandLineBuilder ();
270                         
271                         clb.AppendSwitchIfNotNull ("/switch", items, null);
272                 }
273                 
274                 [Test]
275                 public void TestAppendSwitchIfNotNull10 ()
276                 {
277                         clb = new CommandLineBuilder ();
278                         
279                         clb.AppendSwitchIfNotNull ("/switch:", (ITaskItem[]) null, ";");
280                         
281                         Assert.AreEqual (String.Empty, clb.ToString (), "A1");
282                         
283                         clb.AppendSwitchIfNotNull ("/switch:", items, ";");
284                         
285                         Assert.AreEqual ("/switch:a;b", clb.ToString (), "A2");
286                 }
287
288                 [Test]
289                 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
290                 public void TestAppendSwitchUnquotedIfNotNull1 ()
291                 {
292                         clb = new CommandLineBuilder ();
293                         
294                         clb.AppendSwitchUnquotedIfNotNull (null, "parameter");
295                 }
296                 
297                 [Test]
298                 public void TestAppendSwitchUnquotedIfNotNull2 ()
299                 {
300                         string name = "/switch:";
301                         string parameter = "parameter";
302                         
303                         clb = new CommandLineBuilder ();
304                         
305                         clb.AppendSwitchUnquotedIfNotNull (name, (string) null);
306                         
307                         Assert.AreEqual (String.Empty, clb.ToString (), "A1");
308                         
309                         clb.AppendSwitchUnquotedIfNotNull (name, parameter);
310                         
311                         Assert.AreEqual (name + parameter, clb.ToString (), "A2");
312                 }
313                 
314                 [Test]
315                 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
316                 public void TestAppendSwitchUnquotedIfNotNull3 ()
317                 {
318                         clb = new CommandLineBuilder ();
319                         
320                         clb.AppendSwitchUnquotedIfNotNull (null, items [0]);
321                 }
322                 
323                 [Test]
324                 public void TestAppendSwitchUnquotedIfNotNull4 ()
325                 {
326                         string name = "/switch:";
327                         
328                         clb = new CommandLineBuilder ();
329                         
330                         clb.AppendSwitchUnquotedIfNotNull (name, (ITaskItem) null);
331                         
332                         Assert.AreEqual (String.Empty, clb.ToString (), "A1");
333                         
334                         clb.AppendSwitchUnquotedIfNotNull (name, items [0]);
335                         
336                         Assert.AreEqual (name + items [0].ItemSpec, clb.ToString (), "A2");
337                 }
338                 
339                 [Test]
340                 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
341                 public void TestAppendSwitchUnquotedIfNotNull5 ()
342                 {
343                         clb = new CommandLineBuilder ();
344                         
345                         clb.AppendSwitchUnquotedIfNotNull (null, array, "delimiter");
346                 }
347                 
348                 [Test]
349                 [ExpectedException (typeof (ArgumentNullException), "Parameter \"delimiter\" cannot be null.")]
350                 public void TestAppendSwitchUnquotedIfNotNull6 ()
351                 {
352                         clb = new CommandLineBuilder ();
353                         
354                         clb.AppendSwitchUnquotedIfNotNull ("/switch", array, null);
355                 }
356                 
357                 [Test]
358                 public void TestAppendSwitchUnquotedIfNotNull7 ()
359                 {
360                         clb = new CommandLineBuilder ();
361                         
362                         clb.AppendSwitchUnquotedIfNotNull ("/switch:", (string[]) null, ";");
363                         
364                         Assert.AreEqual (String.Empty, clb.ToString (), "A1");
365                         
366                         clb.AppendSwitchUnquotedIfNotNull ("/switch:", array, ";");
367                         
368                         Assert.AreEqual ("/switch:a;b;c", clb.ToString (), "A2");
369                 }
370
371                 [Test]
372                 [ExpectedException (typeof (ArgumentNullException), "Parameter \"switchName\" cannot be null.")]
373                 public void TestAppendSwitchUnquotedIfNotNull8 ()
374                 {
375                         clb = new CommandLineBuilder ();
376                         
377                         clb.AppendSwitchUnquotedIfNotNull (null, items, "delimiter");
378                 }
379                 
380                 [Test]
381                 [ExpectedException (typeof (ArgumentNullException), "Parameter \"delimiter\" cannot be null.")]
382                 public void TestAppendSwitchUnquotedIfNotNull9 ()
383                 {
384                         clb = new CommandLineBuilder ();
385                         
386                         clb.AppendSwitchUnquotedIfNotNull ("/switch", items, null);
387                 }
388                 
389                 [Test]
390                 public void TestAppendUnquotedSwitchIfNotNull10 ()
391                 {
392                         clb = new CommandLineBuilder ();
393                         
394                         clb.AppendSwitchUnquotedIfNotNull ("/switch:", (ITaskItem[]) null, ";");
395                         
396                         Assert.AreEqual (String.Empty, clb.ToString (), "A1");
397                         
398                         clb.AppendSwitchUnquotedIfNotNull ("/switch:", items, ";");
399                         
400                         Assert.AreEqual ("/switch:a;b", clb.ToString (), "A2");
401                 }
402
403                 [Test]
404                 public void TestIsQuotingRequired ()
405                 {
406                         CLBTester clbt = new CLBTester ();
407
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");
420                 }
421
422                 [Test]
423                 public void TestVerifyThrowNoEmbeddedDoubleQuotes1 ()
424                 {
425                         CLBTester clbt = new CLBTester ();
426
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 ("\'\'", "\'\'");
434                 }
435
436                 [Test]
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 ()
441                 {
442                         CLBTester clbt = new CLBTester ();
443
444                         clbt.VerifyThrowNoEmbeddedDoubleQuotes ("a", "\"\"");
445                 }
446         }
447 }