2 // BuildPropertyTest.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;
31 using Microsoft.Build.BuildEngine;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Utilities;
34 using NUnit.Framework;
36 namespace MonoTests.Microsoft.Build.BuildEngine {
38 public class BuildPropertyTest {
44 BuildProperty [] GetProperties (BuildPropertyGroup bpg)
46 BuildProperty [] arr = new BuildProperty [bpg.Count];
48 foreach (BuildProperty bp in bpg)
54 public void TestCtor1 ()
57 string value = "value";
59 bp = new BuildProperty (name, value);
61 Assert.AreEqual (name, bp.Name, "A1");
62 Assert.AreEqual (value, bp.Value, "A2");
63 Assert.AreEqual (String.Empty, bp.Condition, "A3");
64 Assert.AreEqual (value, bp.FinalValue, "A4");
65 Assert.AreEqual (false, bp.IsImported, "A5");
66 Assert.AreEqual (value, bp.ToString (), "A6");
69 value = "$(AnotherProperty)";
71 bp = new BuildProperty (name, value);
73 Assert.AreEqual (name, bp.Name, "A7");
74 Assert.AreEqual (value, bp.Value, "A8");
75 Assert.AreEqual (String.Empty, bp.Condition, "A9");
76 Assert.AreEqual (value, bp.FinalValue, "A10");
77 Assert.AreEqual (false, bp.IsImported, "A11");
78 Assert.AreEqual (value, bp.ToString (), "A12");
82 [ExpectedException (typeof (ArgumentNullException))]
83 public void TestCtor2 ()
85 bp = new BuildProperty (null, "value");
90 [ExpectedException (typeof (ArgumentNullException))]
91 public void TestCtor3 ()
93 bp = new BuildProperty ("name", null);
97 // A shallow clone of this object cannot be created.
99 [ExpectedException (typeof (InvalidOperationException))]
100 public void TestClone1 ()
102 bp = new BuildProperty ("name", "value");
108 public void TestClone2 ()
110 bp = new BuildProperty ("name", "value");
116 [Category ("NotWorking")]
117 public void TestClone3 ()
121 string documentString = @"
122 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
129 engine = new Engine (Consts.BinPath);
131 project = engine.CreateNewProject ();
132 project.LoadXml (documentString);
134 a = project.EvaluatedProperties ["Name"];
135 Assert.AreEqual ("Value", a.Value, "A1");
139 b.Value = "AnotherValue";
140 Assert.AreEqual ("Value", a.Value, "A2");
144 [Category ("NotWorking")]
145 public void TestClone4 ()
149 string documentString = @"
150 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
157 engine = new Engine (Consts.BinPath);
159 project = engine.CreateNewProject ();
160 project.LoadXml (documentString);
162 a = project.EvaluatedProperties ["Name"];
163 Assert.AreEqual ("Value", a.Value, "A1");
167 b.Value = "AnotherValue";
168 Assert.AreEqual ("Value", a.Value, "A2");
172 [Category ("NotWorking")]
173 public void TestClone5 ()
176 IList properties = new ArrayList ();
178 string documentString = @"
179 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
186 engine = new Engine (Consts.BinPath);
188 project = engine.CreateNewProject ();
189 project.LoadXml (documentString);
191 foreach (BuildPropertyGroup bpg in project.PropertyGroups)
192 foreach (BuildProperty bpr in bpg)
193 properties.Add (bpr);
195 a = (BuildProperty) properties [0];
196 Assert.AreEqual ("Value", a.Value, "A1");
200 b.Value = "AnotherValue";
201 Assert.AreEqual ("Value", a.Value, "A2");
205 [Category ("NotWorking")]
206 public void TestClone6 ()
209 IList properties = new ArrayList ();
211 string documentString = @"
212 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
219 engine = new Engine (Consts.BinPath);
221 project = engine.CreateNewProject ();
222 project.LoadXml (documentString);
224 foreach (BuildPropertyGroup bpg in project.PropertyGroups)
225 foreach (BuildProperty bpr in bpg)
226 properties.Add (bpr);
228 a = (BuildProperty) properties [0];
229 Assert.AreEqual ("Value", a.Value, "A1");
233 b.Value = "AnotherValue";
234 Assert.AreEqual ("Value", a.Value, "A2");
238 [Category ("NotWorking")]
239 public void TestCondition1 ()
241 string documentString = @"
242 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
249 engine = new Engine (Consts.BinPath);
250 project = engine.CreateNewProject ();
251 project.LoadXml (documentString);
253 BuildProperty a = project.EvaluatedProperties ["Name"];
255 a.Condition = "true";
256 Assert.AreEqual ("true", a.Condition, "A1");
259 // Cannot set a condition on an object not represented by an XML element in the project file.
261 [ExpectedException (typeof (InvalidOperationException))]
262 public void TestCondition2 ()
264 BuildProperty a = new BuildProperty ("name", "value");
265 a.Condition = "true";
269 public void TestOpExplicit1 ()
271 bp = new BuildProperty ("name", "value");
273 Assert.AreEqual ("value", (string) bp, "A1");
277 public void TestOpExplicit2 ()
279 BuildProperty bp = null;
281 Assert.AreEqual (String.Empty, (string) bp, "A1");
285 public void TestToString ()
287 bp = new BuildProperty ("name", "a;b");
288 Assert.AreEqual ("a;b", bp.ToString ());
292 public void TestValue1 ()
295 BuildPropertyGroup [] bpgs = new BuildPropertyGroup [1];
296 BuildProperty [] props;
298 string documentString = @"
299 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
306 engine = new Engine (Consts.BinPath);
308 project = engine.CreateNewProject ();
309 project.LoadXml (documentString);
311 a = project.EvaluatedProperties ["Name"];
312 a.Value = "$(something)";
313 Assert.AreEqual ("$(something)", a.Value, "A1");
315 project.PropertyGroups.CopyTo (bpgs, 0);
316 props = GetProperties (bpgs [0]);
317 Assert.AreEqual ("Value", props [0].Value, "A2");
321 public void TestValue2 ()
323 BuildPropertyGroup [] bpgs = new BuildPropertyGroup [1];
324 BuildProperty [] props;
328 string documentString = @"
329 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
336 engine = new Engine (Consts.BinPath);
338 project = engine.CreateNewProject ();
339 project.LoadXml (documentString);
341 project.PropertyGroups.CopyTo (bpgs, 0);
342 props = GetProperties (bpgs [0]);
343 props [0].Value = "AnotherValue";
345 xd = new XmlDocument ();
346 xd.LoadXml (project.Xml);
347 node = xd.SelectSingleNode ("tns:Project/tns:PropertyGroup/tns:Name", TestNamespaceManager.NamespaceManager);
348 Assert.AreEqual ("AnotherValue", node.InnerText, "A1");
352 public void TestValueXml ()
354 BuildPropertyGroup [] bpgs = new BuildPropertyGroup [1];
355 BuildProperty [] props;
359 string documentString = @"
360 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
367 engine = new Engine (Consts.BinPath);
369 project = engine.CreateNewProject ();
370 project.LoadXml (documentString);
372 project.PropertyGroups.CopyTo (bpgs, 0);
373 bpgs[0].AddNewProperty("XmlProp", "<XmlStuff></XmlStuff>");
375 xd = new XmlDocument ();
376 xd.LoadXml (project.Xml);
377 Console.WriteLine(project.Xml);
378 node = xd.SelectSingleNode ("tns:Project/tns:PropertyGroup/tns:XmlProp/tns:XmlStuff", TestNamespaceManager.NamespaceManager);
379 Assert.IsNotNull (node, "A1");