Merge pull request #444 from knocte/xbuild_improvements
authorAnkit Jain <radical@corewars.org>
Mon, 3 Sep 2012 11:28:32 +0000 (04:28 -0700)
committerAnkit Jain <radical@corewars.org>
Mon, 3 Sep 2012 11:28:32 +0000 (04:28 -0700)
MSBuild.Engine: use a buildtask enumerator that throws exceptions

mcs/class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine/Target.cs
mcs/class/Microsoft.Build.Engine/Test/Microsoft.Build.BuildEngine/TargetTest.cs

index 92e369b888b5ba56542aaa6c14f89511f044f252..4ce46a2247baed332c129e451d0c44f1b1c74648 100644 (file)
@@ -82,7 +82,7 @@ namespace Microsoft.Build.BuildEngine {
                                }
                        }
                }
-               
+
                [MonoTODO]
                public BuildTask AddNewTask (string taskName)
                {
@@ -99,8 +99,7 @@ namespace Microsoft.Build.BuildEngine {
 
                public IEnumerator GetEnumerator ()
                {
-                       foreach (BuildTask bt in buildTasks)
-                               yield return bt;
+                       return buildTasks.ToArray ().GetEnumerator ();
                }
 
                // FIXME: shouldn't we remove it from XML?
index ae0f18e78c914a7692c55064c16fb46d162fb7da..9f70f291f117497844b987337391dc2837827e6a 100644 (file)
@@ -1,10 +1,12 @@
 //
 // TargetTest.cs
 //
-// Author:
+// Authors:
 //   Marek Sieradzki (marek.sieradzki@gmail.com)
+//   Andres G. Aragoneses (knocte@gmail.com)
 //
 // (C) 2006 Marek Sieradzki
+// (C) 2012 Andres G. Aragoneses
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -165,6 +167,51 @@ namespace MonoTests.Microsoft.Build.BuildEngine {
                        Assert.IsFalse (e.MoveNext (), "A3");
                }
 
+               [Test]
+               public void TestOutOfRangeElementsOfTheEnumerator()
+               {
+                       string documentString =
+                               @"
+                               <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
+                                       <Target Name='A'>
+                                               <Message Text='text' />
+                                       </Target>
+                               </Project>";
+
+                       engine = new Engine (Consts.BinPath);
+
+                       project = engine.CreateNewProject ();
+                       project.LoadXml (documentString);
+
+                       Assert.IsFalse (project.Targets == null, "A1");
+                       Assert.AreEqual (1, project.Targets.Count, "A2");
+
+                       Target target = project.Targets ["A"];
+                       Assert.IsFalse (target == null, "A3");
+
+                       IEnumerator e = target.GetEnumerator ();
+
+                       bool thrown = false;
+                       try {
+                               var name = ((BuildTask)e.Current).Name;
+                       } catch (InvalidOperationException) { // "Enumeration has not started. Call MoveNext"
+                               thrown = true;
+                       }
+                       if (!thrown)
+                               Assert.Fail ("A4: Should have thrown IOE");
+
+
+                       Assert.AreEqual (true, e.MoveNext (), "A5");
+                       Assert.AreEqual ("Message", ((BuildTask)e.Current).Name, "A6");
+                       Assert.AreEqual (false, e.MoveNext (), "A7");
+                       try {
+                               var name = ((BuildTask) e.Current).Name;
+                       } catch (InvalidOperationException) { //"Enumeration already finished."
+                               return;
+                       }
+                       Assert.Fail ("A8: Should have thrown IOE, because there's only one buidTask");
+               }
+
                [Test]
                [ExpectedException (typeof (InvalidProjectFileException))]
                public void TestOnError1 ()