importing messaging-2008 branch to trunk [continued]
[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                 public void TestItemsInTarget1 ()
284                 {
285                         Engine engine = new Engine (Consts.BinPath);
286                         Project proj = engine.CreateNewProject ();
287
288                         string documentString = @"
289                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
290                                         <UsingTask TaskName='StringTestTask' AssemblyFile='Test\resources\TestTasks.dll' />
291                                         <PropertyGroup>
292                                                 <A>A</A>
293                                         </PropertyGroup>
294                                         <ItemGroup>
295                                                 <A Include='A;B;C' />
296                                         </ItemGroup>
297
298                                         <Target Name='1'>
299                                                 <StringTestTask Property='@(A)@(Z)'>
300                                                         <Output TaskParameter='Property' PropertyName='P1' />
301                                                 </StringTestTask>
302                                                 <StringTestTask Property=""@(A,'')"">
303                                                         <Output TaskParameter='Property' PropertyName='P2' />
304                                                 </StringTestTask>
305                                                 <StringTestTask Property=""@(A,'@(A)')"">
306                                                         <Output TaskParameter='Property' PropertyName='P3' />
307                                                 </StringTestTask>
308                                                 <StringTestTask Property=""@(A,'$(A)')"">
309                                                         <Output TaskParameter='Property' PropertyName='P4' />
310                                                 </StringTestTask>
311                                                 <StringTestTask Property=""@(A,'@(A,'')')"">
312                                                         <Output TaskParameter='Property' PropertyName='P5' />
313                                                 </StringTestTask>
314                                                 <StringTestTask Property=""%(A.Filename)"">
315                                                         <Output TaskParameter='Property' ItemName='I1' />
316                                                 </StringTestTask>
317                                                 <StringTestTask Property=""@(A) %(Filename)"">
318                                                         <Output TaskParameter='Property' ItemName='I2' />
319                                                 </StringTestTask>
320                                         </Target>
321                                 </Project>
322                         ";
323
324                         proj.LoadXml (documentString);
325                         Assert.IsTrue (proj.Build ("1"), "A0, Build failed");
326
327                         Assert.AreEqual ("A;B;C", proj.GetEvaluatedProperty ("P1"), "A1");
328                         Assert.AreEqual ("ABC", proj.GetEvaluatedProperty ("P2"), "A2");
329                         Assert.AreEqual ("A@(A)B@(A)C", proj.GetEvaluatedProperty ("P3"), "A3");
330                         Assert.AreEqual ("AABAC", proj.GetEvaluatedProperty ("P4"), "A4");
331                         Assert.AreEqual ("@(A,'ABC')", proj.GetEvaluatedProperty ("P5"), "A5");
332                         Assert.AreEqual ("A;B;C", GetItems (proj, "I1"), "A6");
333                         Assert.AreEqual ("A A;B B;C C", GetItems (proj, "I2"), "A7");
334                 }
335
336                 [Test]
337                 [Category ("NotWorking")]
338                 public void TestItemsInTarget2 ()
339                 {
340                         Engine engine = new Engine (Consts.BinPath);
341                         Project proj = engine.CreateNewProject ();
342
343                         string documentString = @"
344                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
345                                         <UsingTask TaskName='StringTestTask' AssemblyFile='Test\resources\TestTasks.dll' />
346                                         <ItemGroup>
347                                                 <A Include='A;B;C' />
348                                         </ItemGroup>
349
350                                         <Target Name='1'>
351                                                 <StringTestTask Property=""%(Filename)"">
352                                                         <Output TaskParameter='Property' ItemName='I2' />
353                                                 </StringTestTask>
354                                         </Target>
355                                 </Project>
356                         ";
357
358                         proj.LoadXml (documentString);
359                         Assert.IsFalse (proj.Build ("1"), "A1");
360                 }
361
362                 [Test]
363                 [Category ("NotWorking")]
364                 public void TestItemsInTarget3 ()
365                 {
366                         Engine engine = new Engine (Consts.BinPath);
367                         Project proj = engine.CreateNewProject ();
368
369                         string documentString = @"
370                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
371                                         <UsingTask TaskName='StringTestTask' AssemblyFile='Test\resources\TestTasks.dll' />
372                                         <PropertyGroup>
373                                                 <A>A</A>
374                                                 <B>A;B</B>
375                                                 <C>A;</C>
376                                         </PropertyGroup>
377                                         <ItemGroup>
378                                                 <A Include='A;B;C' />
379                                         </ItemGroup>
380
381                                         <Target Name='1'>
382                                                 <StringTestTask Array='$(A)$(A)'>
383                                                         <Output TaskParameter='Array' ItemName='I0' />
384                                                 </StringTestTask>
385                                                 <StringTestTask Array='$(B)$(B)'>
386                                                         <Output TaskParameter='Array' ItemName='I1' />
387                                                 </StringTestTask>
388                                                 <StringTestTask Array='$(C)'>
389                                                         <Output TaskParameter='Array' ItemName='I2' />
390                                                 </StringTestTask>
391                                                 <StringTestTask Array='$(C)$(C)'>
392                                                         <Output TaskParameter='Array' ItemName='I3' />
393                                                 </StringTestTask>
394
395                                                 <StringTestTask Array='@(A);$(C)'>
396                                                         <Output TaskParameter='Array' ItemName='I4' />
397                                                 </StringTestTask>
398                                                 <StringTestTask Array='@(A);A;B;C'>
399                                                         <Output TaskParameter='Array' ItemName='I5' />
400                                                 </StringTestTask>
401                                         </Target>
402                                 </Project>
403                         ";
404
405                         proj.LoadXml (documentString);
406                         proj.Build ("1");
407
408                         Assert.AreEqual ("AA", GetItems (proj, "I0"), "A1");
409                         Assert.AreEqual ("A;BA;B", GetItems (proj, "I1"), "A2");
410                         Assert.AreEqual (3, proj.GetEvaluatedItemsByName ("I1").Count, "A3");
411                         Assert.AreEqual ("A", GetItems (proj, "I2"), "A4");
412                         Assert.AreEqual (1, proj.GetEvaluatedItemsByName ("I2").Count, "A5");
413                         Assert.AreEqual ("A;A", GetItems (proj, "I3"), "A6");
414                         Assert.AreEqual (2, proj.GetEvaluatedItemsByName ("I3").Count, "A7");
415
416                         Assert.AreEqual ("A;B;C;A", GetItems (proj, "I4"), "A8");
417                         Assert.AreEqual (4, proj.GetEvaluatedItemsByName ("I4").Count, "A9");
418                         Assert.AreEqual ("A;B;C;A;B;C", GetItems (proj, "I5"), "A10");
419                         Assert.AreEqual (6, proj.GetEvaluatedItemsByName ("I5").Count, "A11");
420                 }
421
422                 [Test]
423                 [Category ("NotWorking")]
424                 public void TestItemsInTarget4 ()
425                 {
426                         Engine engine = new Engine (Consts.BinPath);
427                         Project proj = engine.CreateNewProject ();
428
429                         string documentString = @"
430                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
431                                         <UsingTask TaskName='StringTestTask' AssemblyFile='Test\resources\TestTasks.dll' />
432                                         <ItemGroup>
433                                                 <A Include='A;B;C' />
434                                         </ItemGroup>
435                                         <Target Name='1'>
436                                                 <StringTestTask Array='@(A)@(A)'>
437                                                         <Output TaskParameter='Array' ItemName='I1' />
438                                                 </StringTestTask>
439                                         </Target>
440                                 </Project>
441                         ";
442
443                         proj.LoadXml (documentString);
444                         Assert.IsFalse (proj.Build ("1"));
445                 }
446
447                 [Test]
448                 [Category ("NotWorking")]
449                 public void TestItemsInTarget5 ()
450                 {
451                         Engine engine = new Engine (Consts.BinPath);
452                         Project proj = engine.CreateNewProject ();
453
454                         string documentString = @"
455                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
456                                         <UsingTask TaskName='StringTestTask' AssemblyFile='Test\resources\TestTasks.dll' />
457                                         <ItemGroup>
458                                                 <A Include='A;B;C' />
459                                         </ItemGroup>
460                                         <Target Name='1'>
461                                                 <StringTestTask Array='@(A)AAA'>
462                                                         <Output TaskParameter='Array' ItemName='I1' />
463                                                 </StringTestTask>
464                                         </Target>
465                                 </Project>
466                         ";
467
468                         proj.LoadXml (documentString);
469                         Assert.IsFalse (proj.Build ("1"));
470                 }
471
472                 [Test]
473                 [Category ("NotWorking")]
474                 public void TestItemsInTarget6 ()
475                 {
476                         Engine engine = new Engine (Consts.BinPath);
477                         Project proj = engine.CreateNewProject ();
478
479                         string documentString = @"
480                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
481                                         <UsingTask TaskName='StringTestTask' AssemblyFile='Test\resources\TestTasks.dll' />
482                                         <ItemGroup>
483                                                 <A Include='A;B;C' />
484                                         </ItemGroup>
485                                         <PropertyGroup>
486                                                 <A>A</A>
487                                         </PropertyGroup>
488                                         <Target Name='1'>
489                                                 <StringTestTask Array='@(A)$(A)'>
490                                                         <Output TaskParameter='Array' ItemName='I1' />
491                                                 </StringTestTask>
492                                         </Target>
493                                 </Project>
494                         ";
495
496                         proj.LoadXml (documentString);
497                         Assert.IsFalse (proj.Build ("1"));
498                 }
499         }
500 }