minor fix for bug 9520:
[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                 public void TestAddNewProperty1 ()
65                 {
66                         string name = "name";
67                         string value = "value";
68
69                         bpg = new BuildPropertyGroup ();
70
71                         bpg.AddNewProperty (name, value);
72                 }
73
74                 [Test]
75                 [ExpectedException (typeof (InvalidOperationException))]
76                 public void TestAddNewProperty2 ()
77                 {
78                         Engine engine;
79                         Project project;
80
81                         engine = new Engine (Consts.BinPath);
82                         project = engine.CreateNewProject ();
83
84                         project.EvaluatedProperties.AddNewProperty ("a", "b");
85                 }
86
87                 [Test]
88                 public void TestAddNewProperty3 ()
89                 {
90                         Engine engine;
91                         Project project;
92                         BuildPropertyGroup [] groups = new BuildPropertyGroup [2];
93                         XmlDocument xd;
94                         XmlNode node;
95
96                         string documentString = @"
97                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
98                                         <PropertyGroup>
99                                         </PropertyGroup>
100                                         <PropertyGroup>
101                                                 <B>$(A)</B>
102                                         </PropertyGroup>
103                                 </Project>
104                         ";
105
106                         engine = new Engine (Consts.BinPath);
107                         project = engine.CreateNewProject ();
108                         project.LoadXml (documentString);
109
110                         project.PropertyGroups.CopyTo (groups, 0);
111                         Assert.AreEqual ("", project.EvaluatedProperties ["B"].FinalValue, "A0");
112                         groups [0].AddNewProperty ("A", "A");
113
114                         Assert.AreEqual (1, groups [0].Count, "A1");
115                         Assert.AreEqual ("A", project.EvaluatedProperties ["A"].FinalValue, "A2");
116                         Assert.AreEqual ("A", project.EvaluatedProperties ["B"].FinalValue, "A3");
117
118                         xd = new XmlDocument ();
119                         xd.LoadXml (project.Xml);
120                         node = xd.SelectSingleNode ("/tns:Project/tns:PropertyGroup/tns:A", TestNamespaceManager.NamespaceManager);
121                         Assert.IsNotNull (node, "A4");
122                 }
123
124                 // FIXME: what was that supposed to test?
125                 // Properties in persisted property groups cannot be accessed by name.
126                 [Test]
127                 [ExpectedException (typeof (InvalidOperationException))]
128                 public void TestClone1 ()
129                 {
130                         string documentString = @"
131                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
132                                         <PropertyGroup>
133                                                 <Name>Value</Name>
134                                         </PropertyGroup>
135                                 </Project>
136                         ";
137
138                         engine = new Engine (Consts.BinPath);
139
140                         project = engine.CreateNewProject ();
141                         project.LoadXml (documentString);
142
143                         IEnumerator en = project.PropertyGroups.GetEnumerator ();
144                         en.MoveNext ();
145                         bpg = (BuildPropertyGroup) en.Current;
146                         Assert.AreEqual ("Value", bpg ["Name"].Value, "A3");
147                 }
148
149                 [Test]
150                 public void TestClear1 ()
151                 {
152                         bpg = new BuildPropertyGroup ();
153                         
154                         bpg.SetProperty ("a", "b");
155                         Assert.AreEqual (1, bpg.Count, "A1");
156                         bpg.Clear ();
157                         Assert.AreEqual (0, bpg.Count, "A2");
158                 }
159
160                 [Test]
161                 public void TestClear2 ()
162                 {
163                         string documentString = @"
164                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
165                                         <PropertyGroup>
166                                                 <Name>Value</Name>
167                                         </PropertyGroup>
168                                 </Project>
169                         ";
170
171                         engine = new Engine (Consts.BinPath);
172                         project = engine.CreateNewProject ();
173                         project.LoadXml (documentString);
174                         BuildPropertyGroup [] array = new BuildPropertyGroup [1];
175                         project.PropertyGroups.CopyTo (array, 0);
176
177                         array [0].Clear ();
178
179                         Assert.AreEqual (0, array [0].Count, "A1");
180                 }
181
182                 // Cannot set a condition on an object not represented by an XML element in the project file.
183                 [Test]
184                 [ExpectedException (typeof (InvalidOperationException))]
185                 public void TestCondition1 ()
186                 {
187                         string condition = "condition";
188                 
189                         bpg = new BuildPropertyGroup ();
190                 
191                         bpg.Condition = condition;
192                 }
193
194                 [Test]
195                 public void TestCondition2 ()
196                 {
197                         bpg = new BuildPropertyGroup ();
198                 
199                         Assert.AreEqual (String.Empty, bpg.Condition, "A1");
200                 }
201
202                 [Test]
203                 public void TestCondition3 ()
204                 {
205                         string documentString = @"
206                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
207                                         <PropertyGroup>
208                                         </PropertyGroup>
209                                 </Project>
210                         ";
211
212                         engine = new Engine (Consts.BinPath);
213                         project = engine.CreateNewProject ();
214                         project.LoadXml (documentString);
215                         BuildPropertyGroup [] array = new BuildPropertyGroup [1];
216                         project.PropertyGroups.CopyTo (array, 0);
217
218                         array [0].Condition = "true";
219                         Assert.AreEqual ("true", array [0].Condition, "A1");
220                 }
221
222                 [Test]
223                 public void TestGetEnumerator1 ()
224                 {
225                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
226                         bpg.SetProperty ("a", "c");
227                         bpg.SetProperty ("b", "d");
228
229                         IEnumerator e = bpg.GetEnumerator ();
230                         e.MoveNext ();
231                         Assert.AreEqual ("a", ((BuildProperty) e.Current).Name, "A1");
232                         Assert.AreEqual ("c", ((BuildProperty) e.Current).Value, "A2");
233                         Assert.AreEqual ("c", ((BuildProperty) e.Current).FinalValue, "A3");
234                         e.MoveNext ();
235                         Assert.AreEqual ("b", ((BuildProperty) e.Current).Name, "A4");
236                         Assert.AreEqual ("d", ((BuildProperty) e.Current).Value, "A5");
237                         Assert.AreEqual ("d", ((BuildProperty) e.Current).FinalValue, "A6");
238
239                         Assert.IsFalse (e.MoveNext (), "A7");
240                 }
241
242                 [Test]
243                 public void TestGetEnumerator2 ()
244                 {
245                         string documentString = @"
246                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
247                                         <PropertyGroup>
248                                                 <P1>1</P1>
249                                                 <P2>2</P2>
250                                         </PropertyGroup>
251                                 </Project>
252                         ";
253
254                         engine = new Engine (Consts.BinPath);
255                         project = engine.CreateNewProject ();
256                         project.LoadXml (documentString);
257                         BuildPropertyGroup [] array = new BuildPropertyGroup [1];
258                         project.PropertyGroups.CopyTo (array, 0);
259
260                         IEnumerator e = array [0].GetEnumerator ();
261                         e.MoveNext ();
262                         Assert.AreEqual ("P1", ((BuildProperty) e.Current).Name, "A1");
263                         Assert.AreEqual ("1", ((BuildProperty) e.Current).Value, "A2");
264                         Assert.AreEqual ("1", ((BuildProperty) e.Current).FinalValue, "A3");
265                         e.MoveNext ();
266                         Assert.AreEqual ("P2", ((BuildProperty) e.Current).Name, "A4");
267                         Assert.AreEqual ("2", ((BuildProperty) e.Current).Value, "A5");
268                         Assert.AreEqual ("2", ((BuildProperty) e.Current).FinalValue, "A6");
269
270                         Assert.IsFalse (e.MoveNext (), "A7");
271                 }
272
273                 [Test]
274                 public void TestIndexer1 ()
275                 {
276                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
277                         bpg.SetProperty ("a", "1");
278                         bpg.SetProperty ("b", "2");
279
280                         Assert.AreEqual ("a", bpg ["a"].Name, "A1");
281                         Assert.AreEqual ("b", bpg ["b"].Name, "A2");
282                         Assert.IsNull (bpg ["something_that_doesnt_exist"], "A3");
283                         bpg ["a"].Value = "3";
284                         Assert.AreEqual ("3", bpg ["a"].Value, "A4");
285                 }
286
287                 // Properties in persisted property groups cannot be accessed by name.
288                 [Test]
289                 [ExpectedException (typeof (InvalidOperationException))]
290                 public void TestIndexer2 ()
291                 {
292                         string documentString = @"
293                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
294                                         <PropertyGroup>
295                                                 <a>1</a>
296                                                 <b>2</b>
297                                         </PropertyGroup>
298                                 </Project>
299                         ";
300
301                         engine = new Engine (Consts.BinPath);
302                         project = engine.CreateNewProject ();
303                         project.LoadXml (documentString);
304                         BuildPropertyGroup [] array = new BuildPropertyGroup [1];
305                         project.PropertyGroups.CopyTo (array, 0);
306
307                         Assert.AreEqual ("a", array [0] ["a"].Name, "A1");
308                 }
309
310                 // Properties in persisted property groups cannot be accessed by name.
311                 [Test]
312                 [ExpectedException (typeof (InvalidOperationException))]
313                 public void TestIndexer3 ()
314                 {
315                         string documentString = @"
316                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
317                                         <PropertyGroup>
318                                                 <a>1</a>
319                                                 <b>2</b>
320                                         </PropertyGroup>
321                                 </Project>
322                         ";
323
324                         engine = new Engine (Consts.BinPath);
325                         project = engine.CreateNewProject ();
326                         project.LoadXml (documentString);
327                         BuildPropertyGroup [] array = new BuildPropertyGroup [1];
328                         project.PropertyGroups.CopyTo (array, 0);
329
330                         array [0] ["a"].Value = "3";
331                 }
332
333                 [Test]
334                 [Category ("NotDotNet")]
335                 [ExpectedException (typeof (ArgumentNullException))]
336                 public void TestRemoveProperty1 ()
337                 {
338                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
339                         bpg.RemoveProperty ((BuildProperty) null);
340                 }
341
342                 [Test]
343                 [ExpectedException (typeof (ArgumentNullException))]
344                 public void TestRemoveProperty2 ()
345                 {
346                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
347                         bpg.SetProperty ("a", "b");
348                         bpg.SetProperty ("c", "d");
349
350                         bpg.RemoveProperty ((string) null);
351                 }
352
353                 [Test]
354                 public void TestRemoveProperty3 ()
355                 {
356                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
357                         bpg.SetProperty ("a", "b");
358                         bpg.SetProperty ("c", "d");
359
360                         bpg.RemoveProperty ("value_not_in_group");
361                         bpg.RemoveProperty (new BuildProperty ("name", "value"));
362
363                         BuildProperty bp = bpg ["a"];
364
365                         bpg.RemoveProperty (bp);
366                 }
367
368                 [Test]
369                 public void TestRemoveProperty4 ()
370                 {
371                         Engine engine;
372                         Project project;
373                         BuildPropertyGroup [] bpg = new BuildPropertyGroup [1];
374                         XmlDocument xd;
375                         XmlNode node;
376
377                         string documentString = @"
378                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
379                                         <PropertyGroup>
380                                                 <A>A</A>
381                                         </PropertyGroup>
382                                 </Project>
383                         ";
384
385                         engine = new Engine (Consts.BinPath);
386                         project = engine.CreateNewProject ();
387                         project.LoadXml (documentString);
388                         project.PropertyGroups.CopyTo (bpg, 0);
389
390                         bpg [0].RemoveProperty ("A");
391                         Assert.AreEqual (0, bpg [0].Count, "A1");
392                         xd = new XmlDocument ();
393                         xd.LoadXml (project.Xml);
394                         node = xd.SelectSingleNode ("/tns:Project/tns:PropertyGroup/tns:A", TestNamespaceManager.NamespaceManager);
395                         Assert.IsNull (node, "A3");
396
397                         bpg [0].RemoveProperty ("B");
398                 }
399
400                 [Test]
401                 public void TestRemoveProperty5 ()
402                 {
403                         Engine engine;
404                         Project project;
405                         BuildPropertyGroup [] bpg = new BuildPropertyGroup [1];
406                         XmlDocument xd;
407                         XmlNode node;
408                         BuildProperty [] properties;
409
410                         string documentString = @"
411                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
412                                         <PropertyGroup>
413                                                 <A>A</A>
414                                         </PropertyGroup>
415                                 </Project>
416                         ";
417
418                         engine = new Engine (Consts.BinPath);
419                         project = engine.CreateNewProject ();
420                         project.LoadXml (documentString);
421                         project.PropertyGroups.CopyTo (bpg, 0);
422
423                         properties = GetProperties (bpg [0]);
424
425                         bpg [0].RemoveProperty (properties [0]);
426                         Assert.AreEqual (0, bpg [0].Count, "A1");
427                         xd = new XmlDocument ();
428                         xd.LoadXml (project.Xml);
429                         node = xd.SelectSingleNode ("/tns:Project/tns:PropertyGroup/tns:A", TestNamespaceManager.NamespaceManager);
430                         Assert.IsNull (node, "A3");
431                 }
432
433                 // The specified property does not belong to the current property group.
434                 [Test]
435                 [ExpectedException (typeof (InvalidOperationException))]
436                 public void TestRemoveProperty6 ()
437                 {
438                         Engine engine;
439                         Project project;
440                         BuildPropertyGroup [] bpg = new BuildPropertyGroup [1];
441
442                         string documentString = @"
443                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
444                                         <PropertyGroup>
445                                         </PropertyGroup>
446                                 </Project>
447                         ";
448
449                         engine = new Engine (Consts.BinPath);
450                         project = engine.CreateNewProject ();
451                         project.LoadXml (documentString);
452                         project.PropertyGroups.CopyTo (bpg, 0);
453
454                         bpg [0].RemoveProperty (new BuildProperty ("A", "b"));
455                 }
456
457                 [Test]
458                 [Category ("NotDotNet")]
459                 [Category ("NotWorking")]
460                 [ExpectedException (typeof (InvalidOperationException))]
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                 // The name "1" contains an invalid character "1".
539                 [Test]
540                 [ExpectedException (typeof (ArgumentException))]
541                 public void TestSetProperty5 ()
542                 {
543                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
544
545                         bpg.SetProperty ("1", "$(A)");
546                 }
547
548                 [Test]
549                 public void TestSetProperty6 ()
550                 {
551                         Engine engine;
552                         Project project;
553
554                         string documentString = @"
555                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
556                                         <PropertyGroup>
557                                                 <Property Condition=""'$(P)' != ''"">a</Property>
558                                         </PropertyGroup>
559                                 </Project>
560                         ";
561
562                         engine = new Engine (Consts.BinPath);
563                         project = engine.CreateNewProject ();
564                         project.LoadXml (documentString);
565
566                         project.GlobalProperties.SetProperty ("P", "V");
567
568                         Assert.IsNotNull (project.EvaluatedProperties ["Property"], "A1");
569                 }
570
571                 [Test]
572                 public void TestSetProperty7 ()
573                 {
574                         Engine engine;
575                         Project project;
576
577                         string documentString = @"
578                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
579                                         <PropertyGroup>
580                                                 <Property Condition=""'$(P)' != ''"">a</Property>
581                                                 <A>A</A>
582                                         </PropertyGroup>
583                                 </Project>
584                         ";
585
586                         engine = new Engine (Consts.BinPath);
587                         project = engine.CreateNewProject ();
588                         project.LoadXml (documentString);
589
590                         BuildProperty p1 = project.EvaluatedProperties ["A"];
591                         p1.Value = "B";
592
593                         project.GlobalProperties.SetProperty ("P", "V");
594
595                         Assert.AreEqual ("A", project.EvaluatedProperties ["A"].Value, "A1");
596                 }
597
598                 [Test]
599                 public void TestSetProperty8 ()
600                 {
601                         Engine engine;
602                         Project project;
603
604                         string documentString = @"
605                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
606                                 </Project>
607                         ";
608
609                         engine = new Engine (Consts.BinPath);
610                         project = engine.CreateNewProject ();
611                         project.LoadXml (documentString);
612
613                         project.EvaluatedProperties.SetProperty ("A", "B");
614
615                         Assert.IsNull (project.EvaluatedProperties ["A"], "A1");
616                 }
617
618                 // This method is only valid for virtual property groups, not <PropertyGroup> elements.
619                 [Test]
620                 [ExpectedException (typeof (InvalidOperationException))]
621                 public void TestSetProperty9 ()
622                 {
623                         Engine engine;
624                         Project project;
625                         BuildPropertyGroup [] groups = new BuildPropertyGroup [1];
626
627                         string documentString = @"
628                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
629                                         <PropertyGroup />
630                                 </Project>
631                         ";
632
633                         engine = new Engine (Consts.BinPath);
634                         project = engine.CreateNewProject ();
635                         project.LoadXml (documentString);
636
637                         project.PropertyGroups.CopyTo (groups, 0);
638                         groups [0].SetProperty ("A", "B");
639                 }
640         }
641 }