5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2006 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections;
30 using Microsoft.Build.BuildEngine;
31 using Microsoft.Build.Framework;
32 using Microsoft.Build.Utilities;
33 using NUnit.Framework;
35 namespace MonoTests.Microsoft.Build.BuildEngine {
37 public class TargetTest {
43 public void TestFromXml1 ()
45 string documentString = @"
46 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
47 <Target Name='Target'>
52 engine = new Engine (Consts.BinPath);
54 project = engine.CreateNewProject ();
55 project.LoadXml (documentString);
57 Target[] t = new Target [1];
58 project.Targets.CopyTo (t, 0);
60 Assert.AreEqual (String.Empty, t [0].Condition, "A1");
61 Assert.AreEqual (String.Empty, t [0].DependsOnTargets, "A2");
62 Assert.IsFalse (t [0].IsImported, "A3");
63 Assert.AreEqual ("Target", t [0].Name, "A4");
67 public void TestFromXml2 ()
69 string documentString = @"
70 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
71 <Target Name='Target' Condition='false' DependsOnTargets='X' >
76 engine = new Engine (Consts.BinPath);
78 project = engine.CreateNewProject ();
79 project.LoadXml (documentString);
81 Target[] t = new Target [1];
82 project.Targets.CopyTo (t, 0);
84 Assert.AreEqual ("false", t [0].Condition, "A1");
85 Assert.AreEqual ("X", t [0].DependsOnTargets, "A2");
87 t [0].Condition = "true";
88 t [0].DependsOnTargets = "A;B";
90 Assert.AreEqual ("true", t [0].Condition, "A3");
91 Assert.AreEqual ("A;B", t [0].DependsOnTargets, "A4");
95 public void TestAddNewTask1 ()
97 string documentString = @"
98 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
99 <Target Name='Target' >
104 engine = new Engine (Consts.BinPath);
106 project = engine.CreateNewProject ();
107 project.LoadXml (documentString);
109 Target[] t = new Target [1];
110 project.Targets.CopyTo (t, 0);
112 BuildTask bt = t [0].AddNewTask ("Message");
114 Assert.AreEqual ("Message", bt.Name, "A1");
118 [ExpectedException (typeof (ArgumentNullException))]
119 public void TestAddNewTask2 ()
121 string documentString = @"
122 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
123 <Target Name='Target' >
128 engine = new Engine (Consts.BinPath);
130 project = engine.CreateNewProject ();
131 project.LoadXml (documentString);
133 Target[] t = new Target [1];
134 project.Targets.CopyTo (t, 0);
136 t [0].AddNewTask (null);
140 public void TestGetEnumerator ()
142 string documentString = @"
143 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
144 <Target Name='Target' >
145 <Message Text='text' />
146 <Warning Text='text' />
151 engine = new Engine (Consts.BinPath);
153 project = engine.CreateNewProject ();
154 project.LoadXml (documentString);
156 Target[] t = new Target [1];
157 project.Targets.CopyTo (t, 0);
159 IEnumerator e = t [0].GetEnumerator ();
161 Assert.AreEqual ("Message", ((BuildTask) e.Current).Name, "A1");
163 Assert.AreEqual ("Warning", ((BuildTask) e.Current).Name, "A2");
164 Assert.IsFalse (e.MoveNext (), "A3");
168 [ExpectedException (typeof (InvalidProjectFileException))]
169 public void TestOnError1 ()
174 string documentString = @"
175 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
177 <OnError ExecuteTargets='B' />
178 <Error Text='text' />
183 engine = new Engine (Consts.BinPath);
184 project = engine.CreateNewProject ();
185 project.LoadXml (documentString);
189 public void TestRemoveTask1 ()
191 string documentString = @"
192 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
193 <Target Name='Target' >
194 <Message Text='text' />
195 <Warning Text='text' />
200 engine = new Engine (Consts.BinPath);
202 project = engine.CreateNewProject ();
203 project.LoadXml (documentString);
205 Target[] t = new Target [1];
206 project.Targets.CopyTo (t, 0);
208 IEnumerator e = t [0].GetEnumerator ();
210 t [0].RemoveTask ((BuildTask) e.Current);
211 e = t [0].GetEnumerator ();
213 Assert.AreEqual ("Warning", ((BuildTask) e.Current).Name, "A1");
214 Assert.IsFalse (e.MoveNext (), "A2");
218 [ExpectedException (typeof (ArgumentNullException))]
219 public void TestRemoveTask2 ()
221 string documentString = @"
222 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
223 <Target Name='Target' >
228 engine = new Engine (Consts.BinPath);
230 project = engine.CreateNewProject ();
231 project.LoadXml (documentString);
233 Target[] t = new Target [1];
234 project.Targets.CopyTo (t, 0);
235 t [0].RemoveTask (null);