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;
30 using Microsoft.Build.BuildEngine;
31 using Microsoft.Build.Framework;
32 using Microsoft.Build.Utilities;
33 using NUnit.Framework;
35 namespace MonoTests.Microsoft.Build.BuildEngine {
37 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 [Category ("NotWorking")]
69 public void TestAddNewItem2 ()
74 string include = "$(Property)";
76 string documentString = @"
77 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
79 <Property>a</Property>
84 engine = new Engine (Consts.BinPath);
85 project = engine.CreateNewProject ();
86 project.LoadXml (documentString);
88 BuildItem bi = project.EvaluatedItems.AddNewItem (name, include, true);
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");
97 bi = project.EvaluatedItems.AddNewItem (name, include, false);
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");
108 public void TestClear ()
110 BuildItemGroup big = new BuildItemGroup ();
111 big.AddNewItem ("a", "a");
112 big.AddNewItem ("b", "a");
114 Assert.AreEqual (2, big.Count, "A1");
118 Assert.AreEqual (0, big.Count, "A2");
122 [Category ("NotWorking")]
123 public void TestClone1 ()
125 BuildItemGroup big = new BuildItemGroup ();
126 big.AddNewItem ("a", "a");
127 big.AddNewItem ("b", "a");
129 BuildItemGroup big2 = big.Clone (false);
130 BuildItem[] items = big2.ToArray ();
132 Assert.AreEqual (2, big2.Count, "A1");
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");
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");
150 [Category ("NotWorking")]
151 public void TestClone2 ()
153 BuildItemGroup big = new BuildItemGroup ();
154 big.AddNewItem ("a", "a");
155 big.AddNewItem ("b", "a");
157 BuildItemGroup big2 = big.Clone (true);
158 BuildItem[] items = big2.ToArray ();
160 Assert.AreEqual (2, big2.Count, "A1");
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");
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");
178 [Category ("NotWorking")]
179 public void TestClone3 ()
184 string documentString = @"
185 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
192 engine = new Engine (Consts.BinPath);
193 project = engine.CreateNewProject ();
194 project.LoadXml (documentString);
196 BuildItemGroup big2 = project.EvaluatedItems.Clone (false);
197 BuildItem[] items = big2.ToArray ();
199 Assert.AreEqual (1, big2.Count, "A1");
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");
210 [Category ("NotWorking")]
211 public void TestClone4 ()
216 string documentString = @"
217 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
224 engine = new Engine (Consts.BinPath);
225 project = engine.CreateNewProject ();
226 project.LoadXml (documentString);
228 BuildItemGroup big2 = project.EvaluatedItems.Clone (true);
229 BuildItem[] items = big2.ToArray ();
231 Assert.AreEqual (1, big2.Count, "A1");
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");
242 [ExpectedException (typeof (InvalidOperationException),
243 "A shallow clone of this object cannot be created.")]
244 [Category ("NotWorking")]
245 public void TestClone5 ()
250 string documentString = @"
251 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
258 engine = new Engine (Consts.BinPath);
259 project = engine.CreateNewProject ();
260 project.LoadXml (documentString);
262 BuildItemGroup[] groups = new BuildItemGroup [1];
263 project.ItemGroups.CopyTo (groups, 0);
265 groups [0].Clone (false);
269 [Category ("NotWorking")]
270 public void TestClone6 ()
275 string documentString = @"
276 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
283 engine = new Engine (Consts.BinPath);
284 project = engine.CreateNewProject ();
285 project.LoadXml (documentString);
287 BuildItemGroup[] groups = new BuildItemGroup [1];
288 project.ItemGroups.CopyTo (groups, 0);
290 BuildItemGroup big2 = groups [0].Clone (true);
291 BuildItem[] items = big2.ToArray ();
293 Assert.AreEqual (1, big2.Count, "A1");
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");
304 public void TestGetEnumerator ()
306 BuildItemGroup big = new BuildItemGroup ();
307 big.AddNewItem ("a", "c");
308 big.AddNewItem ("b", "d");
310 IEnumerator e = big.GetEnumerator ();
312 Assert.AreEqual ("a", ((BuildItem) e.Current).Name, "A1");
313 Assert.AreEqual ("c", ((BuildItem) e.Current).FinalItemSpec, "A2");
315 Assert.AreEqual ("b", ((BuildItem) e.Current).Name, "A3");
316 Assert.AreEqual ("d", ((BuildItem) e.Current).FinalItemSpec, "A4");
318 Assert.IsFalse (e.MoveNext ());
322 public void TestRemoveItem1 ()
324 BuildItemGroup big = new BuildItemGroup ();
326 big.AddNewItem ("a", "b");
327 BuildItem b = big.AddNewItem ("b", "c");
328 big.AddNewItem ("c", "d");
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");
339 // NOTE: maybe it should throw an exception?
340 // at the moment it probably doesn't find a "null" element
341 public void TestRemoveItem2 ()
343 BuildItemGroup big = new BuildItemGroup ();
345 big.RemoveItem (null);
349 public void TestRemoveItemAt1 ()
351 BuildItemGroup big = new BuildItemGroup ();
353 big.AddNewItem ("a", "b");
354 big.AddNewItem ("b", "c");
355 big.AddNewItem ("c", "d");
357 big.RemoveItemAt (1);
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");
366 [ExpectedException (typeof (ArgumentOutOfRangeException))]
367 public void TestRemoveItemAt2 ()
369 BuildItemGroup big = new BuildItemGroup ();
371 big.AddNewItem ("a", "b");
372 big.AddNewItem ("b", "c");
373 big.AddNewItem ("c", "d");
375 big.RemoveItemAt (-1);
379 [ExpectedException (typeof (ArgumentOutOfRangeException))]
380 public void TestRemoveItemAt3 ()
382 BuildItemGroup big = new BuildItemGroup ();
384 big.AddNewItem ("a", "b");
385 big.AddNewItem ("b", "c");
386 big.AddNewItem ("c", "d");
388 big.RemoveItemAt (3);
392 public void TestToArray1 ()
394 BuildItemGroup big = new BuildItemGroup ();
396 BuildItem[] items = big.ToArray ();
398 Assert.AreEqual (0, items.Length, "A1");
400 big.AddNewItem ("a", "b");
401 big.AddNewItem ("c", "d");
403 items = big.ToArray ();
405 Assert.AreEqual ("a", items [0].Name, "A2");
406 Assert.AreEqual ("c", items [1].Name, "A3");
410 public void TestIndexer1 ()
412 BuildItemGroup big = new BuildItemGroup ();
413 big.AddNewItem ("a", "b");
414 big.AddNewItem ("c", "d");
416 Assert.AreEqual ("a", big [0].Name, "A1");
417 Assert.AreEqual ("c", big [1].Name, "A2");
421 [ExpectedException (typeof (ArgumentOutOfRangeException))]
422 public void TestIndexer2 ()
424 BuildItemGroup big = new BuildItemGroup ();
425 Assert.IsNotNull (big [0], "A1");
429 [ExpectedException (typeof (ArgumentOutOfRangeException))]
430 public void TestIndexer3 ()
432 BuildItemGroup big = new BuildItemGroup ();
433 Assert.IsNotNull (big [-1], "A1");
437 public void TestCondition ()
442 string documentString = @"
443 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
444 <ItemGroup Condition='true' >
450 engine = new Engine (Consts.BinPath);
451 project = engine.CreateNewProject ();
452 project.LoadXml (documentString);
454 BuildItemGroup[] groups = new BuildItemGroup [1];
455 project.ItemGroups.CopyTo (groups, 0);
457 Assert.AreEqual ("true", groups [0].Condition, "A1");
458 Assert.IsFalse (groups [0].IsImported, "A2");