PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/main/tests/UnitTests/MonoDevelop.Ide.Templates/FinalProjectConfigurationTests.cs

http://github.com/mono/monodevelop
C# | 357 lines | 265 code | 59 blank | 33 comment | 3 complexity | 7441480f2b342a71536fe72bee8e927b MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0, CC-BY-SA-3.0, MIT, LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. //
  2. // FinalProjectConfigurationTests.cs
  3. //
  4. // Author:
  5. // Matt Ward <matt.ward@xamarin.com>
  6. //
  7. // Copyright (c) 2014 Xamarin Inc. (http://xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System.IO;
  27. using System.Linq;
  28. using MonoDevelop.Ide.Projects;
  29. using NUnit.Framework;
  30. namespace MonoDevelop.Ide.Templates
  31. {
  32. [TestFixture]
  33. public class FinalProjectConfigurationTests
  34. {
  35. NewProjectConfiguration config;
  36. void CreateProjectConfig (string location)
  37. {
  38. config = new NewProjectConfiguration {
  39. Location = ToNativePath (location)
  40. };
  41. }
  42. /// <summary>
  43. /// Converts a Windows path to a native path by converting any
  44. /// directory separators.
  45. /// </summary>
  46. static string ToNativePath (string filePath)
  47. {
  48. if (Path.DirectorySeparatorChar == '\\')
  49. return filePath;
  50. if (filePath.Contains (":")) {
  51. filePath = filePath.Replace (":", "_drive");
  52. filePath = "/" + filePath;
  53. }
  54. return filePath.Replace ('\\', Path.DirectorySeparatorChar);
  55. }
  56. void AssertPathsAreEqual (string expected, string actual)
  57. {
  58. Assert.AreEqual (ToNativePath (expected), actual);
  59. }
  60. [Test]
  61. public void NewSolutionAndCreateProjectDirectory ()
  62. {
  63. CreateProjectConfig (@"d:\projects");
  64. config.ProjectName = "MyProject";
  65. config.SolutionName = "MySolution";
  66. config.CreateProjectDirectoryInsideSolutionDirectory = true;
  67. config.CreateSolution = true;
  68. AssertPathsAreEqual (@"d:\projects\MySolution\MyProject", config.ProjectLocation);
  69. AssertPathsAreEqual (@"d:\projects\MySolution", config.SolutionLocation);
  70. }
  71. [Test]
  72. public void NewSolutionAndDoNotCreateProjectDirectory ()
  73. {
  74. CreateProjectConfig (@"d:\projects");
  75. config.ProjectName = "MyProject";
  76. config.SolutionName = "MySolution";
  77. config.CreateProjectDirectoryInsideSolutionDirectory = false;
  78. config.CreateSolution = true;
  79. AssertPathsAreEqual (@"d:\projects\MyProject", config.ProjectLocation);
  80. AssertPathsAreEqual (@"d:\projects\MyProject", config.SolutionLocation);
  81. }
  82. [Test]
  83. public void AddProjectToExistingSolutionAndCreateProjectDirectory ()
  84. {
  85. CreateProjectConfig (@"d:\projects");
  86. config.ProjectName = "MyProject";
  87. config.CreateProjectDirectoryInsideSolutionDirectory = true;
  88. config.CreateSolution = false;
  89. AssertPathsAreEqual (@"d:\projects\MyProject", config.ProjectLocation);
  90. }
  91. [Test]
  92. public void AddProjectToExistingSolutionAndDoNotCreateProjectDirectory ()
  93. {
  94. CreateProjectConfig (@"d:\projects");
  95. config.ProjectName = "MyProject";
  96. config.CreateProjectDirectoryInsideSolutionDirectory = false;
  97. config.CreateSolution = false;
  98. AssertPathsAreEqual (@"d:\projects", config.ProjectLocation);
  99. }
  100. [Test]
  101. public void NewSolutionWithoutAnyProjects ()
  102. {
  103. CreateProjectConfig (@"d:\projects");
  104. config.SolutionName = "MySolution";
  105. config.CreateProjectDirectoryInsideSolutionDirectory = false;
  106. config.CreateSolution = true;
  107. config.IsNewSolutionWithoutProjects = true;
  108. AssertPathsAreEqual (@"d:\projects\MySolution", config.SolutionLocation);
  109. AssertPathsAreEqual (@"d:\projects\MySolution", config.ProjectLocation);
  110. }
  111. [Test]
  112. public void NewSolutionWithoutAnyProjectsAndCreateProjectDirectoryInsideSolutionDirectory ()
  113. {
  114. CreateProjectConfig (@"d:\projects");
  115. config.SolutionName = "MySolution";
  116. config.CreateProjectDirectoryInsideSolutionDirectory = true;
  117. config.CreateSolution = true;
  118. config.IsNewSolutionWithoutProjects = true;
  119. AssertPathsAreEqual (@"d:\projects\MySolution", config.SolutionLocation);
  120. AssertPathsAreEqual (@"d:\projects\MySolution", config.ProjectLocation);
  121. }
  122. [Test]
  123. public void NewSolutionWithoutAnyProjectsWithProjectNameSpecifiedAndCreateProjectDirectoryInsideSolutionDirectory ()
  124. {
  125. CreateProjectConfig (@"d:\projects");
  126. config.SolutionName = "MySolution";
  127. config.CreateProjectDirectoryInsideSolutionDirectory = true;
  128. config.CreateSolution = true;
  129. config.ProjectName = "MyProject";
  130. config.IsNewSolutionWithoutProjects = true;
  131. AssertPathsAreEqual (@"d:\projects\MySolution", config.SolutionLocation);
  132. AssertPathsAreEqual (@"d:\projects\MySolution", config.ProjectLocation);
  133. }
  134. [Test]
  135. public void EmptyProjectNameAndLocationIsNotValid ()
  136. {
  137. CreateProjectConfig (@"d:\projects");
  138. config.SolutionName = "a";
  139. config.ProjectName = string.Empty;
  140. config.Location = string.Empty;
  141. bool result = config.IsValid ();
  142. Assert.IsFalse (result);
  143. }
  144. [Test]
  145. public void ProjectNameAndSolutionNameAndLocationAreNotEmptyIsValid ()
  146. {
  147. CreateProjectConfig (@"d:\projects");
  148. config.SolutionName = "a";
  149. config.ProjectName = "b";
  150. bool result = config.IsValid ();
  151. Assert.IsTrue (result);
  152. }
  153. [Test]
  154. public void EmptyProjectNameIsNotValid ()
  155. {
  156. CreateProjectConfig (@"d:\projects");
  157. config.SolutionName = "a";
  158. config.ProjectName = string.Empty;
  159. bool result = config.IsValid ();
  160. Assert.IsFalse (result);
  161. }
  162. [Test]
  163. public void ProjectNameWithSpacesIsNotValid ()
  164. {
  165. CreateProjectConfig (@"d:\projects");
  166. config.SolutionName = "a";
  167. config.ProjectName = "a b";
  168. bool result = config.IsValid ();
  169. Assert.IsFalse (result);
  170. }
  171. [Test]
  172. public void EmptyProjectNameWhenCreatingOnlySolutionIsValid ()
  173. {
  174. CreateProjectConfig (@"d:\projects");
  175. config.SolutionName = "a";
  176. config.ProjectName = string.Empty;
  177. config.IsNewSolutionWithoutProjects = true;
  178. bool result = config.IsValid ();
  179. Assert.IsTrue (result);
  180. }
  181. [Test]
  182. public void SolutionNameWithInvalidCharactersWhenCreatingSolutionOnlyIsNotValid ()
  183. {
  184. CreateProjectConfig (@"d:\projects");
  185. config.SolutionName = "a" + Path.GetInvalidPathChars ().First ();
  186. config.ProjectName = string.Empty;
  187. config.CreateSolution = true;
  188. config.IsNewSolutionWithoutProjects = true;
  189. bool result = config.IsValid ();
  190. Assert.IsFalse (result);
  191. }
  192. [Test]
  193. public void SolutionLocationWithInvalidCharactersWhenCreatingSolutionOnlyIsNotValid ()
  194. {
  195. CreateProjectConfig (@"d:\projects");
  196. config.SolutionName = "a";
  197. config.Location = config.Location + Path.GetInvalidPathChars ().First ();
  198. config.IsNewSolutionWithoutProjects = true;
  199. bool result = config.IsValid ();
  200. Assert.IsFalse (result);
  201. }
  202. [TestCase ("a", true)]
  203. [TestCase ("a.b", true)]
  204. [TestCase ("a_b", true)]
  205. [TestCase ("a&b", false)]
  206. [TestCase ("a<b", false)]
  207. [TestCase ("a*b", false)]
  208. [TestCase ("a;b", false)]
  209. [TestCase ("a?b", false)]
  210. [TestCase ("a>b", false)]
  211. [TestCase ("a%b", false)]
  212. [TestCase ("a:b", false)]
  213. [TestCase ("a#b", false)]
  214. [TestCase ("a|b", false)]
  215. public void CreateSolutionDirectoryWhenInvalidSolutionNameCharactersCauseConfigToBeInvalid (string solutionName, bool valid)
  216. {
  217. CreateProjectConfig (@"d:\projects");
  218. config.CreateSolution = true;
  219. config.ProjectName = "a";
  220. config.SolutionName = solutionName;
  221. bool result = config.IsValid ();
  222. Assert.AreEqual (valid, result);
  223. }
  224. [TestCase ("a", true)]
  225. [TestCase ("a.b", true)]
  226. [TestCase ("a_b", true)]
  227. [TestCase ("a&b", false)]
  228. [TestCase ("a<b", false)]
  229. [TestCase ("a*b", false)]
  230. [TestCase ("a;b", false)]
  231. [TestCase ("a?b", false)]
  232. [TestCase ("a>b", false)]
  233. [TestCase ("a%b", false)]
  234. [TestCase ("a:b", false)]
  235. [TestCase ("a#b", false)]
  236. [TestCase ("a|b", false)]
  237. public void CreateSolutionWithoutSeparateSolutionDirectoryWhenInvalidSolutionNameCharactersCauseConfigToBeInvalid (string solutionName, bool valid)
  238. {
  239. CreateProjectConfig (@"d:\projects");
  240. config.CreateSolution = true;
  241. config.CreateProjectDirectoryInsideSolutionDirectory = false;
  242. config.ProjectName = "a";
  243. config.SolutionName = solutionName;
  244. bool result = config.IsValid ();
  245. Assert.AreEqual (valid, result);
  246. }
  247. [TestCase ("a", true)]
  248. [TestCase ("a.b", true)]
  249. [TestCase ("a_b", true)]
  250. [TestCase ("a&b", false)]
  251. [TestCase ("a<b", false)]
  252. [TestCase ("a*b", false)]
  253. [TestCase ("a;b", false)]
  254. [TestCase ("a?b", false)]
  255. [TestCase ("a>b", false)]
  256. [TestCase ("a%b", false)]
  257. [TestCase ("a:b", false)]
  258. [TestCase ("a#b", false)]
  259. [TestCase ("a|b", false)]
  260. public void InvalidProjectNameCharactersCauseConfigToBeInvalid (string projectName, bool valid)
  261. {
  262. CreateProjectConfig (@"d:\projects");
  263. config.SolutionName = "a";
  264. config.ProjectName = projectName;
  265. bool result = config.IsValid ();
  266. Assert.AreEqual (valid, result);
  267. }
  268. [Test]
  269. public void EmojiProjectNameCharactersCauseConfigToBeInvalid ()
  270. {
  271. CreateProjectConfig (@"d:\projects");
  272. config.SolutionName = "a";
  273. // Mahjong tile
  274. config.ProjectName = "\U0001F004";
  275. Assert.IsFalse (config.IsValid ());
  276. // Smiley face
  277. config.ProjectName = "\U0001F600";
  278. Assert.IsFalse (config.IsValid ());
  279. // Zimbabwe flag.
  280. config.ProjectName = "\U0001F1FF\U0001F1FC";
  281. Assert.IsFalse (config.IsValid ());
  282. // Double exclamation mark.
  283. config.ProjectName = "\U0000203C";
  284. Assert.IsFalse (config.IsValid ());
  285. }
  286. [Test]
  287. public void NewProjectOnlyAndCreateProjectDirectory ()
  288. {
  289. CreateProjectConfig (@"d:\projects\MySolution");
  290. config.ProjectName = "MyProject";
  291. config.SolutionName = "MySolution";
  292. config.CreateProjectDirectoryInsideSolutionDirectory = true;
  293. config.CreateSolution = false;
  294. AssertPathsAreEqual (@"d:\projects\MySolution\MyProject", config.ProjectLocation);
  295. AssertPathsAreEqual (@"d:\projects\MySolution", config.SolutionLocation);
  296. }
  297. }
  298. }