2 // Conditions.cs: Tests various conditions by checking if a property
3 // is added to EvaluatedProperties.
6 // Marek Sieradzki (marek.sieradzki@gmail.com)
8 // (C) 2006 Marek Sieradzki
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using Microsoft.Build.BuildEngine;
32 using NUnit.Framework;
34 namespace MonoTests.Microsoft.Build.BuildEngine.Various {
36 public class Conditions {
39 public void TestCondition1 ()
41 Engine engine = new Engine (Consts.BinPath);
42 Project proj = engine.CreateNewProject ();
44 string documentString = @"
45 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
47 <A Condition='true'></A>
48 <B Condition='false'></B>
49 <C Condition='TRUE'></C>
50 <D Condition='FALSE'></D>
56 proj.LoadXml (documentString);
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");
66 public void TestCondition2 ()
68 Engine engine = new Engine (Consts.BinPath);
69 Project proj = engine.CreateNewProject ();
71 string documentString = @"
72 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
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>
82 proj.LoadXml (documentString);
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");
91 public void TestCondition3 ()
93 Engine engine = new Engine (Consts.BinPath);
94 Project proj = engine.CreateNewProject ();
96 string documentString = @"
97 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
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>
107 proj.LoadXml (documentString);
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");
116 public void TestCondition4 ()
118 Engine engine = new Engine (Consts.BinPath);
119 Project proj = engine.CreateNewProject ();
121 string documentString = @"
122 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
124 <A Condition='!true'></A>
125 <B Condition='!false'></B>
130 proj.LoadXml (documentString);
132 Assert.IsNull (proj.EvaluatedProperties ["A"], "A1");
133 Assert.IsNotNull (proj.EvaluatedProperties ["B"], "A2");
137 public void TestCondition5 ()
139 Engine engine = new Engine (Consts.BinPath);
140 Project proj = engine.CreateNewProject ();
142 string documentString = @"
143 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
145 <a Condition='-1 < 0'></a>
146 <b Condition='-1 <= 0'></b>
147 <c Condition='-1.0 < 0.0'></c>
148 <d Condition='-1.0 <= 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 > 0'></i>
154 <j Condition='1 >= 0'></j>
155 <k Condition='-1 > 0'></k>
156 <l Condition='-1 >= 0'></l>
161 proj.LoadXml (documentString);
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");
178 [Category ("NotWorking")]
179 public void TestCondition6 ()
181 Engine engine = new Engine (Consts.BinPath);
182 Project proj = engine.CreateNewProject ();
184 string documentString = @"
185 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
189 <C Condition='$(A)'></C>
190 <D Condition='$(B)'></D>
191 <E Condition="" '$(A)' ""></E>
192 <F Condition="" '$(B)' ""></F>
197 proj.LoadXml (documentString);
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");
206 [Category ("NotWorking")]
207 public void TestCondition7 ()
209 Engine engine = new Engine (Consts.BinPath);
210 Project proj = engine.CreateNewProject ();
212 string documentString = @"
213 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
219 <C Include='true;false' />
223 <Message Text='a' Condition='$(A)' />
224 <Message Text='b' Condition='@(B)' />
225 <Message Text='c' Condition='%(C.Filename)' />
230 proj.LoadXml (documentString);
231 Assert.IsTrue (proj.Build ("1"), "A1");
235 public void TestCondition8 ()
237 Engine engine = new Engine (Consts.BinPath);
238 Project proj = engine.CreateNewProject ();
240 string documentString = @"
241 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
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>
252 proj.LoadXml (documentString);
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");
262 public void TestCondition9 ()
264 Engine engine = new Engine (Consts.BinPath);
265 Project proj = engine.CreateNewProject ();
267 string documentString = @"
268 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
270 <A Condition=""'A' == 'A'""></A>
271 <B Condition="" 'A' == 'A' ""></B>
272 <C Condition=""'A' == 'a'""></C>
273 <D Condition=""'A' == 'b'""></D>
278 proj.LoadXml (documentString);
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");
287 public void TestCondition10 ()
289 Engine engine = new Engine (Consts.BinPath);
290 Project proj = engine.CreateNewProject ();
292 string documentString = @"
293 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
295 <A Condition="" !'true' ""></A>
296 <B Condition="" 'on' == 'true' ""></B>
297 <C Condition="" 4 == 4.0 and 04 == 4""></C>
298 <D Condition="" !(false and false) == !false or !false ""></D>
299 <E Condition="" Exists ('Test\resources\Import.csproj') ""></E>
304 proj.LoadXml (documentString);
306 Assert.IsNull (proj.EvaluatedProperties ["A"], "A1");
307 Assert.IsNotNull (proj.EvaluatedProperties ["B"], "A2");
308 Assert.IsNotNull (proj.EvaluatedProperties ["C"], "A3");
309 Assert.IsNotNull (proj.EvaluatedProperties ["D"], "A4");
310 Assert.IsNotNull (proj.EvaluatedProperties ["E"], "A5");
313 public void TestCondition11 ()
315 Engine engine = new Engine (Consts.BinPath);
316 Project proj = engine.CreateNewProject ();
317 string documentString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
319 <FooProp>true</FooProp>
322 <FooList Include=""abc.exe""/>
323 <List1 Include=""fr_a.txt"" Condition="" $(FooProp) == 'true'"" />
324 <List1 Include=""fr_b.txt"" Condition="" '@(FooList->'%(Extension)a(foo', ',')' == '.exea(foo'"" />
325 <List1 Include=""fr_c.txt"" Condition="" @(FooList -> '%(Extension)', ',') == '.exe'"" />
329 proj.LoadXml (documentString);
331 BuildItemGroup bgp = proj.GetEvaluatedItemsByName ("List1");
332 Assert.IsNotNull (bgp, "Expected values in List1");
333 Assert.AreEqual (3, bgp.Count, "A1");
334 Assert.AreEqual ("fr_a.txt", bgp [0].FinalItemSpec, "A2");
335 Assert.AreEqual ("fr_b.txt", bgp [1].FinalItemSpec, "A3");
336 Assert.AreEqual ("fr_c.txt", bgp [2].FinalItemSpec, "A4");
340 public void TestHasTrailingSlash1 ()
342 Engine engine = new Engine (Consts.BinPath);
343 Project proj = engine.CreateNewProject ();
345 string documentString = @"
346 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
348 <EmptyProp></EmptyProp>
349 <WithTrailingBackSlash>foo\ </WithTrailingBackSlash>
350 <WithTrailingFwdSlash>foo/ </WithTrailingFwdSlash>
351 <NoTrailing>Foo</NoTrailing>
353 <A Condition="" HasTrailingSlash('$(EmptyProp)') ""></A>
354 <B Condition="" HasTrailingSlash('$(WithTrailingBackSlash)') ""></B>
355 <C Condition="" HasTrailingSlash('$(WithTrailingFwdSlash)') ""></C>
356 <D Condition="" HasTrailingSlash('$(NoTrailing)') ""></D>
357 <E Condition="" HasTrailingSlash('$(NonExistant)') ""></E>
362 proj.LoadXml (documentString);
364 Assert.IsNull (proj.EvaluatedProperties ["A"], "A1");
365 Assert.IsNotNull (proj.EvaluatedProperties ["B"], "A2");
366 Assert.IsNotNull (proj.EvaluatedProperties ["C"], "A3");
367 Assert.IsNull (proj.EvaluatedProperties ["D"], "A4");
368 Assert.IsNull (proj.EvaluatedProperties ["E"], "A5");
372 [ExpectedException (typeof (InvalidProjectFileException))]
373 public void TestUnknownFunction ()
375 Engine engine = new Engine (Consts.BinPath);
376 Project proj = engine.CreateNewProject ();
378 string documentString = @"
379 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
381 <A Condition="" NonExistantFunction('$(EmptyProp)') ""></A>
386 proj.LoadXml (documentString);
390 [ExpectedException (typeof (InvalidProjectFileException))]
391 public void TestIncorrectCondition1 ()
393 Engine engine = new Engine (Consts.BinPath);
394 Project proj = engine.CreateNewProject ();
396 string documentString = @"
397 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
399 <A Condition='x'>A</A>
404 proj.LoadXml (documentString);
407 // A reference to an item list at position 1 is not allowed in this condition "@(A)".
409 [ExpectedException (typeof (InvalidProjectFileException))]
410 [Category ("NotWorking")]
411 public void TestIncorrectCondition2 ()
413 Engine engine = new Engine (Consts.BinPath);
414 Project proj = engine.CreateNewProject ();
416 string documentString = @"
417 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
419 <A Condition='@(A)'>A</A>
424 proj.LoadXml (documentString);
427 // Found an unexpected character '%' at position 0 in condition \%(A)\.
429 [ExpectedException (typeof (InvalidProjectFileException))]
430 [Category ("NotWorking")]
431 public void TestIncorrectCondition3 ()
433 Engine engine = new Engine (Consts.BinPath);
434 Project proj = engine.CreateNewProject ();
436 string documentString = @"
437 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
439 <A Condition='%(A)'>A</A>
444 proj.LoadXml (documentString);
447 // Found an unexpected character '%' at position 0 in condition "%(A)\.
449 [ExpectedException (typeof (InvalidProjectFileException))]
450 [Category ("NotWorking")]
451 public void TestIncorrectCondition4 ()
453 Engine engine = new Engine (Consts.BinPath);
454 Project proj = engine.CreateNewProject ();
456 string documentString = @"
457 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
459 <A Include='a' Condition='%(A)' />
464 proj.LoadXml (documentString);
468 [ExpectedException (typeof (InvalidProjectFileException))]
469 public void TestIncorrectCondition5 ()
471 Engine engine = new Engine (Consts.BinPath);
472 Project proj = engine.CreateNewProject ();
474 string documentString = @"
475 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
477 <A Include='a' Condition="" ' == '' "" />
482 proj.LoadXml (documentString);