2 // BuildPropertyGroupTest.cs
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2006 Marek Sieradzki
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 System.Collections.Generic;
32 using Microsoft.Build.BuildEngine;
33 using Microsoft.Build.Framework;
34 using Microsoft.Build.Utilities;
35 using NUnit.Framework;
37 namespace MonoTests.Microsoft.Build.BuildEngine {
39 public class BuildPropertyGroupTest {
41 BuildPropertyGroup bpg;
45 BuildProperty [] GetProperties (BuildPropertyGroup bpg)
47 List<BuildProperty> list = new List<BuildProperty> ();
48 foreach (BuildProperty bp in bpg)
50 return list.ToArray ();
54 public void TestAssignment ()
56 bpg = new BuildPropertyGroup ();
58 Assert.AreEqual (0, bpg.Count);
59 Assert.AreEqual (false, bpg.IsImported);
63 [ExpectedException (typeof (InvalidOperationException))]
64 public void TestAddNewProperty1 ()
67 string value = "value";
69 bpg = new BuildPropertyGroup ();
71 bpg.AddNewProperty (name, value);
75 [ExpectedException (typeof (InvalidOperationException))]
76 public void TestAddNewProperty2 ()
81 engine = new Engine (Consts.BinPath);
82 project = engine.CreateNewProject ();
84 project.EvaluatedProperties.AddNewProperty ("a", "b");
88 public void TestAddNewProperty3 ()
92 BuildPropertyGroup [] groups = new BuildPropertyGroup [2];
96 string documentString = @"
97 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
106 engine = new Engine (Consts.BinPath);
107 project = engine.CreateNewProject ();
108 project.LoadXml (documentString);
110 project.PropertyGroups.CopyTo (groups, 0);
111 Assert.AreEqual ("", project.EvaluatedProperties ["B"].FinalValue, "A0");
112 groups [0].AddNewProperty ("A", "A");
114 Assert.AreEqual (1, groups [0].Count, "A1");
115 Assert.AreEqual ("A", project.EvaluatedProperties ["A"].FinalValue, "A2");
116 Assert.AreEqual ("A", project.EvaluatedProperties ["B"].FinalValue, "A3");
118 xd = new XmlDocument ();
119 xd.LoadXml (project.Xml);
120 node = xd.SelectSingleNode ("/tns:Project/tns:PropertyGroup/tns:A", TestNamespaceManager.NamespaceManager);
121 Assert.IsNotNull (node, "A4");
124 // FIXME: what was that supposed to test?
125 // Properties in persisted property groups cannot be accessed by name.
127 [ExpectedException (typeof (InvalidOperationException))]
128 public void TestClone1 ()
130 string documentString = @"
131 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
138 engine = new Engine (Consts.BinPath);
140 project = engine.CreateNewProject ();
141 project.LoadXml (documentString);
143 IEnumerator en = project.PropertyGroups.GetEnumerator ();
145 bpg = (BuildPropertyGroup) en.Current;
146 Assert.AreEqual ("Value", bpg ["Name"].Value, "A3");
150 public void TestClear1 ()
152 bpg = new BuildPropertyGroup ();
154 bpg.SetProperty ("a", "b");
155 Assert.AreEqual (1, bpg.Count, "A1");
157 Assert.AreEqual (0, bpg.Count, "A2");
161 public void TestClear2 ()
163 string documentString = @"
164 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
171 engine = new Engine (Consts.BinPath);
172 project = engine.CreateNewProject ();
173 project.LoadXml (documentString);
174 BuildPropertyGroup [] array = new BuildPropertyGroup [1];
175 project.PropertyGroups.CopyTo (array, 0);
179 Assert.AreEqual (0, array [0].Count, "A1");
182 // Cannot set a condition on an object not represented by an XML element in the project file.
184 [ExpectedException (typeof (InvalidOperationException))]
185 public void TestCondition1 ()
187 string condition = "condition";
189 bpg = new BuildPropertyGroup ();
191 bpg.Condition = condition;
195 public void TestCondition2 ()
197 bpg = new BuildPropertyGroup ();
199 Assert.AreEqual (String.Empty, bpg.Condition, "A1");
203 public void TestCondition3 ()
205 string documentString = @"
206 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
212 engine = new Engine (Consts.BinPath);
213 project = engine.CreateNewProject ();
214 project.LoadXml (documentString);
215 BuildPropertyGroup [] array = new BuildPropertyGroup [1];
216 project.PropertyGroups.CopyTo (array, 0);
218 array [0].Condition = "true";
219 Assert.AreEqual ("true", array [0].Condition, "A1");
223 public void TestGetEnumerator1 ()
225 BuildPropertyGroup bpg = new BuildPropertyGroup ();
226 bpg.SetProperty ("a", "c");
227 bpg.SetProperty ("b", "d");
229 IEnumerator e = bpg.GetEnumerator ();
231 Assert.AreEqual ("a", ((BuildProperty) e.Current).Name, "A1");
232 Assert.AreEqual ("c", ((BuildProperty) e.Current).Value, "A2");
233 Assert.AreEqual ("c", ((BuildProperty) e.Current).FinalValue, "A3");
235 Assert.AreEqual ("b", ((BuildProperty) e.Current).Name, "A4");
236 Assert.AreEqual ("d", ((BuildProperty) e.Current).Value, "A5");
237 Assert.AreEqual ("d", ((BuildProperty) e.Current).FinalValue, "A6");
239 Assert.IsFalse (e.MoveNext (), "A7");
243 public void TestGetEnumerator2 ()
245 string documentString = @"
246 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
254 engine = new Engine (Consts.BinPath);
255 project = engine.CreateNewProject ();
256 project.LoadXml (documentString);
257 BuildPropertyGroup [] array = new BuildPropertyGroup [1];
258 project.PropertyGroups.CopyTo (array, 0);
260 IEnumerator e = array [0].GetEnumerator ();
262 Assert.AreEqual ("P1", ((BuildProperty) e.Current).Name, "A1");
263 Assert.AreEqual ("1", ((BuildProperty) e.Current).Value, "A2");
264 Assert.AreEqual ("1", ((BuildProperty) e.Current).FinalValue, "A3");
266 Assert.AreEqual ("P2", ((BuildProperty) e.Current).Name, "A4");
267 Assert.AreEqual ("2", ((BuildProperty) e.Current).Value, "A5");
268 Assert.AreEqual ("2", ((BuildProperty) e.Current).FinalValue, "A6");
270 Assert.IsFalse (e.MoveNext (), "A7");
274 public void TestIndexer1 ()
276 BuildPropertyGroup bpg = new BuildPropertyGroup ();
277 bpg.SetProperty ("a", "1");
278 bpg.SetProperty ("b", "2");
280 Assert.AreEqual ("a", bpg ["a"].Name, "A1");
281 Assert.AreEqual ("b", bpg ["b"].Name, "A2");
282 Assert.IsNull (bpg ["something_that_doesnt_exist"], "A3");
283 bpg ["a"].Value = "3";
284 Assert.AreEqual ("3", bpg ["a"].Value, "A4");
287 // Properties in persisted property groups cannot be accessed by name.
289 [ExpectedException (typeof (InvalidOperationException))]
290 public void TestIndexer2 ()
292 string documentString = @"
293 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
301 engine = new Engine (Consts.BinPath);
302 project = engine.CreateNewProject ();
303 project.LoadXml (documentString);
304 BuildPropertyGroup [] array = new BuildPropertyGroup [1];
305 project.PropertyGroups.CopyTo (array, 0);
307 Assert.AreEqual ("a", array [0] ["a"].Name, "A1");
310 // Properties in persisted property groups cannot be accessed by name.
312 [ExpectedException (typeof (InvalidOperationException))]
313 public void TestIndexer3 ()
315 string documentString = @"
316 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
324 engine = new Engine (Consts.BinPath);
325 project = engine.CreateNewProject ();
326 project.LoadXml (documentString);
327 BuildPropertyGroup [] array = new BuildPropertyGroup [1];
328 project.PropertyGroups.CopyTo (array, 0);
330 array [0] ["a"].Value = "3";
334 [Category ("NotDotNet")]
335 [ExpectedException (typeof (ArgumentNullException))]
336 public void TestRemoveProperty1 ()
338 BuildPropertyGroup bpg = new BuildPropertyGroup ();
339 bpg.RemoveProperty ((BuildProperty) null);
343 [ExpectedException (typeof (ArgumentNullException))]
344 public void TestRemoveProperty2 ()
346 BuildPropertyGroup bpg = new BuildPropertyGroup ();
347 bpg.SetProperty ("a", "b");
348 bpg.SetProperty ("c", "d");
350 bpg.RemoveProperty ((string) null);
354 public void TestRemoveProperty3 ()
356 BuildPropertyGroup bpg = new BuildPropertyGroup ();
357 bpg.SetProperty ("a", "b");
358 bpg.SetProperty ("c", "d");
360 bpg.RemoveProperty ("value_not_in_group");
361 bpg.RemoveProperty (new BuildProperty ("name", "value"));
363 BuildProperty bp = bpg ["a"];
365 bpg.RemoveProperty (bp);
369 public void TestRemoveProperty4 ()
373 BuildPropertyGroup [] bpg = new BuildPropertyGroup [1];
377 string documentString = @"
378 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
385 engine = new Engine (Consts.BinPath);
386 project = engine.CreateNewProject ();
387 project.LoadXml (documentString);
388 project.PropertyGroups.CopyTo (bpg, 0);
390 bpg [0].RemoveProperty ("A");
391 Assert.AreEqual (0, bpg [0].Count, "A1");
392 xd = new XmlDocument ();
393 xd.LoadXml (project.Xml);
394 node = xd.SelectSingleNode ("/tns:Project/tns:PropertyGroup/tns:A", TestNamespaceManager.NamespaceManager);
395 Assert.IsNull (node, "A3");
397 bpg [0].RemoveProperty ("B");
401 public void TestRemoveProperty5 ()
405 BuildPropertyGroup [] bpg = new BuildPropertyGroup [1];
408 BuildProperty [] properties;
410 string documentString = @"
411 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
418 engine = new Engine (Consts.BinPath);
419 project = engine.CreateNewProject ();
420 project.LoadXml (documentString);
421 project.PropertyGroups.CopyTo (bpg, 0);
423 properties = GetProperties (bpg [0]);
425 bpg [0].RemoveProperty (properties [0]);
426 Assert.AreEqual (0, bpg [0].Count, "A1");
427 xd = new XmlDocument ();
428 xd.LoadXml (project.Xml);
429 node = xd.SelectSingleNode ("/tns:Project/tns:PropertyGroup/tns:A", TestNamespaceManager.NamespaceManager);
430 Assert.IsNull (node, "A3");
433 // The specified property does not belong to the current property group.
435 [ExpectedException (typeof (InvalidOperationException))]
436 public void TestRemoveProperty6 ()
440 BuildPropertyGroup [] bpg = new BuildPropertyGroup [1];
442 string documentString = @"
443 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
449 engine = new Engine (Consts.BinPath);
450 project = engine.CreateNewProject ();
451 project.LoadXml (documentString);
452 project.PropertyGroups.CopyTo (bpg, 0);
454 bpg [0].RemoveProperty (new BuildProperty ("A", "b"));
458 [Category ("NotDotNet")]
459 [Category ("NotWorking")]
460 [ExpectedException (typeof (InvalidOperationException))]
461 public void TestRemoveProperty7 ()
465 BuildPropertyGroup [] bpg = new BuildPropertyGroup [2];
466 BuildProperty [] properties;
468 string documentString = @"
469 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
478 engine = new Engine (Consts.BinPath);
479 project = engine.CreateNewProject ();
480 project.LoadXml (documentString);
481 project.PropertyGroups.CopyTo (bpg, 0);
483 properties = GetProperties (bpg [0]);
484 bpg [1].RemoveProperty (properties [0]);
485 Assert.AreEqual (1, bpg [0].Count, "A1");
489 [ExpectedException (typeof (ArgumentNullException))]
490 public void TestSetProperty1 ()
492 BuildPropertyGroup bpg = new BuildPropertyGroup ();
493 bpg.SetProperty (null, null);
497 [ExpectedException (typeof (ArgumentNullException))]
498 public void TestSetProperty2 ()
500 BuildPropertyGroup bpg = new BuildPropertyGroup ();
501 bpg.SetProperty ("name", null);
505 public void TestSetProperty3 ()
507 BuildPropertyGroup bpg = new BuildPropertyGroup ();
509 bpg.SetProperty ("name", "$(A)");
511 BuildProperty bp = bpg ["name"];
513 Assert.AreEqual ("name", bp.Name, "A1");
514 Assert.AreEqual ("$(A)", bp.Value, "A2");
515 Assert.AreEqual ("$(A)", bp.FinalValue, "A3");
519 [Category ("NotWorking")]
520 public void TestSetProperty4 ()
522 BuildPropertyGroup bpg = new BuildPropertyGroup ();
524 bpg.SetProperty ("P1", "$(A)", true);
525 bpg.SetProperty ("P2", "$(A)", false);
527 BuildProperty b1 = bpg ["P1"];
528 BuildProperty b2 = bpg ["P2"];
530 Assert.AreEqual ("P1", b1.Name, "A1");
531 Assert.AreEqual (Utilities.Escape ("$(A)"), b1.Value, "A2");
532 Assert.AreEqual ("$(A)", b1.FinalValue, "A3");
533 Assert.AreEqual ("P2", b2.Name, "A4");
534 Assert.AreEqual ("$(A)", b2.Value, "A5");
535 Assert.AreEqual ("$(A)", b2.FinalValue, "A6");
538 // The name "1" contains an invalid character "1".
540 [ExpectedException (typeof (ArgumentException))]
541 public void TestSetProperty5 ()
543 BuildPropertyGroup bpg = new BuildPropertyGroup ();
545 bpg.SetProperty ("1", "$(A)");
549 public void TestSetProperty6 ()
554 string documentString = @"
555 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
557 <Property Condition=""'$(P)' != ''"">a</Property>
562 engine = new Engine (Consts.BinPath);
563 project = engine.CreateNewProject ();
564 project.LoadXml (documentString);
566 project.GlobalProperties.SetProperty ("P", "V");
568 Assert.IsNotNull (project.EvaluatedProperties ["Property"], "A1");
572 public void TestSetProperty7 ()
577 string documentString = @"
578 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
580 <Property Condition=""'$(P)' != ''"">a</Property>
586 engine = new Engine (Consts.BinPath);
587 project = engine.CreateNewProject ();
588 project.LoadXml (documentString);
590 BuildProperty p1 = project.EvaluatedProperties ["A"];
593 project.GlobalProperties.SetProperty ("P", "V");
595 Assert.AreEqual ("A", project.EvaluatedProperties ["A"].Value, "A1");
599 public void TestSetProperty8 ()
604 string documentString = @"
605 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
609 engine = new Engine (Consts.BinPath);
610 project = engine.CreateNewProject ();
611 project.LoadXml (documentString);
613 project.EvaluatedProperties.SetProperty ("A", "B");
615 Assert.IsNull (project.EvaluatedProperties ["A"], "A1");
618 // This method is only valid for virtual property groups, not <PropertyGroup> elements.
620 [ExpectedException (typeof (InvalidOperationException))]
621 public void TestSetProperty9 ()
625 BuildPropertyGroup [] groups = new BuildPropertyGroup [1];
627 string documentString = @"
628 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
633 engine = new Engine (Consts.BinPath);
634 project = engine.CreateNewProject ();
635 project.LoadXml (documentString);
637 project.PropertyGroups.CopyTo (groups, 0);
638 groups [0].SetProperty ("A", "B");