2 // BuildItemGroupTest.cs
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections;
31 using Microsoft.Build.BuildEngine;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Utilities;
34 using NUnit.Framework;
36 namespace MonoTests.Microsoft.Build.BuildEngine {
38 public class BuildItemGroupTest {
40 public void TestCtor ()
42 BuildItemGroup big = new BuildItemGroup ();
44 Assert.AreEqual (String.Empty, big.Condition, "A1");
45 Assert.AreEqual (0, big.Count, "A2");
46 Assert.IsFalse (big.IsImported, "A3");
50 public void TestAddNewItem1 ()
53 string include = "a;b;c";
55 BuildItemGroup big = new BuildItemGroup ();
56 BuildItem bi = big.AddNewItem (name, include);
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");
68 public void TestAddNewItem2 ()
73 string include = "$(Property)";
75 string documentString = @"
76 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
78 <Property>a</Property>
83 engine = new Engine (Consts.BinPath);
84 project = engine.CreateNewProject ();
85 project.LoadXml (documentString);
87 BuildItem bi = project.EvaluatedItems.AddNewItem (name, include, true);
89 Assert.AreEqual (String.Empty, bi.Condition, "A1");
90 Assert.AreEqual (String.Empty, bi.Exclude, "A2");
91 Assert.AreEqual (include, bi.FinalItemSpec, "A3");
92 Assert.AreEqual (Utilities.Escape (include), bi.Include, "A4");
93 Assert.IsFalse (bi.IsImported, "A5");
94 Assert.AreEqual (name, bi.Name, "A6");
96 bi = project.EvaluatedItems.AddNewItem (name, include, false);
98 Assert.AreEqual (String.Empty, bi.Condition, "A7");
99 Assert.AreEqual (String.Empty, bi.Exclude, "A8");
100 Assert.AreEqual (include, bi.FinalItemSpec, "A9");
101 Assert.AreEqual (include, bi.Include, "A10");
102 Assert.IsFalse (bi.IsImported, "A11");
103 Assert.AreEqual (name, bi.Name, "A12");
107 public void TestAddNewItem3 ()
111 string name = "name";
112 string include = "$(Property)";
114 string documentString = @"
115 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
117 <Property>a</Property>
122 engine = new Engine (Consts.BinPath);
123 project = engine.CreateNewProject ();
124 project.LoadXml (documentString);
126 BuildItem bi = project.EvaluatedItems.AddNewItem (name, include, true);
128 Assert.AreEqual (String.Empty, bi.Condition, "A1");
129 Assert.AreEqual (String.Empty, bi.Exclude, "A2");
130 Assert.AreEqual (include, bi.FinalItemSpec, "A3");
131 Assert.AreEqual (Utilities.Escape (include), bi.Include, "A4");
132 Assert.IsFalse (bi.IsImported, "A5");
133 Assert.AreEqual (name, bi.Name, "A6");
135 bi = project.EvaluatedItems.AddNewItem (name, include, false);
137 Assert.AreEqual (String.Empty, bi.Condition, "A7");
138 Assert.AreEqual (String.Empty, bi.Exclude, "A8");
139 Assert.AreEqual (include, bi.FinalItemSpec, "A9");
140 Assert.AreEqual (include, bi.Include, "A10");
141 Assert.IsFalse (bi.IsImported, "A11");
142 Assert.AreEqual (name, bi.Name, "A12");
144 Assert.AreEqual (0, project.EvaluatedItems.Count, "A13");
146 project.GlobalProperties.SetProperty ("a", "b");
148 Assert.AreEqual (0, project.EvaluatedItems.Count, "A14");
152 public void TestAddNewItem4 ()
157 string documentString = @"
158 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
162 engine = new Engine (Consts.BinPath);
163 project = engine.CreateNewProject ();
164 project.LoadXml (documentString);
166 project.EvaluatedItems.AddNewItem ("I1", "Value");
168 BuildItem bi = project.EvaluatedItems.AddNewItem ("I2", "@(I1)");
170 Assert.AreEqual ("@(I1)", bi.FinalItemSpec, "A1");
171 Assert.AreEqual ("@(I1)", bi.Include, "A2");
172 Assert.AreEqual (0, project.EvaluatedItems.Count, "A3");
176 public void TestAddNewItem5 ()
178 BuildItemGroup big = new BuildItemGroup ();
180 big.AddNewItem ("I1", "Value");
182 BuildItem bi = big.AddNewItem ("I2", "@(I1)");
184 Assert.AreEqual ("@(I1)", bi.FinalItemSpec, "A1");
185 Assert.AreEqual ("@(I1)", bi.Include, "A2");
186 Assert.AreEqual (2, big.Count, "A3");
190 public void TestAddNewItem6 ()
194 string name = "name";
195 string include = "include";
196 BuildItemGroup [] groups = new BuildItemGroup [1];
198 string documentString = @"
199 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
204 engine = new Engine (Consts.BinPath);
205 project = engine.CreateNewProject ();
206 project.LoadXml (documentString);
208 project.ItemGroups.CopyTo (groups, 0);
209 BuildItem bi = groups [0].AddNewItem (name, include, true);
211 Assert.AreEqual (String.Empty, bi.Condition, "A1");
212 Assert.AreEqual (String.Empty, bi.Exclude, "A2");
213 Assert.AreEqual (include, bi.FinalItemSpec, "A3");
214 Assert.AreEqual (Utilities.Escape (include), bi.Include, "A4");
215 Assert.IsFalse (bi.IsImported, "A5");
216 Assert.AreEqual (name, bi.Name, "A6");
217 //Assert.AreSame (bi, groups [0] [0], "A7");
218 Assert.IsTrue (object.ReferenceEquals (bi, groups [0][0]), "A7");
219 //Assert.AreNotSame (bi, project.EvaluatedItems [0], "A8");
220 Assert.IsFalse (object.ReferenceEquals (bi, project.EvaluatedItems [0]), "A8");
224 public void TestClear1 ()
226 BuildItemGroup big = new BuildItemGroup ();
227 big.AddNewItem ("a", "a");
228 big.AddNewItem ("b", "a");
230 Assert.AreEqual (2, big.Count, "A1");
234 Assert.AreEqual (0, big.Count, "A2");
238 public void TestClear2 ()
244 string documentString = @"
245 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
252 engine = new Engine (Consts.BinPath);
253 project = engine.CreateNewProject ();
254 project.LoadXml (documentString);
256 xd = new XmlDocument ();
257 xd.LoadXml (project.Xml);
258 node = xd.SelectSingleNode ("tns:Project/tns:ItemGroup/*", TestNamespaceManager.NamespaceManager);
259 Assert.IsNotNull (node, "A1");
261 BuildItemGroup [] big = new BuildItemGroup [1];
262 project.ItemGroups.CopyTo (big, 0);
265 Assert.AreEqual (0, big [0].Count, "A2");
266 xd = new XmlDocument ();
267 xd.LoadXml (project.Xml);
268 node = xd.SelectSingleNode ("tns:Project/tns:ItemGroup/*", TestNamespaceManager.NamespaceManager);
269 Assert.IsNull (node, "A3");
272 [Category ("NotWorking")]
273 public void TestClone1 ()
275 BuildItemGroup big = new BuildItemGroup ();
276 big.AddNewItem ("a", "a");
277 big.AddNewItem ("b", "a");
279 BuildItemGroup big2 = big.Clone (false);
280 BuildItem[] items = big2.ToArray ();
282 Assert.AreEqual (2, big2.Count, "A1");
284 Assert.AreEqual (String.Empty, items [0].Condition, "A2");
285 Assert.AreEqual (String.Empty, items [0].Exclude, "A3");
286 Assert.AreEqual ("a", items [0].FinalItemSpec, "A4");
287 Assert.AreEqual ("a", items [0].Include, "A5");
288 Assert.IsFalse (items [0].IsImported, "A6");
289 Assert.AreEqual ("a", items [0].Name, "A7");
291 Assert.AreEqual (String.Empty, items [1].Condition, "A8");
292 Assert.AreEqual (String.Empty, items [1].Exclude, "A9");
293 Assert.AreEqual ("a", items [1].FinalItemSpec, "A10");
294 Assert.AreEqual ("a", items [1].Include, "A11");
295 Assert.IsFalse (items [1].IsImported, "A12");
296 Assert.AreEqual ("b", items [1].Name, "A13");
300 [Category ("NotWorking")]
301 public void TestClone2 ()
303 BuildItemGroup big = new BuildItemGroup ();
304 big.AddNewItem ("a", "a");
305 big.AddNewItem ("b", "a");
307 BuildItemGroup big2 = big.Clone (true);
308 BuildItem[] items = big2.ToArray ();
310 Assert.AreEqual (2, big2.Count, "A1");
312 Assert.AreEqual (String.Empty, items [0].Condition, "A2");
313 Assert.AreEqual (String.Empty, items [0].Exclude, "A3");
314 Assert.AreEqual ("a", items [0].FinalItemSpec, "A4");
315 Assert.AreEqual ("a", items [0].Include, "A5");
316 Assert.IsFalse (items [0].IsImported, "A6");
317 Assert.AreEqual ("a", items [0].Name, "A7");
319 Assert.AreEqual (String.Empty, items [1].Condition, "A8");
320 Assert.AreEqual (String.Empty, items [1].Exclude, "A9");
321 Assert.AreEqual ("a", items [1].FinalItemSpec, "A10");
322 Assert.AreEqual ("a", items [1].Include, "A11");
323 Assert.IsFalse (items [1].IsImported, "A12");
324 Assert.AreEqual ("b", items [1].Name, "A13");
328 [Category ("NotWorking")]
329 public void TestClone3 ()
334 string documentString = @"
335 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
342 engine = new Engine (Consts.BinPath);
343 project = engine.CreateNewProject ();
344 project.LoadXml (documentString);
346 BuildItemGroup big2 = project.EvaluatedItems.Clone (false);
347 BuildItem[] items = big2.ToArray ();
349 Assert.AreEqual (1, big2.Count, "A1");
351 Assert.AreEqual (String.Empty, items [0].Condition, "A2");
352 Assert.AreEqual (String.Empty, items [0].Exclude, "A3");
353 Assert.AreEqual ("a", items [0].FinalItemSpec, "A4");
354 Assert.AreEqual ("a", items [0].Include, "A5");
355 Assert.IsFalse (items [0].IsImported, "A6");
356 Assert.AreEqual ("Item", items [0].Name, "A7");
360 [Category ("NotWorking")]
361 public void TestClone4 ()
366 string documentString = @"
367 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
374 engine = new Engine (Consts.BinPath);
375 project = engine.CreateNewProject ();
376 project.LoadXml (documentString);
378 BuildItemGroup big2 = project.EvaluatedItems.Clone (true);
379 BuildItem[] items = big2.ToArray ();
381 Assert.AreEqual (1, big2.Count, "A1");
383 Assert.AreEqual (String.Empty, items [0].Condition, "A2");
384 Assert.AreEqual (String.Empty, items [0].Exclude, "A3");
385 Assert.AreEqual ("a", items [0].FinalItemSpec, "A4");
386 Assert.AreEqual ("a", items [0].Include, "A5");
387 Assert.IsFalse (items [0].IsImported, "A6");
388 Assert.AreEqual ("Item", items [0].Name, "A7");
391 // A shallow clone of this object cannot be created
393 [ExpectedException (typeof (InvalidOperationException))]
394 public void TestClone5 ()
399 string documentString = @"
400 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
407 engine = new Engine (Consts.BinPath);
408 project = engine.CreateNewProject ();
409 project.LoadXml (documentString);
411 BuildItemGroup[] groups = new BuildItemGroup [1];
412 project.ItemGroups.CopyTo (groups, 0);
414 groups [0].Clone (false);
418 [Category ("NotWorking")]
419 public void TestClone6 ()
424 string documentString = @"
425 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
432 engine = new Engine (Consts.BinPath);
433 project = engine.CreateNewProject ();
434 project.LoadXml (documentString);
436 BuildItemGroup[] groups = new BuildItemGroup [1];
437 project.ItemGroups.CopyTo (groups, 0);
439 BuildItemGroup big2 = groups [0].Clone (true);
440 BuildItem[] items = big2.ToArray ();
442 Assert.AreEqual (1, big2.Count, "A1");
444 Assert.AreEqual (String.Empty, items [0].Condition, "A2");
445 Assert.AreEqual (String.Empty, items [0].Exclude, "A3");
446 Assert.AreEqual ("a", items [0].FinalItemSpec, "A4");
447 Assert.AreEqual ("a", items [0].Include, "A5");
448 Assert.IsFalse (items [0].IsImported, "A6");
449 Assert.AreEqual ("Item", items [0].Name, "A7");
453 public void TestCondition1 ()
458 string documentString = @"
459 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
460 <ItemGroup Condition='true' >
466 engine = new Engine (Consts.BinPath);
467 project = engine.CreateNewProject ();
468 project.LoadXml (documentString);
470 BuildItemGroup [] groups = new BuildItemGroup [1];
471 project.ItemGroups.CopyTo (groups, 0);
473 Assert.AreEqual ("true", groups [0].Condition, "A1");
474 Assert.IsFalse (groups [0].IsImported, "A2");
477 // Cannot set a condition on an object not represented by an XML element in the project file.
479 [ExpectedException (typeof (InvalidOperationException))]
480 public void TestCondition2 ()
482 BuildItemGroup big = new BuildItemGroup ();
484 big.Condition = "true";
488 public void TestCondition3 ()
493 string documentString = @"
494 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
495 <ItemGroup Condition='true' >
500 engine = new Engine (Consts.BinPath);
501 project = engine.CreateNewProject ();
502 project.LoadXml (documentString);
504 BuildItemGroup [] groups = new BuildItemGroup [1];
505 project.ItemGroups.CopyTo (groups, 0);
507 Assert.AreEqual ("true", groups [0].Condition, "A1");
508 groups [0].Condition = "false";
510 Assert.AreEqual ("false", groups [0].Condition, "A2");
514 public void TestGetEnumerator ()
516 BuildItemGroup big = new BuildItemGroup ();
517 big.AddNewItem ("a", "c");
518 big.AddNewItem ("b", "d");
520 IEnumerator e = big.GetEnumerator ();
522 Assert.AreEqual ("a", ((BuildItem) e.Current).Name, "A1");
523 Assert.AreEqual ("c", ((BuildItem) e.Current).FinalItemSpec, "A2");
525 Assert.AreEqual ("b", ((BuildItem) e.Current).Name, "A3");
526 Assert.AreEqual ("d", ((BuildItem) e.Current).FinalItemSpec, "A4");
528 Assert.IsFalse (e.MoveNext ());
532 public void TestRemoveItem1 ()
534 BuildItemGroup big = new BuildItemGroup ();
536 big.AddNewItem ("a", "b");
537 BuildItem b = big.AddNewItem ("b", "c");
538 big.AddNewItem ("c", "d");
542 BuildItem[] items = big.ToArray ();
543 Assert.AreEqual (2, big.Count, "A1");
544 Assert.AreEqual ("a", items [0].Name, "A2");
545 Assert.AreEqual ("c", items [1].Name, "A3");
549 // NOTE: maybe it should throw an exception?
550 // at the moment it probably doesn't find a "null" element
551 public void TestRemoveItem2 ()
553 BuildItemGroup big = new BuildItemGroup ();
555 big.RemoveItem (null);
559 public void TestRemoveItem3 ()
566 string documentString = @"
567 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
574 engine = new Engine (Consts.BinPath);
575 project = engine.CreateNewProject ();
576 project.LoadXml (documentString);
578 BuildItemGroup [] groups = new BuildItemGroup [1];
579 project.ItemGroups.CopyTo (groups, 0);
581 groups [0].RemoveItem (groups [0] [0]);
582 Assert.AreEqual (0, groups [0].Count, "A1");
583 Assert.AreEqual (1, project.ItemGroups.Count, "A2");
585 xd = new XmlDocument ();
586 xd.LoadXml (project.Xml);
587 node = xd.SelectSingleNode ("tns:Project/tns:ItemGroup/tns:A", TestNamespaceManager.NamespaceManager);
588 Assert.IsNull (node, "A3");
592 public void TestRemoveItemAt1 ()
594 BuildItemGroup big = new BuildItemGroup ();
596 big.AddNewItem ("a", "b");
597 big.AddNewItem ("b", "c");
598 big.AddNewItem ("c", "d");
600 big.RemoveItemAt (1);
602 BuildItem[] items = big.ToArray ();
603 Assert.AreEqual (2, big.Count, "A1");
604 Assert.AreEqual ("a", items [0].Name, "A2");
605 Assert.AreEqual ("c", items [1].Name, "A3");
609 [ExpectedException (typeof (ArgumentOutOfRangeException))]
610 public void TestRemoveItemAt2 ()
612 BuildItemGroup big = new BuildItemGroup ();
614 big.AddNewItem ("a", "b");
615 big.AddNewItem ("b", "c");
616 big.AddNewItem ("c", "d");
618 big.RemoveItemAt (-1);
622 [ExpectedException (typeof (ArgumentOutOfRangeException))]
623 public void TestRemoveItemAt3 ()
625 BuildItemGroup big = new BuildItemGroup ();
627 big.AddNewItem ("a", "b");
628 big.AddNewItem ("b", "c");
629 big.AddNewItem ("c", "d");
631 big.RemoveItemAt (3);
635 public void TestRemoveItemAt4 ()
642 string documentString = @"
643 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
652 engine = new Engine (Consts.BinPath);
653 project = engine.CreateNewProject ();
654 project.LoadXml (documentString);
656 BuildItemGroup [] groups = new BuildItemGroup [1];
657 project.ItemGroups.CopyTo (groups, 0);
659 groups [0].RemoveItemAt (1);
660 Assert.AreEqual (2, groups [0].Count, "A1");
661 Assert.AreEqual (1, project.ItemGroups.Count, "A2");
663 xd = new XmlDocument ();
664 xd.LoadXml (project.Xml);
665 node = xd.SelectSingleNode ("tns:Project/tns:ItemGroup/tns:B", TestNamespaceManager.NamespaceManager);
666 Assert.IsNull (node, "A3");
667 Assert.IsTrue (project.IsDirty, "A4");
671 public void TestToArray1 ()
673 BuildItemGroup big = new BuildItemGroup ();
675 BuildItem[] items = big.ToArray ();
677 Assert.AreEqual (0, items.Length, "A1");
679 big.AddNewItem ("a", "b");
680 big.AddNewItem ("c", "d");
682 items = big.ToArray ();
684 Assert.AreEqual ("a", items [0].Name, "A2");
685 Assert.AreEqual ("c", items [1].Name, "A3");
689 public void TestIndexer1 ()
691 BuildItemGroup big = new BuildItemGroup ();
692 big.AddNewItem ("a", "b");
693 big.AddNewItem ("c", "d");
695 Assert.AreEqual ("a", big [0].Name, "A1");
696 Assert.AreEqual ("c", big [1].Name, "A2");
700 [ExpectedException (typeof (ArgumentOutOfRangeException))]
701 public void TestIndexer2 ()
703 BuildItemGroup big = new BuildItemGroup ();
704 Assert.IsNotNull (big [0], "A1");
708 [ExpectedException (typeof (ArgumentOutOfRangeException))]
709 public void TestIndexer3 ()
711 BuildItemGroup big = new BuildItemGroup ();
712 Assert.IsNotNull (big [-1], "A1");