5 // Jonathan Chambers (joncham@gmail.com)
7 // (C) 2009 Jonathan Chambers
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 System.Collections;
30 using Microsoft.Build.BuildEngine;
31 using NUnit.Framework;
33 namespace MonoTests.Microsoft.Build.BuildEngine {
35 public class BuildChooseTest {
38 public void TestEmptyWhen () {
41 BuildItemGroup[] groups = new BuildItemGroup[1];
43 string documentString = @"
44 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
46 <When Condition=""'$(Configuration)' == ''"">
48 <!-- A user comment is allowed here -->
59 engine = new Engine (Consts.BinPath);
60 project = engine.CreateNewProject ();
61 project.LoadXml (documentString);
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");
70 public void TestFalseWhen () {
73 BuildItemGroup[] groups = new BuildItemGroup[1];
75 string documentString = @"
76 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
78 <When Condition=""'$(Configuration)' == 'False'"">
87 engine = new Engine (Consts.BinPath);
88 project = engine.CreateNewProject ();
89 project.LoadXml (documentString);
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");
98 public void TestMultipleTrueWhen () {
101 BuildItemGroup[] groups = new BuildItemGroup[1];
103 string documentString = @"
104 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
106 <When Condition=""'$(Configuration)' == ''"">
111 <When Condition=""'$(Configuration)' == ''"">
120 engine = new Engine (Consts.BinPath);
121 project = engine.CreateNewProject ();
122 project.LoadXml (documentString);
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");
133 public void TestMultipleFalseWhen () {
136 BuildItemGroup[] groups = new BuildItemGroup[1];
138 string documentString = @"
139 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
141 <When Condition=""'$(Configuration)' == 'False'"">
146 <When Condition=""'$(Configuration)' == 'False'"">
155 engine = new Engine (Consts.BinPath);
156 project = engine.CreateNewProject ();
157 project.LoadXml (documentString);
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");
166 [ExpectedException (typeof (InvalidProjectFileException))]
167 public void TestMissingWhen () {
170 BuildItemGroup[] groups = new BuildItemGroup[1];
172 // a <Choose> requires at least one <When>
173 string documentString = @"
174 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
185 engine = new Engine (Consts.BinPath);
186 project = engine.CreateNewProject ();
187 project.LoadXml (documentString);
191 [ExpectedException (typeof (InvalidProjectFileException))]
192 public void TestMissingConditionWhen () {
195 BuildItemGroup[] groups = new BuildItemGroup[1];
197 // a <When> requires a Condition
198 string documentString = @"
199 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
210 engine = new Engine (Consts.BinPath);
211 project = engine.CreateNewProject ();
212 project.LoadXml (documentString);
216 public void TestWhenOtherwise1 () {
219 BuildItemGroup[] groups = new BuildItemGroup[1];
221 string documentString = @"
222 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
224 <When Condition=""'$(Configuration)' == ''"">
238 engine = new Engine (Consts.BinPath);
239 project = engine.CreateNewProject ();
240 project.LoadXml (documentString);
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");
250 public void TestWhenOtherwise2 () {
253 BuildItemGroup[] groups = new BuildItemGroup[1];
255 string documentString = @"
256 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
258 <When Condition=""'$(Configuration)' == 'False'"">
272 engine = new Engine (Consts.BinPath);
273 project = engine.CreateNewProject ();
274 project.LoadXml (documentString);
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");
284 public void ChooseWhenPropertyGroup () {
286 string documentString = @"
287 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
289 <When Condition=""'$(Configuration)' == ''"">
303 Engine engine = new Engine (Consts.BinPath);
304 Project project = engine.CreateNewProject ();
305 project.LoadXml (documentString);
307 Assert.AreEqual ("Bar", project.GetEvaluatedProperty ("Foo"), "A1");
311 public void ChooseOtherwisePropertyGroup () {
313 string documentString = @"
314 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
316 <When Condition=""'$(Configuration)' == 'dummy'"">
330 Engine engine = new Engine (Consts.BinPath);
331 Project project = engine.CreateNewProject ();
332 project.LoadXml (documentString);
334 Assert.AreEqual ("Baz", project.GetEvaluatedProperty ("Foo"), "A1");
338 public void NestedChooseInOtherwise () {
340 string documentString = @"
341 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
343 <When Condition=""'$(Configuration)' == 'dummy'"">
350 <When Condition="" 'foo' == 'bar' "">
366 Engine engine = new Engine (Consts.BinPath);
367 Project project = engine.CreateNewProject ();
368 project.LoadXml (documentString);
370 Assert.AreEqual ("Baz", project.GetEvaluatedProperty ("Foo"), "A1");
375 public void UndefinedPropertyInExistsCondition()
377 string documentString = @"
378 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
383 <When Condition = "" '$(teamcity_dotnet_nunitlauncher_msbuild_task)' == '' "" >
385 <When Condition=""Exists('$(UndefinedProperty)\Foo\Bar')"">
401 Engine engine = new Engine (Consts.BinPath);
402 Project project = engine.CreateNewProject ();
403 project.LoadXml (documentString);
405 Assert.AreEqual ("no", project.GetEvaluatedProperty ("Exists"), "A1");
409 public void EmptyExistsCondition()
411 string documentString = @"
412 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
414 <When Condition=""Exists('$(UndefinedProperty)')"">
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;
433 project.LoadXml (documentString);
435 Assert.AreEqual ("no", project.GetEvaluatedProperty ("Exists"), "A1");
439 public void EvaluationOrder ()
441 string documentString = @"
442 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
444 <When Condition=""true"">
462 Engine engine = new Engine (Consts.BinPath);
463 Project project = engine.CreateNewProject ();
464 project.LoadXml (documentString);
465 Assert.AreEqual ("Bar", project.GetEvaluatedProperty ("Test"));