Merge pull request #1063 from esdrubal/bug18482
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / various / Conditions.cs
1 //
2 // Conditions.cs: Tests various conditions by checking if a property
3 // is added to EvaluatedProperties.
4 //
5 // Author:
6 //   Marek Sieradzki (marek.sieradzki@gmail.com)
7 //
8 // (C) 2006 Marek Sieradzki
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 using System;
30 using System.Xml;
31 using Microsoft.Build.BuildEngine;
32 using NUnit.Framework;
33
34 namespace MonoTests.Microsoft.Build.BuildEngine.Various {
35         [TestFixture]
36         public class Conditions {
37
38                 [Test]
39                 public void TestCondition1 ()
40                 {
41                         Engine engine = new Engine (Consts.BinPath);
42                         Project proj = engine.CreateNewProject ();
43
44                         string documentString = @"
45                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
46                                         <PropertyGroup>
47                                                 <A Condition='true'></A>
48                                                 <B Condition='false'></B>
49                                                 <C Condition='TRUE'></C>
50                                                 <D Condition='FALSE'></D>
51                                                 <E Condition=''>A</E>
52                                         </PropertyGroup>
53                                 </Project>
54                         ";
55
56                         proj.LoadXml (documentString);
57
58                         Assert.IsNotNull (proj.EvaluatedProperties ["A"], "A1");
59                         Assert.IsNull (proj.EvaluatedProperties ["B"], "A2");
60                         Assert.IsNotNull (proj.EvaluatedProperties ["C"], "A3");
61                         Assert.IsNull (proj.EvaluatedProperties ["D"], "A4");
62                         Assert.IsNotNull (proj.EvaluatedProperties ["E"], "A5");
63                 }
64
65                 [Test]
66                 public void TestCondition2 ()
67                 {
68                         Engine engine = new Engine (Consts.BinPath);
69                         Project proj = engine.CreateNewProject ();
70
71                         string documentString = @"
72                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
73                                         <PropertyGroup>
74                                                 <A Condition='true or true'></A>
75                                                 <B Condition='false or false'></B>
76                                                 <C Condition='true or false'></C>
77                                                 <D Condition='false or true'></D>
78                                         </PropertyGroup>
79                                 </Project>
80                         ";
81
82                         proj.LoadXml (documentString);
83
84                         Assert.IsNotNull (proj.EvaluatedProperties ["A"], "A1");
85                         Assert.IsNull (proj.EvaluatedProperties ["B"], "A2");
86                         Assert.IsNotNull (proj.EvaluatedProperties ["C"], "A3");
87                         Assert.IsNotNull (proj.EvaluatedProperties ["D"], "A4");
88                 }
89
90                 [Test]
91                 public void TestCondition3 ()
92                 {
93                         Engine engine = new Engine (Consts.BinPath);
94                         Project proj = engine.CreateNewProject ();
95
96                         string documentString = @"
97                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
98                                         <PropertyGroup>
99                                                 <A Condition='true and true'></A>
100                                                 <B Condition='false and false'></B>
101                                                 <C Condition='true and false'></C>
102                                                 <D Condition='false and true'></D>
103                                         </PropertyGroup>
104                                 </Project>
105                         ";
106
107                         proj.LoadXml (documentString);
108
109                         Assert.IsNotNull (proj.EvaluatedProperties ["A"], "A1");
110                         Assert.IsNull (proj.EvaluatedProperties ["B"], "A2");
111                         Assert.IsNull (proj.EvaluatedProperties ["C"], "A3");
112                         Assert.IsNull (proj.EvaluatedProperties ["D"], "A4");
113                 }
114
115                 [Test]
116                 public void TestCondition4 ()
117                 {
118                         Engine engine = new Engine (Consts.BinPath);
119                         Project proj = engine.CreateNewProject ();
120
121                         string documentString = @"
122                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
123                                         <PropertyGroup>
124                                                 <A Condition='!true'></A>
125                                                 <B Condition='!false'></B>
126                                         </PropertyGroup>
127                                 </Project>
128                         ";
129
130                         proj.LoadXml (documentString);
131
132                         Assert.IsNull (proj.EvaluatedProperties ["A"], "A1");
133                         Assert.IsNotNull (proj.EvaluatedProperties ["B"], "A2");
134                 }
135
136                 [Test]
137                 public void TestCondition5 ()
138                 {
139                         Engine engine = new Engine (Consts.BinPath);
140                         Project proj = engine.CreateNewProject ();
141
142                         string documentString = @"
143                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
144                                         <PropertyGroup>
145                                                 <a Condition='-1 &lt; 0'></a>
146                                                 <b Condition='-1 &lt;= 0'></b>
147                                                 <c Condition='-1.0 &lt; 0.0'></c>
148                                                 <d Condition='-1.0 &lt;= 0.0'></d>
149                                                 <e Condition='-1.0 == 0.0'></e>
150                                                 <f Condition='0.0 == 0.0'></f>
151                                                 <g Condition='-1.0 != 0.0'></g>
152                                                 <h Condition='0.0 != 0.0'></h>
153                                                 <i Condition='1 &gt; 0'></i>
154                                                 <j Condition='1 &gt;= 0'></j>
155                                                 <k Condition='-1 &gt; 0'></k>
156                                                 <l Condition='-1 &gt;= 0'></l>
157                                         </PropertyGroup>
158                                 </Project>
159                         ";
160
161                         proj.LoadXml (documentString);
162
163                         Assert.IsNotNull (proj.EvaluatedProperties ["a"], "A1");
164                         Assert.IsNotNull (proj.EvaluatedProperties ["b"], "A2");
165                         Assert.IsNotNull (proj.EvaluatedProperties ["c"], "A3");
166                         Assert.IsNotNull (proj.EvaluatedProperties ["d"], "A4");
167                         Assert.IsNull (proj.EvaluatedProperties ["e"], "A5");
168                         Assert.IsNotNull (proj.EvaluatedProperties ["f"], "A6");
169                         Assert.IsNotNull (proj.EvaluatedProperties ["g"], "A7");
170                         Assert.IsNull (proj.EvaluatedProperties ["h"], "A8");
171                         Assert.IsNotNull (proj.EvaluatedProperties ["i"], "A1");
172                         Assert.IsNotNull (proj.EvaluatedProperties ["j"], "A2");
173                         Assert.IsNull (proj.EvaluatedProperties ["k"], "A3");
174                         Assert.IsNull (proj.EvaluatedProperties ["l"], "A4");
175                 }
176
177                 [Test]
178                 [Category ("NotWorking")]
179                 public void TestCondition6 ()
180                 {
181                         Engine engine = new Engine (Consts.BinPath);
182                         Project proj = engine.CreateNewProject ();
183
184                         string documentString = @"
185                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
186                                         <PropertyGroup>
187                                                 <A>true</A>
188                                                 <B>false</B>
189                                                 <C Condition='$(A)'></C>
190                                                 <D Condition='$(B)'></D>
191                                                 <E Condition="" '$(A)' ""></E>
192                                                 <F Condition="" '$(B)' ""></F>
193                                         </PropertyGroup>
194                                 </Project>
195                         ";
196
197                         proj.LoadXml (documentString);
198
199                         Assert.IsNotNull (proj.EvaluatedProperties ["C"], "A1");
200                         Assert.IsNull (proj.EvaluatedProperties ["D"], "A2");
201                         Assert.IsNotNull (proj.EvaluatedProperties ["E"], "A3");
202                         Assert.IsNull (proj.EvaluatedProperties ["F"], "A4");
203                 }
204
205                 [Test]
206                 [Category ("NotWorking")]
207                 public void TestCondition7 ()
208                 {
209                         Engine engine = new Engine (Consts.BinPath);
210                         Project proj = engine.CreateNewProject ();
211
212                         string documentString = @"
213                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
214                                         <PropertyGroup>
215                                                 <A>true</A>
216                                         </PropertyGroup>
217                                         <ItemGroup>
218                                                 <B Include='true' />
219                                                 <C Include='true;false' />
220                                         </ItemGroup>
221
222                                         <Target Name='1'>
223                                                 <Message Text='a' Condition='$(A)' />
224                                                 <Message Text='b' Condition='@(B)' />
225                                                 <Message Text='c' Condition='%(C.Filename)' />
226                                         </Target>
227                                 </Project>
228                         ";
229
230                         proj.LoadXml (documentString);
231                         Assert.IsTrue (proj.Build ("1"), "A1");
232                 }
233
234                 [Test]
235                 public void TestCondition8 ()
236                 {
237                         Engine engine = new Engine (Consts.BinPath);
238                         Project proj = engine.CreateNewProject ();
239
240                         string documentString = @"
241                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
242                                         <PropertyGroup>
243                                                 <A Condition='true == true'></A>
244                                                 <B Condition='true != true'></B>
245                                                 <C Condition='false == false'></C>
246                                                 <D Condition='false != false'></D>
247                                                 <E Condition='true != false'></E>
248                                         </PropertyGroup>
249                                 </Project>
250                         ";
251
252                         proj.LoadXml (documentString);
253
254                         Assert.IsNotNull (proj.EvaluatedProperties ["A"], "A1");
255                         Assert.IsNull (proj.EvaluatedProperties ["B"], "A2");
256                         Assert.IsNotNull (proj.EvaluatedProperties ["C"], "A3");
257                         Assert.IsNull (proj.EvaluatedProperties ["D"], "A4");
258                         Assert.IsNotNull (proj.EvaluatedProperties ["E"], "A5");
259                 }
260
261                 [Test]
262                 public void TestCondition9 ()
263                 {
264                         Engine engine = new Engine (Consts.BinPath);
265                         Project proj = engine.CreateNewProject ();
266
267                         string documentString = @"
268                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
269                                         <PropertyGroup>
270                                                 <A Condition=""'A' == 'A'""></A>
271                                                 <B Condition="" 'A' == 'A' ""></B>
272                                                 <C Condition=""'A' == 'a'""></C>
273                                                 <D Condition=""'A' == 'b'""></D>
274                                         </PropertyGroup>
275                                 </Project>
276                         ";
277
278                         proj.LoadXml (documentString);
279
280                         Assert.IsNotNull (proj.EvaluatedProperties ["A"], "A1");
281                         Assert.IsNotNull (proj.EvaluatedProperties ["B"], "A2");
282                         Assert.IsNotNull (proj.EvaluatedProperties ["C"], "A3");
283                         Assert.IsNull (proj.EvaluatedProperties ["D"], "A4");
284                 }
285
286                 [Test]
287                 [Category ("NotDotNet")]
288                 public void TestCondition10 ()
289                 {
290                         Engine engine = new Engine (Consts.BinPath);
291                         Project proj = engine.CreateNewProject ();
292
293                         string documentString = @"
294                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
295                                         <PropertyGroup>
296                                                 <A Condition="" !'true' ""></A>
297                                                 <B Condition="" 'on' == 'true' ""></B>
298                                                 <C Condition="" 4 == 4.0 and 04 == 4""></C>
299                                                 <D Condition="" !(false and false) ==  !false or !false ""></D>
300                                                 <E Condition="" Exists ('Test\resources\Import.csproj') ""></E>
301                                         </PropertyGroup>
302                                 </Project>
303                         ";
304
305                         proj.LoadXml (documentString);
306
307                         Assert.IsNull (proj.EvaluatedProperties ["A"], "A1");
308                         Assert.IsNotNull (proj.EvaluatedProperties ["B"], "A2");
309                         Assert.IsNotNull (proj.EvaluatedProperties ["C"], "A3");
310                         Assert.IsNotNull (proj.EvaluatedProperties ["D"], "A4");
311                         Assert.IsNotNull (proj.EvaluatedProperties ["E"], "A5");
312                 }
313                 [Test]
314                 public void TestCondition11 ()
315                 {
316                         Engine engine = new Engine (Consts.BinPath);
317                         Project proj = engine.CreateNewProject ();
318                         string documentString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
319         <PropertyGroup>
320                 <FooProp>true</FooProp>
321         </PropertyGroup>
322         <ItemGroup>
323                 <FooList Include=""abc.exe""/>
324                 <List1 Include=""fr_a.txt"" Condition="" $(FooProp) == 'true'"" />
325                 <List1 Include=""fr_b.txt"" Condition="" '@(FooList->'%(Extension)a(foo', ',')' == '.exea(foo'"" />
326                 <List1 Include=""fr_c.txt"" Condition="" @(FooList -> '%(Extension)', ',') == '.exe'"" />
327         </ItemGroup>
328 </Project>";
329
330                         proj.LoadXml (documentString);
331
332                         BuildItemGroup bgp = proj.GetEvaluatedItemsByName ("List1");
333                         Assert.IsNotNull (bgp, "Expected values in List1");
334                         Assert.AreEqual (3, bgp.Count, "A1");
335                         Assert.AreEqual ("fr_a.txt", bgp [0].FinalItemSpec, "A2");
336                         Assert.AreEqual ("fr_b.txt", bgp [1].FinalItemSpec, "A3");
337                         Assert.AreEqual ("fr_c.txt", bgp [2].FinalItemSpec, "A4");
338                 }
339
340                 // Test shortcircuiting
341                 [Test]
342                 public void TestCondition12 ()
343                 {
344                         Engine engine = new Engine (Consts.BinPath);
345                         Project proj = engine.CreateNewProject ();
346
347                         string documentString = @"
348                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
349                                         <PropertyGroup>
350                                                 <A Condition=""'$(NonExistant)' != '' and $(NonExistant)""></A>
351                                         </PropertyGroup>
352                                 </Project>
353                         ";
354
355                         proj.LoadXml (documentString);
356
357                         Assert.IsNull (proj.EvaluatedProperties ["A"], "A1");
358                 }
359
360                 [Test]
361                 public void TestCondition_References ()
362                 {
363                         Engine engine = new Engine (Consts.BinPath);
364                         Project proj = engine.CreateNewProject ();
365
366                         string documentString = @"
367                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
368                                         <PropertyGroup>
369                                                 <A Condition=""$([System.String]::new('test').StartsWith(`te`))"">valid</A>
370                                         </PropertyGroup>
371                                 </Project>
372                         ";
373
374                         proj.LoadXml (documentString);
375
376                         Assert.AreEqual ("valid", proj.GetEvaluatedProperty ("A"), "#1");
377                 }
378
379                 [Test]
380                 public void TestHasTrailingSlash1 ()
381                 {
382                         Engine engine = new Engine (Consts.BinPath);
383                         Project proj = engine.CreateNewProject ();
384
385                         string documentString = @"
386                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
387                                         <PropertyGroup>
388                                                 <EmptyProp></EmptyProp>
389                                                 <WithTrailingBackSlash>foo\ </WithTrailingBackSlash>
390                                                 <WithTrailingFwdSlash>foo/  </WithTrailingFwdSlash>
391                                                 <NoTrailing>Foo</NoTrailing>
392
393                                                 <A Condition="" HasTrailingSlash('$(EmptyProp)') ""></A>
394                                                 <B Condition="" HasTrailingSlash('$(WithTrailingBackSlash)') ""></B>
395                                                 <C Condition="" HasTrailingSlash('$(WithTrailingFwdSlash)') ""></C>
396                                                 <D Condition="" HasTrailingSlash('$(NoTrailing)') ""></D>
397                                                 <E Condition="" HasTrailingSlash('$(NonExistant)') ""></E>
398                                         </PropertyGroup>
399                                 </Project>
400                         ";
401
402                         proj.LoadXml (documentString);
403
404                         Assert.IsNull (proj.EvaluatedProperties ["A"], "A1");
405                         Assert.IsNotNull (proj.EvaluatedProperties ["B"], "A2");
406                         Assert.IsNotNull (proj.EvaluatedProperties ["C"], "A3");
407                         Assert.IsNull (proj.EvaluatedProperties ["D"], "A4");
408                         Assert.IsNull (proj.EvaluatedProperties ["E"], "A5");
409                 }
410
411                 [Test]
412                 [ExpectedException (typeof (InvalidProjectFileException))]
413                 public void TestUnknownFunction ()
414                 {
415                         Engine engine = new Engine (Consts.BinPath);
416                         Project proj = engine.CreateNewProject ();
417
418                         string documentString = @"
419                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
420                                         <PropertyGroup>
421                                                 <A Condition="" NonExistantFunction('$(EmptyProp)') ""></A>
422                                         </PropertyGroup>
423                                 </Project>
424                         ";
425
426                         proj.LoadXml (documentString);
427                 }
428
429                 [Test]
430                 [ExpectedException (typeof (InvalidProjectFileException))]
431                 public void TestIncorrectCondition1 ()
432                 {
433                         Engine engine = new Engine (Consts.BinPath);
434                         Project proj = engine.CreateNewProject ();
435
436                         string documentString = @"
437                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
438                                         <PropertyGroup>
439                                                 <A Condition='x'>A</A>
440                                         </PropertyGroup>
441                                 </Project>
442                         ";
443
444                         proj.LoadXml (documentString);
445                 }
446
447                 // A reference to an item list at position 1 is not allowed in this condition "@(A)".
448                 [Test]
449                 [ExpectedException (typeof (InvalidProjectFileException))]
450                 [Category ("NotWorking")]
451                 public void TestIncorrectCondition2 ()
452                 {
453                         Engine engine = new Engine (Consts.BinPath);
454                         Project proj = engine.CreateNewProject ();
455
456                         string documentString = @"
457                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
458                                         <PropertyGroup>
459                                                 <A Condition='@(A)'>A</A>
460                                         </PropertyGroup>
461                                 </Project>
462                         ";
463
464                         proj.LoadXml (documentString);
465                 }
466
467                 // Found an unexpected character '%' at position 0 in condition \%(A)\.
468                 [Test]
469                 [ExpectedException (typeof (InvalidProjectFileException))]
470                 [Category ("NotWorking")]
471                 public void TestIncorrectCondition3 ()
472                 {
473                         Engine engine = new Engine (Consts.BinPath);
474                         Project proj = engine.CreateNewProject ();
475
476                         string documentString = @"
477                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
478                                         <PropertyGroup>
479                                                 <A Condition='%(A)'>A</A>
480                                         </PropertyGroup>
481                                 </Project>
482                         ";
483
484                         proj.LoadXml (documentString);
485                 }
486
487                 // Found an unexpected character '%' at position 0 in condition "%(A)\.
488                 [Test]
489                 [ExpectedException (typeof (InvalidProjectFileException))]
490                 [Category ("NotWorking")]
491                 public void TestIncorrectCondition4 ()
492                 {
493                         Engine engine = new Engine (Consts.BinPath);
494                         Project proj = engine.CreateNewProject ();
495
496                         string documentString = @"
497                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
498                                         <ItemGroup>
499                                                 <A Include='a' Condition='%(A)' />
500                                         </ItemGroup>
501                                 </Project>
502                         ";
503
504                         proj.LoadXml (documentString);
505                 }
506
507                 [Test]
508                 [ExpectedException (typeof (InvalidProjectFileException))]
509                 public void TestIncorrectCondition5 ()
510                 {
511                         Engine engine = new Engine (Consts.BinPath);
512                         Project proj = engine.CreateNewProject ();
513
514                         string documentString = @"
515                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
516                                         <ItemGroup>
517                                                 <A Include='a' Condition="" '  == ''  "" />
518                                         </ItemGroup>
519                                 </Project>
520                         ";
521
522                         proj.LoadXml (documentString);
523                 }
524
525                 [Test]
526                 [ExpectedException (typeof (InvalidProjectFileException))]
527                 public void TestIncorrectCondition6 ()
528                 {
529                         Engine engine = new Engine (Consts.BinPath);
530                         Project proj = engine.CreateNewProject ();
531
532                         string documentString = @"
533                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
534                                         <ItemGroup>
535                                                 <A Include='a' Condition=""'$(NonExistant)' != '' or $(NonExistant)""/>
536                                         </ItemGroup>
537                                 </Project>
538                         ";
539
540                         proj.LoadXml (documentString);
541                 }
542
543         }
544 }