2007-01-12 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / Microsoft.Build.BuildEngine / BuildPropertyGroupTest.cs
1 //
2 // BuildPropertyGroupTest.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.Collections.Generic;
31 using System.Xml;
32 using Microsoft.Build.BuildEngine;
33 using Microsoft.Build.Framework;
34 using Microsoft.Build.Utilities;
35 using NUnit.Framework;
36
37 namespace MonoTests.Microsoft.Build.BuildEngine {
38         [TestFixture]
39         public class BuildPropertyGroupTest {
40                 
41                 BuildPropertyGroup      bpg;
42                 Engine                  engine;
43                 Project                 project;
44
45                 BuildProperty [] GetProperties (BuildPropertyGroup bpg)
46                 {
47                         List<BuildProperty> list = new List<BuildProperty> ();
48                         foreach (BuildProperty bp in bpg)
49                                 list.Add (bp);
50                         return list.ToArray ();
51                 }
52                 
53                 [Test]
54                 public void TestAssignment ()
55                 {
56                         bpg = new BuildPropertyGroup ();
57                         
58                         Assert.AreEqual (0, bpg.Count);
59                         Assert.AreEqual (false, bpg.IsImported);
60                 }
61
62                 [Test]
63                 [ExpectedException (typeof (InvalidOperationException),
64                         "This method is only valid for persisted <System.Object[]> elements.")]
65                 public void TestAddNewProperty1 ()
66                 {
67                         string name = "name";
68                         string value = "value";
69
70                         bpg = new BuildPropertyGroup ();
71
72                         bpg.AddNewProperty (name, value);
73                 }
74
75                 [Test]
76                 [ExpectedException (typeof (InvalidOperationException),
77                         "This method is only valid for persisted <System.Object[]> elements.")]
78                 public void TestAddNewProperty2 ()
79                 {
80                         Engine engine;
81                         Project project;
82
83                         engine = new Engine (Consts.BinPath);
84                         project = engine.CreateNewProject ();
85
86                         project.EvaluatedProperties.AddNewProperty ("a", "b");
87                 }
88
89                 [Test]
90                 [Category ("NotWorking")]
91                 public void TestAddNewProperty3 ()
92                 {
93                         Engine engine;
94                         Project project;
95                         BuildPropertyGroup [] groups = new BuildPropertyGroup [2];
96                         XmlDocument xd;
97                         XmlNode node;
98
99                         string documentString = @"
100                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
101                                         <PropertyGroup>
102                                         </PropertyGroup>
103                                         <PropertyGroup>
104                                                 <B>$(A)</B>
105                                         </PropertyGroup>
106                                 </Project>
107                         ";
108
109                         engine = new Engine (Consts.BinPath);
110                         project = engine.CreateNewProject ();
111                         project.LoadXml (documentString);
112
113                         project.PropertyGroups.CopyTo (groups, 0);
114                         Assert.AreEqual ("", project.EvaluatedProperties ["B"].FinalValue, "A0");
115                         groups [0].AddNewProperty ("A", "A");
116
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");
120
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");
125                 }
126
127                 // FIXME: what was that supposed to test?
128                 [Test]
129                 [ExpectedException (typeof (InvalidOperationException),
130                         "Properties in persisted property groups cannot be accessed by name.")]
131                 public void TestClone1 ()
132                 {
133                         string documentString = @"
134                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
135                                         <PropertyGroup>
136                                                 <Name>Value</Name>
137                                         </PropertyGroup>
138                                 </Project>
139                         ";
140
141                         engine = new Engine (Consts.BinPath);
142
143                         project = engine.CreateNewProject ();
144                         project.LoadXml (documentString);
145
146                         IEnumerator en = project.PropertyGroups.GetEnumerator ();
147                         en.MoveNext ();
148                         bpg = (BuildPropertyGroup) en.Current;
149                         Assert.AreEqual ("Value", bpg ["Name"].Value, "A3");
150                 }
151
152                 [Test]
153                 public void TestClear1 ()
154                 {
155                         bpg = new BuildPropertyGroup ();
156                         
157                         bpg.SetProperty ("a", "b");
158                         Assert.AreEqual (1, bpg.Count, "A1");
159                         bpg.Clear ();
160                         Assert.AreEqual (0, bpg.Count, "A2");
161                 }
162
163                 [Test]
164                 public void TestClear2 ()
165                 {
166                         string documentString = @"
167                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
168                                         <PropertyGroup>
169                                                 <Name>Value</Name>
170                                         </PropertyGroup>
171                                 </Project>
172                         ";
173
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);
179
180                         array [0].Clear ();
181
182                         Assert.AreEqual (0, array [0].Count, "A1");
183                 }
184
185                 [Test]
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 ()
189                 {
190                         string condition = "condition";
191                 
192                         bpg = new BuildPropertyGroup ();
193                 
194                         bpg.Condition = condition;
195                 }
196
197                 [Test]
198                 public void TestCondition2 ()
199                 {
200                         bpg = new BuildPropertyGroup ();
201                 
202                         Assert.AreEqual (String.Empty, bpg.Condition, "A1");
203                 }
204
205                 [Test]
206                 public void TestCondition3 ()
207                 {
208                         string documentString = @"
209                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
210                                         <PropertyGroup>
211                                         </PropertyGroup>
212                                 </Project>
213                         ";
214
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);
220
221                         array [0].Condition = "true";
222                         Assert.AreEqual ("true", array [0].Condition, "A1");
223                 }
224
225                 [Test]
226                 public void TestGetEnumerator1 ()
227                 {
228                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
229                         bpg.SetProperty ("a", "c");
230                         bpg.SetProperty ("b", "d");
231
232                         IEnumerator e = bpg.GetEnumerator ();
233                         e.MoveNext ();
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");
237                         e.MoveNext ();
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");
241
242                         Assert.IsFalse (e.MoveNext (), "A7");
243                 }
244
245                 [Test]
246                 public void TestGetEnumerator2 ()
247                 {
248                         string documentString = @"
249                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
250                                         <PropertyGroup>
251                                                 <P1>1</P1>
252                                                 <P2>2</P2>
253                                         </PropertyGroup>
254                                 </Project>
255                         ";
256
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);
262
263                         IEnumerator e = array [0].GetEnumerator ();
264                         e.MoveNext ();
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");
268                         e.MoveNext ();
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");
272
273                         Assert.IsFalse (e.MoveNext (), "A7");
274                 }
275
276                 [Test]
277                 public void TestIndexer1 ()
278                 {
279                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
280                         bpg.SetProperty ("a", "1");
281                         bpg.SetProperty ("b", "2");
282
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");
288                 }
289
290                 [Test]
291                 [ExpectedException (typeof (InvalidOperationException),
292                         "Properties in persisted property groups cannot be accessed by name.")]
293                 public void TestIndexer2 ()
294                 {
295                         string documentString = @"
296                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
297                                         <PropertyGroup>
298                                                 <a>1</a>
299                                                 <b>2</b>
300                                         </PropertyGroup>
301                                 </Project>
302                         ";
303
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);
309
310                         Assert.AreEqual ("a", array [0] ["a"].Name, "A1");
311                 }
312
313                 [Test]
314                 [ExpectedException (typeof (InvalidOperationException),
315                         "Properties in persisted property groups cannot be accessed by name.")]
316                 public void TestIndexer3 ()
317                 {
318                         string documentString = @"
319                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
320                                         <PropertyGroup>
321                                                 <a>1</a>
322                                                 <b>2</b>
323                                         </PropertyGroup>
324                                 </Project>
325                         ";
326
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);
332
333                         array [0] ["a"].Value = "3";
334                 }
335
336                 [Test]
337                 [Ignore ("NullRefException on MS .NET 2.0")]
338                 public void TestRemoveProperty1 ()
339                 {
340                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
341                         bpg.RemoveProperty ((BuildProperty) null);
342                 }
343
344                 [Test]
345                 [ExpectedException (typeof (ArgumentNullException))]
346                 public void TestRemoveProperty2 ()
347                 {
348                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
349                         bpg.SetProperty ("a", "b");
350                         bpg.SetProperty ("c", "d");
351
352                         bpg.RemoveProperty ((string) null);
353                 }
354
355                 [Test]
356                 public void TestRemoveProperty3 ()
357                 {
358                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
359                         bpg.SetProperty ("a", "b");
360                         bpg.SetProperty ("c", "d");
361
362                         bpg.RemoveProperty ("value_not_in_group");
363                         bpg.RemoveProperty (new BuildProperty ("name", "value"));
364
365                         BuildProperty bp = bpg ["a"];
366
367                         bpg.RemoveProperty (bp);
368                 }
369
370                 [Test]
371                 public void TestRemoveProperty4 ()
372                 {
373                         Engine engine;
374                         Project project;
375                         BuildPropertyGroup [] bpg = new BuildPropertyGroup [1];
376                         XmlDocument xd;
377                         XmlNode node;
378
379                         string documentString = @"
380                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
381                                         <PropertyGroup>
382                                                 <A>A</A>
383                                         </PropertyGroup>
384                                 </Project>
385                         ";
386
387                         engine = new Engine (Consts.BinPath);
388                         project = engine.CreateNewProject ();
389                         project.LoadXml (documentString);
390                         project.PropertyGroups.CopyTo (bpg, 0);
391
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");
398
399                         bpg [0].RemoveProperty ("B");
400                 }
401
402                 [Test]
403                 public void TestRemoveProperty5 ()
404                 {
405                         Engine engine;
406                         Project project;
407                         BuildPropertyGroup [] bpg = new BuildPropertyGroup [1];
408                         XmlDocument xd;
409                         XmlNode node;
410                         BuildProperty [] properties;
411
412                         string documentString = @"
413                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
414                                         <PropertyGroup>
415                                                 <A>A</A>
416                                         </PropertyGroup>
417                                 </Project>
418                         ";
419
420                         engine = new Engine (Consts.BinPath);
421                         project = engine.CreateNewProject ();
422                         project.LoadXml (documentString);
423                         project.PropertyGroups.CopyTo (bpg, 0);
424
425                         properties = GetProperties (bpg [0]);
426
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");
433                 }
434
435                 [Test]
436                 [ExpectedException (typeof (InvalidOperationException),
437                         "The specified property does not belong to the current property group.")]
438                 public void TestRemoveProperty6 ()
439                 {
440                         Engine engine;
441                         Project project;
442                         BuildPropertyGroup [] bpg = new BuildPropertyGroup [1];
443
444                         string documentString = @"
445                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
446                                         <PropertyGroup>
447                                         </PropertyGroup>
448                                 </Project>
449                         ";
450
451                         engine = new Engine (Consts.BinPath);
452                         project = engine.CreateNewProject ();
453                         project.LoadXml (documentString);
454                         project.PropertyGroups.CopyTo (bpg, 0);
455
456                         bpg [0].RemoveProperty (new BuildProperty ("A", "b"));
457                 }
458
459                 [Test]
460                 [Ignore ("It doesn't throw an exception on MS .NET 2.0")]
461                 public void TestRemoveProperty7 ()
462                 {
463                         Engine engine;
464                         Project project;
465                         BuildPropertyGroup [] bpg = new BuildPropertyGroup [2];
466                         BuildProperty [] properties;
467
468                         string documentString = @"
469                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
470                                         <PropertyGroup>
471                                                 <A></A>
472                                         </PropertyGroup>
473                                         <PropertyGroup>
474                                         </PropertyGroup>
475                                 </Project>
476                         ";
477
478                         engine = new Engine (Consts.BinPath);
479                         project = engine.CreateNewProject ();
480                         project.LoadXml (documentString);
481                         project.PropertyGroups.CopyTo (bpg, 0);
482
483                         properties = GetProperties (bpg [0]);
484                         bpg [1].RemoveProperty (properties [0]);
485                         Assert.AreEqual (1, bpg [0].Count, "A1");
486                 }
487
488                 [Test]
489                 [ExpectedException (typeof (ArgumentNullException))]
490                 public void TestSetProperty1 ()
491                 {
492                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
493                         bpg.SetProperty (null, null);
494                 }
495
496                 [Test]
497                 [ExpectedException (typeof (ArgumentNullException))]
498                 public void TestSetProperty2 ()
499                 {
500                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
501                         bpg.SetProperty ("name", null);
502                 }
503
504                 [Test]
505                 public void TestSetProperty3 ()
506                 {
507                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
508                         
509                         bpg.SetProperty ("name", "$(A)");
510
511                         BuildProperty bp = bpg ["name"];
512
513                         Assert.AreEqual ("name", bp.Name, "A1");
514                         Assert.AreEqual ("$(A)", bp.Value, "A2");
515                         Assert.AreEqual ("$(A)", bp.FinalValue, "A3");
516                 }
517
518                 [Test]
519                 [Category ("NotWorking")]
520                 public void TestSetProperty4 ()
521                 {
522                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
523
524                         bpg.SetProperty ("P1", "$(A)", true);
525                         bpg.SetProperty ("P2", "$(A)", false);
526
527                         BuildProperty b1 = bpg ["P1"];
528                         BuildProperty b2 = bpg ["P2"];
529
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");
536                 }
537
538                 [Test]
539                 [ExpectedException (typeof (ArgumentException),
540                         "The name \"1\" contains an invalid character \"1\".")]
541                 [Category ("NotWorking")]
542                 public void TestSetProperty5 ()
543                 {
544                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
545
546                         bpg.SetProperty ("1", "$(A)");
547                 }
548
549                 [Test]
550                 public void TestSetProperty6 ()
551                 {
552                         Engine engine;
553                         Project project;
554
555                         string documentString = @"
556                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
557                                         <PropertyGroup>
558                                                 <Property Condition=""'$(P)' != ''"">a</Property>
559                                         </PropertyGroup>
560                                 </Project>
561                         ";
562
563                         engine = new Engine (Consts.BinPath);
564                         project = engine.CreateNewProject ();
565                         project.LoadXml (documentString);
566
567                         project.GlobalProperties.SetProperty ("P", "V");
568
569                         Assert.IsNotNull (project.EvaluatedProperties ["Property"], "A1");
570                 }
571
572                 [Test]
573                 public void TestSetProperty7 ()
574                 {
575                         Engine engine;
576                         Project project;
577
578                         string documentString = @"
579                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
580                                         <PropertyGroup>
581                                                 <Property Condition=""'$(P)' != ''"">a</Property>
582                                                 <A>A</A>
583                                         </PropertyGroup>
584                                 </Project>
585                         ";
586
587                         engine = new Engine (Consts.BinPath);
588                         project = engine.CreateNewProject ();
589                         project.LoadXml (documentString);
590
591                         BuildProperty p1 = project.EvaluatedProperties ["A"];
592                         p1.Value = "B";
593
594                         project.GlobalProperties.SetProperty ("P", "V");
595
596                         Assert.AreEqual ("A", project.EvaluatedProperties ["A"].Value, "A1");
597                 }
598
599                 [Test]
600                 public void TestSetProperty8 ()
601                 {
602                         Engine engine;
603                         Project project;
604
605                         string documentString = @"
606                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
607                                 </Project>
608                         ";
609
610                         engine = new Engine (Consts.BinPath);
611                         project = engine.CreateNewProject ();
612                         project.LoadXml (documentString);
613
614                         project.EvaluatedProperties.SetProperty ("A", "B");
615
616                         Assert.IsNull (project.EvaluatedProperties ["A"], "A1");
617                 }
618         }
619 }