merged Sys.Web.Services 2.0 support in my branch:
[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 Microsoft.Build.BuildEngine;
31 using Microsoft.Build.Framework;
32 using Microsoft.Build.Utilities;
33 using NUnit.Framework;
34
35 namespace MonoTests.Microsoft.Build.BuildEngine {
36         [TestFixture]
37         public class BuildPropertyGroupTest {
38                 
39                 BuildPropertyGroup      bpg;
40                 Engine                  engine;
41                 Project                 project;
42                 
43                 [Test]
44                 public void TestAssignment ()
45                 {
46                         bpg = new BuildPropertyGroup ();
47                         
48                         Assert.AreEqual (0, bpg.Count);
49                         Assert.AreEqual (false, bpg.IsImported);
50                 }
51
52                 [Test]
53                 [ExpectedException (typeof (InvalidOperationException),
54                         "This method is only valid for persisted <System.Object[]> elements.")]
55                 public void TestAddNewProperty1 ()
56                 {
57                         string name = "name";
58                         string value = "value";
59
60                         bpg = new BuildPropertyGroup ();
61
62                         bpg.AddNewProperty (name, value);
63                 }
64
65                 [Test]
66                 [ExpectedException (typeof (InvalidOperationException),
67                         "Properties in persisted property groups cannot be accessed by name.")]
68                 public void TestClone1 ()
69                 {
70                         string documentString = @"
71                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
72                                         <PropertyGroup>
73                                                 <Name>Value</Name>
74                                         </PropertyGroup>
75                                 </Project>
76                         ";
77
78                         engine = new Engine (Consts.BinPath);
79
80                         project = engine.CreateNewProject ();
81                         project.LoadXml (documentString);
82
83                         IEnumerator en = project.PropertyGroups.GetEnumerator ();
84                         en.MoveNext ();
85                         bpg = (BuildPropertyGroup) en.Current;
86                         Assert.AreEqual ("Value", bpg ["Name"].Value, "A3");
87                 }
88
89                 [Test]
90                 public void TestClear1 ()
91                 {
92                         bpg = new BuildPropertyGroup ();
93                         
94                         bpg.SetProperty ("a", "b");
95                         Assert.AreEqual (1, bpg.Count, "A1");
96                         bpg.Clear ();
97                         Assert.AreEqual (0, bpg.Count, "A2");
98                 }
99
100                 [Test]
101                 public void TestClear2 ()
102                 {
103                         string documentString = @"
104                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
105                                         <PropertyGroup>
106                                                 <Name>Value</Name>
107                                         </PropertyGroup>
108                                 </Project>
109                         ";
110
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);
116
117                         array [0].Clear ();
118
119                         Assert.AreEqual (0, array [0].Count, "A1");
120                 }
121
122                 [Test]
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 ()
126                 {
127                         string condition = "condition";
128                 
129                         bpg = new BuildPropertyGroup ();
130                 
131                         bpg.Condition = condition;
132                 }
133
134                 [Test]
135                 public void TestCondition2 ()
136                 {
137                         bpg = new BuildPropertyGroup ();
138                 
139                         Assert.AreEqual (String.Empty, bpg.Condition, "A1");
140                 }
141
142                 [Test]
143                 public void TestGetEnumerator1 ()
144                 {
145                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
146                         bpg.SetProperty ("a", "c");
147                         bpg.SetProperty ("b", "d");
148
149                         IEnumerator e = bpg.GetEnumerator ();
150                         e.MoveNext ();
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");
154                         e.MoveNext ();
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");
158
159                         Assert.IsFalse (e.MoveNext (), "A7");
160                 }
161
162                 [Test]
163                 public void TestGetEnumerator2 ()
164                 {
165                         string documentString = @"
166                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
167                                         <PropertyGroup>
168                                                 <P1>1</P1>
169                                                 <P2>2</P2>
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                         IEnumerator e = array [0].GetEnumerator ();
181                         e.MoveNext ();
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");
185                         e.MoveNext ();
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");
189
190                         Assert.IsFalse (e.MoveNext (), "A7");
191                 }
192
193                 [Test]
194                 public void TestIndexer1 ()
195                 {
196                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
197                         bpg.SetProperty ("a", "1");
198                         bpg.SetProperty ("b", "2");
199
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");
203                 }
204
205                 [Test]
206                 [ExpectedException (typeof (InvalidOperationException),
207                         "Properties in persisted property groups cannot be accessed by name.")]
208                 public void TestIndexer2 ()
209                 {
210                         string documentString = @"
211                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
212                                         <PropertyGroup>
213                                                 <a>1</a>
214                                                 <b>2</b>
215                                         </PropertyGroup>
216                                 </Project>
217                         ";
218
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);
224
225                         Assert.AreEqual ("a", array [0] ["a"].Name, "A1");
226                 }
227
228                 [Test]
229                 [Ignore ("NullRefException on MS .NET 2.0")]
230                 public void TestRemoveProperty1 ()
231                 {
232                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
233                         bpg.RemoveProperty ((BuildProperty) null);
234                 }
235
236                 [Test]
237                 [ExpectedException (typeof (ArgumentNullException))]
238                 public void TestRemoveProperty2 ()
239                 {
240                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
241                         bpg.SetProperty ("a", "b");
242                         bpg.SetProperty ("c", "d");
243
244                         bpg.RemoveProperty ((string) null);
245                 }
246
247                 [Test]
248                 public void TestRemoveProperty3 ()
249                 {
250                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
251                         bpg.SetProperty ("a", "b");
252                         bpg.SetProperty ("c", "d");
253
254                         bpg.RemoveProperty ("value_not_in_group");
255                 }
256
257                 [Test]
258                 public void TestRemoveProperty4 ()
259                 {
260                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
261                         bpg.SetProperty ("a", "b");
262                         bpg.SetProperty ("c", "d");
263
264                         bpg.RemoveProperty (new BuildProperty ("name", "value"));
265                 }
266
267                 [Test]
268                 [ExpectedException (typeof (ArgumentNullException))]
269                 public void TestSetProperty1 ()
270                 {
271                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
272                         bpg.SetProperty (null, null);
273                 }
274
275                 [Test]
276                 [ExpectedException (typeof (ArgumentNullException))]
277                 public void TestSetProperty2 ()
278                 {
279                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
280                         bpg.SetProperty ("name", null);
281                 }
282
283                 [Test]
284                 public void TestSetProperty3 ()
285                 {
286                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
287                         
288                         bpg.SetProperty ("name", "$(A)");
289
290                         BuildProperty bp = bpg ["name"];
291
292                         Assert.AreEqual ("name", bp.Name, "A1");
293                         Assert.AreEqual ("$(A)", bp.Value, "A2");
294                         Assert.AreEqual ("$(A)", bp.FinalValue, "A3");
295                 }
296
297                 [Test]
298                 [Category ("NotWorking")]
299                 public void TestSetProperty4 ()
300                 {
301                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
302
303                         bpg.SetProperty ("P1", "$(A)", true);
304                         bpg.SetProperty ("P2", "$(A)", false);
305
306                         BuildProperty b1 = bpg ["P1"];
307                         BuildProperty b2 = bpg ["P2"];
308
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");
315                 }
316
317                 [Test]
318                 [ExpectedException (typeof (ArgumentException),
319                         "The name \"1\" contains an invalid character \"1\".")]
320                 [Category ("NotWorking")]
321                 public void TestSetProperty5 ()
322                 {
323                         BuildPropertyGroup bpg = new BuildPropertyGroup ();
324
325                         bpg.SetProperty ("1", "$(A)");
326                 }
327
328                 [Test]
329                 public void TestSetProperty6 ()
330                 {
331                         Engine engine;
332                         Project project;
333
334                         string documentString = @"
335                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
336                                         <PropertyGroup>
337                                                 <Property Condition=""'$(P)' != ''"">a</Property>
338                                         </PropertyGroup>
339                                 </Project>
340                         ";
341
342                         engine = new Engine (Consts.BinPath);
343                         project = engine.CreateNewProject ();
344                         project.LoadXml (documentString);
345
346                         project.GlobalProperties.SetProperty ("P", "V");
347
348                         Assert.IsNotNull (project.EvaluatedProperties ["Property"], "A1");
349                 }
350
351                 [Test]
352                 public void TestSetProperty7 ()
353                 {
354                         Engine engine;
355                         Project project;
356
357                         string documentString = @"
358                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
359                                         <PropertyGroup>
360                                                 <Property Condition=""'$(P)' != ''"">a</Property>
361                                                 <A>A</A>
362                                         </PropertyGroup>
363                                 </Project>
364                         ";
365
366                         engine = new Engine (Consts.BinPath);
367                         project = engine.CreateNewProject ();
368                         project.LoadXml (documentString);
369
370                         BuildProperty p1 = project.EvaluatedProperties ["A"];
371                         p1.Value = "B";
372
373                         project.GlobalProperties.SetProperty ("P", "V");
374
375                         Assert.AreEqual ("A", project.EvaluatedProperties ["A"].Value, "A1");
376                 }
377
378                 [Test]
379                 public void TestSetProperty8 ()
380                 {
381                         Engine engine;
382                         Project project;
383
384                         string documentString = @"
385                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
386                                 </Project>
387                         ";
388
389                         engine = new Engine (Consts.BinPath);
390                         project = engine.CreateNewProject ();
391                         project.LoadXml (documentString);
392
393                         project.EvaluatedProperties.SetProperty ("A", "B");
394
395                         Assert.IsNull (project.EvaluatedProperties ["A"], "A1");
396                 }
397         }
398 }