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 "This method is only valid for persisted <System.Object[]> elements.")]
65 public void TestAddNewProperty1 ()
68 string value = "value";
70 bpg = new BuildPropertyGroup ();
72 bpg.AddNewProperty (name, value);
76 [ExpectedException (typeof (InvalidOperationException),
77 "This method is only valid for persisted <System.Object[]> elements.")]
78 public void TestAddNewProperty2 ()
83 engine = new Engine (Consts.BinPath);
84 project = engine.CreateNewProject ();
86 project.EvaluatedProperties.AddNewProperty ("a", "b");
90 [Category ("NotWorking")]
91 public void TestAddNewProperty3 ()
95 BuildPropertyGroup [] groups = new BuildPropertyGroup [2];
99 string documentString = @"
100 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
109 engine = new Engine (Consts.BinPath);
110 project = engine.CreateNewProject ();
111 project.LoadXml (documentString);
113 project.PropertyGroups.CopyTo (groups, 0);
114 Assert.AreEqual ("", project.EvaluatedProperties ["B"].FinalValue, "A0");
115 groups [0].AddNewProperty ("A", "A");
117 Assert.AreEqual (1, groups [0].Count, "A1");
118 Assert.AreEqual ("A", project.EvaluatedProperties ["A"].FinalValue, "A2");
119 Assert.AreEqual ("A", project.EvaluatedProperties ["B"].FinalValue, "A3");
121 xd = new XmlDocument ();
122 xd.LoadXml (project.Xml);
123 node = xd.SelectSingleNode ("/tns:Project/tns:PropertyGroup/tns:A", TestNamespaceManager.NamespaceManager);
124 Assert.IsNotNull (node, "A4");
127 // FIXME: what was that supposed to test?
129 [ExpectedException (typeof (InvalidOperationException),
130 "Properties in persisted property groups cannot be accessed by name.")]
131 public void TestClone1 ()
133 string documentString = @"
134 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
141 engine = new Engine (Consts.BinPath);
143 project = engine.CreateNewProject ();
144 project.LoadXml (documentString);
146 IEnumerator en = project.PropertyGroups.GetEnumerator ();
148 bpg = (BuildPropertyGroup) en.Current;
149 Assert.AreEqual ("Value", bpg ["Name"].Value, "A3");
153 public void TestClear1 ()
155 bpg = new BuildPropertyGroup ();
157 bpg.SetProperty ("a", "b");
158 Assert.AreEqual (1, bpg.Count, "A1");
160 Assert.AreEqual (0, bpg.Count, "A2");
164 public void TestClear2 ()
166 string documentString = @"
167 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
174 engine = new Engine (Consts.BinPath);
175 project = engine.CreateNewProject ();
176 project.LoadXml (documentString);
177 BuildPropertyGroup [] array = new BuildPropertyGroup [1];
178 project.PropertyGroups.CopyTo (array, 0);
182 Assert.AreEqual (0, array [0].Count, "A1");
186 [ExpectedException (typeof (InvalidOperationException),
187 "Cannot set a condition on an object not represented by an XML element in the project file.")]
188 public void TestCondition1 ()
190 string condition = "condition";
192 bpg = new BuildPropertyGroup ();
194 bpg.Condition = condition;
198 public void TestCondition2 ()
200 bpg = new BuildPropertyGroup ();
202 Assert.AreEqual (String.Empty, bpg.Condition, "A1");
206 public void TestCondition3 ()
208 string documentString = @"
209 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
215 engine = new Engine (Consts.BinPath);
216 project = engine.CreateNewProject ();
217 project.LoadXml (documentString);
218 BuildPropertyGroup [] array = new BuildPropertyGroup [1];
219 project.PropertyGroups.CopyTo (array, 0);
221 array [0].Condition = "true";
222 Assert.AreEqual ("true", array [0].Condition, "A1");
226 public void TestGetEnumerator1 ()
228 BuildPropertyGroup bpg = new BuildPropertyGroup ();
229 bpg.SetProperty ("a", "c");
230 bpg.SetProperty ("b", "d");
232 IEnumerator e = bpg.GetEnumerator ();
234 Assert.AreEqual ("a", ((BuildProperty) e.Current).Name, "A1");
235 Assert.AreEqual ("c", ((BuildProperty) e.Current).Value, "A2");
236 Assert.AreEqual ("c", ((BuildProperty) e.Current).FinalValue, "A3");
238 Assert.AreEqual ("b", ((BuildProperty) e.Current).Name, "A4");
239 Assert.AreEqual ("d", ((BuildProperty) e.Current).Value, "A5");
240 Assert.AreEqual ("d", ((BuildProperty) e.Current).FinalValue, "A6");
242 Assert.IsFalse (e.MoveNext (), "A7");
246 public void TestGetEnumerator2 ()
248 string documentString = @"
249 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
257 engine = new Engine (Consts.BinPath);
258 project = engine.CreateNewProject ();
259 project.LoadXml (documentString);
260 BuildPropertyGroup [] array = new BuildPropertyGroup [1];
261 project.PropertyGroups.CopyTo (array, 0);
263 IEnumerator e = array [0].GetEnumerator ();
265 Assert.AreEqual ("P1", ((BuildProperty) e.Current).Name, "A1");
266 Assert.AreEqual ("1", ((BuildProperty) e.Current).Value, "A2");
267 Assert.AreEqual ("1", ((BuildProperty) e.Current).FinalValue, "A3");
269 Assert.AreEqual ("P2", ((BuildProperty) e.Current).Name, "A4");
270 Assert.AreEqual ("2", ((BuildProperty) e.Current).Value, "A5");
271 Assert.AreEqual ("2", ((BuildProperty) e.Current).FinalValue, "A6");
273 Assert.IsFalse (e.MoveNext (), "A7");
277 public void TestIndexer1 ()
279 BuildPropertyGroup bpg = new BuildPropertyGroup ();
280 bpg.SetProperty ("a", "1");
281 bpg.SetProperty ("b", "2");
283 Assert.AreEqual ("a", bpg ["a"].Name, "A1");
284 Assert.AreEqual ("b", bpg ["b"].Name, "A2");
285 Assert.IsNull (bpg ["something_that_doesnt_exist"], "A3");
286 bpg ["a"].Value = "3";
287 Assert.AreEqual ("3", bpg ["a"].Value, "A4");
291 [ExpectedException (typeof (InvalidOperationException),
292 "Properties in persisted property groups cannot be accessed by name.")]
293 public void TestIndexer2 ()
295 string documentString = @"
296 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
304 engine = new Engine (Consts.BinPath);
305 project = engine.CreateNewProject ();
306 project.LoadXml (documentString);
307 BuildPropertyGroup [] array = new BuildPropertyGroup [1];
308 project.PropertyGroups.CopyTo (array, 0);
310 Assert.AreEqual ("a", array [0] ["a"].Name, "A1");
314 [ExpectedException (typeof (InvalidOperationException),
315 "Properties in persisted property groups cannot be accessed by name.")]
316 public void TestIndexer3 ()
318 string documentString = @"
319 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
327 engine = new Engine (Consts.BinPath);
328 project = engine.CreateNewProject ();
329 project.LoadXml (documentString);
330 BuildPropertyGroup [] array = new BuildPropertyGroup [1];
331 project.PropertyGroups.CopyTo (array, 0);
333 array [0] ["a"].Value = "3";
337 [Ignore ("NullRefException on MS .NET 2.0")]
338 public void TestRemoveProperty1 ()
340 BuildPropertyGroup bpg = new BuildPropertyGroup ();
341 bpg.RemoveProperty ((BuildProperty) null);
345 [ExpectedException (typeof (ArgumentNullException))]
346 public void TestRemoveProperty2 ()
348 BuildPropertyGroup bpg = new BuildPropertyGroup ();
349 bpg.SetProperty ("a", "b");
350 bpg.SetProperty ("c", "d");
352 bpg.RemoveProperty ((string) null);
356 public void TestRemoveProperty3 ()
358 BuildPropertyGroup bpg = new BuildPropertyGroup ();
359 bpg.SetProperty ("a", "b");
360 bpg.SetProperty ("c", "d");
362 bpg.RemoveProperty ("value_not_in_group");
363 bpg.RemoveProperty (new BuildProperty ("name", "value"));
365 BuildProperty bp = bpg ["a"];
367 bpg.RemoveProperty (bp);
371 public void TestRemoveProperty4 ()
375 BuildPropertyGroup [] bpg = new BuildPropertyGroup [1];
379 string documentString = @"
380 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
387 engine = new Engine (Consts.BinPath);
388 project = engine.CreateNewProject ();
389 project.LoadXml (documentString);
390 project.PropertyGroups.CopyTo (bpg, 0);
392 bpg [0].RemoveProperty ("A");
393 Assert.AreEqual (0, bpg [0].Count, "A1");
394 xd = new XmlDocument ();
395 xd.LoadXml (project.Xml);
396 node = xd.SelectSingleNode ("/tns:Project/tns:PropertyGroup/tns:A", TestNamespaceManager.NamespaceManager);
397 Assert.IsNull (node, "A3");
399 bpg [0].RemoveProperty ("B");
403 public void TestRemoveProperty5 ()
407 BuildPropertyGroup [] bpg = new BuildPropertyGroup [1];
410 BuildProperty [] properties;
412 string documentString = @"
413 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
420 engine = new Engine (Consts.BinPath);
421 project = engine.CreateNewProject ();
422 project.LoadXml (documentString);
423 project.PropertyGroups.CopyTo (bpg, 0);
425 properties = GetProperties (bpg [0]);
427 bpg [0].RemoveProperty (properties [0]);
428 Assert.AreEqual (0, bpg [0].Count, "A1");
429 xd = new XmlDocument ();
430 xd.LoadXml (project.Xml);
431 node = xd.SelectSingleNode ("/tns:Project/tns:PropertyGroup/tns:A", TestNamespaceManager.NamespaceManager);
432 Assert.IsNull (node, "A3");
436 [ExpectedException (typeof (InvalidOperationException),
437 "The specified property does not belong to the current property group.")]
438 public void TestRemoveProperty6 ()
442 BuildPropertyGroup [] bpg = new BuildPropertyGroup [1];
444 string documentString = @"
445 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
451 engine = new Engine (Consts.BinPath);
452 project = engine.CreateNewProject ();
453 project.LoadXml (documentString);
454 project.PropertyGroups.CopyTo (bpg, 0);
456 bpg [0].RemoveProperty (new BuildProperty ("A", "b"));
460 [Ignore ("It doesn't throw an exception on MS .NET 2.0")]
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");
539 [ExpectedException (typeof (ArgumentException),
540 "The name \"1\" contains an invalid character \"1\".")]
541 [Category ("NotWorking")]
542 public void TestSetProperty5 ()
544 BuildPropertyGroup bpg = new BuildPropertyGroup ();
546 bpg.SetProperty ("1", "$(A)");
550 public void TestSetProperty6 ()
555 string documentString = @"
556 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
558 <Property Condition=""'$(P)' != ''"">a</Property>
563 engine = new Engine (Consts.BinPath);
564 project = engine.CreateNewProject ();
565 project.LoadXml (documentString);
567 project.GlobalProperties.SetProperty ("P", "V");
569 Assert.IsNotNull (project.EvaluatedProperties ["Property"], "A1");
573 public void TestSetProperty7 ()
578 string documentString = @"
579 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
581 <Property Condition=""'$(P)' != ''"">a</Property>
587 engine = new Engine (Consts.BinPath);
588 project = engine.CreateNewProject ();
589 project.LoadXml (documentString);
591 BuildProperty p1 = project.EvaluatedProperties ["A"];
594 project.GlobalProperties.SetProperty ("P", "V");
596 Assert.AreEqual ("A", project.EvaluatedProperties ["A"].Value, "A1");
600 public void TestSetProperty8 ()
605 string documentString = @"
606 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
610 engine = new Engine (Consts.BinPath);
611 project = engine.CreateNewProject ();
612 project.LoadXml (documentString);
614 project.EvaluatedProperties.SetProperty ("A", "B");
616 Assert.IsNull (project.EvaluatedProperties ["A"], "A1");