New tests, updates
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / Microsoft.Build.BuildEngine / ProjectTest.cs
1 //
2 // ProjectTest.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 System.Collections.Generic;
31 using System.IO;
32 using System.Xml;
33 using Microsoft.Build.BuildEngine;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
36 using NUnit.Framework;
37 using System.Text;
38
39 using MBT = MonoTests.Microsoft.Build.Tasks;
40
41 namespace MonoTests.Microsoft.Build.BuildEngine {
42
43         class TestLogger : Logger {
44                 int target_started_events = 0;
45                 int target_finished_events = 0;
46
47                 public override void Initialize (IEventSource eventSource)
48                 {
49                         eventSource.TargetStarted += new TargetStartedEventHandler(TargetStarted);
50                         eventSource.TargetFinished += new TargetFinishedEventHandler(TargetFinished);
51                         eventSource.MessageRaised += new BuildMessageEventHandler(Message);
52                         eventSource.WarningRaised += new BuildWarningEventHandler(Warning);
53                 }
54
55                 void TargetStarted (object sender, TargetStartedEventArgs args)
56                 {
57                         target_started_events++;
58                 }
59
60                 void TargetFinished (object sender, TargetFinishedEventArgs args)
61                 {
62                         target_finished_events++;
63                 }
64
65                 void Message (object sender, BuildMessageEventArgs args)
66                 {
67                 }
68                 
69                 void Warning (object sender, BuildWarningEventArgs args)
70                 {
71                 }
72
73                 public int TargetStartedEvents { get { return target_started_events; } }
74
75                 public int TargetFinishedEvents { get { return target_finished_events; } }
76         }
77
78         [TestFixture]
79         public class ProjectTest {
80
81                 /*
82                 Import [] GetImports (ImportCollection ic)
83                 {
84                         List<Import> list = new List<Import> ();
85                         foreach (Import i in ic)
86                                 list.Add (i);
87                         return list.ToArray ();
88                 }
89                 */
90
91                 [Test]
92                 public void TestAssignment1 ()
93                 {
94                         Engine engine;
95                         Project project;
96                         string documentString =
97                                 "<Project></Project>";
98                         
99                         engine = new Engine (Consts.BinPath);
100                         DateTime time = DateTime.Now;
101                         project = engine.CreateNewProject ();
102                         try {
103                                 project.LoadXml (documentString);
104                         } catch (InvalidProjectFileException) {
105                                 Assert.AreEqual (true, project.BuildEnabled, "A1");
106                                 Assert.AreEqual (String.Empty, project.DefaultTargets, "A2");
107                                 Assert.AreEqual (String.Empty, project.FullFileName, "A3");
108                                 Assert.AreEqual (false, project.IsDirty, "A4");
109                                 Assert.AreEqual (false, project.IsValidated, "A5");
110                                 Assert.AreEqual (engine, project.ParentEngine, "A6");
111                                 Console.WriteLine ("time: {0} p.t: {1}", time, project.TimeOfLastDirty);
112                                 Assert.IsTrue (time <= project.TimeOfLastDirty, "A7");
113                                 Assert.IsTrue (String.Empty != project.Xml, "A8");
114                                 return;
115                         }
116
117                         Assert.Fail ("Expected InvalidProjectFileException");
118                 }
119
120                 [Test]
121                 public void TestAssignment2 ()
122                 {
123                         Engine engine;
124                         Project project;
125                         string documentString =
126                                 "<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\"></Project>";
127                         
128                         engine = new Engine (Consts.BinPath);
129                         DateTime time = DateTime.Now;
130                         project = engine.CreateNewProject ();
131                         project.LoadXml (documentString);
132
133                         Assert.AreEqual (true, project.BuildEnabled, "A1");
134                         Assert.AreEqual (String.Empty, project.DefaultTargets, "A2");
135                         Assert.AreEqual (String.Empty, project.FullFileName, "A3");
136                         Assert.AreEqual (true, project.IsDirty, "A4");
137                         Assert.AreEqual (false, project.IsValidated, "A5");
138                         Assert.AreEqual (engine, project.ParentEngine, "A6");
139                         Assert.IsTrue (time <= project.TimeOfLastDirty, "A7");
140                         Assert.IsTrue (String.Empty != project.Xml, "A8");
141                 }
142
143                 [Test]
144                 [Category ("NotWorking")]
145                 public void TestAddNewImport1 ()
146                 {
147                         Engine engine;
148                         Project project;
149
150                         string documentString = @"
151                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
152                                         <PropertyGroup />
153                                         <ItemGroup />
154                                         <Target Name='a' />
155                                         <Import Project='Test/resources/Import.csproj' />
156                                 </Project>
157                         ";
158
159                         engine = new Engine (Consts.BinPath);
160                         project = engine.CreateNewProject ();
161                         project.LoadXml (documentString);
162
163                         project.AddNewImport ("a", "true");
164                         // reevaluation wasn't caused by anything so it has only old import
165                         Assert.AreEqual (1, project.Imports.Count, "A1");
166                 }
167
168                 [Test]
169                 [Ignore ("Too detailed probably (implementation specific)")]
170                 public void TestAddNewItem1 ()
171                 {
172                         Engine engine;
173                         Project project;
174                         BuildItemGroup [] groups = new BuildItemGroup [1];
175
176                         string documentString = @"
177                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
178                                 </Project>
179                         ";
180
181                         engine = new Engine (Consts.BinPath);
182                         project = engine.CreateNewProject ();
183                         project.LoadXml (documentString);
184
185                         BuildItem item = project.AddNewItem ("A", "B");
186
187                         Assert.AreEqual (1, project.ItemGroups.Count, "A1");
188                         project.ItemGroups.CopyTo (groups, 0);
189                         Assert.AreEqual (1, groups [0].Count, "A2");
190                         Assert.AreEqual ("B", groups [0] [0].Include, "A3");
191                         Assert.AreEqual ("B", groups [0] [0].FinalItemSpec, "A4");
192                         Assert.AreEqual ("A", groups [0] [0].Name, "A5");
193                         //Assert.AreNotSame (item, groups [0] [0], "A6");
194                         Assert.IsFalse (object.ReferenceEquals (item, groups [0] [0]), "A6");
195
196                         Assert.AreEqual (1, project.EvaluatedItems.Count, "A7");
197                         Assert.AreEqual ("B", project.EvaluatedItems [0].Include, "A8");
198                         Assert.AreEqual ("B", project.EvaluatedItems [0].FinalItemSpec, "A9");
199                         Assert.AreEqual ("A", project.EvaluatedItems [0].Name, "A10");
200                         //Assert.AreNotSame (item, project.EvaluatedItems [0], "A11");
201                         Assert.IsFalse (object.ReferenceEquals (item, project.EvaluatedItems [0]), "A11");
202                 }
203
204                 [Test]
205                 [Category ("NotWorking")]
206                 public void TestAddNewItem2 ()
207                 {
208                         Engine engine;
209                         Project project;
210
211                         engine = new Engine (Consts.BinPath);
212                         project = engine.CreateNewProject ();
213
214                         BuildItem item = project.AddNewItem ("A", "a;b;c");
215                         Assert.AreEqual ("a;b;c", item.Include, "A1");
216                         Assert.AreEqual ("a", item.FinalItemSpec, "A2");
217
218                         Assert.AreEqual (3, project.EvaluatedItems.Count, "A3");
219                 }
220
221                 [Test]
222                 public void TestAddNewItem3 ()
223                 {
224                         Engine engine;
225                         Project project;
226                         BuildItemGroup [] groups = new BuildItemGroup [4];
227
228                         string documentString = @"
229                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
230                                         <ItemGroup />
231                                         <ItemGroup>
232                                                 <A Include='a'/>
233                                         </ItemGroup>
234                                         <ItemGroup>
235                                                 <B Include='a'/>
236                                         </ItemGroup>
237                                         <ItemGroup>
238                                                 <B Include='a'/>
239                                         </ItemGroup>
240                                 </Project>
241                         ";
242
243                         engine = new Engine (Consts.BinPath);
244                         project = engine.CreateNewProject ();
245                         project.LoadXml (documentString);
246
247                         project.AddNewItem ("B", "b");
248
249                         project.ItemGroups.CopyTo (groups, 0);
250                         Assert.AreEqual (0, groups [0].Count, "A1");
251                         Assert.AreEqual (1, groups [1].Count, "A2");
252                         Assert.AreEqual (1, groups [2].Count, "A3");
253                         Assert.AreEqual (2, groups [3].Count, "A4");
254                 }
255                 [Test]
256                 public void TestAddNewItemGroup ()
257                 {
258                         Engine engine;
259                         Project project;
260
261                         string documentString = @"
262                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
263                                 </Project>
264                         ";
265
266                         engine = new Engine (Consts.BinPath);
267                         project = engine.CreateNewProject ();
268                         project.LoadXml (documentString);
269
270                         BuildItemGroup big = project.AddNewItemGroup ();
271                         Assert.IsNotNull (big, "A1");
272                         Assert.AreEqual (String.Empty, big.Condition, "A2");
273                         Assert.AreEqual (0, big.Count, "A3");
274                         Assert.AreEqual (false, big.IsImported, "A4");
275                         Assert.IsTrue (project.IsDirty, "A5");
276                 }
277
278                 [Test]
279                 public void TestAddNewPropertyGroup ()
280                 {
281                         Engine engine;
282                         Project project;
283
284                         string documentString = @"
285                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
286                                 </Project>
287                         ";
288
289                         engine = new Engine (Consts.BinPath);
290                         project = engine.CreateNewProject ();
291                         project.LoadXml (documentString);
292
293                         BuildPropertyGroup bpg = project.AddNewPropertyGroup (false);
294                         Assert.IsNotNull (bpg, "A1");
295                         Assert.AreEqual (String.Empty, bpg.Condition, "A2");
296                         Assert.AreEqual (0, bpg.Count, "A3");
297                         Assert.AreEqual (false, bpg.IsImported, "A4");
298                         Assert.IsTrue (project.IsDirty, "A5");
299                 }
300
301                 [Test]
302                 public void TestBuild0 ()
303                 {
304                         Engine engine;
305                         Project project;
306                         IDictionary hashtable = new Hashtable ();
307
308                         string documentString = @"
309                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
310                                         <Target 
311                                                 Name='Main'
312                                                 Inputs='a;b;c'
313                                                 Outputs='d;e;f'
314                                         >
315                                         </Target>
316                                 </Project>
317                         ";
318
319                         engine = new Engine (Consts.BinPath);
320                         project = engine.CreateNewProject ();
321                         project.LoadXml (documentString);
322
323                         Assert.AreEqual (true, project.Build (new string [] { "Main" }, hashtable), "A1");
324                         Assert.AreEqual (1, hashtable.Count, "A2");
325
326                         IDictionaryEnumerator e = hashtable.GetEnumerator ();
327                         e.MoveNext ();
328
329                         string name = (string) e.Key;
330                         Assert.AreEqual ("Main", name, "A3");
331                         ITaskItem [] arr = (ITaskItem []) e.Value;
332
333                         Assert.AreEqual (3, arr.Length, "A4");
334                         Assert.AreEqual ("d", arr [0].ItemSpec, "A5");
335                         Assert.AreEqual ("e", arr [1].ItemSpec, "A6");
336                         Assert.AreEqual ("f", arr [2].ItemSpec, "A7");
337                 }
338
339                 [Test]
340                 public void TestBuild1 ()
341                 {
342                         Engine engine;
343                         Project project;
344                         IDictionary hashtable = new Hashtable ();
345                         
346                         string documentString = @"
347                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
348                                         <Target Name='Main'>
349                                                 <Microsoft.Build.Tasks.Message Text='Text' />
350                                         </Target>
351                                 </Project>
352                         ";
353                         
354                         engine = new Engine (Consts.BinPath);
355                         project = engine.CreateNewProject ();
356                         project.LoadXml (documentString);
357
358                         Assert.AreEqual (true, project.Build (new string[] { "Main" }, hashtable), "A1");
359                         Assert.AreEqual (1, hashtable.Count, "A2");
360
361                         IDictionaryEnumerator e = hashtable.GetEnumerator ();
362                         e.MoveNext ();
363
364                         string name = (string) e.Key;
365                         Assert.AreEqual ("Main", name, "A3");
366                         Assert.IsNotNull ((ITaskItem []) e.Value, "A4");
367                 }
368
369                 [Test]
370                 public void TestBuild2 ()
371                 {
372                         Engine engine;
373                         Project project;
374
375                         string documentString = @"
376                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
377                                         <Target Name='T'>
378                                                 <Message Text='text' />
379                                         </Target>
380                                 </Project>
381                         ";
382
383                         engine = new Engine (Consts.BinPath);
384                         MBT.TestMessageLogger tl = new MBT.TestMessageLogger();
385                         engine.RegisterLogger (tl);
386                         project = engine.CreateNewProject ();
387                         project.LoadXml (documentString);
388
389                         project.Build ("T");
390                         project.Build ("T");
391
392                         Assert.AreEqual (2, tl.TargetStarted, "A1");
393                         Assert.AreEqual (2, tl.TargetFinished, "A2");
394                         Assert.AreEqual (2, tl.TaskStarted, "A3");
395                         Assert.AreEqual (2, tl.TaskFinished, "A4");
396                 }
397
398                 [Test]
399                 public void TestBuild3 ()
400                 {
401                         Engine engine;
402                         Project project;
403
404                         string documentString = @"
405                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
406                                         <Target Name='T'>
407                                                 <Message Text='text' />
408                                         </Target>
409                                 </Project>
410                         ";
411
412                         engine = new Engine (Consts.BinPath);
413                         MBT.TestMessageLogger tl = new MBT.TestMessageLogger ();
414                         engine.RegisterLogger (tl);
415                         project = engine.CreateNewProject ();
416                         project.LoadXml (documentString);
417
418                         project.Build (new string [1] { "T" }, null, BuildSettings.None);
419                         project.Build (new string [1] { "T" }, null, BuildSettings.None);
420
421                         Assert.AreEqual (2, tl.TargetStarted, "A1");
422                         Assert.AreEqual (2, tl.TargetFinished, "A2");
423                         Assert.AreEqual (2, tl.TaskStarted, "A3");
424                         Assert.AreEqual (2, tl.TaskFinished, "A4");
425                 }
426
427                 [Test]
428                 public void TestBuild4 ()
429                 {
430                         Engine engine;
431                         Project project;
432
433                         string documentString = @"
434                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
435                                         <Target Name='T'>
436                                                 <Message Text='text' />
437                                         </Target>
438                                 </Project>
439                         ";
440
441                         engine = new Engine (Consts.BinPath);
442                         MBT.TestMessageLogger tl = new MBT.TestMessageLogger ();
443                         engine.RegisterLogger (tl);
444                         project = engine.CreateNewProject ();
445                         project.LoadXml (documentString);
446
447                         project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
448                         project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
449
450                         Assert.AreEqual (1, tl.TargetStarted, "A1");
451                         Assert.AreEqual (1, tl.TargetFinished, "A2");
452                         Assert.AreEqual (1, tl.TaskStarted, "A3");
453                         Assert.AreEqual (1, tl.TaskFinished, "A4");
454                 }
455
456                 [Test]
457                 public void TestBuild5 ()
458                 {
459                         Engine engine;
460                         Project project;
461
462                         string documentString = @"
463                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
464                                 </Project>
465                         ";
466
467                         engine = new Engine (Consts.BinPath);
468                         project = engine.CreateNewProject ();
469                         project.LoadXml (documentString);
470
471                         Assert.IsFalse (project.Build ("target_that_doesnt_exist"));
472                 }
473
474                 [Test]
475                 public void TestEvaluatedItems1 ()
476                 {
477                         Engine engine;
478                         Project project;
479
480                         string documentString = @"
481                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
482                                         <ItemGroup>
483                                                 <A Include='a' />
484                                                 <B Include='b' Condition='false' />
485                                         </ItemGroup>
486                                 </Project>
487                         ";
488
489                         engine = new Engine (Consts.BinPath);
490                         project = engine.CreateNewProject ();
491                         project.LoadXml (documentString);
492
493                         Assert.AreEqual (1, project.EvaluatedItems.Count, "A1");
494
495                         BuildItem bi = project.EvaluatedItems [0];
496
497                         bi.Name = "C";
498                         bi.Include = "c";
499
500                         BuildItemGroup [] big = new BuildItemGroup [1];
501                         project.ItemGroups.CopyTo (big, 0);
502                         Assert.AreEqual ("C", big [0] [0].Name, "A2");
503                         Assert.AreEqual ("c", big [0] [0].Include, "A3");
504                 }
505
506                 [Test]
507                 public void TestEvaluatedItems2 ()
508                 {
509                         Engine engine;
510                         Project project;
511
512                         string documentString = @"
513                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
514                                         <ItemGroup>
515                                                 <A Include='a;b;c' />
516                                         </ItemGroup>
517                                 </Project>
518                         ";
519
520                         engine = new Engine (Consts.BinPath);
521                         project = engine.CreateNewProject ();
522                         project.LoadXml (documentString);
523
524                         BuildItemGroup [] big = new BuildItemGroup [1];
525                         project.ItemGroups.CopyTo (big, 0);
526
527                         Assert.AreEqual (3, project.EvaluatedItems.Count, "A1");
528                         Assert.AreEqual ("a;b;c", big [0] [0].Include, "A2");
529                         Assert.AreEqual (1, big [0].Count, "A3");
530
531                         BuildItem bi = project.EvaluatedItems [0];
532
533                         bi.Include = "d";
534
535                         Assert.AreEqual (3, big [0].Count, "A4");
536                         Assert.AreEqual ("d", big [0] [0].Include, "A5");
537                         Assert.AreEqual ("b", big [0] [1].Include, "A6");
538                         Assert.AreEqual ("c", big [0] [2].Include, "A7");
539                 }
540
541                 [Test]
542                 [Category ("NotWorking")]
543                 public void TestGetConditionedPropertyValues ()
544                 {
545                         Engine engine;
546                         Project project;
547
548                         string documentString = @"
549                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
550                                         <PropertyGroup Condition='true'>
551                                                 <A>A</A>
552                                                 <B Condition='true'>A</B>
553                                         </PropertyGroup>
554                                         <PropertyGroup>
555                                                 <C Condition='true'>A</C>
556                                                 <C Condition='false'>B</C>
557                                                 <C Condition='!false'>C</C>
558                                                 <D>A</D>
559                                                 <E Condition="" '$(C)' == 'A' "">E</E>
560                                         </PropertyGroup>
561                                 </Project>
562                         ";
563
564                         engine = new Engine (Consts.BinPath);
565                         project = engine.CreateNewProject ();
566                         project.LoadXml (documentString);
567
568                         Assert.AreEqual (0, project.GetConditionedPropertyValues ("A").Length, "A1");
569                         Assert.AreEqual (0, project.GetConditionedPropertyValues ("B").Length, "A2");
570                         Assert.AreEqual (1, project.GetConditionedPropertyValues ("C").Length, "A3");
571                         Assert.AreEqual (0, project.GetConditionedPropertyValues ("D").Length, "A4");
572                         Assert.AreEqual (0, project.GetConditionedPropertyValues ("E").Length, "A5");
573                         Assert.AreEqual ("A", project.GetConditionedPropertyValues ("C") [0], "A6");
574                 }
575
576                 [Test]
577                 [ExpectedException (typeof (ArgumentNullException))]
578                 public void TestGetEvaluatedItemsByName1 ()
579                 {
580                         Engine engine;
581                         Project project;
582
583                         string documentString = @"
584                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
585                                 </Project>
586                         ";
587
588                         engine = new Engine (Consts.BinPath);
589                         project = engine.CreateNewProject ();
590                         project.LoadXml (documentString);
591
592                         project.GetEvaluatedItemsByName (null);
593                 }
594
595                 [Test]
596                 public void TestGetEvaluatedItemsByName2 ()
597                 {
598                         Engine engine;
599                         Project project;
600
601                         string documentString = @"
602                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
603                                         <ItemGroup>
604                                                 <A Include='1' />
605                                                 <B Include='2' Condition='true' />
606                                                 <C Include='3' Condition='false' />
607                                         </ItemGroup>
608                                 </Project>
609                         ";
610
611                         engine = new Engine (Consts.BinPath);
612                         project = engine.CreateNewProject ();
613                         project.LoadXml (documentString);
614
615                         BuildItemGroup big;
616
617                         big = project.GetEvaluatedItemsByName (String.Empty);
618
619                         Assert.AreEqual (0, big.Count, "A1");
620
621                         big = project.GetEvaluatedItemsByName ("A");
622
623                         Assert.AreEqual (1, big.Count, "A2");
624                         Assert.AreEqual ("1", big [0].FinalItemSpec, "A3");
625
626                         big = project.GetEvaluatedItemsByName ("B");
627
628                         Assert.AreEqual (1, big.Count, "A4");
629                         Assert.AreEqual ("2", big [0].FinalItemSpec, "A5");
630
631                         big = project.GetEvaluatedItemsByName ("C");
632
633                         Assert.AreEqual (0, big.Count, "A6");
634                 }
635
636                 [Test]
637                 [ExpectedException (typeof (ArgumentNullException))]
638                 public void TestGetEvaluatedItemsByNameIgnoringCondition1 ()
639                 {
640                         Engine engine;
641                         Project project;
642
643                         string documentString = @"
644                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
645                                 </Project>
646                         ";
647
648                         engine = new Engine (Consts.BinPath);
649                         project = engine.CreateNewProject ();
650                         project.LoadXml (documentString);
651
652                         project.GetEvaluatedItemsByNameIgnoringCondition (null);
653                 }
654
655                 [Test]
656                 public void TestGetEvaluatedItemsByNameIgnoringCondition2 ()
657                 {
658                         Engine engine;
659                         Project project;
660
661                         string documentString = @"
662                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
663                                         <ItemGroup>
664                                                 <A Include='1' />
665                                                 <B Include='2' Condition='true' />
666                                                 <C Include='3' Condition='false' />
667                                         </ItemGroup>
668                                 </Project>
669                         ";
670
671                         engine = new Engine (Consts.BinPath);
672                         project = engine.CreateNewProject ();
673                         project.LoadXml (documentString);
674
675                         BuildItemGroup big;
676
677                         big = project.GetEvaluatedItemsByNameIgnoringCondition (String.Empty);
678
679                         Assert.AreEqual (0, big.Count, "A1");
680
681                         big = project.GetEvaluatedItemsByNameIgnoringCondition ("A");
682
683                         Assert.AreEqual (1, big.Count, "A2");
684                         Assert.AreEqual ("1", big [0].FinalItemSpec, "A3");
685
686                         big = project.GetEvaluatedItemsByNameIgnoringCondition ("B");
687
688                         Assert.AreEqual (1, big.Count, "A4");
689                         Assert.AreEqual ("2", big [0].FinalItemSpec, "A5");
690
691                         big = project.GetEvaluatedItemsByNameIgnoringCondition ("C");
692
693                         Assert.AreEqual (1, big.Count, "A6");
694                         Assert.AreEqual ("3", big [0].FinalItemSpec, "A7");
695                 }
696
697                 [Test]
698                 [ExpectedException (typeof (ArgumentNullException))]
699                 public void TestGetEvaluatedProperty1 ()
700                 {
701                         Engine engine;
702                         Project project;
703
704                         string documentString = @"
705                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
706                                 </Project>
707                         ";
708
709                         engine = new Engine (Consts.BinPath);
710                         project = engine.CreateNewProject ();
711                         project.LoadXml (documentString);
712
713                         project.GetEvaluatedProperty (null);
714                 }
715                 [Test]
716                 public void TestGetEvaluatedProperty2 ()
717                 {
718                         Engine engine;
719                         Project project;
720
721                         string documentString = @"
722                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
723                                         <PropertyGroup>
724                                                 <A>1</A>
725                                                 <B Condition='true'>2</B>
726                                                 <C Condition='false'>3</C>
727                                         </PropertyGroup>
728                                 </Project>
729                         ";
730
731                         engine = new Engine (Consts.BinPath);
732                         project = engine.CreateNewProject ();
733                         project.LoadXml (documentString);
734
735                         Assert.AreEqual ("1", project.GetEvaluatedProperty ("A"), "A1");
736                         Assert.AreEqual ("2", project.GetEvaluatedProperty ("B"), "A2");
737                         Assert.IsNull (project.GetEvaluatedProperty ("C"), "A3");
738                 }
739
740                 [Test]
741                 public void TestGetProjectExtensions ()
742                 {
743                         Engine engine;
744                         Project project;
745
746                         string documentString = @"
747                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
748                                         <ProjectExtensions>
749                                                 <Node>Text</Node>
750                                         </ProjectExtensions>
751                                 </Project>
752                         ";
753
754                         engine = new Engine (Consts.BinPath);
755                         project = engine.CreateNewProject ();
756                         project.LoadXml (documentString);
757
758                         Assert.AreEqual (String.Empty, project.GetProjectExtensions (null), "A1");
759                         Assert.AreEqual (String.Empty, project.GetProjectExtensions (String.Empty), "A2");
760                         Assert.AreEqual (String.Empty, project.GetProjectExtensions ("something"), "A3");
761                         Assert.AreEqual ("Text", project.GetProjectExtensions ("Node"), "A4");
762                 }
763
764                 [Test]
765                 public void TestGlobalProperties1 ()
766                 {
767                         Engine engine;
768                         Project project;
769                         
770                         string documentString = @"
771                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
772                                 </Project>
773                         ";
774                         
775                         engine = new Engine (Consts.BinPath);
776                         project = engine.CreateNewProject ();
777                         project.LoadXml (documentString);
778
779                         Assert.AreEqual (0, project.GlobalProperties.Count, "A1");
780                 }
781
782                 [Test]
783                 public void TestGlobalProperties2 ()
784                 {
785                         Engine engine;
786                         Project project;
787                         
788                         string documentString = @"
789                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
790                                 </Project>
791                         ";
792                         
793                         engine = new Engine (Consts.BinPath);
794                         engine.GlobalProperties.SetProperty ("Property", "Value");
795                         
796                         project = engine.CreateNewProject ();
797                         project.LoadXml (documentString);
798
799                         Assert.AreEqual (1, project.GlobalProperties.Count, "A1");
800                         Assert.AreEqual ("Property", project.GlobalProperties ["Property"].Name, "A2");
801                         Assert.AreEqual ("Value", project.GlobalProperties ["Property"].Value, "A3");
802                         Assert.AreEqual ("Value", project.GlobalProperties ["Property"].FinalValue, "A4");
803                         Assert.AreEqual ("Property", project.EvaluatedProperties ["Property"].Name, "A2");
804                         Assert.AreEqual ("Value", project.EvaluatedProperties ["Property"].Value, "A3");
805                         Assert.AreEqual ("Value", project.EvaluatedProperties ["Property"].FinalValue, "A4");
806                 }
807
808                 [Test]
809                 [Category ("NotDotNet")]
810                 [ExpectedException (typeof (ArgumentNullException))]
811                 public void TestGlobalProperties3 ()
812                 {
813                         Engine engine;
814                         Project project;
815                         
816                         string documentString = @"
817                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
818                                 </Project>
819                         ";
820                         
821                         engine = new Engine (Consts.BinPath);
822                         project = engine.CreateNewProject ();
823                         project.LoadXml (documentString);
824
825                         project.GlobalProperties = null;
826                 }
827
828                 [Test]
829                 [Ignore ("needs rewriting")]
830                 public void TestGlobalProperties4 ()
831                 {
832                         Engine engine;
833                         Project project;
834                         
835                         string documentString = @"
836                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
837                                         <PropertyGroup>
838                                                 <Property>a</Property>
839                                         </PropertyGroup>
840                                 </Project>
841                         ";
842                         
843                         engine = new Engine (Consts.BinPath);
844                         project = engine.CreateNewProject ();
845                         project.LoadXml (documentString);
846
847                         BuildPropertyGroup[] groups = new BuildPropertyGroup [1];
848                         project.PropertyGroups.CopyTo (groups, 0);
849
850                         project.GlobalProperties = groups [0];
851                         project.GlobalProperties = project.EvaluatedProperties;
852                 }
853
854                 [Test]
855                 [Category ("NotDotNet")]
856                 [ExpectedException (typeof (InvalidOperationException))]
857                 public void TestGlobalProperties5 ()
858                 {
859                         Engine engine;
860                         Project project;
861                         
862                         string documentString = @"
863                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
864                                         <PropertyGroup>
865                                                 <Property>a</Property>
866                                         </PropertyGroup>
867                                 </Project>
868                         ";
869                         
870                         engine = new Engine (Consts.BinPath);
871                         project = engine.CreateNewProject ();
872                         project.LoadXml (documentString);
873
874                         BuildPropertyGroup[] groups = new BuildPropertyGroup [1];
875                         project.PropertyGroups.CopyTo (groups, 0);
876                         project.GlobalProperties = groups [0];
877                 }
878
879                 [Test]
880                 [ExpectedException (typeof (InvalidProjectFileException))]
881                 public void TestLoad1 ()
882                 {
883                         Engine engine;
884                         Project project;
885
886                         string documentString = @"
887                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
888                                         <PropertyGroup>
889                                 </Project>
890                         ";
891
892                         engine = new Engine (Consts.BinPath);
893                         project = engine.CreateNewProject ();
894                         project.LoadXml (documentString);
895                 }
896
897                 [Test]
898                 [ExpectedException (typeof (InvalidProjectFileException))]
899                 public void TestLoad2 ()
900                 {
901                         Engine engine;
902                         Project project;
903
904                         engine = new Engine (Consts.BinPath);
905                         project = engine.CreateNewProject ();
906                         project.LoadXml ("project_file_that_doesnt_exist");
907                 }
908
909                 [Test]
910                 public void TestParentEngine ()
911                 {
912                         Engine engine;
913                         Project project;
914                         
915                         engine = new Engine (Consts.BinPath);
916                         project = engine.CreateNewProject ();
917
918                         Assert.AreEqual (engine, project.ParentEngine, "A1");
919                 }
920
921                 [Test]
922                 [Category ("NotDotNet")]
923                 [ExpectedException (typeof (ArgumentNullException))]
924                 public void TestRemoveItemGroup1 ()
925                 {
926                         Engine engine;
927                         Project p1;
928
929                         engine = new Engine (Consts.BinPath);
930                         p1 = engine.CreateNewProject ();
931
932                         p1.RemoveItemGroup (null);
933                 }
934
935                 // The "BuildItemGroup" object specified does not belong to the correct "Project" object.
936                 [Test]
937                 [ExpectedException (typeof (InvalidOperationException))]
938                 [Category ("NotWorking")]
939                 public void TestRemoveItemGroup2 ()
940                 {
941                         Engine engine;
942                         Project p1;
943                         Project p2;
944                         BuildItemGroup [] groups  = new BuildItemGroup [1];
945
946                         engine = new Engine (Consts.BinPath);
947                         p1 = engine.CreateNewProject ();
948                         p2 = engine.CreateNewProject ();
949
950                         p1.AddNewItem ("A", "B");
951                         p1.ItemGroups.CopyTo (groups, 0);
952
953                         p2.RemoveItemGroup (groups [0]);
954                 }
955
956                 [Test]
957                 [Category ("NotDotNet")]
958                 [ExpectedException (typeof (ArgumentNullException))]
959                 public void TestRemoveItem1 ()
960                 {
961                         Engine engine;
962                         Project project;
963
964                         engine = new Engine (Consts.BinPath);
965                         project = engine.CreateNewProject ();
966
967                         project.RemoveItem (null);
968                 }
969
970                 // The object passed in is not part of the project.
971                 [Test]
972                 [ExpectedException (typeof (InvalidOperationException))]
973                 public void TestRemoveItem2 ()
974                 {
975                         Engine engine;
976                         Project project;
977
978                         engine = new Engine (Consts.BinPath);
979                         project = engine.CreateNewProject ();
980
981                         project.RemoveItem (new BuildItem ("name", "include"));
982                 }
983
984                 // The "BuildItemGroup" object specified does not belong to the correct "Project" object.
985                 [Test]
986                 [ExpectedException (typeof (InvalidOperationException))]
987                 public void TestRemoveItem3 ()
988                 {
989                         Engine engine;
990                         Project p1;
991                         Project p2;
992
993                         engine = new Engine (Consts.BinPath);
994                         p1 = engine.CreateNewProject ();
995                         p2 = engine.CreateNewProject ();
996
997                         p1.AddNewItem ("A", "B");
998
999                         p2.RemoveItem (p1.EvaluatedItems [0]);
1000                 }
1001
1002                 [Test]
1003                 [Category ("NotDotNet")]
1004                 [ExpectedException (typeof (InvalidOperationException))]
1005                 public void TestRemoveItem4 ()
1006                 {
1007                         Engine engine;
1008                         Project p1;
1009                         Project p2;
1010
1011                         engine = new Engine (Consts.BinPath);
1012                         p1 = engine.CreateNewProject ();
1013                         p2 = engine.CreateNewProject ();
1014
1015                         p1.AddNewItem ("A", "B");
1016                         p1.AddNewItem ("A", "C");
1017
1018                         p2.RemoveItem (p1.EvaluatedItems [0]);
1019                 }
1020
1021                 [Test]
1022                 public void TestRemoveItem5 ()
1023                 {
1024                         Engine engine;
1025                         Project project;
1026                         BuildItemGroup [] groups = new BuildItemGroup [1];
1027
1028                         string documentString = @"
1029                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1030                                         <ItemGroup>
1031                                                 <A Include='a'/>
1032                                         </ItemGroup>
1033                                 </Project>
1034                         ";
1035
1036                         engine = new Engine (Consts.BinPath);
1037                         project = engine.CreateNewProject ();
1038                         project.LoadXml (documentString);
1039
1040                         project.RemoveItem (project.EvaluatedItems [0]);
1041                         Assert.AreEqual (0, project.EvaluatedItems.Count, "A1");
1042                         project.ItemGroups.CopyTo (groups, 0);
1043                         Assert.IsNull (groups [0], "A2");
1044                         Assert.AreEqual (0, project.ItemGroups.Count, "A3");
1045                 }
1046
1047                 [Test]
1048                 public void TestResetBuildStatus ()
1049                 {
1050                         Engine engine;
1051                         Project project;
1052
1053                         string documentString = @"
1054                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1055                                         <Target Name='T' Inputs='Test\resources\TestTasks.cs' Outputs='Test\resources\TestTasks.dll'>
1056                                                 <Message Text='text' />
1057                                         </Target>
1058                                 </Project>
1059                         ";
1060
1061                         engine = new Engine (Consts.BinPath);
1062                         TestLogger tl = new TestLogger ();
1063                         engine.RegisterLogger (tl);
1064                         project = engine.CreateNewProject ();
1065                         project.LoadXml (documentString);
1066
1067                         project.Build ("T");
1068                         project.ResetBuildStatus ();
1069                         project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
1070
1071                         Assert.AreEqual (2, tl.TargetStartedEvents, "A1");
1072                         Assert.AreEqual (2, tl.TargetFinishedEvents, "A1");
1073                 }
1074                 
1075                 [Test]
1076                 public void TestSchemaFile ()
1077                 {
1078                         Engine engine;
1079                         Project project;
1080                         
1081                         string documentString = @"
1082                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1083                                 </Project>
1084                         ";
1085                         
1086                         engine = new Engine (Consts.BinPath);
1087                         project = engine.CreateNewProject ();
1088                         project.LoadXml (documentString);
1089
1090                         Assert.IsNull (project.SchemaFile, "A1");
1091                 }
1092                 [Test]
1093                 [Category ("NotDotNet")]
1094                 [ExpectedException (typeof (ArgumentNullException))]
1095                 public void TestSetProjectExtensions1 ()
1096                 {
1097                         Engine engine;
1098                         Project project;
1099
1100                         string documentString = @"
1101                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1102                                 </Project>
1103                         ";
1104
1105                         engine = new Engine (Consts.BinPath);
1106                         project = engine.CreateNewProject ();
1107                         project.LoadXml (documentString);
1108
1109                         project.SetProjectExtensions (null, null);
1110                 }
1111
1112                 [Test]
1113                 public void TestSetProjectExtensions2 ()
1114                 {
1115                         Engine engine;
1116                         Project project;
1117
1118                         string documentString = @"
1119                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1120                                 </Project>
1121                         ";
1122
1123                         engine = new Engine (Consts.BinPath);
1124                         project = engine.CreateNewProject ();
1125                         project.LoadXml (documentString);
1126
1127                         project.SetProjectExtensions ("name", "1");
1128                         Assert.AreEqual ("1", project.GetProjectExtensions ("name"), "A1");
1129                         project.SetProjectExtensions ("name", "2");
1130                         Assert.AreEqual ("2", project.GetProjectExtensions ("name"), "A2");
1131                         Assert.IsTrue (project.IsDirty, "A3");
1132                 }
1133
1134                 [Test]
1135                 public void TestSetProjectExtensions3 ()
1136                 {
1137                         Engine engine;
1138                         Project project;
1139
1140                         string documentString = @"
1141                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1142                                         <ProjectExtensions>
1143                                         </ProjectExtensions>
1144                                 </Project>
1145                         ";
1146
1147                         engine = new Engine (Consts.BinPath);
1148                         project = engine.CreateNewProject ();
1149                         project.LoadXml (documentString);
1150
1151                         project.SetProjectExtensions ("name", "1");
1152                         Assert.AreEqual ("1", project.GetProjectExtensions ("name"), "A1");
1153                         Assert.IsTrue (project.IsDirty, "A2");
1154                 }
1155
1156                 [Test]
1157                 public void TestBuildProjectError1 ()
1158                 {
1159                         Engine engine = new Engine (Consts.BinPath);
1160                         Project project = engine.CreateNewProject ();
1161
1162                         Assert.IsFalse (project.Build ((string) null), "A1");
1163                         Assert.IsFalse (project.Build ((string[]) null), "A2");
1164                         Assert.IsFalse (project.Build ((string []) null, null), "A3");
1165                         Assert.IsFalse (project.Build ((string []) null, null, BuildSettings.None), "A4");
1166                 }
1167
1168                 [Test]
1169                 public void TestBuildProjectError2 ()
1170                 {
1171                         Engine engine = new Engine (Consts.BinPath);
1172                         Project project = engine.CreateNewProject ();
1173
1174                         try {
1175                                 project.Build (new string [] { null });
1176                         } catch {
1177                                 return;
1178                         }
1179                         Assert.Fail ("Expected exception for project.Build, null string in targetNames []");
1180                 }
1181
1182                 [Test]
1183                 public void TestBuildProjectFile1 ()
1184                 {
1185                         Project project = CreateAndLoadProject ("foo.proj", false, new string [] { "1", "2" }, new bool [] { true, true }, "TBPF1");
1186                         CheckProjectBuild (project, new string [] { "main" }, true, new string [] { "main" }, "TBPF1");
1187                 }
1188
1189                 [Test]
1190                 public void TestBuildProjectFileXml1 ()
1191                 {
1192                         Project project = CreateAndLoadProject (null, false, new string [] { "1", "2" }, new bool [] { true, true }, "TBPFX1");
1193                         CheckProjectBuild (project, new string [] { "main" }, true, new string [] { "main" }, "TBPFX1");
1194                 }
1195
1196                 [Test]
1197                 public void TestBuildProjectFile2 ()
1198                 {
1199                         Project project = CreateAndLoadProject ("foo.proj", false, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPF2");
1200                         CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPF2");
1201                 }
1202
1203                 [Test]
1204                 public void TestBuildProjectFileXml2 ()
1205                 {
1206                         Project project = CreateAndLoadProject (null, false, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPFX2");
1207                         CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPFX2");
1208                 }
1209
1210                 [Test]
1211                 public void TestBuildProjectFile3 ()
1212                 {
1213                         Project project = CreateAndLoadProject ("foo.proj", false, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPF3");
1214                         CheckProjectBuild (project, new string [] { "1", "2" }, true, new string [] { "1", "2" }, "TBPF3");
1215                 }
1216
1217                 [Test]
1218                 public void TestBuildProjectFileXml3 ()
1219                 {
1220                         Project project = CreateAndLoadProject (null, false, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPFX3");
1221                         CheckProjectBuild (project, new string [] { "1", "2" }, true, new string [] { "1", "2" }, "TBPFX3");
1222                 }
1223
1224                 [Test]
1225                 public void TestBuildProjectFile4 ()
1226                 {
1227                         Project project = CreateAndLoadProject ("foo.proj", false, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPF4");
1228                         CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPF4");
1229                 }
1230
1231                 [Test]
1232                 public void TestBuildProjectFileXml4 ()
1233                 {
1234                         Project project = CreateAndLoadProject (null, false, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPFX4");
1235                         CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPFX4");
1236                 }
1237
1238                 //Run separate tests
1239
1240                 //Run single target
1241                 [Test]
1242                 public void TestBuildProjectFile5 ()
1243                 {
1244                         Project project = CreateAndLoadProject ("foo.proj", true, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPF5");
1245                         CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPF5");
1246                 }
1247
1248                 [Test]
1249                 public void TestBuildProjectFileXml5 ()
1250                 {
1251                         Project project = CreateAndLoadProject (null, true, new string [] { "1", "2", "3" }, new bool [] { true, false, true }, "TBPFX5");
1252                         CheckProjectBuild (project, new string [] { "main" }, false, new string [0], "TBPFX5");
1253                 }
1254
1255                 [Test]
1256                 public void TestBuildProjectFile6 ()
1257                 {
1258                         Project project = CreateAndLoadProject ("foo.proj", true, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPF6");
1259                         CheckProjectBuild (project, new string [] { "main" }, true, new string [] { "main" }, "TBPF6");
1260                 }
1261
1262                 [Test]
1263                 public void TestBuildProjectFileXml6 ()
1264                 {
1265                         Project project = CreateAndLoadProject (null, true, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPFX6");
1266                         CheckProjectBuild (project, new string [] { "main" }, true, new string [] { "main" }, "TBPFX6");
1267                 }
1268
1269                 // run multiple targets
1270                 [Test]
1271                 public void TestBuildProjectFile7 ()
1272                 {
1273                         Project project = CreateAndLoadProject ("foo.proj", true, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPF7");
1274                         CheckProjectBuild (project, new string [] { "1", "2", "3" }, true, new string [] { "1", "2", "3" }, "TBPF7");
1275                 }
1276
1277                 [Test]
1278                 public void TestBuildProjectFileXml7 ()
1279                 {
1280                         Project project = CreateAndLoadProject (null, true, new string [] { "1", "2", "3" }, new bool [] { true, true, true }, "TBPFX7");
1281                         CheckProjectBuild (project, new string [] { "1", "2", "3" }, true, new string [] { "1", "2", "3" }, "TBPFX7");
1282                 }
1283
1284                 [Test]
1285                 public void TestBuildProjectFile8 ()
1286                 {
1287                         Project project = CreateAndLoadProject ("foo.proj", true, new string [] { "1", "2", "3" }, new bool [] { true, true, false }, "TBPF8");
1288                         CheckProjectBuild (project, new string [] { "1", "2", "3" }, false, new string [] { "1", "2"}, "TBPF8");
1289                 }
1290
1291                 [Test]
1292                 public void TestBuildProjectFileXml8 ()
1293                 {
1294                         Project project = CreateAndLoadProject (null, true, new string [] { "1", "2", "3" }, new bool [] { true, true, false }, "TBPFX8");
1295                         CheckProjectBuild (project, new string [] { "1", "2", "3" }, false, new string [] { "1", "2"}, "TBPFX8");
1296                 }
1297
1298                 [Test]
1299                 public void TestBatchedMetadataRef1 ()
1300                 {
1301                         //test for multiple items with same metadata also
1302                          string projectString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1303                         <UsingTask TaskName=""BatchingTestTask"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1304                         <ItemGroup>
1305                                 <Coll1 Include=""A1""><Name>Abc</Name></Coll1>
1306                                 <Coll1 Include=""A2""><Name>Def</Name></Coll1>
1307                                 <Coll1 Include=""A3""><Name>Xyz</Name></Coll1>
1308                                 <Coll1 Include=""A4""><Name>Xyz</Name></Coll1>
1309                                 <Coll2 Include=""B1""></Coll2>
1310                         </ItemGroup>
1311                                 <Target Name=""ShowMessage"">
1312                                         <BatchingTestTask Sources=""%(Coll1.Name)"">
1313                                                 <Output TaskParameter=""Output"" ItemName=""FinalList"" />
1314                                         </BatchingTestTask>
1315                                         <Message Text=""Msg: %(Coll1.Name)"" />
1316                                 </Target>
1317                  </Project>";
1318
1319                         Engine engine = new Engine (Consts.BinPath);
1320                         Project project = engine.CreateNewProject ();
1321
1322                         project.LoadXml (projectString);
1323                         Assert.IsTrue (project.Build ("ShowMessage"), "A1: Build failed");
1324
1325                         BuildItemGroup include = project.GetEvaluatedItemsByName ("FinalList");
1326                         Assert.AreEqual (3, include.Count, "A2");
1327
1328                         Assert.AreEqual ("FinalList", include [0].Name, "A3");
1329                         Assert.AreEqual ("Abc", include [0].FinalItemSpec, "A4");
1330
1331                         Assert.AreEqual ("FinalList", include [1].Name, "A5");
1332                         Assert.AreEqual ("Def", include [1].FinalItemSpec, "A6");
1333
1334                         Assert.AreEqual ("FinalList", include [2].Name, "A7");
1335                         Assert.AreEqual ("Xyz", include [2].FinalItemSpec, "A8");
1336                 }
1337
1338                 [Test]
1339                 public void TestBatchedMetadataRef2 ()
1340                 {
1341                         string projectString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1342                         <UsingTask TaskName=""BatchingTestTask"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1343                         <ItemGroup>
1344                                 <Coll1 Include=""A1""><Name>Abc</Name></Coll1>
1345                                 <Coll1 Include=""A2""><Name>Def</Name></Coll1>
1346                                 <Coll1 Include=""A3""><Name>Xyz</Name></Coll1>
1347                                 <Coll1 Include=""A4""><Name>Abc</Name></Coll1>
1348                                 <Coll2 Include=""B1""><Name>Bar</Name></Coll2>
1349                                 <Coll2 Include=""B2""><Name>Bar</Name></Coll2>
1350                         </ItemGroup>
1351                                 <Target Name=""ShowMessage"">
1352                                         <BatchingTestTask Sources=""%(Name)"" Strings=""@(Coll2)"">
1353                                                 <Output TaskParameter=""Output"" ItemName=""FinalList"" />
1354                                         </BatchingTestTask>
1355                                         <Message Text=""Msg: %(Coll1.Name)"" />
1356                                 </Target>
1357                                 <Target Name=""ShowMessage2"">
1358                                         <BatchingTestTask Sources=""%(Name)"" Strings=""@(Coll1)"">
1359                                                 <Output TaskParameter=""Output"" ItemName=""FinalList2"" />
1360                                         </BatchingTestTask>
1361                                         <Message Text=""Msg: %(Coll1.Name)"" />
1362                                 </Target>
1363                  </Project>";
1364
1365                         Engine engine = new Engine (Consts.BinPath);
1366                         Project project = engine.CreateNewProject ();
1367
1368                         project.LoadXml (projectString);
1369                         Assert.IsTrue (project.Build ("ShowMessage"), "A1: Build failed");
1370
1371                         BuildItemGroup include = project.GetEvaluatedItemsByName ("FinalList");
1372                         Assert.AreEqual (1, include.Count, "A2");
1373
1374                         Assert.AreEqual ("FinalList", include [0].Name, "A3");
1375                         Assert.AreEqual ("Bar", include [0].FinalItemSpec, "A4");
1376
1377                         Assert.IsTrue (project.Build ("ShowMessage2"), "A1: Build failed");
1378                         include = project.GetEvaluatedItemsByName ("FinalList2");
1379                         Assert.AreEqual (3, include.Count, "A5");
1380
1381                         Assert.AreEqual ("FinalList2", include [0].Name, "A6");
1382                         Assert.AreEqual ("Abc", include [0].FinalItemSpec, "A7");
1383
1384                         Assert.AreEqual ("FinalList2", include [1].Name, "A8");
1385                         Assert.AreEqual ("Def", include [1].FinalItemSpec, "A9");
1386
1387                         Assert.AreEqual ("FinalList2", include [2].Name, "A10");
1388                         Assert.AreEqual ("Xyz", include [2].FinalItemSpec, "A11");
1389                 }
1390
1391                 [Test]
1392                 public void TestBatchedMetadataRef3 ()
1393                 {
1394                         string projectString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1395                         <UsingTask TaskName=""BatchingTestTask"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1396                         <ItemGroup>
1397                                 <Coll1 Include=""A1""><Name>Abc</Name></Coll1>
1398                                 <Coll1 Include=""A2""><Name>Def</Name></Coll1>
1399                                 <Coll1 Include=""A3""><Name>Xyz</Name></Coll1>
1400                                 <Coll1 Include=""A4""><Name>Abc</Name></Coll1>
1401                                 <Coll2 Include=""B1""><Name>Bar</Name></Coll2>
1402                                 <Coll2 Include=""B2""><Name>Bar</Name></Coll2>
1403                         </ItemGroup>
1404                                 <Target Name=""ShowMessage"">
1405                                         <BatchingTestTask SingleTaskItem=""%(Coll2.Name)"" >
1406                                                 <Output TaskParameter=""SingleStringOutput"" ItemName=""FinalList"" />
1407                                         </BatchingTestTask>
1408                                         <Message Text=""Msg: %(Coll1.Name)"" />
1409                                 </Target>
1410                  </Project>";
1411
1412                         Engine engine = new Engine (Consts.BinPath);
1413                         Project project = engine.CreateNewProject ();
1414
1415                         project.LoadXml (projectString);
1416                         Assert.IsTrue (project.Build ("ShowMessage"), "A1: Build failed");
1417
1418                         BuildItemGroup include = project.GetEvaluatedItemsByName ("FinalList");
1419                         Assert.AreEqual (1, include.Count, "A2");
1420
1421                         Assert.AreEqual ("FinalList", include [0].Name, "A3");
1422                         Assert.AreEqual ("Bar", include [0].FinalItemSpec, "A4");
1423
1424                 }
1425
1426                 [Test]
1427                 public void TestBatchedMetadataRef4 ()
1428                 {
1429                         string projectString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1430                         <UsingTask TaskName=""BatchingTestTask"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1431                         <ItemGroup>
1432                                 <Coll1 Include=""A1""><Name>Abc</Name></Coll1>
1433                                 <Coll1 Include=""A2""><Name>Def</Name></Coll1>
1434                                 <Coll1 Include=""A3""><Name>Xyz</Name></Coll1>
1435                                 <Coll2 Include=""B1""><Name>Bar</Name></Coll2>
1436                         </ItemGroup>
1437                                 <Target Name=""ShowMessage"">
1438                                         <BatchingTestTask SingleTaskItem=""%(Coll3.Name)"" >
1439                                                 <Output TaskParameter=""SingleStringOutput"" ItemName=""FinalList"" />
1440                                         </BatchingTestTask>
1441                                 </Target>
1442                  </Project>";
1443
1444                         Engine engine = new Engine (Consts.BinPath);
1445                         Project project = engine.CreateNewProject ();
1446
1447                         project.LoadXml (projectString);
1448                         Assert.IsTrue (project.Build ("ShowMessage"), "A1: Build failed");
1449
1450                         BuildItemGroup include = project.GetEvaluatedItemsByName ("FinalList");
1451                         Assert.AreEqual (0, include.Count, "A2");
1452                 }
1453
1454                 [Test]
1455                 public void TestBatchedMetadataRef5 ()
1456                 {
1457                         string projectString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1458                         <UsingTask TaskName=""BatchingTestTask"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1459                         <ItemGroup>
1460                                 <Coll1 Include=""A1""><Name>Abc</Name></Coll1>
1461                                 <Coll1 Include=""A2""><Name>Def</Name></Coll1>
1462                                 <Coll1 Include=""A3""><Name>Xyz</Name></Coll1>
1463                                 <Coll2 Include=""B1""><Name>Bar</Name></Coll2>
1464                         </ItemGroup>
1465                                 <Target Name=""ShowMessage"">
1466                                         <Message Text=""Coll1: @(Coll1->'Foo%(Name)Bar')"" />
1467                                         <BatchingTestTask Sources=""@(Coll1->'Foo%(Name)Bar')"" >
1468                                                 <Output TaskParameter=""Output"" ItemName=""FinalList"" />
1469                                         </BatchingTestTask>
1470                                 </Target>
1471                  </Project>";
1472
1473                         Engine engine = new Engine (Consts.BinPath);
1474                         Project project = engine.CreateNewProject ();
1475                         MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
1476                                 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
1477                         engine.RegisterLogger (logger);
1478
1479                         project.LoadXml (projectString);
1480                         bool result = project.Build ("ShowMessage");
1481                         if (!result) {
1482                                 logger.DumpMessages ();
1483                                 Assert.Fail ("A1: Build failed");
1484                         }
1485                         logger.DumpMessages ();
1486                         BuildItemGroup include = project.GetEvaluatedItemsByName ("FinalList");
1487                         Assert.AreEqual (3, include.Count, "A2");
1488                 }
1489
1490                 [Test]
1491                 public void TestInitialTargets ()
1492                 {
1493                         Engine engine = new Engine (Consts.BinPath);
1494                         Project project = engine.CreateNewProject ();
1495
1496                         project.LoadXml (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" InitialTargets=""pre  "">
1497                                 <Target Name=""boo"">
1498                                         <Message Text=""Executing boo target""/>
1499                                 </Target>
1500                                 <Target Name=""pre"">
1501                                         <Message Text=""Executing pre target""/>
1502                                 </Target>
1503                         </Project>");
1504
1505                         MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
1506                                 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
1507                         engine.RegisterLogger (logger);
1508
1509                         try {
1510                                 Assert.IsTrue (project.Build (), "Build failed");
1511
1512                                 Assert.AreEqual (0, logger.CheckHead ("Executing pre target", MessageImportance.Normal), "A1");
1513                                 Assert.AreEqual (0, logger.CheckHead ("Executing boo target", MessageImportance.Normal), "A2");
1514                                 Assert.AreEqual (0, logger.Count, "A3");
1515                         } catch {
1516                                 logger.DumpMessages ();
1517                                 throw;
1518                         }
1519                 }
1520
1521                 [Test]
1522                 public void TestRequiredTask_String1 ()
1523                 {
1524                         CheckProjectForRequiredTests ("RequiredTestTask_String", "@(NonExistant)",
1525                                 false, "Should've failed: No value specified for required field - 'Property' of RequiredTestTask_String");
1526                 }
1527
1528                 [Test]
1529                 public void TestRequiredTask_String2 ()
1530                 {
1531                         CheckProjectForRequiredTests ("RequiredTestTask_String", "$(NonExistant)",
1532                                 false, "Should've failed: No value specified for required field - 'Property' of RequiredTestTask_String");
1533                 }
1534
1535                 [Test]
1536                 public void TestRequiredTask_TaskItem1 ()
1537                 {
1538                         Project p = CheckProjectForRequiredTests ("RequiredTestTask_TaskItem", "@(NonExistant)",
1539                                 false, "Should've failed: No value specified for required field - 'Property' of RequiredTestTask_TaskItem");
1540                 }
1541
1542                 [Test]
1543                 public void TestRequiredTask_TaskItem2 ()
1544                 {
1545                         Project p = CheckProjectForRequiredTests ("RequiredTestTask_TaskItem", "$(NonExistant)",
1546                                 false, "Should've failed: No value specified for required field - 'Property' of RequiredTestTask_TaskItem");
1547                 }
1548
1549                 [Test]
1550                 public void TestRequiredTask_TaskItemArray1 ()
1551                 {
1552                         Project p = CheckProjectForRequiredTests ("RequiredTestTask_TaskItems", "@(NonExistant)",
1553                                 true, "Build failed");
1554
1555                         BuildItemGroup group = p.GetEvaluatedItemsByName ("OutItem");
1556                         Assert.AreEqual (1, group.Count, "A2");
1557                         Assert.AreEqual ("count: 0", group [0].FinalItemSpec, "A3");
1558                 }
1559
1560                 [Test]
1561                 public void TestRequiredTask_TaskItemArray2 ()
1562                 {
1563                         Project p = CheckProjectForRequiredTests ("RequiredTestTask_TaskItems", "$(NonExistant)",
1564                                 true, "Build failed");
1565
1566                         BuildItemGroup group = p.GetEvaluatedItemsByName ("OutItem");
1567                         Assert.AreEqual (1, group.Count, "A2");
1568                         Assert.AreEqual ("count: 0", group [0].FinalItemSpec, "A3");
1569                 }
1570
1571                 [Test]
1572                 public void TestRequiredTask_TaskItemArray3 ()
1573                 {
1574                         Project p = CheckProjectForRequiredTests ("RequiredTestTask_IntArray", "$(NonExistant)",
1575                                 true, "Build failed");
1576
1577                         BuildItemGroup group = p.GetEvaluatedItemsByName ("OutItem");
1578                         Assert.AreEqual (1, group.Count, "A2");
1579                         Assert.AreEqual ("count: 0", group [0].FinalItemSpec, "A3");
1580                 }
1581
1582
1583                 Project CheckProjectForRequiredTests (string taskname, string property_arg, bool expected_result, string error_msg)
1584                 {
1585                         string projectString = String.Format (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
1586                                 <UsingTask TaskName=""{0}"" AssemblyFile=""Test/resources/TestTasks.dll"" />
1587                                 <Target Name=""foo"">
1588                                         <{0} Property=""{1}"">
1589                                                 <Output TaskParameter=""Output"" ItemName=""OutItem""/>
1590                                         </{0}>
1591                                 </Target>
1592                         </Project>", taskname, property_arg);
1593
1594                         Engine engine = new Engine (Consts.BinPath);
1595                         MonoTests.Microsoft.Build.Tasks.TestMessageLogger logger =
1596                                 new MonoTests.Microsoft.Build.Tasks.TestMessageLogger ();
1597                         engine.RegisterLogger (logger);
1598                         Project project = engine.CreateNewProject ();
1599                         project.LoadXml (projectString);
1600
1601                         try {
1602                                 Assert.AreEqual (expected_result, project.Build (), error_msg);
1603                         } finally {
1604                                 logger.DumpMessages ();
1605                         }
1606                         return project;
1607                 }
1608
1609                 static void CheckBuildItem (BuildItem item, string name, string [,] metadata, string finalItemSpec, string prefix)
1610                 {
1611                         Assert.AreEqual (name, item.Name, prefix + "#1");
1612                         for (int i = 0; i < metadata.GetLength (0); i++) {
1613                                 string key = metadata [i, 0];
1614                                 string val = metadata [i, 1];
1615                                 Assert.IsTrue (item.HasMetadata (key), String.Format ("{0}#2: Expected metadata '{1}' not found", prefix, key));
1616                                 Assert.AreEqual (val, item.GetMetadata (key), String.Format ("{0}#3: Value for metadata {1}", prefix, key));
1617                                 Assert.AreEqual (val, item.GetEvaluatedMetadata (key), String.Format ("{0}#4: Value for evaluated metadata {1}", prefix, key));
1618                         }
1619                         Assert.AreEqual (finalItemSpec, item.FinalItemSpec, prefix + "#5");
1620                 }
1621
1622                 void CheckProjectBuild (Project project, string [] targetNames, bool result, string [] outputNames, string prefix)
1623                 {
1624                         IDictionary targetOutputs = new Hashtable ();
1625
1626                         Assert.AreEqual (result, project.Build (targetNames, targetOutputs), prefix + "A1");
1627                         Assert.AreEqual (outputNames.Length, targetOutputs.Keys.Count, prefix + "A2");
1628
1629                         foreach (string outputName in outputNames) {
1630                                 Assert.IsTrue (targetOutputs.Contains (outputName), prefix + " A3: target " + outputName);
1631
1632                                 object o = targetOutputs [outputName];
1633                                 Assert.IsTrue (typeof (ITaskItem []).IsAssignableFrom (o.GetType ()), prefix + " A4: target " + outputName);
1634
1635                                 ITaskItem [] items = (ITaskItem [])o;
1636                                 Assert.AreEqual (0, items.Length, prefix + "A5: target " + outputName);
1637                         }
1638                 }
1639
1640                 string CreateProjectString (bool run_separate, string [] targets, bool [] results, string prefix)
1641                 {
1642                         StringBuilder sb = new StringBuilder ();
1643                         sb.Append (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">");
1644                         sb.AppendFormat ("<Target Name = \"{0}\"><Message Text = \"#Target {1}:{0} called\" />", "main", prefix);
1645
1646                         sb.AppendFormat ("<CallTarget Targets=\"");
1647                         for (int i = 0; i < targets.Length; i++)
1648                                 sb.AppendFormat ("{0};", targets [i]);
1649                         sb.AppendFormat ("\" ");
1650
1651                         if (run_separate)
1652                                 sb.AppendFormat (" RunEachTargetSeparately=\"true\" ");
1653                         sb.AppendFormat ("/></Target>\n");
1654
1655                         for (int i = 0; i < targets.Length; i++) {
1656                                 sb.AppendFormat ("<Target Name = \"{0}\"><Message Text = \"#Target {1}:{0} called\" />", targets [i], prefix);
1657                                 if (!results [i])
1658                                         sb.AppendFormat ("<Error Text = \"#Error message for target {0}:{1}\"/>", prefix, targets [i]);
1659                                 sb.Append ("</Target>\n");
1660                         }
1661
1662                         sb.Append ("</Project>");
1663
1664                         return sb.ToString ();
1665                 }
1666
1667                 void CreateProjectFile (string fname, bool run_separate, string [] targets, bool [] results, string prefix)
1668                 {
1669                         using (StreamWriter sw = new StreamWriter (fname))
1670                                 sw.Write (CreateProjectString (run_separate, targets, results, prefix));
1671                 }
1672
1673                 Project CreateAndLoadProject (string fname, bool run_separate, string [] targets, bool [] results, string prefix)
1674                 {
1675                         Engine engine = new Engine (Consts.BinPath);
1676                         Project project = engine.CreateNewProject ();
1677
1678                         string projectXml = CreateProjectString (run_separate, targets, results, prefix);
1679                         if (fname == null) {
1680                                 project.LoadXml (projectXml);
1681                         } else {
1682                                 using (StreamWriter sw = new StreamWriter (fname))
1683                                         sw.Write (projectXml);
1684                                 project.Load (fname);
1685                                 File.Delete (fname);
1686                         }
1687
1688                         return project;
1689                 }
1690         }
1691 }