Merge pull request #1542 from ninjarobot/UriTemplateMatchException
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / Microsoft.Build.BuildEngine / BuildChooseTest.cs
1 //
2 // BuildChoose.cs
3 //
4 // Author:
5 //   Jonathan Chambers (joncham@gmail.com)
6 //
7 // (C) 2009 Jonathan Chambers
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 System.Collections;
30 using Microsoft.Build.BuildEngine;
31 using NUnit.Framework;
32
33 namespace MonoTests.Microsoft.Build.BuildEngine {
34         [TestFixture]
35         public class BuildChooseTest {
36
37         [Test]
38         public void TestEmptyWhen () {
39             Engine engine;
40             Project project;
41             BuildItemGroup[] groups = new BuildItemGroup[1];
42
43             string documentString = @"
44                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
45                     <Choose>
46                         <When Condition=""'$(Configuration)' == ''"">
47                             
48                             <!-- A user comment is allowed here -->
49
50                                                 <ItemGroup>
51                                                         <A Include='a' />
52                                                 </ItemGroup>
53                             
54                         </When>
55                     </Choose>
56                                 </Project>
57                         ";
58
59             engine = new Engine (Consts.BinPath);
60             project = engine.CreateNewProject ();
61             project.LoadXml (documentString);
62
63             //Assert.AreEqual (1, project.ItemGroups.Count, "A1");
64             Assert.AreEqual (0, project.PropertyGroups.Count, "A2");
65             Assert.AreEqual (1, project.EvaluatedItems.Count, "A3");
66             Assert.AreEqual (1, project.EvaluatedItemsIgnoringCondition.Count, "A4");
67         }
68
69         [Test]
70         public void TestFalseWhen () {
71             Engine engine;
72             Project project;
73             BuildItemGroup[] groups = new BuildItemGroup[1];
74
75             string documentString = @"
76                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
77                     <Choose>
78                         <When Condition=""'$(Configuration)' == 'False'"">
79                                                 <ItemGroup>
80                                                         <A Include='a' />
81                                                 </ItemGroup>
82                         </When>
83                     </Choose>
84                                 </Project>
85                         ";
86
87             engine = new Engine (Consts.BinPath);
88             project = engine.CreateNewProject ();
89             project.LoadXml (documentString);
90
91             //Assert.AreEqual (1, project.ItemGroups.Count, "A1");
92             Assert.AreEqual (0, project.PropertyGroups.Count, "A2");
93             Assert.AreEqual (0, project.EvaluatedItems.Count, "A3");
94             Assert.AreEqual (0, project.EvaluatedItemsIgnoringCondition.Count, "A4");
95         }
96
97         [Test]
98         public void TestMultipleTrueWhen () {
99             Engine engine;
100             Project project;
101             BuildItemGroup[] groups = new BuildItemGroup[1];
102
103             string documentString = @"
104                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
105                     <Choose>
106                         <When Condition=""'$(Configuration)' == ''"">
107                                                 <ItemGroup>
108                                                         <A Include='a' />
109                                                 </ItemGroup>
110                         </When>
111                         <When Condition=""'$(Configuration)' == ''"">
112                                                 <ItemGroup>
113                                                         <B Include='a' />
114                                                 </ItemGroup>
115                         </When>
116                     </Choose>
117                                 </Project>
118                         ";
119
120             engine = new Engine (Consts.BinPath);
121             project = engine.CreateNewProject ();
122             project.LoadXml (documentString);
123
124
125             //Assert.AreEqual (2, project.ItemGroups.Count, "A1");
126             Assert.AreEqual (0, project.PropertyGroups.Count, "A2");
127             Assert.AreEqual (1, project.EvaluatedItems.Count, "A3");
128             Assert.AreEqual ("A", project.EvaluatedItems[0].Name, "A4");
129             Assert.AreEqual (1, project.EvaluatedItemsIgnoringCondition.Count, "A5");
130         }
131
132         [Test]
133         public void TestMultipleFalseWhen () {
134             Engine engine;
135             Project project;
136             BuildItemGroup[] groups = new BuildItemGroup[1];
137
138             string documentString = @"
139                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
140                     <Choose>
141                         <When Condition=""'$(Configuration)' == 'False'"">
142                                             <ItemGroup>
143                                                     <A Include='a' />
144                                             </ItemGroup>
145                         </When>
146                         <When Condition=""'$(Configuration)' == 'False'"">
147                                             <ItemGroup>
148                                                     <B Include='a' />
149                                             </ItemGroup>
150                         </When>
151                     </Choose>
152                                 </Project>
153                         ";
154
155             engine = new Engine (Consts.BinPath);
156             project = engine.CreateNewProject ();
157             project.LoadXml (documentString);
158
159             //Assert.AreEqual (2, project.ItemGroups.Count, "A1");
160             Assert.AreEqual (0, project.PropertyGroups.Count, "A2");
161             Assert.AreEqual (0, project.EvaluatedItems.Count, "A3");
162             Assert.AreEqual (0, project.EvaluatedItemsIgnoringCondition.Count, "A4");
163         }
164
165         [Test]
166         [ExpectedException (typeof (InvalidProjectFileException))]
167         public void TestMissingWhen () {
168             Engine engine;
169             Project project;
170             BuildItemGroup[] groups = new BuildItemGroup[1];
171
172             // a <Choose> requires at least one <When>
173             string documentString = @"
174                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
175                     <Choose>
176                         <Otherwise>
177                                                 <ItemGroup>
178                                                         <A Include='a' />
179                                                 </ItemGroup>
180                         </Otherwise>
181                     </Choose>
182                                 </Project>
183                         ";
184
185             engine = new Engine (Consts.BinPath);
186             project = engine.CreateNewProject ();
187             project.LoadXml (documentString);
188         }
189
190         [Test]
191         [ExpectedException (typeof (InvalidProjectFileException))]
192         public void TestMissingConditionWhen () {
193             Engine engine;
194             Project project;
195             BuildItemGroup[] groups = new BuildItemGroup[1];
196
197             // a <When> requires a Condition
198             string documentString = @"
199                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
200                     <Choose>
201                         <When>
202                                                 <ItemGroup>
203                                                         <A Include='a' />
204                                                 </ItemGroup>
205                         </When>
206                     </Choose>
207                                 </Project>
208                         ";
209
210             engine = new Engine (Consts.BinPath);
211             project = engine.CreateNewProject ();
212             project.LoadXml (documentString);
213         }
214
215         [Test]
216         public void TestWhenOtherwise1 () {
217             Engine engine;
218             Project project;
219             BuildItemGroup[] groups = new BuildItemGroup[1];
220
221             string documentString = @"
222                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
223                     <Choose>
224                         <When Condition=""'$(Configuration)' == ''"">
225                                                 <ItemGroup>
226                                                         <A Include='a' />
227                                                 </ItemGroup>
228                         </When>
229                         <Otherwise>
230                                                 <ItemGroup>
231                                                         <B Include='a' />
232                                                 </ItemGroup>
233                         </Otherwise>
234                     </Choose>
235                                 </Project>
236                         ";
237
238             engine = new Engine (Consts.BinPath);
239             project = engine.CreateNewProject ();
240             project.LoadXml (documentString);
241
242             //Assert.AreEqual (2, project.ItemGroups.Count, "A1");
243             Assert.AreEqual (0, project.PropertyGroups.Count, "A2");
244             Assert.AreEqual (1, project.EvaluatedItems.Count, "A3");
245             Assert.AreEqual ("A", project.EvaluatedItems[0].Name, "A4");
246             Assert.AreEqual (1, project.EvaluatedItemsIgnoringCondition.Count, "A5");
247         }
248
249         [Test]
250         public void TestWhenOtherwise2 () {
251             Engine engine;
252             Project project;
253             BuildItemGroup[] groups = new BuildItemGroup[1];
254
255             string documentString = @"
256                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
257                     <Choose>
258                         <When Condition=""'$(Configuration)' == 'False'"">
259                                                 <ItemGroup>
260                                                         <A Include='a' />
261                                                 </ItemGroup>
262                         </When>
263                         <Otherwise>
264                                                 <ItemGroup>
265                                                         <B Include='a' />
266                                                 </ItemGroup>
267                         </Otherwise>
268                     </Choose>
269                                 </Project>
270                         ";
271
272             engine = new Engine (Consts.BinPath);
273             project = engine.CreateNewProject ();
274             project.LoadXml (documentString);
275
276             //Assert.AreEqual (2, project.ItemGroups.Count, "A1");
277             Assert.AreEqual (0, project.PropertyGroups.Count, "A2");
278             Assert.AreEqual (1, project.EvaluatedItems.Count, "A3");
279             Assert.AreEqual ("B", project.EvaluatedItems[0].Name, "A4");
280             Assert.AreEqual (1, project.EvaluatedItemsIgnoringCondition.Count, "A5");
281         }
282                 
283                 [Test]
284         public void ChooseWhenPropertyGroup () {
285             
286                         string documentString = @"
287                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
288                     <Choose>
289                         <When Condition=""'$(Configuration)' == ''"">
290                                                 <PropertyGroup>
291                                                         <Foo>Bar</Foo>
292                                                 </PropertyGroup>
293                         </When>
294                         <Otherwise>
295                                                 <PropertyGroup>
296                                                         <Foo>Baz</Foo>
297                                                 </PropertyGroup>
298                         </Otherwise>
299                     </Choose>
300                                 </Project>
301                         ";
302
303                         Engine engine = new Engine (Consts.BinPath);
304             Project project = engine.CreateNewProject ();
305             project.LoadXml (documentString);
306                         
307                         Assert.AreEqual ("Bar", project.GetEvaluatedProperty ("Foo"), "A1");
308         }
309                 
310                 [Test]
311         public void ChooseOtherwisePropertyGroup () {
312             
313                         string documentString = @"
314                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
315                     <Choose>
316                         <When Condition=""'$(Configuration)' == 'dummy'"">
317                                                 <PropertyGroup>
318                                                         <Foo>Bar</Foo>
319                                                 </PropertyGroup>
320                         </When>
321                         <Otherwise>
322                                                 <PropertyGroup>
323                                                         <Foo>Baz</Foo>
324                                                 </PropertyGroup>
325                         </Otherwise>
326                     </Choose>
327                                 </Project>
328                         ";
329
330                         Engine engine = new Engine (Consts.BinPath);
331             Project project = engine.CreateNewProject ();
332             project.LoadXml (documentString);
333                         
334                         Assert.AreEqual ("Baz", project.GetEvaluatedProperty ("Foo"), "A1");
335         }
336                 
337                 [Test]
338         public void NestedChooseInOtherwise () {
339             
340                         string documentString = @"
341                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
342                     <Choose>
343                         <When Condition=""'$(Configuration)' == 'dummy'"">
344                                                 <PropertyGroup>
345                                                         <Foo>Bar</Foo>
346                                                 </PropertyGroup>
347                         </When>
348                         <Otherwise>
349                                                         <Choose>
350                                                                 <When Condition="" 'foo' == 'bar' "">
351                                                                         <PropertyGroup>
352                                                                                 <Foo>Baz</Foo>
353                                                                         </PropertyGroup>
354                                                                 </When>
355                                                                 <Otherwise>
356                                                                         <PropertyGroup>
357                                                                                 <Foo>Baz</Foo>
358                                                                         </PropertyGroup>
359                                                                 </Otherwise>
360                                                         </Choose>
361                         </Otherwise>
362                     </Choose>
363                                 </Project>
364                         ";
365
366                         Engine engine = new Engine (Consts.BinPath);
367             Project project = engine.CreateNewProject ();
368             project.LoadXml (documentString);
369                         
370                         Assert.AreEqual ("Baz", project.GetEvaluatedProperty ("Foo"), "A1");
371         }
372                 
373                 
374                 [Test]
375                 public void UndefinedPropertyInExistsCondition()
376                 {
377                         string documentString = @"
378                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
379                                         <PropertyGroup>
380                                                 <Foo>Bar</Foo>
381                                         </PropertyGroup>
382                                         <Choose>
383                                                 <When Condition = "" '$(teamcity_dotnet_nunitlauncher_msbuild_task)' == '' "" >
384                                                         <Choose>
385                                                                 <When Condition=""Exists('$(UndefinedProperty)\Foo\Bar')"">
386                                                                         <PropertyGroup>
387                                                                                 <Exists>yes</Exists>
388                                                                         </PropertyGroup>
389                                                                 </When>
390                                                                 <Otherwise>
391                                                                         <PropertyGroup>
392                                                                                 <Exists>no</Exists>
393                                                                         </PropertyGroup>
394                                                                 </Otherwise>
395                                                         </Choose>
396                                                 </When>
397                                         </Choose>
398                                 </Project>
399                         ";
400
401                         Engine engine = new Engine (Consts.BinPath);
402             Project project = engine.CreateNewProject ();
403             project.LoadXml (documentString);
404                         
405                         Assert.AreEqual ("no", project.GetEvaluatedProperty ("Exists"), "A1");
406                 }
407
408                 [Test]
409                 public void EmptyExistsCondition()
410                 {
411                         string documentString = @"
412                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
413                                         <Choose>
414                                                 <When Condition=""Exists('$(UndefinedProperty)')"">
415                                                         <PropertyGroup>
416                                                                 <Exists>yes</Exists>
417                                                         </PropertyGroup>
418                                                 </When>
419                                                 <Otherwise>
420                                                         <PropertyGroup>
421                                                                 <Exists>no</Exists>
422                                                         </PropertyGroup>
423                                                 </Otherwise>
424                                         </Choose>
425                                 </Project>
426                         ";
427
428                         Engine engine = new Engine (Consts.BinPath);
429                         Project project = engine.CreateNewProject ();
430                         //assign a real filename to be used as base path for the Exists
431                         project.FullFileName = typeof (BuildChooseTest).Assembly.Location;
432
433                         project.LoadXml (documentString);
434
435                         Assert.AreEqual ("no", project.GetEvaluatedProperty ("Exists"), "A1");
436                 }
437         
438         [Test]
439         public void EvaluationOrder ()
440         {
441             string documentString = @"
442                         <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
443                             <Choose>
444                                 <When Condition=""true"">
445                                     <PropertyGroup>
446                                         <Foo>Bar</Foo>
447                                     </PropertyGroup>
448                                 </When>
449                                 <Otherwise>
450                                     <PropertyGroup>
451                                         <Foo>Baz</Foo>
452                                     </PropertyGroup>
453                                 </Otherwise>
454                             </Choose>
455
456                             <PropertyGroup>
457                                 <Test>$(Foo)</Test>
458                             </PropertyGroup>
459                         </Project>
460                     ";
461
462             Engine engine = new Engine (Consts.BinPath);
463             Project project = engine.CreateNewProject ();
464             project.LoadXml (documentString);
465             Assert.AreEqual ("Bar", project.GetEvaluatedProperty ("Test"));
466         }
467         }
468 }