PageRenderTime 1224ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/AddIns/BackendBindings/AspNet.Mvc/Test/Src/CodeTemplates/RazorCSharpCreateViewTemplateTests.cs

http://github.com/icsharpcode/SharpDevelop
C# | 386 lines | 320 code | 49 blank | 17 comment | 1 complexity | 55c4e6bf722a9f4cf8e8d61a8eecae2e MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CPL-1.0, LGPL-2.1
  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using AspNet.Mvc.Tests.CodeTemplates.Models;
  22. using AspNet.Mvc.Tests.Helpers;
  23. using ICSharpCode.AspNet.Mvc.CSHtml;
  24. using NUnit.Framework;
  25. namespace AspNet.Mvc.Tests.CodeTemplates
  26. {
  27. [TestFixture]
  28. public class RazorCSharpCreateViewTemplateTests
  29. {
  30. Create templatePreprocessor;
  31. TestableMvcTextTemplateHost mvcHost;
  32. void CreateViewTemplatePreprocessor()
  33. {
  34. mvcHost = new TestableMvcTextTemplateHost();
  35. templatePreprocessor = new Create();
  36. templatePreprocessor.Host = mvcHost;
  37. }
  38. IEnumerable<Create.ModelProperty> GetModelProperties()
  39. {
  40. return templatePreprocessor.GetModelProperties();
  41. }
  42. Create.ModelProperty GetFirstModelProperty()
  43. {
  44. return GetModelProperties().First();
  45. }
  46. Create.ModelProperty GetModelProperty(string name)
  47. {
  48. return GetModelProperties().First(p => p.Name == name);
  49. }
  50. [Test]
  51. public void GetModelDirective_HostViewDataTypeNameIsMyAppMyModel_ReturnsRazorModelFollowedByMyAppMyModel()
  52. {
  53. CreateViewTemplatePreprocessor();
  54. mvcHost.ViewDataTypeName = "MyApp.MyModel";
  55. string modelDirective = templatePreprocessor.GetModelDirective();
  56. Assert.AreEqual("@model MyApp.MyModel", modelDirective);
  57. }
  58. [Test]
  59. public void GetModelDirective_HostViewDataTypeNameIsNull_ReturnsEmptyString()
  60. {
  61. CreateViewTemplatePreprocessor();
  62. mvcHost.ViewDataTypeName = null;
  63. string modelDirective = templatePreprocessor.GetModelDirective();
  64. Assert.AreEqual(String.Empty, modelDirective);
  65. }
  66. [Test]
  67. public void GetModelDirective_HostViewDataTypeNameIsEmptyString_ReturnsEmptyString()
  68. {
  69. CreateViewTemplatePreprocessor();
  70. mvcHost.ViewDataTypeName = String.Empty;
  71. string modelDirective = templatePreprocessor.GetModelDirective();
  72. Assert.AreEqual(String.Empty, modelDirective);
  73. }
  74. [Test]
  75. public void TransformText_ModelHasNoPropertiesAndNoMasterPage_ReturnsFullHtmlPageWithFormAndFieldSetForModel()
  76. {
  77. CreateViewTemplatePreprocessor();
  78. Type modelType = typeof(ModelWithNoProperties);
  79. mvcHost.ViewDataType = modelType;
  80. mvcHost.ViewDataTypeName = modelType.FullName;
  81. mvcHost.ViewName = "MyView";
  82. string output = templatePreprocessor.TransformText();
  83. string expectedOutput =
  84. @"@model AspNet.Mvc.Tests.CodeTemplates.Models.ModelWithNoProperties
  85. <!DOCTYPE html>
  86. <html>
  87. <head runat=""server"">
  88. <title>MyView</title>
  89. </head>
  90. <body>
  91. @using (Html.BeginForm()) {
  92. @Html.ValidationSummary(true)
  93. <fieldset>
  94. <legend>ModelWithNoProperties</legend>
  95. <p>
  96. <input type=""submit"" value=""Create""/>
  97. </p>
  98. </fieldset>
  99. }
  100. <div>
  101. @Html.ActionLink(""Back"", ""Index"")
  102. </div>
  103. </body>
  104. </html>
  105. ";
  106. Assert.AreEqual(expectedOutput, output);
  107. }
  108. [Test]
  109. public void TransformText_ModelHasNoPropertiesAndIsPartialView_ReturnsControlWithFormAndFieldSetForModel()
  110. {
  111. CreateViewTemplatePreprocessor();
  112. mvcHost.IsPartialView = true;
  113. Type modelType = typeof(ModelWithNoProperties);
  114. mvcHost.ViewDataType = modelType;
  115. mvcHost.ViewDataTypeName = modelType.FullName;
  116. mvcHost.ViewName = "MyView";
  117. string output = templatePreprocessor.TransformText();
  118. string expectedOutput =
  119. @"@model AspNet.Mvc.Tests.CodeTemplates.Models.ModelWithNoProperties
  120. @using (Html.BeginForm()) {
  121. @Html.ValidationSummary(true)
  122. <fieldset>
  123. <legend>ModelWithNoProperties</legend>
  124. <p>
  125. <input type=""submit"" value=""Create""/>
  126. </p>
  127. </fieldset>
  128. }
  129. <div>
  130. @Html.ActionLink(""Back"", ""Index"")
  131. </div>
  132. ";
  133. Assert.AreEqual(expectedOutput, output);
  134. }
  135. [Test]
  136. public void TransformText_ModelHasNoPropertiesAndIsContentPage_ReturnsContentPageWithFormAndFieldSetForModel()
  137. {
  138. CreateViewTemplatePreprocessor();
  139. mvcHost.IsContentPage = true;
  140. Type modelType = typeof(ModelWithNoProperties);
  141. mvcHost.ViewDataType = modelType;
  142. mvcHost.ViewDataTypeName = modelType.FullName;
  143. mvcHost.ViewName = "MyView";
  144. mvcHost.MasterPageFile = "~/Views/Shared/Site.master";
  145. mvcHost.PrimaryContentPlaceHolderID = "Main";
  146. string output = templatePreprocessor.TransformText();
  147. string expectedOutput =
  148. @"@model AspNet.Mvc.Tests.CodeTemplates.Models.ModelWithNoProperties
  149. @{
  150. ViewBag.Title = ""MyView"";
  151. Layout = ""~/Views/Shared/Site.master"";
  152. }
  153. <h2>MyView</h2>
  154. @using (Html.BeginForm()) {
  155. @Html.ValidationSummary(true)
  156. <fieldset>
  157. <legend>ModelWithNoProperties</legend>
  158. <p>
  159. <input type=""submit"" value=""Create""/>
  160. </p>
  161. </fieldset>
  162. }
  163. <div>
  164. @Html.ActionLink(""Back"", ""Index"")
  165. </div>
  166. ";
  167. Assert.AreEqual(expectedOutput, output);
  168. }
  169. [Test]
  170. public void GetModelProperties_ModelHasOnePropertyCalledName_ReturnsModelPropertyCalledName()
  171. {
  172. CreateViewTemplatePreprocessor();
  173. mvcHost.ViewDataType = typeof(ModelWithOneProperty);
  174. Create.ModelProperty modelProperty = GetFirstModelProperty();
  175. Assert.AreEqual("Name", modelProperty.Name);
  176. }
  177. [Test]
  178. public void TransformText_ModelHasOnePropertyAndIsPartialView_ReturnsControlWithHtmlEditorsForModelProperties()
  179. {
  180. CreateViewTemplatePreprocessor();
  181. mvcHost.IsPartialView = true;
  182. Type modelType = typeof(ModelWithOneProperty);
  183. mvcHost.ViewDataType = modelType;
  184. mvcHost.ViewDataTypeName = modelType.FullName;
  185. mvcHost.ViewName = "MyView";
  186. string output = templatePreprocessor.TransformText();
  187. string expectedOutput =
  188. @"@model AspNet.Mvc.Tests.CodeTemplates.Models.ModelWithOneProperty
  189. @using (Html.BeginForm()) {
  190. @Html.ValidationSummary(true)
  191. <fieldset>
  192. <legend>ModelWithOneProperty</legend>
  193. <div class=""editor-label"">
  194. @Html.LabelFor(model => model.Name)
  195. </div>
  196. <div class=""editor-field"">
  197. @Html.EditorFor(model => model.Name)
  198. @Html.ValidationMessageFor(model => model.Name)
  199. </div>
  200. <p>
  201. <input type=""submit"" value=""Create""/>
  202. </p>
  203. </fieldset>
  204. }
  205. <div>
  206. @Html.ActionLink(""Back"", ""Index"")
  207. </div>
  208. ";
  209. Assert.AreEqual(expectedOutput, output);
  210. }
  211. [Test]
  212. public void TransformText_ModelHasTwoPropertiesAndIsPartialView_ReturnsControlWithHtmlEditorsForModelProperties()
  213. {
  214. CreateViewTemplatePreprocessor();
  215. mvcHost.IsPartialView = true;
  216. Type modelType = typeof(ModelWithTwoProperties);
  217. mvcHost.ViewDataType = modelType;
  218. mvcHost.ViewDataTypeName = modelType.FullName;
  219. mvcHost.ViewName = "MyView";
  220. string output = templatePreprocessor.TransformText();
  221. string expectedOutput =
  222. @"@model AspNet.Mvc.Tests.CodeTemplates.Models.ModelWithTwoProperties
  223. @using (Html.BeginForm()) {
  224. @Html.ValidationSummary(true)
  225. <fieldset>
  226. <legend>ModelWithTwoProperties</legend>
  227. <div class=""editor-label"">
  228. @Html.LabelFor(model => model.FirstName)
  229. </div>
  230. <div class=""editor-field"">
  231. @Html.EditorFor(model => model.FirstName)
  232. @Html.ValidationMessageFor(model => model.FirstName)
  233. </div>
  234. <div class=""editor-label"">
  235. @Html.LabelFor(model => model.LastName)
  236. </div>
  237. <div class=""editor-field"">
  238. @Html.EditorFor(model => model.LastName)
  239. @Html.ValidationMessageFor(model => model.LastName)
  240. </div>
  241. <p>
  242. <input type=""submit"" value=""Create""/>
  243. </p>
  244. </fieldset>
  245. }
  246. <div>
  247. @Html.ActionLink(""Back"", ""Index"")
  248. </div>
  249. ";
  250. Assert.AreEqual(expectedOutput, output);
  251. }
  252. [Test]
  253. public void TransformText_ModelHasIdPropertyAndIsPartialView_ReturnsControlWithHtmlEditorsForNonIdModelProperty()
  254. {
  255. CreateViewTemplatePreprocessor();
  256. mvcHost.IsPartialView = true;
  257. Type modelType = typeof(ModelWithIdProperty);
  258. mvcHost.ViewDataType = modelType;
  259. mvcHost.ViewDataTypeName = modelType.FullName;
  260. mvcHost.ViewName = "MyView";
  261. string output = templatePreprocessor.TransformText();
  262. string expectedOutput =
  263. @"@model AspNet.Mvc.Tests.CodeTemplates.Models.ModelWithIdProperty
  264. @using (Html.BeginForm()) {
  265. @Html.ValidationSummary(true)
  266. <fieldset>
  267. <legend>ModelWithIdProperty</legend>
  268. <div class=""editor-label"">
  269. @Html.LabelFor(model => model.Name)
  270. </div>
  271. <div class=""editor-field"">
  272. @Html.EditorFor(model => model.Name)
  273. @Html.ValidationMessageFor(model => model.Name)
  274. </div>
  275. <p>
  276. <input type=""submit"" value=""Create""/>
  277. </p>
  278. </fieldset>
  279. }
  280. <div>
  281. @Html.ActionLink(""Back"", ""Index"")
  282. </div>
  283. ";
  284. Assert.AreEqual(expectedOutput, output);
  285. }
  286. [Test]
  287. public void GetModelProperties_ModelHasIdAndNameProperty_IdPropertyIsMarkedAsPrimaryKey()
  288. {
  289. CreateViewTemplatePreprocessor();
  290. mvcHost.ViewDataType = typeof(ModelWithIdProperty);
  291. Create.ModelProperty modelProperty = GetModelProperty("Id");
  292. Assert.IsTrue(modelProperty.IsPrimaryKey);
  293. }
  294. [Test]
  295. public void GetModelProperties_ModelHasIdAndNameProperty_NamePropertyIsNotMarkedAsPrimaryKey()
  296. {
  297. CreateViewTemplatePreprocessor();
  298. mvcHost.ViewDataType = typeof(ModelWithIdProperty);
  299. Create.ModelProperty modelProperty = GetModelProperty("Name");
  300. Assert.IsFalse(modelProperty.IsPrimaryKey);
  301. }
  302. [Test]
  303. public void GetModelProperties_ModelHasIdPropertyInLowerCase_IdPropertyIsMarkedAsPrimaryKey()
  304. {
  305. CreateViewTemplatePreprocessor();
  306. mvcHost.ViewDataType = typeof(ModelWithIdPropertyInLowerCase);
  307. Create.ModelProperty modelProperty = GetModelProperty("id");
  308. Assert.IsTrue(modelProperty.IsPrimaryKey);
  309. }
  310. [Test]
  311. public void GetModelProperties_ModelHasPrefixedIdPropertyInLowerCase_PrefixedIdPropertyIsMarkedAsPrimaryKey()
  312. {
  313. CreateViewTemplatePreprocessor();
  314. mvcHost.ViewDataType = typeof(ModelWithPrefixedIdPropertyInLowerCase);
  315. Create.ModelProperty modelProperty = GetModelProperty("modelwithprefixedidpropertyinlowercaseid");
  316. Assert.IsTrue(modelProperty.IsPrimaryKey);
  317. }
  318. }
  319. }