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 Microsoft.Build.BuildEngine;
31 using Microsoft.Build.Framework;
32 using Microsoft.Build.Utilities;
33 using NUnit.Framework;
35 namespace MonoTests.Microsoft.Build.BuildEngine {
37 public class BuildPropertyGroupTest {
39 BuildPropertyGroup bpg;
44 public void TestAssignment ()
46 bpg = new BuildPropertyGroup ();
48 Assert.AreEqual (0, bpg.Count);
49 Assert.AreEqual (false, bpg.IsImported);
53 [ExpectedException (typeof (InvalidOperationException),
54 "This method is only valid for persisted <System.Object[]> elements.")]
55 public void TestAddNewProperty1 ()
58 string value = "value";
60 bpg = new BuildPropertyGroup ();
62 bpg.AddNewProperty (name, value);
66 [ExpectedException (typeof (InvalidOperationException),
67 "Properties in persisted property groups cannot be accessed by name.")]
68 public void TestClone1 ()
70 string documentString = @"
71 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
78 engine = new Engine (Consts.BinPath);
80 project = engine.CreateNewProject ();
81 project.LoadXml (documentString);
83 IEnumerator en = project.PropertyGroups.GetEnumerator ();
85 bpg = (BuildPropertyGroup) en.Current;
86 Assert.AreEqual ("Value", bpg ["Name"].Value, "A3");
90 public void TestClear1 ()
92 bpg = new BuildPropertyGroup ();
94 bpg.SetProperty ("a", "b");
95 Assert.AreEqual (1, bpg.Count, "A1");
97 Assert.AreEqual (0, bpg.Count, "A2");
101 public void TestClear2 ()
103 string documentString = @"
104 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
111 engine = new Engine (Consts.BinPath);
112 project = engine.CreateNewProject ();
113 project.LoadXml (documentString);
114 BuildPropertyGroup [] array = new BuildPropertyGroup [1];
115 project.PropertyGroups.CopyTo (array, 0);
119 Assert.AreEqual (0, array [0].Count, "A1");
123 [ExpectedException (typeof (InvalidOperationException),
124 "Cannot set a condition on an object not represented by an XML element in the project file.")]
125 public void TestCondition1 ()
127 string condition = "condition";
129 bpg = new BuildPropertyGroup ();
131 bpg.Condition = condition;
135 public void TestCondition2 ()
137 bpg = new BuildPropertyGroup ();
139 Assert.AreEqual (String.Empty, bpg.Condition, "A1");
143 public void TestGetEnumerator1 ()
145 BuildPropertyGroup bpg = new BuildPropertyGroup ();
146 bpg.SetProperty ("a", "c");
147 bpg.SetProperty ("b", "d");
149 IEnumerator e = bpg.GetEnumerator ();
151 Assert.AreEqual ("a", ((BuildProperty) e.Current).Name, "A1");
152 Assert.AreEqual ("c", ((BuildProperty) e.Current).Value, "A2");
153 Assert.AreEqual ("c", ((BuildProperty) e.Current).FinalValue, "A3");
155 Assert.AreEqual ("b", ((BuildProperty) e.Current).Name, "A4");
156 Assert.AreEqual ("d", ((BuildProperty) e.Current).Value, "A5");
157 Assert.AreEqual ("d", ((BuildProperty) e.Current).FinalValue, "A6");
159 Assert.IsFalse (e.MoveNext (), "A7");
163 public void TestGetEnumerator2 ()
165 string documentString = @"
166 <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);
180 IEnumerator e = array [0].GetEnumerator ();
182 Assert.AreEqual ("P1", ((BuildProperty) e.Current).Name, "A1");
183 Assert.AreEqual ("1", ((BuildProperty) e.Current).Value, "A2");
184 Assert.AreEqual ("1", ((BuildProperty) e.Current).FinalValue, "A3");
186 Assert.AreEqual ("P2", ((BuildProperty) e.Current).Name, "A4");
187 Assert.AreEqual ("2", ((BuildProperty) e.Current).Value, "A5");
188 Assert.AreEqual ("2", ((BuildProperty) e.Current).FinalValue, "A6");
190 Assert.IsFalse (e.MoveNext (), "A7");
194 public void TestIndexer1 ()
196 BuildPropertyGroup bpg = new BuildPropertyGroup ();
197 bpg.SetProperty ("a", "1");
198 bpg.SetProperty ("b", "2");
200 Assert.AreEqual ("a", bpg ["a"].Name, "A1");
201 Assert.AreEqual ("b", bpg ["b"].Name, "A2");
202 Assert.IsNull (bpg ["something_that_doesnt_exist"], "A3");
206 [ExpectedException (typeof (InvalidOperationException),
207 "Properties in persisted property groups cannot be accessed by name.")]
208 public void TestIndexer2 ()
210 string documentString = @"
211 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
219 engine = new Engine (Consts.BinPath);
220 project = engine.CreateNewProject ();
221 project.LoadXml (documentString);
222 BuildPropertyGroup [] array = new BuildPropertyGroup [1];
223 project.PropertyGroups.CopyTo (array, 0);
225 Assert.AreEqual ("a", array [0] ["a"].Name, "A1");
229 [Ignore ("NullRefException on MS .NET 2.0")]
230 public void TestRemoveProperty1 ()
232 BuildPropertyGroup bpg = new BuildPropertyGroup ();
233 bpg.RemoveProperty ((BuildProperty) null);
237 [ExpectedException (typeof (ArgumentNullException))]
238 public void TestRemoveProperty2 ()
240 BuildPropertyGroup bpg = new BuildPropertyGroup ();
241 bpg.SetProperty ("a", "b");
242 bpg.SetProperty ("c", "d");
244 bpg.RemoveProperty ((string) null);
248 public void TestRemoveProperty3 ()
250 BuildPropertyGroup bpg = new BuildPropertyGroup ();
251 bpg.SetProperty ("a", "b");
252 bpg.SetProperty ("c", "d");
254 bpg.RemoveProperty ("value_not_in_group");
258 public void TestRemoveProperty4 ()
260 BuildPropertyGroup bpg = new BuildPropertyGroup ();
261 bpg.SetProperty ("a", "b");
262 bpg.SetProperty ("c", "d");
264 bpg.RemoveProperty (new BuildProperty ("name", "value"));
268 [ExpectedException (typeof (ArgumentNullException))]
269 public void TestSetProperty1 ()
271 BuildPropertyGroup bpg = new BuildPropertyGroup ();
272 bpg.SetProperty (null, null);
276 [ExpectedException (typeof (ArgumentNullException))]
277 public void TestSetProperty2 ()
279 BuildPropertyGroup bpg = new BuildPropertyGroup ();
280 bpg.SetProperty ("name", null);
284 public void TestSetProperty3 ()
286 BuildPropertyGroup bpg = new BuildPropertyGroup ();
288 bpg.SetProperty ("name", "$(A)");
290 BuildProperty bp = bpg ["name"];
292 Assert.AreEqual ("name", bp.Name, "A1");
293 Assert.AreEqual ("$(A)", bp.Value, "A2");
294 Assert.AreEqual ("$(A)", bp.FinalValue, "A3");
298 [Category ("NotWorking")]
299 public void TestSetProperty4 ()
301 BuildPropertyGroup bpg = new BuildPropertyGroup ();
303 bpg.SetProperty ("P1", "$(A)", true);
304 bpg.SetProperty ("P2", "$(A)", false);
306 BuildProperty b1 = bpg ["P1"];
307 BuildProperty b2 = bpg ["P2"];
309 Assert.AreEqual ("P1", b1.Name, "A1");
310 Assert.AreEqual (Utilities.Escape ("$(A)"), b1.Value, "A2");
311 Assert.AreEqual ("$(A)", b1.FinalValue, "A3");
312 Assert.AreEqual ("P2", b2.Name, "A4");
313 Assert.AreEqual ("$(A)", b2.Value, "A5");
314 Assert.AreEqual ("$(A)", b2.FinalValue, "A6");
318 [ExpectedException (typeof (ArgumentException),
319 "The name \"1\" contains an invalid character \"1\".")]
320 [Category ("NotWorking")]
321 public void TestSetProperty5 ()
323 BuildPropertyGroup bpg = new BuildPropertyGroup ();
325 bpg.SetProperty ("1", "$(A)");
329 public void TestSetProperty6 ()
334 string documentString = @"
335 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
337 <Property Condition=""'$(P)' != ''"">a</Property>
342 engine = new Engine (Consts.BinPath);
343 project = engine.CreateNewProject ();
344 project.LoadXml (documentString);
346 project.GlobalProperties.SetProperty ("P", "V");
348 Assert.IsNotNull (project.EvaluatedProperties ["Property"], "A1");
352 public void TestSetProperty7 ()
357 string documentString = @"
358 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
360 <Property Condition=""'$(P)' != ''"">a</Property>
366 engine = new Engine (Consts.BinPath);
367 project = engine.CreateNewProject ();
368 project.LoadXml (documentString);
370 BuildProperty p1 = project.EvaluatedProperties ["A"];
373 project.GlobalProperties.SetProperty ("P", "V");
375 Assert.AreEqual ("A", project.EvaluatedProperties ["A"].Value, "A1");
379 public void TestSetProperty8 ()
384 string documentString = @"
385 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
389 engine = new Engine (Consts.BinPath);
390 project = engine.CreateNewProject ();
391 project.LoadXml (documentString);
393 project.EvaluatedProperties.SetProperty ("A", "B");
395 Assert.IsNull (project.EvaluatedProperties ["A"], "A1");