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.
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Tasks;
34 using NUnit.Framework;
35 using Microsoft.Build.Construction;
36 using Microsoft.Build.Evaluation;
37 using Microsoft.Build.Logging;
39 namespace MonoTests.Microsoft.Build.Tasks
42 public class CodeTaskFactoryTest
45 public void EmptyFactory ()
47 var f = new CodeTaskFactory ();
48 Assert.AreEqual ("Code Task Factory", f.FactoryName, "Name");
49 Assert.IsNull (f.TaskType, "TaskType");
50 Assert.IsNull (f.CreateTask (null), "CreateTask");
56 string project_xml = @"<Project ToolsVersion='4.0' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
59 TaskFactory='CodeTaskFactory'
60 AssemblyFile='$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll' >
63 <Code Type='Fragment' Language='cs'>
65 // Display ""Hello, world!""
66 Log.LogWarning(""Hello, world!"");
70 <Target Name='default'>
74 var root = ProjectRootElement.Create (XmlReader.Create (new StringReader (project_xml)));
75 root.FullPath = "CodeTaskFactoryTest.Hello.proj";
76 var project = new Project (root);
77 Assert.IsTrue (project.Build (new ConsoleLogger (LoggerVerbosity.Diagnostic)), "Build");
81 public void HelloWithParameter ()
83 string project_xml = @"<Project ToolsVersion='4.0' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
86 TaskFactory='CodeTaskFactory'
87 AssemblyFile='$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll' >
89 <Message ParameterType='System.String' Required='true' />
92 <Code Type='Fragment' Language='cs'>
94 Log.LogWarning(""Message: "" + Message);
98 <Target Name='default'>
99 <DoNothing Message='Hello, world!'/>
102 var root = ProjectRootElement.Create (XmlReader.Create (new StringReader (project_xml)));
103 root.FullPath = "CodeTaskFactoryTest.HelloWithParameter.proj";
104 var project = new Project (root);
105 Assert.IsTrue (project.Build (new ConsoleLogger (LoggerVerbosity.Diagnostic)), "Build");
109 public void Reference ()
111 string project_xml = @"<Project ToolsVersion='4.0' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
114 TaskFactory='CodeTaskFactory'
115 AssemblyFile='$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll' >
118 <Reference Include='System.Drawing' />
119 <Code Type='Fragment' Language='cs'>
121 Log.LogWarning(""Color: "" + System.Drawing.Color.CornflowerBlue);
125 <Target Name='default'>
129 var root = ProjectRootElement.Create (XmlReader.Create (new StringReader (project_xml)));
130 root.FullPath = "CodeTaskFactoryTest.Reference.proj";
131 var project = new Project (root);
132 Assert.IsTrue (project.Build (new ConsoleLogger (LoggerVerbosity.Diagnostic)), "Build");
136 public void MultipleCodeElements ()
138 string project_xml = @"<Project ToolsVersion='4.0' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
141 TaskFactory='CodeTaskFactory'
142 AssemblyFile='$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll' >
145 <Code Type='Fragment' Language='cs'></Code>
146 <Code Type='Fragment' Language='cs'></Code>
149 <Target Name='default'>
153 var root = ProjectRootElement.Create (XmlReader.Create (new StringReader (project_xml)));
154 root.FullPath = "CodeTaskFactoryTest.MultipleCodeElements.proj";
155 var project = new Project (root);
156 Assert.IsFalse (project.Build (), "Build");
160 public void InvalidLanguage ()
162 string project_xml = @"<Project ToolsVersion='4.0' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
165 TaskFactory='CodeTaskFactory'
166 AssemblyFile='$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll' >
169 <Code Type='Fragment' Language='ts'></Code>
172 <Target Name='default'>
176 var root = ProjectRootElement.Create (XmlReader.Create (new StringReader (project_xml)));
177 root.FullPath = "CodeTaskFactoryTest.InvalidLanguage.proj";
178 var project = new Project (root);
179 Assert.IsFalse (project.Build (), "Build");
183 public void InvalidCSharp ()
185 string project_xml = @"<Project ToolsVersion='4.0' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
188 TaskFactory='CodeTaskFactory'
189 AssemblyFile='$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll' >
192 <Code Type='Fragment' Language='cs'>
197 <Target Name='default'>
201 var root = ProjectRootElement.Create (XmlReader.Create (new StringReader (project_xml)));
202 root.FullPath = "CodeTaskFactoryTest.InvalidCSharp.proj";
203 var project = new Project (root);
204 Assert.IsFalse (project.Build (), "Build");