d3c944c7d2bc301a272ca00a27132530a64f6f27
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / Microsoft.Build.BuildEngine / BuildPropertyTest.cs
1 //
2 // BuildPropertyTest.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2006 Marek Sieradzki
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27
28 using System;
29 using System.Collections;
30 using System.Xml;
31 using Microsoft.Build.BuildEngine;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Utilities;
34 using NUnit.Framework;
35
36 namespace MonoTests.Microsoft.Build.BuildEngine {
37         [TestFixture]
38         public class BuildPropertyTest {
39                 
40                 BuildProperty   bp;
41                 Engine          engine;
42                 Project         project;
43
44                 BuildProperty [] GetProperties (BuildPropertyGroup bpg)
45                 {
46                         BuildProperty [] arr = new BuildProperty [bpg.Count];
47                         int i = 0;
48                         foreach (BuildProperty bp in bpg)
49                                 arr [i++] = bp;
50                         return arr;
51                 }
52
53                 [Test]
54                 public void TestCtor1 ()
55                 {
56                         string name = "name";
57                         string value = "value";
58                 
59                         bp = new BuildProperty (name, value);
60                         
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");
67                 
68                         name = "name";
69                         value = "$(AnotherProperty)";
70                 
71                         bp = new BuildProperty (name, value);
72                         
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");
79                 }
80                 
81                 [Test]
82                 [ExpectedException (typeof (ArgumentNullException))]
83                 public void TestCtor2 ()
84                 {
85                         bp = new BuildProperty (null, "value");
86                         
87                 }
88                 
89                 [Test]
90                 [ExpectedException (typeof (ArgumentNullException))]
91                 public void TestCtor3 ()
92                 {
93                         bp = new BuildProperty ("name", null);
94                         
95                 }
96
97                 // A shallow clone of this object cannot be created.
98                 [Test]
99                 [ExpectedException (typeof (InvalidOperationException))]
100                 public void TestClone1 ()
101                 {
102                         bp = new BuildProperty ("name", "value");
103                         
104                         bp.Clone (false);
105                 }
106
107                 [Test]
108                 public void TestClone2 ()
109                 {
110                         bp = new BuildProperty ("name", "value");
111                         
112                         bp.Clone (true);
113                 }
114                 
115                 [Test]
116                 [Category ("NotWorking")]
117                 public void TestClone3 ()
118                 {
119                         BuildProperty a,b;
120                         
121                         string documentString = @"
122                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
123                                         <PropertyGroup>
124                                                 <Name>Value</Name>
125                                         </PropertyGroup>
126                                 </Project>
127                         ";
128
129                         engine = new Engine (Consts.BinPath);
130
131                         project = engine.CreateNewProject ();
132                         project.LoadXml (documentString);
133
134                         a = project.EvaluatedProperties ["Name"];
135                         Assert.AreEqual ("Value", a.Value, "A1");
136                         
137                         b = a.Clone (false);
138                         
139                         b.Value = "AnotherValue";
140                         Assert.AreEqual ("Value", a.Value, "A2");
141                 }
142
143                 [Test]
144                 [Category ("NotWorking")]
145                 public void TestClone4 ()
146                 {
147                         BuildProperty a,b;
148                         
149                         string documentString = @"
150                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
151                                         <PropertyGroup>
152                                                 <Name>Value</Name>
153                                         </PropertyGroup>
154                                 </Project>
155                         ";
156
157                         engine = new Engine (Consts.BinPath);
158
159                         project = engine.CreateNewProject ();
160                         project.LoadXml (documentString);
161
162                         a = project.EvaluatedProperties ["Name"];
163                         Assert.AreEqual ("Value", a.Value, "A1");
164                         
165                         b = a.Clone (true);
166                         
167                         b.Value = "AnotherValue";
168                         Assert.AreEqual ("Value", a.Value, "A2");
169                 }
170
171                 [Test]
172                 [Category ("NotWorking")]
173                 public void TestClone5 ()
174                 {
175                         BuildProperty a,b;
176                         IList properties = new ArrayList ();
177                         
178                         string documentString = @"
179                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
180                                         <PropertyGroup>
181                                                 <Name>Value</Name>
182                                         </PropertyGroup>
183                                 </Project>
184                         ";
185
186                         engine = new Engine (Consts.BinPath);
187
188                         project = engine.CreateNewProject ();
189                         project.LoadXml (documentString);
190
191                         foreach (BuildPropertyGroup bpg in project.PropertyGroups)
192                                 foreach (BuildProperty bpr in bpg)
193                                         properties.Add (bpr);
194                         
195                         a = (BuildProperty) properties [0];
196                         Assert.AreEqual ("Value", a.Value, "A1");
197                         
198                         b = a.Clone (false);
199                         
200                         b.Value = "AnotherValue";
201                         Assert.AreEqual ("Value", a.Value, "A2");
202                 }
203
204                 [Test]
205                 [Category ("NotWorking")]
206                 public void TestClone6 ()
207                 {
208                         BuildProperty a,b;
209                         IList properties = new ArrayList ();
210                         
211                         string documentString = @"
212                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
213                                         <PropertyGroup>
214                                                 <Name>Value</Name>
215                                         </PropertyGroup>
216                                 </Project>
217                         ";
218
219                         engine = new Engine (Consts.BinPath);
220
221                         project = engine.CreateNewProject ();
222                         project.LoadXml (documentString);
223
224                         foreach (BuildPropertyGroup bpg in project.PropertyGroups)
225                                 foreach (BuildProperty bpr in bpg)
226                                         properties.Add (bpr);
227                         
228                         a = (BuildProperty) properties [0];
229                         Assert.AreEqual ("Value", a.Value, "A1");
230                         
231                         b = a.Clone (true);
232                         
233                         b.Value = "AnotherValue";
234                         Assert.AreEqual ("Value", a.Value, "A2");
235                 }
236
237                 [Test]
238                 [Category ("NotWorking")]
239                 public void TestCondition1 ()
240                 {
241                         string documentString = @"
242                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
243                                         <PropertyGroup>
244                                                 <Name>Value</Name>
245                                         </PropertyGroup>
246                                 </Project>
247                         ";
248
249                         engine = new Engine (Consts.BinPath);
250                         project = engine.CreateNewProject ();
251                         project.LoadXml (documentString);
252
253                         BuildProperty a = project.EvaluatedProperties ["Name"];
254
255                         a.Condition = "true";
256                         Assert.AreEqual ("true", a.Condition, "A1");
257                 }
258
259                 // Cannot set a condition on an object not represented by an XML element in the project file.
260                 [Test]
261                 [ExpectedException (typeof (InvalidOperationException))]
262                 public void TestCondition2 ()
263                 {
264                         BuildProperty a = new BuildProperty ("name", "value");
265                         a.Condition = "true";
266                 }
267
268                 [Test]
269                 public void TestOpExplicit1 ()
270                 {
271                         bp = new BuildProperty ("name", "value");
272                         
273                         Assert.AreEqual ("value", (string) bp, "A1");
274                 }
275
276                 [Test]
277                 public void TestOpExplicit2 ()
278                 {
279                         BuildProperty bp = null;
280                         
281                         Assert.AreEqual (String.Empty, (string) bp, "A1");
282                 }
283                 
284                 [Test]
285                 public void TestToString ()
286                 {
287                         bp = new BuildProperty ("name", "a;b");
288                         Assert.AreEqual ("a;b", bp.ToString ());
289                 }
290
291                 [Test]
292                 public void TestValue1 ()
293                 {
294                         BuildProperty a;
295                         BuildPropertyGroup [] bpgs = new BuildPropertyGroup [1];
296                         BuildProperty [] props;
297
298                         string documentString = @"
299                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
300                                         <PropertyGroup>
301                                                 <Name>Value</Name>
302                                         </PropertyGroup>
303                                 </Project>
304                         ";
305
306                         engine = new Engine (Consts.BinPath);
307
308                         project = engine.CreateNewProject ();
309                         project.LoadXml (documentString);
310
311                         a = project.EvaluatedProperties ["Name"];
312                         a.Value = "$(something)";
313                         Assert.AreEqual ("$(something)", a.Value, "A1");
314
315                         project.PropertyGroups.CopyTo (bpgs, 0);
316                         props = GetProperties (bpgs [0]);
317                         Assert.AreEqual ("Value", props [0].Value, "A2");
318                 }
319
320                 [Test]
321                 public void TestValue2 ()
322                 {
323                         BuildPropertyGroup [] bpgs = new BuildPropertyGroup [1];
324                         BuildProperty [] props;
325                         XmlDocument xd;
326                         XmlNode node;
327
328                         string documentString = @"
329                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
330                                         <PropertyGroup>
331                                                 <Name>Value</Name>
332                                         </PropertyGroup>
333                                 </Project>
334                         ";
335
336                         engine = new Engine (Consts.BinPath);
337
338                         project = engine.CreateNewProject ();
339                         project.LoadXml (documentString);
340                         
341                         project.PropertyGroups.CopyTo (bpgs, 0);
342                         props = GetProperties (bpgs [0]);
343                         props [0].Value = "AnotherValue";
344
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");
349                 }
350
351                 [Test]
352                 public void TestValueXml ()
353                 {
354                         BuildPropertyGroup [] bpgs = new BuildPropertyGroup [1];
355                         BuildProperty [] props;
356                         XmlDocument xd;
357                         XmlNode node;
358
359                         string documentString = @"
360                                         <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
361                                                         <PropertyGroup>
362                                                                         <Name>Value</Name>
363                                                         </PropertyGroup>
364                                         </Project>
365                         ";
366
367                         engine = new Engine (Consts.BinPath);
368
369                         project = engine.CreateNewProject ();
370                         project.LoadXml (documentString);
371
372                         project.PropertyGroups.CopyTo (bpgs, 0);
373                         bpgs[0].AddNewProperty("XmlProp", "<XmlStuff></XmlStuff>");
374
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");
380                 }
381
382         }
383 }