2007-01-12 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / Microsoft.Build.BuildEngine / BuildItemGroupTest.cs
index 95d0c41d6198242f1bf0dc0926666bd902f23c52..b5d859e85cdf22d54e5d7377eeb6d3bcffeb0add 100644 (file)
@@ -27,6 +27,7 @@
 
 using System;
 using System.Collections;
+using System.Xml;
 using Microsoft.Build.BuildEngine;
 using Microsoft.Build.Framework;
 using Microsoft.Build.Utilities;
@@ -35,7 +36,6 @@ using NUnit.Framework;
 namespace MonoTests.Microsoft.Build.BuildEngine {
        [TestFixture]
        public class BuildItemGroupTest {
-
                [Test]
                public void TestCtor ()
                {
@@ -65,7 +65,6 @@ namespace MonoTests.Microsoft.Build.BuildEngine {
                }
 
                [Test]
-               [Category ("NotWorking")]
                public void TestAddNewItem2 ()
                {
                        Engine engine;
@@ -105,7 +104,90 @@ namespace MonoTests.Microsoft.Build.BuildEngine {
                }
 
                [Test]
-               public void TestClear ()
+               public void TestAddNewItem3 ()
+               {
+                       Engine engine;
+                       Project project;
+                       string name = "name";
+                       string include = "$(Property)";
+
+                       string documentString = @"
+                               <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
+                                       <PropertyGroup>
+                                               <Property>a</Property>
+                                       </PropertyGroup>
+                               </Project>
+                       ";
+
+                       engine = new Engine (Consts.BinPath);
+                       project = engine.CreateNewProject ();
+                       project.LoadXml (documentString);
+
+                       BuildItem bi = project.EvaluatedItems.AddNewItem (name, include, true);
+
+                       Assert.AreEqual (String.Empty, bi.Condition, "A1");
+                       Assert.AreEqual (String.Empty, bi.Exclude, "A2");
+                       Assert.AreEqual (include, bi.FinalItemSpec, "A3");
+                       Assert.AreEqual (Utilities.Escape (include), bi.Include, "A4");
+                       Assert.IsFalse (bi.IsImported, "A5");
+                       Assert.AreEqual (name, bi.Name, "A6");
+
+                       bi = project.EvaluatedItems.AddNewItem (name, include, false);
+
+                       Assert.AreEqual (String.Empty, bi.Condition, "A7");
+                       Assert.AreEqual (String.Empty, bi.Exclude, "A8");
+                       Assert.AreEqual (include, bi.FinalItemSpec, "A9");
+                       Assert.AreEqual (include, bi.Include, "A10");
+                       Assert.IsFalse (bi.IsImported, "A11");
+                       Assert.AreEqual (name, bi.Name, "A12");
+
+                       Assert.AreEqual (0, project.EvaluatedItems.Count, "A13");
+
+                       project.GlobalProperties.SetProperty ("a", "b");
+
+                       Assert.AreEqual (0, project.EvaluatedItems.Count, "A14");
+               }
+
+               [Test]
+               public void TestAddNewItem4 ()
+               {
+                       Engine engine;
+                       Project project;
+
+                       string documentString = @"
+                               <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
+                               </Project>
+                       ";
+
+                       engine = new Engine (Consts.BinPath);
+                       project = engine.CreateNewProject ();
+                       project.LoadXml (documentString);
+
+                       project.EvaluatedItems.AddNewItem ("I1", "Value");
+
+                       BuildItem bi = project.EvaluatedItems.AddNewItem ("I2", "@(I1)");
+
+                       Assert.AreEqual ("@(I1)", bi.FinalItemSpec, "A1");
+                       Assert.AreEqual ("@(I1)", bi.Include, "A2");
+                       Assert.AreEqual (0, project.EvaluatedItems.Count, "A3");
+               }
+
+               [Test]
+               public void TestAddNewItem5 ()
+               {
+                       BuildItemGroup big = new BuildItemGroup ();
+
+                       big.AddNewItem ("I1", "Value");
+
+                       BuildItem bi = big.AddNewItem ("I2", "@(I1)");
+
+                       Assert.AreEqual ("@(I1)", bi.FinalItemSpec, "A1");
+                       Assert.AreEqual ("@(I1)", bi.Include, "A2");
+                       Assert.AreEqual (2, big.Count, "A3");
+               }
+
+               [Test]
+               public void TestClear1 ()
                {
                        BuildItemGroup big = new BuildItemGroup ();
                        big.AddNewItem ("a", "a");
@@ -118,6 +200,40 @@ namespace MonoTests.Microsoft.Build.BuildEngine {
                        Assert.AreEqual (0, big.Count, "A2");
                }
 
+               [Test]
+               public void TestClear2 ()
+               {
+                       Engine engine;
+                       Project project;
+                       XmlDocument xd;
+                       XmlNode node;
+                       string documentString = @"
+                               <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
+                                       <ItemGroup>
+                                               <Item Include='a' />
+                                       </ItemGroup>
+                               </Project>
+                       ";
+
+                       engine = new Engine (Consts.BinPath);
+                       project = engine.CreateNewProject ();
+                       project.LoadXml (documentString);
+
+                       xd = new XmlDocument ();
+                       xd.LoadXml (project.Xml);
+                       node = xd.SelectSingleNode ("tns:Project/tns:ItemGroup/*", TestNamespaceManager.NamespaceManager);
+                       Assert.IsNotNull (node, "A1");
+
+                       BuildItemGroup [] big = new BuildItemGroup [1];
+                       project.ItemGroups.CopyTo (big, 0);
+                       big [0].Clear ();
+
+                       Assert.AreEqual (0, big [0].Count, "A2");
+                       xd = new XmlDocument ();
+                       xd.LoadXml (project.Xml);
+                       node = xd.SelectSingleNode ("tns:Project/tns:ItemGroup/*", TestNamespaceManager.NamespaceManager);
+                       Assert.IsNull (node, "A3");
+               }
                [Test]
                [Category ("NotWorking")]
                public void TestClone1 ()
@@ -299,6 +415,67 @@ namespace MonoTests.Microsoft.Build.BuildEngine {
                        Assert.AreEqual ("Item", items [0].Name, "A7");
                }
 
+               [Test]
+               public void TestCondition1 ()
+               {
+                       Engine engine;
+                       Project project;
+
+                       string documentString = @"
+                               <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
+                                       <ItemGroup Condition='true' >
+                                               <Item Include='a' />
+                                       </ItemGroup>
+                               </Project>
+                       ";
+
+                       engine = new Engine (Consts.BinPath);
+                       project = engine.CreateNewProject ();
+                       project.LoadXml (documentString);
+
+                       BuildItemGroup [] groups = new BuildItemGroup [1];
+                       project.ItemGroups.CopyTo (groups, 0);
+
+                       Assert.AreEqual ("true", groups [0].Condition, "A1");
+                       Assert.IsFalse (groups [0].IsImported, "A2");
+               }
+
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException),
+                       "Cannot set a condition on an object not represented by an XML element in the project file.")]
+               public void TestCondition2 ()
+               {
+                       BuildItemGroup big = new BuildItemGroup ();
+
+                       big.Condition = "true";
+               }
+
+               [Test]
+               public void TestCondition3 ()
+               {
+                       Engine engine;
+                       Project project;
+
+                       string documentString = @"
+                               <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
+                                       <ItemGroup Condition='true' >
+                                       </ItemGroup>
+                               </Project>
+                       ";
+
+                       engine = new Engine (Consts.BinPath);
+                       project = engine.CreateNewProject ();
+                       project.LoadXml (documentString);
+
+                       BuildItemGroup [] groups = new BuildItemGroup [1];
+                       project.ItemGroups.CopyTo (groups, 0);
+
+                       Assert.AreEqual ("true", groups [0].Condition, "A1");
+                       groups [0].Condition = "false";
+
+                       Assert.AreEqual ("false", groups [0].Condition, "A2");
+               }
+
                [Test]
                public void TestGetEnumerator ()
                {
@@ -431,40 +608,5 @@ namespace MonoTests.Microsoft.Build.BuildEngine {
                        BuildItemGroup big = new BuildItemGroup ();
                        Assert.IsNotNull (big [-1], "A1");
                }
-
-               [Test]
-               public void TestCondition1 ()
-               {
-                       Engine engine;
-                       Project project;
-                       
-                       string documentString = @"
-                               <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
-                                       <ItemGroup Condition='true' >
-                                               <Item Include='a' />
-                                       </ItemGroup>
-                               </Project>
-                       ";
-
-                       engine = new Engine (Consts.BinPath);
-                       project = engine.CreateNewProject ();
-                       project.LoadXml (documentString);
-
-                       BuildItemGroup[] groups = new BuildItemGroup [1];
-                       project.ItemGroups.CopyTo (groups, 0);
-                       
-                       Assert.AreEqual ("true", groups [0].Condition, "A1");
-                       Assert.IsFalse (groups [0].IsImported, "A2");
-               }
-
-               [Test]
-               [ExpectedException (typeof (InvalidOperationException),
-                       "Cannot set a condition on an object not represented by an XML element in the project file.")]
-               public void TestCondition2 ()
-               {
-                       BuildItemGroup big = new BuildItemGroup ();
-
-                       big.Condition = "true";
-               }
        }
 }