2008-11-11 Jonathan Chambers <joncham@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / various / Items.cs
1 //
2 // Items.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.IO;
30 using System.Xml;
31 using Microsoft.Build.BuildEngine;
32 using NUnit.Framework;
33
34 namespace MonoTests.Microsoft.Build.BuildEngine.Various {
35         [TestFixture]
36         public class Items {
37                 private string GetItems (Project proj, string name)
38                 {
39                         BuildItemGroup big = proj.GetEvaluatedItemsByName (name);
40                         string str = String.Empty;
41                         if (big == null)
42                                 return str;
43                         
44                         foreach (BuildItem bi in big) {
45                                 if (str == String.Empty)
46                                         str = bi.FinalItemSpec;
47                                 else 
48                                         str += ";" + bi.FinalItemSpec;
49                         }
50                         
51                         return str;
52                 }
53
54                 [Test]
55                 public void TestItems1 ()
56                 {
57                         Engine engine = new Engine (Consts.BinPath);
58                         Project proj = engine.CreateNewProject ();
59
60                         string documentString = @"
61                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
62                                         <ItemGroup>
63                                                 <Item0 Include='A' />
64                                                 <Item1 Include='A;B;C' />
65                                                 <Item2 Include='@(Item1);A;D' />
66                                                 <Item3 Include='@(Item2)' Exclude='A' />
67                                                 <Item4 Include='@(Item1);Q' Exclude='@(Item2)' />
68                                                 <Item5 Include='@(Item1)' Exclude='@(Item2)' />
69                                                 <Item6 Include='@(Item2)' Exclude='@(Item1)' />
70                                                 <Item7 Include='@(item_that_doesnt_exist)' />
71                                         </ItemGroup>
72                                 </Project>
73                         ";
74
75                         proj.LoadXml (documentString);
76                         Assert.AreEqual ("A", GetItems (proj, "Item0"), "A1");
77                         Assert.AreEqual ("A;B;C", GetItems (proj, "Item1"), "A2");
78                         Assert.AreEqual ("A;B;C;A;D", GetItems (proj, "Item2"), "A3");
79                         Assert.AreEqual ("B;C;D", GetItems (proj, "Item3"), "A4");
80                         Assert.AreEqual ("Q", GetItems (proj, "Item4"), "A5");
81                         Assert.AreEqual (String.Empty, GetItems (proj, "Item5"), "A6");
82                         Assert.AreEqual ("D", GetItems (proj, "Item6"), "A7");
83                         Assert.AreEqual (String.Empty, GetItems (proj, "Item7"), "A8");
84                 }
85
86                 [Test]
87                 public void TestItems2 ()
88                 {
89                         Engine engine = new Engine (Consts.BinPath);
90                         Project proj = engine.CreateNewProject ();
91
92                         string documentString = @"
93                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
94                                         <ItemGroup>
95                                                 <Item1 Include='A;B;C' />
96                                                 <Item2 Include=""@(Item1,'-')"" />
97                                                 <Item3 Include=""@(Item1,'xx')"" />
98                                         </ItemGroup>
99                                 </Project>
100                         ";
101
102                         proj.LoadXml (documentString);
103
104                         Assert.AreEqual ("A-B-C", GetItems (proj, "Item2"), "A1");
105                         Assert.AreEqual ("AxxBxxC", GetItems (proj, "Item3"), "A2");
106                 }
107
108                 [Test]
109                 public void TestItems3 ()
110                 {
111                         Engine engine = new Engine (Consts.BinPath);
112                         Project proj = engine.CreateNewProject ();
113
114                         string documentString = @"
115                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
116                                         <ItemGroup>
117                                                 <Item1 Include='A;B;C' />
118                                                 <Item2 Include=""@(Item1, '-')"" />
119                                         </ItemGroup>
120                                 </Project>
121                         ";
122
123                         proj.LoadXml (documentString);
124
125                         Assert.AreEqual ("A-B-C", GetItems (proj, "Item2"), "A1");
126                 }
127         
128
129                 [Test]
130                 public void TestItems4 ()
131                 {
132                         Engine engine = new Engine (Consts.BinPath);
133                         Project proj = engine.CreateNewProject ();
134
135                         string documentString = @"
136                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
137                                         <ItemGroup>
138                                                 <Item1 Include='A;B;C' />
139                                                 <Item2 Include=""A\B.txt;A\C.txt;B\B.zip;B\C.zip"" />
140                                                 <ItemT1 Include=""@(Item1->'%(Identity)')"" />
141                                                 <ItemT2 Include=""@(Item1->'%(Identity)%(Identity)')"" />
142                                                 <ItemT3 Include=""@(Item1->'(-%(Identity)-)')"" />
143                                                 <ItemT4 Include=""@(Item2->'%(Extension)')"" />
144                                                 <ItemT5 Include=""@(Item2->'%(Filename)/%(Extension)')"" />
145                                         </ItemGroup>
146                                 </Project>
147                         ";
148
149                         proj.LoadXml (documentString);
150
151                         Assert.AreEqual ("A;B;C", GetItems (proj, "ItemT1"), "A1");
152                         Assert.AreEqual ("AA;BB;CC", GetItems (proj, "ItemT2"), "A2");
153                         Assert.AreEqual ("(-A-);(-B-);(-C-)", GetItems (proj, "ItemT3"), "A3");
154                         Assert.AreEqual (".txt;.txt;.zip;.zip", GetItems (proj, "ItemT4"), "A4");
155                         Assert.AreEqual (string.Format ("B{0}.txt;C{0}.txt;B{0}.zip;C{0}.zip", Path.DirectorySeparatorChar), GetItems (proj, "ItemT5"), "A5");
156                 }
157                 [Test]
158                 [Category ("NotWorking")]
159                 public void TestItems5 ()
160                 {
161                         Engine engine = new Engine (Consts.BinPath);
162                         Project proj = engine.CreateNewProject ();
163
164                         string documentString = @"
165                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
166                                         <ItemGroup>
167                                                 <Item Include=""A\B.txt;A\C.txt;B\B.zip;B\C.zip"" />
168                                                 <ItemT Include=""@(Item->'%(RelativeDir)X/%(Filename)')"" />
169                                         </ItemGroup>
170                                 </Project>
171                         ";
172
173                         proj.LoadXml (documentString);
174
175                         Assert.AreEqual (@"A\X/B;A\X/C;B\X/B;B\X/C", GetItems (proj, "ItemT"), "A1");
176                 }
177
178                 [Test]
179                 [Category ("NotWorking")]
180                 public void TestItems6 ()
181                 {
182                         Engine engine = new Engine (Consts.BinPath);
183                         Project proj = engine.CreateNewProject ();
184
185                         string documentString = @"
186                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
187                                         <PropertyGroup>
188                                                 <A>A</A>
189                                         </PropertyGroup>
190
191                                         <ItemGroup>
192                                                 <Item1 Include='A;B;C' />
193                                                 <Item2 Include='%(A.B)' />
194                                                 <Item3 Include='$(Z)' />
195                                                 <Item4 Include=""@(Item1, '$(A)')"" />
196                                                 <Item5 Include=""@(Item1, '%(A)')"" />
197                                                 <Item6 Include=""@(Item1, '@(A)')"" />
198                                                 <Item7 Include=""@(Item1-> '%(Filename)')"" />
199                                         </ItemGroup>
200                                 </Project>
201                         ";
202
203                         proj.LoadXml (documentString);
204
205                         Assert.AreEqual ("%(A.B)", GetItems (proj, "Item2"), "A1");
206                         Assert.AreEqual (String.Empty, GetItems (proj, "Item3"), "A2");
207                         Assert.AreEqual ("AABAC", GetItems (proj, "Item4"), "A3");
208                         Assert.AreEqual ("A%(A)B%(A)C", GetItems (proj, "Item5"), "A4");
209                         Assert.AreEqual ("A@(A)B@(A)C", GetItems (proj, "Item6"), "A6");
210                         Assert.AreEqual ("A;B;C", GetItems (proj, "Item7"), "A6");
211                 }
212
213                 [Test]
214                 [ExpectedException (typeof (InvalidProjectFileException),
215                         "The expression \"@(Item1, '@(A,'')')\" cannot be used in this context. " +
216                         "Item lists cannot be concatenated with other strings where an item list is expected. " +
217                         "Use a semicolon to separate multiple item lists.  ")]
218                 [Category ("NotWorking")]
219                 public void TestItems7 ()
220                 {
221                         Engine engine = new Engine (Consts.BinPath);
222                         Project proj = engine.CreateNewProject ();
223
224                         string documentString = @"
225                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
226                                         <ItemGroup>
227                                                 <Item1 Include='A;B;C' />
228                                                 <Item7 Include=""@(Item1, '@(A,'')')"" />
229                                         </ItemGroup>
230                                 </Project>
231                         ";
232
233                         proj.LoadXml (documentString);
234                 }
235
236                 [Test]
237                 [ExpectedException (typeof (InvalidProjectFileException),
238                         "The expression \"@(Item1, '@(A->'')')\" cannot be used in this context. " +
239                         "Item lists cannot be concatenated with other strings where an item list is expected. " +
240                         "Use a semicolon to separate multiple item lists.  ")]
241                 [Category ("NotWorking")]
242                 public void TestItems8 ()
243                 {
244                         Engine engine = new Engine (Consts.BinPath);
245                         Project proj = engine.CreateNewProject ();
246
247                         string documentString = @"
248                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
249                                         <ItemGroup>
250                                                 <Item1 Include='A;B;C' />
251                                                 <Item8 Include=""@(Item1, '@(A->'')')"" />
252                                         </ItemGroup>
253                                 </Project>
254                         ";
255
256                         proj.LoadXml (documentString);
257                 }
258
259                 [Test]
260                 [ExpectedException (typeof (InvalidProjectFileException),
261                         "The expression \"@(Item1, '@(A->'','')')\" cannot be used in this context. " +
262                         "Item lists cannot be concatenated with other strings where an item list is expected. " +
263                         "Use a semicolon to separate multiple item lists.  ")]
264                 [Category ("NotWorking")]
265                 public void TestItems9 ()
266                 {
267                         Engine engine = new Engine (Consts.BinPath);
268                         Project proj = engine.CreateNewProject ();
269
270                         string documentString = @"
271                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
272                                         <ItemGroup>
273                                                 <Item1 Include='A;B;C' />
274                                                 <Item9 Include=""@(Item1, '@(A->'','')')"" />
275                                         </ItemGroup>
276                                 </Project>
277                         ";
278
279                         proj.LoadXml (documentString);
280                 }
281
282                 [Test]
283                 [Category ("NotWorking")]
284                 public void TestItemsInTarget1 ()
285                 {
286                         Engine engine = new Engine (Consts.BinPath);
287                         Project proj = engine.CreateNewProject ();
288
289                         string documentString = @"
290                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
291                                         <UsingTask TaskName='StringTestTask' AssemblyFile='Test\resources\TestTasks.dll' />
292                                         <PropertyGroup>
293                                                 <A>A</A>
294                                         </PropertyGroup>
295                                         <ItemGroup>
296                                                 <A Include='A;B;C' />
297                                         </ItemGroup>
298
299                                         <Target Name='1'>
300                                                 <StringTestTask Property='@(A)@(Z)'>
301                                                         <Output TaskParameter='Property' PropertyName='P1' />
302                                                 </StringTestTask>
303                                                 <StringTestTask Property=""@(A,'')"">
304                                                         <Output TaskParameter='Property' PropertyName='P2' />
305                                                 </StringTestTask>
306                                                 <StringTestTask Property=""@(A,'@(A)')"">
307                                                         <Output TaskParameter='Property' PropertyName='P3' />
308                                                 </StringTestTask>
309                                                 <StringTestTask Property=""@(A,'$(A)')"">
310                                                         <Output TaskParameter='Property' PropertyName='P4' />
311                                                 </StringTestTask>
312                                                 <StringTestTask Property=""@(A,'@(A,'')')"">
313                                                         <Output TaskParameter='Property' PropertyName='P5' />
314                                                 </StringTestTask>
315                                                 <StringTestTask Property=""%(A.Filename)"">
316                                                         <Output TaskParameter='Property' ItemName='I1' />
317                                                 </StringTestTask>
318                                                 <StringTestTask Property=""@(A) %(Filename)"">
319                                                         <Output TaskParameter='Property' ItemName='I2' />
320                                                 </StringTestTask>
321                                         </Target>
322                                 </Project>
323                         ";
324
325                         proj.LoadXml (documentString);
326                         proj.Build ("1");
327
328                         Assert.AreEqual ("A;B;C", proj.GetEvaluatedProperty ("P1"), "A1");
329                         Assert.AreEqual ("ABC", proj.GetEvaluatedProperty ("P2"), "A2");
330                         Assert.AreEqual ("A@(A)B@(A)C", proj.GetEvaluatedProperty ("P3"), "A3");
331                         Assert.AreEqual ("AABAC", proj.GetEvaluatedProperty ("P4"), "A4");
332                         Assert.AreEqual ("@(A,'ABC')", proj.GetEvaluatedProperty ("P5"), "A5");
333                         Assert.AreEqual ("A;B;C", GetItems (proj, "I1"), "A6");
334                         Assert.AreEqual ("A A;B B;C C", GetItems (proj, "I2"), "A7");
335                 }
336
337                 [Test]
338                 [Category ("NotWorking")]
339                 public void TestItemsInTarget2 ()
340                 {
341                         Engine engine = new Engine (Consts.BinPath);
342                         Project proj = engine.CreateNewProject ();
343
344                         string documentString = @"
345                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
346                                         <UsingTask TaskName='StringTestTask' AssemblyFile='Test\resources\TestTasks.dll' />
347                                         <ItemGroup>
348                                                 <A Include='A;B;C' />
349                                         </ItemGroup>
350
351                                         <Target Name='1'>
352                                                 <StringTestTask Property=""%(Filename)"">
353                                                         <Output TaskParameter='Property' ItemName='I2' />
354                                                 </StringTestTask>
355                                         </Target>
356                                 </Project>
357                         ";
358
359                         proj.LoadXml (documentString);
360                         Assert.IsFalse (proj.Build ("1"), "A1");
361                 }
362
363                 [Test]
364                 [Category ("NotWorking")]
365                 public void TestItemsInTarget3 ()
366                 {
367                         Engine engine = new Engine (Consts.BinPath);
368                         Project proj = engine.CreateNewProject ();
369
370                         string documentString = @"
371                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
372                                         <UsingTask TaskName='StringTestTask' AssemblyFile='Test\resources\TestTasks.dll' />
373                                         <PropertyGroup>
374                                                 <A>A</A>
375                                                 <B>A;B</B>
376                                                 <C>A;</C>
377                                         </PropertyGroup>
378                                         <ItemGroup>
379                                                 <A Include='A;B;C' />
380                                         </ItemGroup>
381
382                                         <Target Name='1'>
383                                                 <StringTestTask Array='$(A)$(A)'>
384                                                         <Output TaskParameter='Array' ItemName='I0' />
385                                                 </StringTestTask>
386                                                 <StringTestTask Array='$(B)$(B)'>
387                                                         <Output TaskParameter='Array' ItemName='I1' />
388                                                 </StringTestTask>
389                                                 <StringTestTask Array='$(C)'>
390                                                         <Output TaskParameter='Array' ItemName='I2' />
391                                                 </StringTestTask>
392                                                 <StringTestTask Array='$(C)$(C)'>
393                                                         <Output TaskParameter='Array' ItemName='I3' />
394                                                 </StringTestTask>
395
396                                                 <StringTestTask Array='@(A);$(C)'>
397                                                         <Output TaskParameter='Array' ItemName='I4' />
398                                                 </StringTestTask>
399                                                 <StringTestTask Array='@(A);A;B;C'>
400                                                         <Output TaskParameter='Array' ItemName='I5' />
401                                                 </StringTestTask>
402                                         </Target>
403                                 </Project>
404                         ";
405
406                         proj.LoadXml (documentString);
407                         proj.Build ("1");
408
409                         Assert.AreEqual ("AA", GetItems (proj, "I0"), "A1");
410                         Assert.AreEqual ("A;BA;B", GetItems (proj, "I1"), "A2");
411                         Assert.AreEqual (3, proj.GetEvaluatedItemsByName ("I1").Count, "A3");
412                         Assert.AreEqual ("A", GetItems (proj, "I2"), "A4");
413                         Assert.AreEqual (1, proj.GetEvaluatedItemsByName ("I2").Count, "A5");
414                         Assert.AreEqual ("A;A", GetItems (proj, "I3"), "A6");
415                         Assert.AreEqual (2, proj.GetEvaluatedItemsByName ("I3").Count, "A7");
416
417                         Assert.AreEqual ("A;B;C;A", GetItems (proj, "I4"), "A8");
418                         Assert.AreEqual (4, proj.GetEvaluatedItemsByName ("I4").Count, "A9");
419                         Assert.AreEqual ("A;B;C;A;B;C", GetItems (proj, "I5"), "A10");
420                         Assert.AreEqual (6, proj.GetEvaluatedItemsByName ("I5").Count, "A11");
421                 }
422
423                 [Test]
424                 [Category ("NotWorking")]
425                 public void TestItemsInTarget4 ()
426                 {
427                         Engine engine = new Engine (Consts.BinPath);
428                         Project proj = engine.CreateNewProject ();
429
430                         string documentString = @"
431                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
432                                         <UsingTask TaskName='StringTestTask' AssemblyFile='Test\resources\TestTasks.dll' />
433                                         <ItemGroup>
434                                                 <A Include='A;B;C' />
435                                         </ItemGroup>
436                                         <Target Name='1'>
437                                                 <StringTestTask Array='@(A)@(A)'>
438                                                         <Output TaskParameter='Array' ItemName='I1' />
439                                                 </StringTestTask>
440                                         </Target>
441                                 </Project>
442                         ";
443
444                         proj.LoadXml (documentString);
445                         Assert.IsFalse (proj.Build ("1"));
446                 }
447
448                 [Test]
449                 [Category ("NotWorking")]
450                 public void TestItemsInTarget5 ()
451                 {
452                         Engine engine = new Engine (Consts.BinPath);
453                         Project proj = engine.CreateNewProject ();
454
455                         string documentString = @"
456                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
457                                         <UsingTask TaskName='StringTestTask' AssemblyFile='Test\resources\TestTasks.dll' />
458                                         <ItemGroup>
459                                                 <A Include='A;B;C' />
460                                         </ItemGroup>
461                                         <Target Name='1'>
462                                                 <StringTestTask Array='@(A)AAA'>
463                                                         <Output TaskParameter='Array' ItemName='I1' />
464                                                 </StringTestTask>
465                                         </Target>
466                                 </Project>
467                         ";
468
469                         proj.LoadXml (documentString);
470                         Assert.IsFalse (proj.Build ("1"));
471                 }
472
473                 [Test]
474                 [Category ("NotWorking")]
475                 public void TestItemsInTarget6 ()
476                 {
477                         Engine engine = new Engine (Consts.BinPath);
478                         Project proj = engine.CreateNewProject ();
479
480                         string documentString = @"
481                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
482                                         <UsingTask TaskName='StringTestTask' AssemblyFile='Test\resources\TestTasks.dll' />
483                                         <ItemGroup>
484                                                 <A Include='A;B;C' />
485                                         </ItemGroup>
486                                         <PropertyGroup>
487                                                 <A>A</A>
488                                         </PropertyGroup>
489                                         <Target Name='1'>
490                                                 <StringTestTask Array='@(A)$(A)'>
491                                                         <Output TaskParameter='Array' ItemName='I1' />
492                                                 </StringTestTask>
493                                         </Target>
494                                 </Project>
495                         ";
496
497                         proj.LoadXml (documentString);
498                         Assert.IsFalse (proj.Build ("1"));
499                 }
500         }
501 }