5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 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 System
.Collections
.Generic
;
31 using Microsoft
.Build
.BuildEngine
;
32 using Microsoft
.Build
.Framework
;
33 using Microsoft
.Build
.Utilities
;
34 using NUnit
.Framework
;
36 namespace MonoTests
.Microsoft
.Build
.BuildEngine
{
38 public class BuildTaskTest
{
40 static BuildTask
[] GetTasks (Target t
)
42 List
<BuildTask
> list
= new List
<BuildTask
> ();
43 foreach (BuildTask bt
in t
)
45 return list
.ToArray ();
49 public void TestFromXml ()
54 string documentString
= @"
55 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
57 <Property>true</Property>
60 <Message Text='Text' />
61 <Message Text='Text' Condition='$(Property)' ContinueOnError='$(Property)' />
66 engine
= new Engine (Consts
.BinPath
);
67 project
= engine
.CreateNewProject ();
68 project
.LoadXml (documentString
);
70 Target
[] t
= new Target
[1];
72 project
.Targets
.CopyTo (t
, 0);
73 bt
= GetTasks (t
[0]);
75 Assert
.AreEqual (String
.Empty
, bt
[0].Condition
, "A1");
76 Assert
.IsFalse (bt
[0].ContinueOnError
, "A2");
77 Assert
.IsNull (bt
[0].HostObject
, "A3");
78 Assert
.AreEqual ("Message", bt
[0].Name
, "A4");
79 Assert
.IsNotNull (bt
[0].Type
, "A5");
81 Assert
.AreEqual ("$(Property)", bt
[1].Condition
, "A6");
82 Assert
.IsTrue (bt
[1].ContinueOnError
, "A7");
83 Assert
.IsNull (bt
[1].HostObject
, "A8");
84 Assert
.AreEqual ("Message", bt
[0].Name
, "A9");
85 Assert
.IsNotNull (bt
[0].Type
, "A10");
89 public void TestGetParameterNames ()
94 string documentString
= @"
95 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
97 <Message Text='Text' Importance='low' Condition='true' ContinueOnError='true' />
102 engine
= new Engine (Consts
.BinPath
);
103 project
= engine
.CreateNewProject ();
104 project
.LoadXml (documentString
);
106 Target
[] t
= new Target
[1];
108 project
.Targets
.CopyTo (t
, 0);
109 bt
= GetTasks (t
[0]);
111 string[] names
= bt
[0].GetParameterNames ();
113 Assert
.AreEqual (2, names
.Length
, "A1");
114 Assert
.AreEqual ("Text", names
[0], "A2");
115 Assert
.AreEqual ("Importance", names
[1], "A3");
119 public void TestGetParameterValue1 ()
124 string documentString
= @"
125 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
127 <Message Text='$(A)' Condition='true' />
132 engine
= new Engine (Consts
.BinPath
);
133 project
= engine
.CreateNewProject ();
134 project
.LoadXml (documentString
);
136 Target
[] t
= new Target
[1];
138 project
.Targets
.CopyTo (t
, 0);
139 bt
= GetTasks (t
[0]);
141 Assert
.AreEqual (String
.Empty
, bt
[0].GetParameterValue ("text"), "A1");
142 Assert
.AreEqual (String
.Empty
, bt
[0].GetParameterValue (null), "A2");
143 Assert
.AreEqual ("$(A)", bt
[0].GetParameterValue ("Text"), "A3");
147 public void TestGetParameterValue2 ()
152 string documentString
= @"
153 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
160 engine
= new Engine (Consts
.BinPath
);
161 project
= engine
.CreateNewProject ();
162 project
.LoadXml (documentString
);
164 Target
[] t
= new Target
[1];
166 project
.Targets
.CopyTo (t
, 0);
167 bt
= GetTasks (t
[0]);
170 bool exception
= false;
173 bt
[0].GetParameterValue ("Condition");
174 } catch (ArgumentException
) {
178 Assert
.IsTrue (exception
, "A1");
181 bool exception
= false;
184 bt
[0].GetParameterValue ("ContinueOnError");
185 } catch (ArgumentException
) {
189 Assert
.IsTrue (exception
, "A2");
194 public void TestSetParameterValue1 ()
199 string documentString
= @"
200 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
207 engine
= new Engine (Consts
.BinPath
);
208 project
= engine
.CreateNewProject ();
209 project
.LoadXml (documentString
);
211 Target
[] t
= new Target
[1];
213 project
.Targets
.CopyTo (t
, 0);
214 bt
= GetTasks (t
[0]);
216 bt
[0].SetParameterValue ("Text", "Value");
217 Assert
.AreEqual ("Value", bt
[0].GetParameterValue ("Text"), "A1");
218 bt
[0].SetParameterValue ("text", "Value");
219 Assert
.AreEqual ("Value", bt
[0].GetParameterValue ("text"), "A2");
220 bt
[0].SetParameterValue ("something", "Value");
221 Assert
.AreEqual ("Value", bt
[0].GetParameterValue ("something"), "A3");
222 bt
[0].SetParameterValue ("Text", "$(A)");
223 Assert
.AreEqual ("$(A)", bt
[0].GetParameterValue ("Text"), "A4");
227 public void TestSetParameterValue2 ()
232 string documentString
= @"
233 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
240 engine
= new Engine (Consts
.BinPath
);
241 project
= engine
.CreateNewProject ();
242 project
.LoadXml (documentString
);
244 Target
[] t
= new Target
[1];
246 project
.Targets
.CopyTo (t
, 0);
247 bt
= GetTasks (t
[0]);
249 bt
[0].SetParameterValue ("Text", "$(A)", true);
250 Assert
.AreEqual (Utilities
.Escape ("$(A)"), bt
[0].GetParameterValue ("Text"), "A1");
251 bt
[0].SetParameterValue ("Text", "$(A)", false);
252 Assert
.AreEqual ("$(A)", bt
[0].GetParameterValue ("Text"), "A2");
256 public void TestProperties ()
261 string documentString
= @"
262 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
269 engine
= new Engine (Consts
.BinPath
);
270 project
= engine
.CreateNewProject ();
271 project
.LoadXml (documentString
);
273 Target
[] t
= new Target
[1];
275 project
.Targets
.CopyTo (t
, 0);
276 bt
= GetTasks (t
[0]);
278 bt
[0].Condition
= null;
279 Assert
.AreEqual (String
.Empty
, bt
[0].Condition
, "A1");
280 bt
[0].Condition
= "something";
281 Assert
.AreEqual ("something", bt
[0].Condition
, "A2");
283 bt
[0].ContinueOnError
= true;
284 Assert
.IsTrue (bt
[0].ContinueOnError
, "A3");
285 bt
[0].ContinueOnError
= false;
286 Assert
.IsFalse (bt
[0].ContinueOnError
, "A4");
290 public void TestAddOutputItem1 ()
295 string documentString
= @"
296 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
303 engine
= new Engine (Consts
.BinPath
);
304 project
= engine
.CreateNewProject ();
305 project
.LoadXml (documentString
);
307 Target
[] t
= new Target
[1];
309 project
.Targets
.CopyTo (t
, 0);
310 bt
= GetTasks (t
[0]);
312 bt
[0].AddOutputItem (null, null);
316 public void TestAddOutputItem2 ()
321 string documentString
= @"
322 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
324 AssemblyFile='Test\resources\TestTasks.dll'
325 TaskName='OutputTestTask'
333 engine
= new Engine (Consts
.BinPath
);
334 project
= engine
.CreateNewProject ();
335 project
.LoadXml (documentString
);
337 Target
[] t
= new Target
[1];
339 project
.Targets
.CopyTo (t
, 0);
340 bt
= GetTasks (t
[0]);
342 bt
[0].AddOutputItem ("Property", "ItemName");
345 Assert
.AreEqual ("ItemName", project
.EvaluatedItems
[0].Name
, "A1");
346 Assert
.AreEqual ("some_text", project
.EvaluatedItems
[0].FinalItemSpec
, "A2");
350 public void TestTaskInNamespace()
355 string documentString
= @"
356 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
358 AssemblyFile='Test\resources\TestTasks.dll'
359 TaskName='NamespacedOutputTestTask'
362 <NamespacedOutputTestTask />
367 engine
= new Engine(Consts
.BinPath
);
368 project
= engine
.CreateNewProject();
369 project
.LoadXml(documentString
);
371 Target
[] t
= new Target
[1];
373 project
.Targets
.CopyTo(t
, 0);
376 bt
[0].AddOutputItem("Property", "ItemName");
379 Assert
.AreEqual("ItemName", project
.EvaluatedItems
[0].Name
, "A1");
380 Assert
.AreEqual("some_text", project
.EvaluatedItems
[0].FinalItemSpec
, "A2");
384 public void TestAddOutputProperty1 ()
389 string documentString
= @"
390 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
397 engine
= new Engine (Consts
.BinPath
);
398 project
= engine
.CreateNewProject ();
399 project
.LoadXml (documentString
);
401 Target
[] t
= new Target
[1];
403 project
.Targets
.CopyTo (t
, 0);
404 bt
= GetTasks (t
[0]);
406 bt
[0].AddOutputProperty (null, null);
410 public void TestAddOutputProperty2 ()
415 string documentString
= @"
416 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
418 AssemblyFile='Test\resources\TestTasks.dll'
419 TaskName='OutputTestTask'
427 engine
= new Engine (Consts
.BinPath
);
428 project
= engine
.CreateNewProject ();
429 project
.LoadXml (documentString
);
431 Target
[] t
= new Target
[1];
433 project
.Targets
.CopyTo (t
, 0);
434 bt
= GetTasks (t
[0]);
435 bt
[0].AddOutputProperty ("Property", "PropertyName");
438 Assert
.AreEqual ("some_text", project
.EvaluatedProperties
["PropertyName"].Value
, "A1");
439 Assert
.AreEqual ("some_text", project
.EvaluatedProperties
["PropertyName"].FinalValue
, "A1");
443 public void TestPublish1 ()
448 string documentString
= @"
449 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
451 AssemblyFile='Test\resources\TestTasks.dll'
452 TaskName='OutputTestTask'
460 engine
= new Engine (Consts
.BinPath
);
461 project
= engine
.CreateNewProject ();
462 project
.LoadXml (documentString
);
464 Target
[] t
= new Target
[1];
466 project
.Targets
.CopyTo (t
, 0);
467 bt
= GetTasks (t
[0]);
468 bt
[0].AddOutputProperty ("Property", "PropertyName");
471 Assert
.AreEqual ("some_text", project
.EvaluatedProperties
["PropertyName"].Value
, "A1");
472 Assert
.AreEqual ("some_text", project
.EvaluatedProperties
["PropertyName"].FinalValue
, "A1");