5 // Leszek Ciesielski (skolima@gmail.com)
7 // (C) 2011 Leszek Ciesielski
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 NUnit.Framework;
31 using System.Collections.Generic;
34 using Microsoft.Build.Construction;
35 using Microsoft.Build.Evaluation;
37 using System.Globalization;
39 namespace MonoTests.Microsoft.Build
42 public class FunctionalTest
45 public void TestFullProjectGeneration ()
47 var project = ProjectRootElement.Create ();
48 var projectFileName = String.Format ("Test{0}FunctionalTestProject.csproj",
49 Path.DirectorySeparatorChar);
50 project.DefaultTargets = "Build";
51 SetKnownProperties (project.AddPropertyGroup ());
52 GenerateReferences (project.AddItemGroup ());
53 GenerateCompileIncludes (project.AddItemGroup ());
54 project.AddImport ("$(MSBuildToolsPath)\\Microsoft.CSharp.targets");
56 project.Save (projectFileName);
57 Assert.AreEqual (projectGuid, GetProjectId (project), "#01");
58 FileAssert.AreEqual (String.Format ("Test{0}FunctionalTestReferenceProject.csproj",
59 Path.DirectorySeparatorChar), String.Format ("Test{0}FunctionalTestProject.csproj",
60 Path.DirectorySeparatorChar), "#02");
64 public void TestLoadAndSave ()
66 var project = ProjectRootElement.Open (Path.Combine ("Test", "Microsoft.Build.csproj"));
67 var projectFileName = String.Format ("Test{0}FunctionalTestProject2.csproj",
68 Path.DirectorySeparatorChar);
69 project.Save (projectFileName);
71 Assert.AreEqual (new Guid ("{B2012E7F-8F8D-4908-8045-413F2BD1022D}"), GetProjectId (project),
73 FileAssert.AreEqual (Path.Combine ("Test", "Microsoft.Build.csproj"), projectFileName, "#04");
77 public void TestLoadAndSave3 ()
79 var referenceProject = String.Format (
80 "Test{0}FunctionalTestReferenceProject3.csproj", Path.DirectorySeparatorChar);
81 var project = ProjectRootElement.Open (referenceProject);
82 var projectFileName = String.Format ("Test{0}FunctionalTestProject3.csproj",
83 Path.DirectorySeparatorChar);
84 project.Save (projectFileName);
86 Assert.AreEqual (new Guid ("{793B20A9-E263-4B54-BB31-305B602087CE}"), GetProjectId (project),
88 FileAssert.AreEqual (referenceProject, projectFileName, "#06");
91 public Guid GetProjectId (ProjectRootElement project)
93 var value = project.Properties.Where (p => p.Name == "ProjectGuid").First ().Value;
94 return Guid.Parse (value);
97 void GenerateCompileIncludes (ProjectItemGroupElement itemGroup)
99 foreach (var include in new[] { "Test.cs", "Test2.cs", "Test3.cs" }) {
100 itemGroup.AddItem ("Compile", ProjectCollection.Escape (include),
101 new[] { new KeyValuePair<string, string> ("SubType", "Code") });
103 foreach (var resource in new[] { "Test.resx", "Test2.resx", "Test3.resx" }) {
104 itemGroup.AddItem ("EmbeddedResource", ProjectCollection.Escape (resource),
105 new[] { new KeyValuePair<string, string> ("LogicalName", "Name.Space.Test") });
109 void GenerateReferences (ProjectItemGroupElement itemGroup)
111 foreach (var reference in new[] { "Test.dll", "Test2.dll", "Test3.dll" }) {
112 var name = Path.GetFileNameWithoutExtension (reference);
113 itemGroup.AddItem ("Reference", name, new[] {
114 new KeyValuePair<string, string> ("Name", name),
115 new KeyValuePair<string, string> ("HintPath",
116 ProjectCollection.Escape (reference)) });
118 foreach (var reference in new[] { "mscorlib", "System", "System.Xml" }) {
119 itemGroup.AddItem ("Reference", reference);
123 void SetKnownProperties (ProjectPropertyGroupElement properties)
125 properties.AddProperty ("AssemblyName", Path.GetFileNameWithoutExtension ("ZigZag.exe"));
126 properties.AddProperty ("ProjectGuid", projectGuid.ToString ("B"));
127 properties.AddProperty ("CheckForOverflowUnderflow", false.ToString ());
128 properties.AddProperty ("CodePage", String.Empty);
129 properties.AddProperty ("DebugSymbols", true.ToString ());
130 properties.AddProperty ("DebugType", "Full");
131 properties.AddProperty ("Optimize", false.ToString ());
132 properties.AddProperty ("Platform", "AnyCPU");
133 properties.AddProperty ("ProjectTypeGuids", projectTypeGuid.ToString ("B"));
134 properties.AddProperty ("AllowUnsafeBlocks", false.ToString ());
135 properties.AddProperty ("WarningLevel", "4");
136 properties.AddProperty ("OutputPath", ProjectCollection.Escape ("bin\\Debug"));
137 properties.AddProperty ("OutputType", "winexe");
138 properties.AddProperty ("DefineConstants", "DEBUG,TRACE");
139 properties.AddProperty ("TreatWarningsAsErrors", true.ToString ());
142 static readonly Guid projectTypeGuid = Guid.Parse ("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}");
143 static readonly Guid projectGuid = Guid.Parse ("{362F36B0-B26C-42DE-8586-20DF66BD05E8}");