2006-12-07 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / Microsoft.Build.BuildEngine / BuildItemGroupTest.cs
1 //
2 // BuildItemGroupTest.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2005 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 BuildItemGroupTest {
38
39                 [Test]
40                 public void TestCtor ()
41                 {
42                         BuildItemGroup big = new BuildItemGroup ();
43
44                         Assert.AreEqual (String.Empty, big.Condition, "A1");
45                         Assert.AreEqual (0, big.Count, "A2");
46                         Assert.IsFalse (big.IsImported, "A3");
47                 }
48
49                 [Test]
50                 public void TestAddNewItem1 ()
51                 {
52                         string name = "name";
53                         string include = "a;b;c";
54                         
55                         BuildItemGroup big = new BuildItemGroup ();
56                         BuildItem bi = big.AddNewItem (name, include);
57
58                         Assert.AreEqual (String.Empty, bi.Condition, "A1");
59                         Assert.AreEqual (String.Empty, bi.Exclude, "A2");
60                         Assert.AreEqual (include, bi.FinalItemSpec, "A3");
61                         Assert.AreEqual (include, bi.Include, "A4");
62                         Assert.IsFalse (bi.IsImported, "A5");
63                         Assert.AreEqual (name, bi.Name, "A6");
64                         Assert.AreEqual (1, big.Count, "A7");
65                 }
66
67                 [Test]
68                 [Category ("NotWorking")]
69                 public void TestAddNewItem2 ()
70                 {
71                         Engine engine;
72                         Project project;
73                         string name = "name";
74                         string include = "$(Property)";
75                         
76                         string documentString = @"
77                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
78                                         <PropertyGroup>
79                                                 <Property>a</Property>
80                                         </PropertyGroup>
81                                 </Project>
82                         ";
83                         
84                         engine = new Engine (Consts.BinPath);
85                         project = engine.CreateNewProject ();
86                         project.LoadXml (documentString);
87
88                         BuildItem bi = project.EvaluatedItems.AddNewItem (name, include, true);
89
90                         Assert.AreEqual (String.Empty, bi.Condition, "A1");
91                         Assert.AreEqual (String.Empty, bi.Exclude, "A2");
92                         Assert.AreEqual (include, bi.FinalItemSpec, "A3");
93                         Assert.AreEqual (Utilities.Escape (include), bi.Include, "A4");
94                         Assert.IsFalse (bi.IsImported, "A5");
95                         Assert.AreEqual (name, bi.Name, "A6");
96
97                         bi = project.EvaluatedItems.AddNewItem (name, include, false);
98
99                         Assert.AreEqual (String.Empty, bi.Condition, "A7");
100                         Assert.AreEqual (String.Empty, bi.Exclude, "A8");
101                         Assert.AreEqual (include, bi.FinalItemSpec, "A9");
102                         Assert.AreEqual (include, bi.Include, "A10");
103                         Assert.IsFalse (bi.IsImported, "A11");
104                         Assert.AreEqual (name, bi.Name, "A12");
105                 }
106
107                 [Test]
108                 public void TestClear ()
109                 {
110                         BuildItemGroup big = new BuildItemGroup ();
111                         big.AddNewItem ("a", "a");
112                         big.AddNewItem ("b", "a");
113
114                         Assert.AreEqual (2, big.Count, "A1");
115                         
116                         big.Clear ();
117
118                         Assert.AreEqual (0, big.Count, "A2");
119                 }
120
121                 [Test]
122                 [Category ("NotWorking")]
123                 public void TestClone1 ()
124                 {
125                         BuildItemGroup big = new BuildItemGroup ();
126                         big.AddNewItem ("a", "a");
127                         big.AddNewItem ("b", "a");
128
129                         BuildItemGroup big2 = big.Clone (false);
130                         BuildItem[] items = big2.ToArray ();
131
132                         Assert.AreEqual (2, big2.Count, "A1");
133
134                         Assert.AreEqual (String.Empty, items [0].Condition, "A2");
135                         Assert.AreEqual (String.Empty, items [0].Exclude, "A3");
136                         Assert.AreEqual ("a", items [0].FinalItemSpec, "A4");
137                         Assert.AreEqual ("a", items [0].Include, "A5");
138                         Assert.IsFalse (items [0].IsImported, "A6");
139                         Assert.AreEqual ("a", items [0].Name, "A7");
140                         
141                         Assert.AreEqual (String.Empty, items [1].Condition, "A8");
142                         Assert.AreEqual (String.Empty, items [1].Exclude, "A9");
143                         Assert.AreEqual ("a", items [1].FinalItemSpec, "A10");
144                         Assert.AreEqual ("a", items [1].Include, "A11");
145                         Assert.IsFalse (items [1].IsImported, "A12");
146                         Assert.AreEqual ("b", items [1].Name, "A13");
147                 }
148
149                 [Test]
150                 [Category ("NotWorking")]
151                 public void TestClone2 ()
152                 {
153                         BuildItemGroup big = new BuildItemGroup ();
154                         big.AddNewItem ("a", "a");
155                         big.AddNewItem ("b", "a");
156
157                         BuildItemGroup big2 = big.Clone (true);
158                         BuildItem[] items = big2.ToArray ();
159
160                         Assert.AreEqual (2, big2.Count, "A1");
161
162                         Assert.AreEqual (String.Empty, items [0].Condition, "A2");
163                         Assert.AreEqual (String.Empty, items [0].Exclude, "A3");
164                         Assert.AreEqual ("a", items [0].FinalItemSpec, "A4");
165                         Assert.AreEqual ("a", items [0].Include, "A5");
166                         Assert.IsFalse (items [0].IsImported, "A6");
167                         Assert.AreEqual ("a", items [0].Name, "A7");
168                         
169                         Assert.AreEqual (String.Empty, items [1].Condition, "A8");
170                         Assert.AreEqual (String.Empty, items [1].Exclude, "A9");
171                         Assert.AreEqual ("a", items [1].FinalItemSpec, "A10");
172                         Assert.AreEqual ("a", items [1].Include, "A11");
173                         Assert.IsFalse (items [1].IsImported, "A12");
174                         Assert.AreEqual ("b", items [1].Name, "A13");
175                 }
176
177                 [Test]
178                 [Category ("NotWorking")]
179                 public void TestClone3 ()
180                 {
181                         Engine engine;
182                         Project project;
183                         
184                         string documentString = @"
185                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
186                                         <ItemGroup>
187                                                 <Item Include='a' />
188                                         </ItemGroup>
189                                 </Project>
190                         ";
191
192                         engine = new Engine (Consts.BinPath);
193                         project = engine.CreateNewProject ();
194                         project.LoadXml (documentString);
195
196                         BuildItemGroup big2 = project.EvaluatedItems.Clone (false);
197                         BuildItem[] items = big2.ToArray ();
198
199                         Assert.AreEqual (1, big2.Count, "A1");
200
201                         Assert.AreEqual (String.Empty, items [0].Condition, "A2");
202                         Assert.AreEqual (String.Empty, items [0].Exclude, "A3");
203                         Assert.AreEqual ("a", items [0].FinalItemSpec, "A4");
204                         Assert.AreEqual ("a", items [0].Include, "A5");
205                         Assert.IsFalse (items [0].IsImported, "A6");
206                         Assert.AreEqual ("Item", items [0].Name, "A7");
207                 }
208
209                 [Test]
210                 [Category ("NotWorking")]
211                 public void TestClone4 ()
212                 {
213                         Engine engine;
214                         Project project;
215                         
216                         string documentString = @"
217                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
218                                         <ItemGroup>
219                                                 <Item Include='a' />
220                                         </ItemGroup>
221                                 </Project>
222                         ";
223
224                         engine = new Engine (Consts.BinPath);
225                         project = engine.CreateNewProject ();
226                         project.LoadXml (documentString);
227
228                         BuildItemGroup big2 = project.EvaluatedItems.Clone (true);
229                         BuildItem[] items = big2.ToArray ();
230
231                         Assert.AreEqual (1, big2.Count, "A1");
232
233                         Assert.AreEqual (String.Empty, items [0].Condition, "A2");
234                         Assert.AreEqual (String.Empty, items [0].Exclude, "A3");
235                         Assert.AreEqual ("a", items [0].FinalItemSpec, "A4");
236                         Assert.AreEqual ("a", items [0].Include, "A5");
237                         Assert.IsFalse (items [0].IsImported, "A6");
238                         Assert.AreEqual ("Item", items [0].Name, "A7");
239                 }
240
241                 [Test]
242                 [ExpectedException (typeof (InvalidOperationException),
243                         "A shallow clone of this object cannot be created.")]
244                 [Category ("NotWorking")]
245                 public void TestClone5 ()
246                 {
247                         Engine engine;
248                         Project project;
249                         
250                         string documentString = @"
251                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
252                                         <ItemGroup>
253                                                 <Item Include='a' />
254                                         </ItemGroup>
255                                 </Project>
256                         ";
257
258                         engine = new Engine (Consts.BinPath);
259                         project = engine.CreateNewProject ();
260                         project.LoadXml (documentString);
261
262                         BuildItemGroup[] groups = new BuildItemGroup [1];
263                         project.ItemGroups.CopyTo (groups, 0);
264                         
265                         groups [0].Clone (false);
266                 }
267
268                 [Test]
269                 [Category ("NotWorking")]
270                 public void TestClone6 ()
271                 {
272                         Engine engine;
273                         Project project;
274                         
275                         string documentString = @"
276                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
277                                         <ItemGroup>
278                                                 <Item Include='a' />
279                                         </ItemGroup>
280                                 </Project>
281                         ";
282
283                         engine = new Engine (Consts.BinPath);
284                         project = engine.CreateNewProject ();
285                         project.LoadXml (documentString);
286
287                         BuildItemGroup[] groups = new BuildItemGroup [1];
288                         project.ItemGroups.CopyTo (groups, 0);
289                         
290                         BuildItemGroup big2 = groups [0].Clone (true);
291                         BuildItem[] items = big2.ToArray ();
292
293                         Assert.AreEqual (1, big2.Count, "A1");
294
295                         Assert.AreEqual (String.Empty, items [0].Condition, "A2");
296                         Assert.AreEqual (String.Empty, items [0].Exclude, "A3");
297                         Assert.AreEqual ("a", items [0].FinalItemSpec, "A4");
298                         Assert.AreEqual ("a", items [0].Include, "A5");
299                         Assert.IsFalse (items [0].IsImported, "A6");
300                         Assert.AreEqual ("Item", items [0].Name, "A7");
301                 }
302
303                 [Test]
304                 public void TestGetEnumerator ()
305                 {
306                         BuildItemGroup big = new BuildItemGroup ();
307                         big.AddNewItem ("a", "c");
308                         big.AddNewItem ("b", "d");
309
310                         IEnumerator e = big.GetEnumerator ();
311                         e.MoveNext ();
312                         Assert.AreEqual ("a", ((BuildItem) e.Current).Name, "A1");
313                         Assert.AreEqual ("c", ((BuildItem) e.Current).FinalItemSpec, "A2");
314                         e.MoveNext ();
315                         Assert.AreEqual ("b", ((BuildItem) e.Current).Name, "A3");
316                         Assert.AreEqual ("d", ((BuildItem) e.Current).FinalItemSpec, "A4");
317                         
318                         Assert.IsFalse (e.MoveNext ());
319                 }
320
321                 [Test]
322                 public void TestRemoveItem1 ()
323                 {
324                         BuildItemGroup big = new BuildItemGroup ();
325
326                         big.AddNewItem ("a", "b");
327                         BuildItem b = big.AddNewItem ("b", "c");
328                         big.AddNewItem ("c", "d");
329
330                         big.RemoveItem (b);
331
332                         BuildItem[] items = big.ToArray ();
333                         Assert.AreEqual (2, big.Count, "A1");
334                         Assert.AreEqual ("a", items [0].Name, "A2");
335                         Assert.AreEqual ("c", items [1].Name, "A3");
336                 }
337
338                 [Test]
339                 // NOTE: maybe it should throw an exception?
340                 // at the moment it probably doesn't find a "null" element
341                 public void TestRemoveItem2 ()
342                 {
343                         BuildItemGroup big = new BuildItemGroup ();
344
345                         big.RemoveItem (null);
346                 }
347
348                 [Test]
349                 public void TestRemoveItemAt1 ()
350                 {
351                         BuildItemGroup big = new BuildItemGroup ();
352
353                         big.AddNewItem ("a", "b");
354                         big.AddNewItem ("b", "c");
355                         big.AddNewItem ("c", "d");
356
357                         big.RemoveItemAt (1);
358
359                         BuildItem[] items = big.ToArray ();
360                         Assert.AreEqual (2, big.Count, "A1");
361                         Assert.AreEqual ("a", items [0].Name, "A2");
362                         Assert.AreEqual ("c", items [1].Name, "A3");
363                 }
364
365                 [Test]
366                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
367                 public void TestRemoveItemAt2 ()
368                 {
369                         BuildItemGroup big = new BuildItemGroup ();
370
371                         big.AddNewItem ("a", "b");
372                         big.AddNewItem ("b", "c");
373                         big.AddNewItem ("c", "d");
374
375                         big.RemoveItemAt (-1);
376                 }
377
378                 [Test]
379                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
380                 public void TestRemoveItemAt3 ()
381                 {
382                         BuildItemGroup big = new BuildItemGroup ();
383
384                         big.AddNewItem ("a", "b");
385                         big.AddNewItem ("b", "c");
386                         big.AddNewItem ("c", "d");
387
388                         big.RemoveItemAt (3);
389                 }
390
391                 [Test]
392                 public void TestToArray1 ()
393                 {
394                         BuildItemGroup big = new BuildItemGroup ();
395
396                         BuildItem[] items = big.ToArray ();
397
398                         Assert.AreEqual (0, items.Length, "A1");
399
400                         big.AddNewItem ("a", "b");
401                         big.AddNewItem ("c", "d");
402
403                         items = big.ToArray ();
404
405                         Assert.AreEqual ("a", items [0].Name, "A2");
406                         Assert.AreEqual ("c", items [1].Name, "A3");
407                 }
408
409                 [Test]
410                 public void TestIndexer1 ()
411                 {
412                         BuildItemGroup big = new BuildItemGroup ();
413                         big.AddNewItem ("a", "b");
414                         big.AddNewItem ("c", "d");
415
416                         Assert.AreEqual ("a", big [0].Name, "A1");
417                         Assert.AreEqual ("c", big [1].Name, "A2");
418                 }
419
420                 [Test]
421                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
422                 public void TestIndexer2 ()
423                 {
424                         BuildItemGroup big = new BuildItemGroup ();
425                         Assert.IsNotNull (big [0], "A1");
426                 }
427
428                 [Test]
429                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
430                 public void TestIndexer3 ()
431                 {
432                         BuildItemGroup big = new BuildItemGroup ();
433                         Assert.IsNotNull (big [-1], "A1");
434                 }
435
436                 [Test]
437                 public void TestCondition ()
438                 {
439                         Engine engine;
440                         Project project;
441                         
442                         string documentString = @"
443                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
444                                         <ItemGroup Condition='true' >
445                                                 <Item Include='a' />
446                                         </ItemGroup>
447                                 </Project>
448                         ";
449
450                         engine = new Engine (Consts.BinPath);
451                         project = engine.CreateNewProject ();
452                         project.LoadXml (documentString);
453
454                         BuildItemGroup[] groups = new BuildItemGroup [1];
455                         project.ItemGroups.CopyTo (groups, 0);
456                         
457                         Assert.AreEqual ("true", groups [0].Condition, "A1");
458                         Assert.IsFalse (groups [0].IsImported, "A2");
459                 }
460         }
461 }