2 // CodeTaskFactoryTest.cs
5 // Atsushi Enomoto <atsushi@xamarin.com>
7 // Copyright (C) 2014 Xamarin Inc.
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.
33 using Microsoft.Build.Framework;
34 using Microsoft.Build.Tasks;
35 using NUnit.Framework;
36 using Microsoft.Build.Construction;
37 using Microsoft.Build.Evaluation;
38 using Microsoft.Build.Logging;
40 namespace MonoTests.Microsoft.Build.Tasks
43 public class CodeTaskFactoryTest
46 public void EmptyFactory ()
48 var f = new CodeTaskFactory ();
49 Assert.AreEqual ("Code Task Factory", f.FactoryName, "Name");
50 Assert.IsNull (f.TaskType, "TaskType");
51 Assert.IsNull (f.CreateTask (null), "CreateTask");
57 string project_xml = @"<Project ToolsVersion='4.0' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
60 TaskFactory='CodeTaskFactory'
61 AssemblyFile='$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll' >
64 <Code Type='Fragment' Language='cs'>
66 // Display ""Hello, world!""
67 Log.LogWarning(""Hello, world!"");
71 <Target Name='default'>
75 var root = ProjectRootElement.Create (XmlReader.Create (new StringReader (project_xml)));
76 root.FullPath = "CodeTaskFactoryTest.Hello.proj";
77 var project = new Project (root);
78 Assert.IsTrue (project.Build (new ConsoleLogger (LoggerVerbosity.Diagnostic)), "Build");
82 public void HelloWithParameter ()
84 string project_xml = @"<Project ToolsVersion='4.0' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
87 TaskFactory='CodeTaskFactory'
88 AssemblyFile='$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll' >
90 <Message ParameterType='System.String' Required='true' />
93 <Code Type='Fragment' Language='cs'>
95 Log.LogWarning(""Message: "" + Message);
99 <Target Name='default'>
100 <DoNothing Message='Hello, world!'/>
103 var root = ProjectRootElement.Create (XmlReader.Create (new StringReader (project_xml)));
104 root.FullPath = "CodeTaskFactoryTest.HelloWithParameter.proj";
105 var project = new Project (root);
106 Assert.IsTrue (project.Build (new ConsoleLogger (LoggerVerbosity.Diagnostic)), "Build");
110 public void Reference ()
112 string project_xml = @"<Project ToolsVersion='4.0' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
115 TaskFactory='CodeTaskFactory'
116 AssemblyFile='$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll' >
119 <Reference Include='System.Drawing' />
120 <Code Type='Fragment' Language='cs'>
122 Log.LogWarning(""Color: "" + System.Drawing.Color.CornflowerBlue);
126 <Target Name='default'>
130 var root = ProjectRootElement.Create (XmlReader.Create (new StringReader (project_xml)));
131 root.FullPath = "CodeTaskFactoryTest.Reference.proj";
132 var project = new Project (root);
133 Assert.IsTrue (project.Build (new ConsoleLogger (LoggerVerbosity.Diagnostic)), "Build");
137 public void MultipleCodeElements ()
139 string project_xml = @"<Project ToolsVersion='4.0' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
142 TaskFactory='CodeTaskFactory'
143 AssemblyFile='$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll' >
146 <Code Type='Fragment' Language='cs'></Code>
147 <Code Type='Fragment' Language='cs'></Code>
150 <Target Name='default'>
154 var root = ProjectRootElement.Create (XmlReader.Create (new StringReader (project_xml)));
155 root.FullPath = "CodeTaskFactoryTest.MultipleCodeElements.proj";
156 var project = new Project (root);
157 Assert.IsFalse (project.Build (), "Build");
161 public void InvalidLanguage ()
163 string project_xml = @"<Project ToolsVersion='4.0' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
166 TaskFactory='CodeTaskFactory'
167 AssemblyFile='$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll' >
170 <Code Type='Fragment' Language='ts'></Code>
173 <Target Name='default'>
177 var root = ProjectRootElement.Create (XmlReader.Create (new StringReader (project_xml)));
178 root.FullPath = "CodeTaskFactoryTest.InvalidLanguage.proj";
179 var project = new Project (root);
180 Assert.IsFalse (project.Build (), "Build");
184 public void InvalidCSharp ()
186 string project_xml = @"<Project ToolsVersion='4.0' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
189 TaskFactory='CodeTaskFactory'
190 AssemblyFile='$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll' >
193 <Code Type='Fragment' Language='cs'>
198 <Target Name='default'>
202 var root = ProjectRootElement.Create (XmlReader.Create (new StringReader (project_xml)));
203 root.FullPath = "CodeTaskFactoryTest.InvalidCSharp.proj";
204 var project = new Project (root);
205 Assert.IsFalse (project.Build (), "Build");