PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Nancy.Tests/Unit/ViewEngines/SuperSimpleViewEngineTests.cs

https://github.com/Mucaccino/Nancy
C# | 1368 lines | 985 code | 347 blank | 36 comment | 2 complexity | c392da5b004ba56e3a99138ec3a0cbe8 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, CC-BY-SA-3.0, MIT

Large files files are truncated, but you can click here to view the full file

  1. namespace Nancy.Tests.Unit.ViewEngines
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Dynamic;
  6. using System.Linq;
  7. using FakeItEasy;
  8. using Nancy.Tests.Fakes;
  9. using Nancy.ViewEngines.SuperSimpleViewEngine;
  10. using Xunit;
  11. public class SuperSimpleViewEngineTests
  12. {
  13. private readonly SuperSimpleViewEngine viewEngine;
  14. private readonly IViewEngineHost fakeHost;
  15. public SuperSimpleViewEngineTests()
  16. {
  17. this.fakeHost = new FakeViewEngineHost();
  18. this.viewEngine = new SuperSimpleViewEngine(Enumerable.Empty<ISuperSimpleViewEngineMatcher>());
  19. }
  20. [Fact]
  21. public void Should_expand_partial_inside_each_section()
  22. {
  23. const string input = @"<html><head></head><body>@Each;@Partial['testing'];@EndEach</body></html>";
  24. var fakeViewEngineHost = new FakeViewEngineHost();
  25. fakeViewEngineHost.GetTemplateCallback = (s, m) => "Hi";
  26. var model = new List<string>() { "foo", "bar" };
  27. var result = viewEngine.Render(input, model, fakeViewEngineHost);
  28. Assert.Equal(@"<html><head></head><body>HiHi</body></html>", result);
  29. }
  30. [Fact]
  31. public void Should_expand_multiple_partials_inside_each_section()
  32. {
  33. const string input = @"<html><head></head><body>@Each;@Partial['greeting'], @Partial['name'];@EndEach</body></html>";
  34. var fakeViewEngineHost = new FakeViewEngineHost();
  35. fakeViewEngineHost.GetTemplateCallback = (s, m) =>
  36. {
  37. return (s.Equals("greeting")) ? "Hi" : "Nancy";
  38. };
  39. var model = new List<string>() { "foo", "bar" };
  40. var result = viewEngine.Render(input, model, fakeViewEngineHost);
  41. Assert.Equal(@"<html><head></head><body>Hi, NancyHi, Nancy</body></html>", result);
  42. }
  43. [Fact]
  44. public void Should_expand_partial_inside_each_section_with_current_as_model()
  45. {
  46. const string input = @"<html><head></head><body>@Each;@Partial['testing', @Current];@EndEach</body></html>";
  47. var fakeViewEngineHost = new FakeViewEngineHost();
  48. fakeViewEngineHost.GetTemplateCallback = (s, m) => "Hi @Model ";
  49. var model = new List<string>() { "foo", "bar" };
  50. var result = viewEngine.Render(input, model, fakeViewEngineHost);
  51. Assert.Equal(@"<html><head></head><body>Hi foo Hi bar </body></html>", result);
  52. }
  53. [Fact]
  54. public void Should_expand_partial_inside_each_section_with_parameter_of_current_as_model()
  55. {
  56. const string input = @"<html><head></head><body>@Each;@Partial['testing', @Current.Name];@EndEach</body></html>";
  57. var fakeViewEngineHost = new FakeViewEngineHost();
  58. fakeViewEngineHost.GetTemplateCallback = (s, m) => "Hi @Model ";
  59. dynamic foo = new ExpandoObject();
  60. foo.Name = "foo";
  61. dynamic bar = new ExpandoObject();
  62. bar.Name = "bar";
  63. var model = new List<ExpandoObject>() { foo, bar };
  64. var result = viewEngine.Render(input, model, fakeViewEngineHost);
  65. Assert.Equal(@"<html><head></head><body>Hi foo Hi bar </body></html>", result);
  66. }
  67. [Fact]
  68. public void Should_expand_multiple_partial_inside_each_section_with_parameter_of_current_as_model()
  69. {
  70. const string input = @"<html><head></head><body>@Each;@Partial['first', @Current.Name];-@Partial['second', @Current.Name];@EndEach</body></html>";
  71. var fakeViewEngineHost = new FakeViewEngineHost
  72. {
  73. GetTemplateCallback = (s, m) =>
  74. {
  75. return (s.Equals("first")) ?
  76. "Hi @Model" :
  77. "Hello @Model";
  78. }
  79. };
  80. dynamic foo = new ExpandoObject();
  81. foo.Name = "foo";
  82. dynamic bar = new ExpandoObject();
  83. bar.Name = "bar";
  84. var model = new List<ExpandoObject>() { foo, bar };
  85. var result = viewEngine.Render(input, model, fakeViewEngineHost);
  86. Assert.Equal(@"<html><head></head><body>Hi foo-Hello fooHi bar-Hello bar</body></html>", result);
  87. }
  88. [Fact]
  89. public void Should_expand_multiple_partial_inside_each_section_with_different_parameter_of_current_as_model()
  90. {
  91. const string input = @"<html><head></head><body>@Each;@Partial['first', @Current.First];-@Partial['second', @Current.Last];@EndEach</body></html>";
  92. var fakeViewEngineHost = new FakeViewEngineHost
  93. {
  94. GetTemplateCallback = (s, m) =>
  95. {
  96. return (s.Equals("first")) ?
  97. "Hi @Model" :
  98. "Hello @Model";
  99. }
  100. };
  101. dynamic foo = new ExpandoObject();
  102. foo.First = "foo";
  103. foo.Last = "bar";
  104. dynamic bar = new ExpandoObject();
  105. bar.First = "baz";
  106. bar.Last = "bin";
  107. var model = new List<ExpandoObject>() { foo, bar };
  108. var result = viewEngine.Render(input, model, fakeViewEngineHost);
  109. Assert.Equal(@"<html><head></head><body>Hi foo-Hello barHi baz-Hello bin</body></html>", result);
  110. }
  111. [Fact]
  112. public void Should_evaluate_current_conditional_inside_each()
  113. {
  114. // Given
  115. const string input = @"<html><head></head><body><ul>@Each.Users;<li>@Current.Name says:@If.IsGreekCitizen;<b>Yay Greece!</b>@EndIf;@IfNot.IsGreekCitizen;<b>Boo Greece!</b>@EndIf;</li>@EndEach;</ul></body></html>";
  116. dynamic model = new ExpandoObject();
  117. model.Users = new List<object>() { new { Name = "Bob", IsGreekCitizen = true }, new { Name = "Malin", IsGreekCitizen = false } };
  118. // When
  119. var output = viewEngine.Render(input, model, this.fakeHost);
  120. // Then
  121. Assert.Equal(@"<html><head></head><body><ul><li>Bob says:<b>Yay Greece!</b></li><li>Malin says:<b>Boo Greece!</b></li></ul></body></html>", output);
  122. }
  123. [Fact]
  124. public void Should_not_evaluate_current_conditional_from_outside_each()
  125. {
  126. // Given
  127. const string input = @"<html><head></head><body>@If.HasUsers;Yay Users!@EndIf<ul>@Each.Users;<li>@Current.Name:@If.IsGreekCitizen;<b>Yay Greece!</b>@EndIf;</li>@EndEach;</ul>@IfNot.HasUsers;Yay Users!@EndIf</body></html>";
  128. dynamic model = new ExpandoObject();
  129. model.Users = new List<object>() { new { Name = "Bob", IsGreekCitizen = true }, new { Name = "Malin", IsGreekCitizen = false } };
  130. // When
  131. var output = viewEngine.Render(input, model, this.fakeHost);
  132. // Then
  133. Assert.Equal(@"<html><head></head><body>Yay Users!<ul><li>Bob:<b>Yay Greece!</b></li><li>Malin:</li></ul></body></html>", output);
  134. }
  135. [Fact]
  136. public void Should_evaluate_viewbag_as_dynamic_dictionary_conditional()
  137. {
  138. const string input = @"@Context.ViewBag.HaveMessage;! @If.Context.ViewBag.HaveMessage;Yay message!@EndIf;";
  139. var context = new { ViewBag = (dynamic)new DynamicDictionary() };
  140. context.ViewBag.HaveMessage = true;
  141. ((FakeViewEngineHost)this.fakeHost).Context = context;
  142. var output = viewEngine.Render(input, null, this.fakeHost);
  143. Assert.Equal(@"True! Yay message!", output);
  144. }
  145. [Fact]
  146. public void Should_not_throw_when_viewbag_property_is_null()
  147. {
  148. const string input = @"<html><head></head><body>Hey@If.Context.ViewBag.HaveMessage;Yay message!@EndIf;</body></html>";
  149. var context = new { ViewBag = (dynamic)new DynamicDictionary() };
  150. ((FakeViewEngineHost)this.fakeHost).Context = context;
  151. var output = viewEngine.Render(input, null, this.fakeHost);
  152. Assert.Equal(@"<html><head></head><body>Hey</body></html>", output);
  153. }
  154. [Fact]
  155. public void Should_replace_primitive_model_with_value()
  156. {
  157. // Given
  158. const string input = @"<html><head></head><body>Hello there @Model</body></html>";
  159. const string model = "Bob";
  160. // When
  161. var output = viewEngine.Render(input, model, this.fakeHost);
  162. // Then
  163. Assert.Equal(@"<html><head></head><body>Hello there Bob</body></html>", output);
  164. }
  165. [Fact]
  166. public void Should_replace_primitive_model_with_value_when_followed_by_closing_tag()
  167. {
  168. // Given
  169. const string input = @"<html><head></head><body>Hello there @Model;</body></html>";
  170. const string model = "Bob";
  171. // When
  172. var output = viewEngine.Render(input, model, this.fakeHost);
  173. // Then
  174. Assert.Equal(@"<html><head></head><body>Hello there Bob</body></html>", output);
  175. }
  176. [Fact]
  177. public void Should_replaces_valid_property_when_followed_by_closing_tag()
  178. {
  179. const string input = @"<html><head></head><body>Hello there @Model.Name;</body></html>";
  180. dynamic model = new ExpandoObject();
  181. model.Name = "Bob";
  182. var output = viewEngine.Render(input, model, this.fakeHost);
  183. Assert.Equal(@"<html><head></head><body>Hello there Bob</body></html>", output);
  184. }
  185. [Fact]
  186. public void Should_replace_multiple_properties_with_the_same_name()
  187. {
  188. const string input = @"<html><head></head><body>Hello there @Model.Name;, nice to see you @Model.Name;</body></html>";
  189. dynamic model = new ExpandoObject();
  190. model.Name = "Bob";
  191. var output = viewEngine.Render(input, model, this.fakeHost);
  192. Assert.Equal(@"<html><head></head><body>Hello there Bob, nice to see you Bob</body></html>", output);
  193. }
  194. [Fact]
  195. public void Should_replace_invalid_properties_with_error_string()
  196. {
  197. const string input = @"<html><head></head><body>Hello there @Model.Wrong;</body></html>";
  198. dynamic model = new ExpandoObject();
  199. model.Name = "Bob";
  200. var output = viewEngine.Render(input, model, this.fakeHost);
  201. Assert.Equal(@"<html><head></head><body>Hello there [ERR!]</body></html>", output);
  202. }
  203. [Fact]
  204. public void Should_not_replace_properties_if_case_is_incorrect()
  205. {
  206. const string input = @"<html><head></head><body>Hello there @Model.name;</body></html>";
  207. dynamic model = new ExpandoObject();
  208. model.Name = "Bob";
  209. var output = viewEngine.Render(input, model, this.fakeHost);
  210. Assert.Equal(@"<html><head></head><body>Hello there [ERR!]</body></html>", output);
  211. }
  212. [Fact]
  213. public void Should_replace_multiple_properties_from_dictionary()
  214. {
  215. const string input = @"<html><head></head><body>Hello there @Model.Name; - welcome to @Model.SiteName;</body></html>";
  216. dynamic model = new ExpandoObject();
  217. model.Name = "Bob";
  218. model.SiteName = "Cool Site!";
  219. var output = viewEngine.Render(input, model, this.fakeHost);
  220. Assert.Equal(@"<html><head></head><body>Hello there Bob - welcome to Cool Site!</body></html>", output);
  221. }
  222. [Fact]
  223. public void Should_create_multiple_entries_for_each_statements()
  224. {
  225. const string input = @"<html><head></head><body><ul>@Each.Users;<li>@Current;</li>@EndEach;</ul></body></html>";
  226. dynamic model = new ExpandoObject();
  227. model.Users = new List<string>() { "Bob", "Jim", "Bill" };
  228. var output = viewEngine.Render(input, model, this.fakeHost);
  229. Assert.Equal(@"<html><head></head><body><ul><li>Bob</li><li>Jim</li><li>Bill</li></ul></body></html>", output);
  230. }
  231. [Fact]
  232. public void Should_use_multiple_current_statements_inside_each()
  233. {
  234. const string input = @"<html><head></head><body><ul>@Each.Users;<li id=""@Current;"">@Current;</li>@EndEach;</ul></body></html>";
  235. dynamic model = new ExpandoObject();
  236. model.Users = new List<string>() { "Bob", "Jim", "Bill" };
  237. var output = viewEngine.Render(input, model, this.fakeHost);
  238. Assert.Equal(@"<html><head></head><body><ul><li id=""Bob"">Bob</li><li id=""Jim"">Jim</li><li id=""Bill"">Bill</li></ul></body></html>", output);
  239. }
  240. [Fact]
  241. public void Should_try_to_use_non_enumerable_in_each_shows_error()
  242. {
  243. const string input = @"<html><head></head><body><ul>@Each.Users;<li id=""@Current;"">@Current;</li>@EndEach;</ul></body></html>";
  244. dynamic model = new ExpandoObject();
  245. model.Users = new object();
  246. var output = viewEngine.Render(input, model, this.fakeHost);
  247. Assert.Equal(@"<html><head></head><body><ul>[ERR!]</ul></body></html>", output);
  248. }
  249. [Fact]
  250. public void Should_combine_single_substitutions_and_each_substitutions()
  251. {
  252. const string input = @"<html><head></head><body><ul>@Each.Users;<li>Hello @Current;, @Model.Name; says hello!</li>@EndEach;</ul></body></html>";
  253. dynamic model = new ExpandoObject();
  254. model.Name = "Nancy";
  255. model.Users = new List<string>() { "Bob", "Jim", "Bill" };
  256. var output = viewEngine.Render(input, model, this.fakeHost);
  257. Assert.Equal(@"<html><head></head><body><ul><li>Hello Bob, Nancy says hello!</li><li>Hello Jim, Nancy says hello!</li><li>Hello Bill, Nancy says hello!</li></ul></body></html>", output);
  258. }
  259. [Fact]
  260. public void Should_allow_model_statement_to_be_followed_by_a_newline()
  261. {
  262. const string input = "<html><head></head><body>Hello there @Model.Name;\n</body></html>";
  263. dynamic model = new ExpandoObject();
  264. model.Name = "Bob";
  265. var output = viewEngine.Render(input, model, this.fakeHost);
  266. Assert.Equal("<html><head></head><body>Hello there Bob\n</body></html>", output);
  267. }
  268. [Fact]
  269. public void Should_allow_each_statements_to_work_over_multiple_lines()
  270. {
  271. const string input = "<html>\n\t<head>\n\t</head>\n\t<body>\n\t\t<ul>@Each.Users;\n\t\t\t<li>@Current;</li>@EndEach;\n\t\t</ul>\n\t</body>\n</html>";
  272. dynamic model = new ExpandoObject();
  273. model.Users = new List<string>() { "Bob", "Jim", "Bill" };
  274. var output = viewEngine.Render(input, model, this.fakeHost);
  275. Assert.Equal("<html>\n\t<head>\n\t</head>\n\t<body>\n\t\t<ul>\n\t\t\t<li>Bob</li>\n\t\t\t<li>Jim</li>\n\t\t\t<li>Bill</li>\n\t\t</ul>\n\t</body>\n</html>", output);
  276. }
  277. [Fact]
  278. public void Single_substitutions_work_with_standard_anonymous_type_objects()
  279. {
  280. const string input = @"<html><head></head><body>Hello there @Model.Name; - welcome to @Model.SiteName;</body></html>";
  281. var model = new { Name = "Bob", SiteName = "Cool Site!" };
  282. var output = viewEngine.Render(input, model, this.fakeHost);
  283. Assert.Equal(@"<html><head></head><body>Hello there Bob - welcome to Cool Site!</body></html>", output);
  284. }
  285. [Fact]
  286. public void Should_allow_each_substitutions_to_work_with_standard_anonymous_type_objects()
  287. {
  288. const string input = @"<html><head></head><body><ul>@Each.Users;<li id=""@Current;"">@Current;</li>@EndEach;</ul></body></html>";
  289. var model = new { Users = new List<string>() { "Bob", "Jim", "Bill" } };
  290. var output = viewEngine.Render(input, model, this.fakeHost);
  291. Assert.Equal(@"<html><head></head><body><ul><li id=""Bob"">Bob</li><li id=""Jim"">Jim</li><li id=""Bill"">Bill</li></ul></body></html>", output);
  292. }
  293. [Fact]
  294. public void Should_allow_substitutions_to_work_with_standard_objects()
  295. {
  296. const string input = @"<html><head></head><body><ul>@Each.Users;<li>Hello @Current;, @Model.Name; says hello!</li>@EndEach;</ul></body></html>";
  297. var model = new FakeModel("Nancy", new List<string>() { "Bob", "Jim", "Bill" });
  298. var output = viewEngine.Render(input, model, this.fakeHost);
  299. Assert.Equal(@"<html><head></head><body><ul><li>Hello Bob, Nancy says hello!</li><li>Hello Jim, Nancy says hello!</li><li>Hello Bill, Nancy says hello!</li></ul></body></html>", output);
  300. }
  301. [Fact]
  302. public void Should_render_block_when_if_statement_returns_true()
  303. {
  304. const string input = @"<html><head></head><body>@If.HasUsers;<ul>@Each.Users;<li>Hello @Current;, @Model.Name; says hello!</li>@EndEach;</ul>@EndIf;</body></html>";
  305. var model = new FakeModel("Nancy", new List<string>() { "Bob", "Jim", "Bill" });
  306. var output = viewEngine.Render(input, model, this.fakeHost);
  307. Assert.Equal(@"<html><head></head><body><ul><li>Hello Bob, Nancy says hello!</li><li>Hello Jim, Nancy says hello!</li><li>Hello Bill, Nancy says hello!</li></ul></body></html>", output);
  308. }
  309. [Fact]
  310. public void Should_not_render_block_when_if_statement_returns_false()
  311. {
  312. const string input = @"<html><head></head><body>@If.HasUsers;<ul>@Each.Users;<li>Hello @Current;, @Model.Name; says hello!</li>@EndEach;</ul>@EndIf;</body></html>";
  313. var model = new FakeModel("Nancy", new List<string>());
  314. var output = viewEngine.Render(input, model, this.fakeHost);
  315. Assert.Equal(@"<html><head></head><body></body></html>", output);
  316. }
  317. [Fact]
  318. public void Should_not_render_block_when_ifnot_statements_returns_true()
  319. {
  320. const string input = @"<html><head></head><body>@IfNot.HasUsers;<p>No users found!</p>@EndIf;<ul>@Each.Users;<li>Hello @Current;, @Model.Name; says hello!</li>@EndEach;</ul></body></html>";
  321. var model = new FakeModel("Nancy", new List<string>() { "Bob", "Jim", "Bill" });
  322. var output = viewEngine.Render(input, model, this.fakeHost);
  323. Assert.Equal(@"<html><head></head><body><ul><li>Hello Bob, Nancy says hello!</li><li>Hello Jim, Nancy says hello!</li><li>Hello Bill, Nancy says hello!</li></ul></body></html>", output);
  324. }
  325. [Fact]
  326. public void Should_render_block_when_ifnot_statement_returns_false()
  327. {
  328. const string input = @"<html><head></head><body>@IfNot.HasUsers;<p>No users found!</p>@EndIf;<ul>@Each.Users;<li>Hello @Current;, @Model.Name; says hello!</li>@EndEach;</ul></body></html>";
  329. var model = new FakeModel("Nancy", new List<string>());
  330. var output = viewEngine.Render(input, model, this.fakeHost);
  331. Assert.Equal(@"<html><head></head><body><p>No users found!</p><ul></ul></body></html>", output);
  332. }
  333. [Fact]
  334. public void Should_not_conflict_when_if_and_ifNot_statements_combined_but_not_nested()
  335. {
  336. const string input = @"<html><head></head><body>@IfNot.HasUsers;<p>No users found!</p>@EndIf;@If.HasUsers;<ul>@Each.Users;<li>Hello @Current;, @Model.Name; says hello!</li>@EndEach;</ul>@EndIf;</body></html>";
  337. var model = new FakeModel("Nancy", new List<string>());
  338. var output = viewEngine.Render(input, model, this.fakeHost);
  339. Assert.Equal(@"<html><head></head><body><p>No users found!</p></body></html>", output);
  340. }
  341. [Fact]
  342. public void Should_match_multiple_if_statements_correctly()
  343. {
  344. const string input = "@If.One;<p>One</p>@EndIf; @If.Two;<p>Two</p>@EndIf;";
  345. var model = new { One = true, Two = true };
  346. var output = viewEngine.Render(input, model, this.fakeHost);
  347. Assert.Equal(@"<p>One</p> <p>Two</p>", output);
  348. }
  349. [Fact]
  350. public void Should_match_correctly_when_multiple_each_statements()
  351. {
  352. const string input = "@Each.Users;<li>@Current;</li>@EndEach; @Each.Admins;<li>@Current;</li>@EndEach;";
  353. var model = new { Users = new List<string> { "1", "2" }, Admins = new List<string> { "3", "4" } };
  354. var output = viewEngine.Render(input, model, this.fakeHost);
  355. Assert.Equal(@"<li>1</li><li>2</li> <li>3</li><li>4</li>", output);
  356. }
  357. [Fact]
  358. public void Should_return_true_for_ifhascollection_when_if_model_has_a_collection_with_items_but_no_bool()
  359. {
  360. const string input = @"<html><head></head><body>@If.HasUsers;<ul>@Each.Users;<li>Hello @Current;, @Model.Name; says hello!</li>@EndEach;</ul>@EndIf;</body></html>";
  361. var model = new { Users = new List<string>() { "Bob", "Jim", "Bill" }, Name = "Nancy" };
  362. var output = viewEngine.Render(input, model, this.fakeHost);
  363. Assert.Equal(@"<html><head></head><body><ul><li>Hello Bob, Nancy says hello!</li><li>Hello Jim, Nancy says hello!</li><li>Hello Bill, Nancy says hello!</li></ul></body></html>", output);
  364. }
  365. [Fact]
  366. public void Should_return_false_for_ifnot_hascollection_when_model_has_a_collection_with_items_but_no_bool()
  367. {
  368. const string input = @"<html><head></head><body>@IfNot.HasUsers;<p>No Users!</p>@EndIf;</body></html>";
  369. var model = new { Users = new List<string>() { "Bob", "Jim", "Bill" } };
  370. var output = viewEngine.Render(input, model, this.fakeHost);
  371. Assert.Equal(@"<html><head></head><body></body></html>", output);
  372. }
  373. [Fact]
  374. public void Should_ignore_item_for_implicit_has_support_when_item_isnt_a_collection()
  375. {
  376. const string input = @"<html><head></head><body>@If.HasUsers;<p>Users!</p>@EndIf;</body></html>";
  377. var model = new { Users = new object() };
  378. var output = viewEngine.Render(input, model, this.fakeHost);
  379. Assert.Equal(@"<html><head></head><body></body></html>", output);
  380. }
  381. [Fact]
  382. public void Should_give_precedence_to_hasitem_bool_when_model_has_bool_and_collection()
  383. {
  384. const string input = @"<html><head></head><body>@If.HasUsers;<ul>@Each.Users;<li>Hello @Current;, @Model.Name; says hello!</li>@EndEach;</ul>@EndIf;</body></html>";
  385. var model = new { HasUsers = false, Users = new List<string>() { "Bob", "Jim", "Bill" }, Name = "Nancy" };
  386. var output = viewEngine.Render(input, model, this.fakeHost);
  387. Assert.Equal(@"<html><head></head><body></body></html>", output);
  388. }
  389. [Fact]
  390. public void Should_allow_property_name_in_current_inside_each_loop_for_dynamic_model_and_dynamic_collection()
  391. {
  392. const string input = @"<html><head></head><body><ul>@Each.Users;<li>@Current.Name;</li>@EndEach;</ul></body></html>";
  393. dynamic model = new ExpandoObject();
  394. dynamic user1 = new ExpandoObject();
  395. user1.Name = "Bob";
  396. dynamic user2 = new ExpandoObject();
  397. user2.Name = "Jim";
  398. dynamic user3 = new ExpandoObject();
  399. user3.Name = "Bill";
  400. model.Users = new List<object>() { user1, user2, user3 };
  401. var output = viewEngine.Render(input, model, this.fakeHost);
  402. Assert.Equal(@"<html><head></head><body><ul><li>Bob</li><li>Jim</li><li>Bill</li></ul></body></html>", output);
  403. }
  404. [Fact]
  405. public void Should_allow_property_name_in_current_inside_each_loop_for_dynamic_model_and_normal_collection()
  406. {
  407. const string input = @"<html><head></head><body><ul>@Each.Users;<li>@Current.Name;</li>@EndEach;</ul></body></html>";
  408. dynamic model = new ExpandoObject();
  409. model.Users = new List<User>() { new User("Bob"), new User("Jim"), new User("Bill") };
  410. var output = viewEngine.Render(input, model, this.fakeHost);
  411. Assert.Equal(@"<html><head></head><body><ul><li>Bob</li><li>Jim</li><li>Bill</li></ul></body></html>", output);
  412. }
  413. [Fact]
  414. public void Should_allow_property_name_in_current_inside_each_loop_for_normal_model_and_dynamic_collection()
  415. {
  416. const string input = @"<html><head></head><body><ul>@Each.Users;<li>@Current.Name;</li>@EndEach;</ul></body></html>";
  417. dynamic user1 = new ExpandoObject();
  418. user1.Name = "Bob";
  419. dynamic user2 = new ExpandoObject();
  420. user2.Name = "Jim";
  421. dynamic user3 = new ExpandoObject();
  422. user3.Name = "Bill";
  423. var model = new { Users = new List<object> { user1, user2, user3 } };
  424. var output = viewEngine.Render(input, model, this.fakeHost);
  425. Assert.Equal(@"<html><head></head><body><ul><li>Bob</li><li>Jim</li><li>Bill</li></ul></body></html>", output);
  426. }
  427. [Fact]
  428. public void Should_allow_sub_properties_using_model_statement()
  429. {
  430. const string input = @"<h1>Hello @Model.User.Name;</h1>";
  431. var model = new { User = new User("Bob") };
  432. var output = viewEngine.Render(input, model, this.fakeHost);
  433. Assert.Equal(@"<h1>Hello Bob</h1>", output);
  434. }
  435. [Fact]
  436. public void Should_allow_sub_properties_using_if_statement()
  437. {
  438. const string input = @"<h1>Hello @If.User.IsFriend;Friend!@EndIf;</h1>";
  439. var model = new { User = new User("Bob", true) };
  440. var output = viewEngine.Render(input, model, this.fakeHost);
  441. Assert.Equal(@"<h1>Hello Friend!</h1>", output);
  442. }
  443. [Fact]
  444. public void Should_allow_sub_properties_using_ifnot_statement()
  445. {
  446. const string input = @"<h1>Hello @IfNot.User.IsFriend;Friend!@EndIf;</h1>";
  447. var model = new { User = new User("Bob", true) };
  448. var output = viewEngine.Render(input, model, this.fakeHost);
  449. Assert.Equal(@"<h1>Hello </h1>", output);
  450. }
  451. [Fact]
  452. public void Should_allow_sub_properties_for_each_statement()
  453. {
  454. const string input = @"<html><head></head><body><ul>@Each.Sub.Users;<li>@Current;</li>@EndEach;</ul></body></html>";
  455. var model = new { Sub = new { Users = new List<string>() { "Bob", "Jim", "Bill" } } };
  456. var output = viewEngine.Render(input, model, this.fakeHost);
  457. Assert.Equal(@"<html><head></head><body><ul><li>Bob</li><li>Jim</li><li>Bill</li></ul></body></html>", output);
  458. }
  459. [Fact]
  460. public void Should_allow_sub_properties_for_current_statement_inside_each()
  461. {
  462. const string input = @"<html><head></head><body><ul>@Each.Users;<li>@Current.Item2.Name;</li>@EndEach;</ul></body></html>";
  463. var model = new { Users = new List<Tuple<int, User>>() { new Tuple<int, User>(1, new User("Bob")), new Tuple<int, User>(1, new User("Jim")), new Tuple<int, User>(1, new User("Bill")) } };
  464. var output = viewEngine.Render(input, model, this.fakeHost);
  465. Assert.Equal(@"<html><head></head><body><ul><li>Bob</li><li>Jim</li><li>Bill</li></ul></body></html>", output);
  466. }
  467. [Fact]
  468. public void Should_allow_Model_substitutions_wihout_semi_colon()
  469. {
  470. const string input = @"<html><head></head><body>Hello there @Model.Name</body></html>";
  471. dynamic model = new ExpandoObject();
  472. model.Name = "Bob";
  473. var output = viewEngine.Render(input, model, this.fakeHost);
  474. Assert.Equal(@"<html><head></head><body>Hello there Bob</body></html>", output);
  475. }
  476. [Fact]
  477. public void Should_allow_each_without_semi_colon()
  478. {
  479. const string input = @"<html><head></head><body><ul>@Each.Users<li>@Current;</li>@EndEach;</ul></body></html>";
  480. dynamic model = new ExpandoObject();
  481. model.Users = new List<string>() { "Bob", "Jim", "Bill" };
  482. var output = viewEngine.Render(input, model, this.fakeHost);
  483. Assert.Equal(@"<html><head></head><body><ul><li>Bob</li><li>Jim</li><li>Bill</li></ul></body></html>", output);
  484. }
  485. [Fact]
  486. public void Should_allow_each_and_end_each_without_semi_colon()
  487. {
  488. const string input = @"<html><head></head><body><ul>@Each.Users<li>@Current;</li>@EndEach</ul></body></html>";
  489. dynamic model = new ExpandoObject();
  490. model.Users = new List<string>() { "Bob", "Jim", "Bill" };
  491. var output = viewEngine.Render(input, model, this.fakeHost);
  492. Assert.Equal(@"<html><head></head><body><ul><li>Bob</li><li>Jim</li><li>Bill</li></ul></body></html>", output);
  493. }
  494. [Fact]
  495. public void Should_allow_current_within_each_without_semi_colon()
  496. {
  497. const string input = @"<html><head></head><body><ul>@Each.Users;<li>@Current</li>@EndEach;</ul></body></html>";
  498. dynamic model = new ExpandoObject();
  499. model.Users = new List<string>() { "Bob", "Jim", "Bill" };
  500. var output = viewEngine.Render(input, model, this.fakeHost);
  501. Assert.Equal(@"<html><head></head><body><ul><li>Bob</li><li>Jim</li><li>Bill</li></ul></body></html>", output);
  502. }
  503. [Fact]
  504. public void Should_allow_if_and_endif_without_semi_colon()
  505. {
  506. const string input = @"<html><head></head><body>@If.HasUsers<ul>@Each.Users;<li>Hello @Current;, @Model.Name; says hello!</li>@EndEach;</ul>@EndIf</body></html>";
  507. var model = new FakeModel("Nancy", new List<string>() { "Bob", "Jim", "Bill" });
  508. var output = viewEngine.Render(input, model, this.fakeHost);
  509. Assert.Equal(@"<html><head></head><body><ul><li>Hello Bob, Nancy says hello!</li><li>Hello Jim, Nancy says hello!</li><li>Hello Bill, Nancy says hello!</li></ul></body></html>", output);
  510. }
  511. [Fact]
  512. public void Should_allow_ifnot_and_endif_without_semi_colon()
  513. {
  514. const string input = @"<html><head></head><body>@IfNot.HasUsers<p>No users found!</p>@EndIf<ul>@Each.Users;<li>Hello @Current;, @Model.Name; says hello!</li>@EndEach;</ul></body></html>";
  515. var model = new FakeModel("Nancy", new List<string>() { "Bob", "Jim", "Bill" });
  516. var output = viewEngine.Render(input, model, this.fakeHost);
  517. Assert.Equal(@"<html><head></head><body><ul><li>Hello Bob, Nancy says hello!</li><li>Hello Jim, Nancy says hello!</li><li>Hello Bill, Nancy says hello!</li></ul></body></html>", output);
  518. }
  519. [Fact]
  520. public void Should_allow_each_without_a_variable_and_iterate_over_the_model_if_it_is_enumerable()
  521. {
  522. const string input = @"<html><head></head><body><ul>@Each<li>Hello @Current</li>@EndEach</ul></body></html>";
  523. var model = new List<string>() { "Bob", "Jim", "Bill" };
  524. var output = viewEngine.Render(input, model, this.fakeHost);
  525. Assert.Equal(@"<html><head></head><body><ul><li>Hello Bob</li><li>Hello Jim</li><li>Hello Bill</li></ul></body></html>", output);
  526. }
  527. [Fact]
  528. public void Should_allow_variableless_each_with_semicolon()
  529. {
  530. const string input = @"<html><head></head><body><ul>@Each;<li>Hello @Current</li>@EndEach</ul></body></html>";
  531. var model = new List<string>() { "Bob", "Jim", "Bill" };
  532. var output = viewEngine.Render(input, model, this.fakeHost);
  533. Assert.Equal(@"<html><head></head><body><ul><li>Hello Bob</li><li>Hello Jim</li><li>Hello Bill</li></ul></body></html>", output);
  534. }
  535. [Fact]
  536. public void Model_with_exclaimation_should_html_encode()
  537. {
  538. const string input = @"<html><head></head><body>Hello there @!Model.Name;</body></html>";
  539. dynamic model = new ExpandoObject();
  540. model.Name = "<b>Bob</b>";
  541. var output = viewEngine.Render(input, model, this.fakeHost);
  542. Assert.Equal(@"<html><head></head><body>Hello there &lt;b&gt;Bob&lt;/b&gt;</body></html>", output);
  543. }
  544. [Fact]
  545. public void Current_with_exclaimation_and_no_parameters_should_html_encode()
  546. {
  547. const string input = @"<html><head></head><body><ul>@Each;<li>Hello @!Current</li>@EndEach</ul></body></html>";
  548. var model = new List<string>() { "Bob<br/>", "Jim<br/>", "Bill<br/>" };
  549. var output = viewEngine.Render(input, model, this.fakeHost);
  550. Assert.Equal(@"<html><head></head><body><ul><li>Hello Bob&lt;br/&gt;</li><li>Hello Jim&lt;br/&gt;</li><li>Hello Bill&lt;br/&gt;</li></ul></body></html>", output);
  551. }
  552. [Fact]
  553. public void Current_with_explaimation_and_parameters_should_html_encode()
  554. {
  555. const string input = @"<html><head></head><body><ul>@Each.Users;<li>@!Current.Name;</li>@EndEach;</ul></body></html>";
  556. dynamic model = new ExpandoObject();
  557. dynamic user1 = new ExpandoObject();
  558. user1.Name = "Bob<br/>";
  559. dynamic user2 = new ExpandoObject();
  560. user2.Name = "Jim<br/>";
  561. dynamic user3 = new ExpandoObject();
  562. user3.Name = "Bill<br/>";
  563. model.Users = new List<object>() { user1, user2, user3 };
  564. var output = viewEngine.Render(input, model, this.fakeHost);
  565. Assert.Equal(@"<html><head></head><body><ul><li>Bob&lt;br/&gt;</li><li>Jim&lt;br/&gt;</li><li>Bill&lt;br/&gt;</li></ul></body></html>", output);
  566. }
  567. [Fact]
  568. public void Should_expand_basic_partials()
  569. {
  570. const string input = @"<html><head></head><body>@Partial['testing'];</body></html>";
  571. var fakeViewEngineHost = new FakeViewEngineHost();
  572. fakeViewEngineHost.GetTemplateCallback = (s, m) => "Test partial content";
  573. var result = viewEngine.Render(input, new object(), fakeViewEngineHost);
  574. Assert.Equal(@"<html><head></head><body>Test partial content</body></html>", result);
  575. }
  576. [Fact]
  577. public void Should_expand_partial_content_with_model_if_no_model_specified()
  578. {
  579. const string input = @"<html><head></head><body>@Partial['testing'];</body></html>";
  580. var fakeViewEngineHost = new FakeViewEngineHost();
  581. fakeViewEngineHost.GetTemplateCallback = (s, m) => "Hello @Model.Name";
  582. dynamic model = new ExpandoObject();
  583. model.Name = "Bob";
  584. var result = viewEngine.Render(input, model, fakeViewEngineHost);
  585. Assert.Equal(@"<html><head></head><body>Hello Bob</body></html>", result);
  586. }
  587. [Fact]
  588. public void Should_expand_partial_content_with_specified_model_property_if_specified()
  589. {
  590. const string input = @"<html><head></head><body>@Partial['testing', Model.User];</body></html>";
  591. var fakeViewEngineHost = new FakeViewEngineHost();
  592. fakeViewEngineHost.GetTemplateCallback = (s, m) => "Hello @Model.Name";
  593. dynamic model = new ExpandoObject();
  594. dynamic subModel = new ExpandoObject();
  595. model.Name = "Jim";
  596. subModel.Name = "Bob";
  597. model.User = subModel;
  598. var result = viewEngine.Render(input, model, fakeViewEngineHost);
  599. Assert.Equal(@"<html><head></head><body>Hello Bob</body></html>", result);
  600. }
  601. [Fact]
  602. public void Should_expand_partial_content_even_with_no_model()
  603. {
  604. const string input = @"<html><head></head><body>@Partial['testing'];</body></html>";
  605. var fakeViewEngineHost = new FakeViewEngineHost();
  606. fakeViewEngineHost.GetTemplateCallback = (s, m) => "Test partial content";
  607. var result = this.viewEngine.Render(input, null, fakeViewEngineHost);
  608. Assert.Equal(@"<html><head></head><body>Test partial content</body></html>", result);
  609. }
  610. [Fact]
  611. public void Should_try_to_locate_master_page_if_one_specified()
  612. {
  613. const string input = "@Master['myMaster']\r\n@Section['Header'];\r\nHeader\r\n@EndSection\r\n@Section['Footer']\r\nFooter\r\n@EndSection";
  614. var called = false;
  615. var fakeViewEngineHost = new FakeViewEngineHost();
  616. fakeViewEngineHost.GetTemplateCallback = (s, m) =>
  617. {
  618. called = (s == "myMaster");
  619. return "";
  620. };
  621. this.viewEngine.Render(input, null, fakeViewEngineHost);
  622. Assert.True(called);
  623. }
  624. [Fact]
  625. public void Should_replace_sections_in_master_page()
  626. {
  627. const string input = "@Master['myMaster']\r\n@Section['Header'];\r\nHeader\r\n@EndSection\r\n@Section['Footer']\r\nFooter\r\n@EndSection";
  628. const string master = @"<div id='header'>@Section['Header'];</div><div id='footer'>@Section['Footer'];</div>";
  629. var fakeViewEngineHost = new FakeViewEngineHost();
  630. fakeViewEngineHost.GetTemplateCallback = (s, m) => master;
  631. var result = this.viewEngine.Render(input, null, fakeViewEngineHost);
  632. Assert.Equal("<div id='header'>\r\nHeader\r\n</div><div id='footer'>\r\nFooter\r\n</div>", result);
  633. }
  634. [Fact]
  635. public void Should_also_expand_master_page_with_same_model()
  636. {
  637. const string input = "@Master['myMaster']\r\n@Section['Header'];\r\nHeader\r\n@EndSection\r\n@Section['Footer']\r\nFooter\r\n@EndSection";
  638. const string master = @"Hello @Model.Name!<div id='header'>@Section['Header'];</div><div id='footer'>@Section['Footer'];</div>";
  639. var fakeViewEngineHost = new FakeViewEngineHost();
  640. fakeViewEngineHost.GetTemplateCallback = (s, m) => master;
  641. var result = viewEngine.Render(input, new { Name = "Bob" }, fakeViewEngineHost);
  642. Assert.Equal("Hello Bob!<div id='header'>\r\nHeader\r\n</div><div id='footer'>\r\nFooter\r\n</div>", result);
  643. }
  644. [Fact]
  645. public void Should_handle_master_page_hierarchies()
  646. {
  647. const string input = "@Master['middle']\r\n@Section['MiddleContent']Middle@EndSection";
  648. const string middle = "@Master['top']\r\n@Section['TopContent']Top\r\n@Section['MiddleContent']@EndSection";
  649. const string top = "Top! @Section['TopContent']";
  650. var fakeViewEngineHost = new FakeViewEngineHost();
  651. fakeViewEngineHost.GetTemplateCallback = (s, m) => s == "middle" ? middle : top;
  652. var result = viewEngine.Render(input, null, fakeViewEngineHost);
  653. Assert.Equal("Top! Top\r\nMiddle", result);
  654. }
  655. [Fact]
  656. public void Should_call_to_expand_paths()
  657. {
  658. const string input = @"<script src='@Path['~/scripts/test.js']'></script>";
  659. var fakeViewEngineHost = new FakeViewEngineHost();
  660. fakeViewEngineHost.ExpandPathCallBack = s => s.Replace("~/", "/BasePath/");
  661. var result = viewEngine.Render(input, null, fakeViewEngineHost);
  662. Assert.Equal("<script src='/BasePath/scripts/test.js'></script>", result);
  663. }
  664. [Fact]
  665. public void Should_expand_anti_forgery_tokens()
  666. {
  667. const string input = "<html><body><form>@AntiForgeryToken</form><body></html>";
  668. var fakeViewEngineHost = new FakeViewEngineHost();
  669. var result = viewEngine.Render(input, null, fakeViewEngineHost);
  670. Assert.Equal("<html><body><form>CSRF</form><body></html>", result);
  671. }
  672. [Fact]
  673. public void Should_replace_primitive_context_with_value()
  674. {
  675. // Given
  676. const string input = @"<html><head></head><body>Hello there @Context</body></html>";
  677. ((FakeViewEngineHost)this.fakeHost).Context = "Frank";
  678. // When
  679. var output = viewEngine.Render(input, null, this.fakeHost);
  680. // Then
  681. Assert.Equal(@"<html><head></head><body>Hello there Frank</body></html>", output);
  682. }
  683. [Fact]
  684. public void Should_replace_primitive_context_with_value_when_followed_by_closing_tag()
  685. {
  686. // Given
  687. const string input = @"<html><head></head><body>Hello there @Context;</body></html>";
  688. ((FakeViewEngineHost)this.fakeHost).Context = "Frank";
  689. // When
  690. var output = viewEngine.Render(input, null, this.fakeHost);
  691. // Then
  692. Assert.Equal(@"<html><head></head><body>Hello there Frank</body></html>", output);
  693. }
  694. [Fact]
  695. public void Should_replaces_valid_context_property_when_followed_by_closing_tag()
  696. {
  697. const string input = @"<html><head></head><body>Hello there @Context.Name;</body></html>";
  698. dynamic context = new ExpandoObject();
  699. context.Name = "Frank";
  700. ((FakeViewEngineHost)this.fakeHost).Context = context;
  701. var output = viewEngine.Render(input, null, this.fakeHost);
  702. Assert.Equal(@"<html><head></head><body>Hello there Frank</body></html>", output);
  703. }
  704. [Fact]
  705. public void Should_replace_multiple_context_properties_with_the_same_name()
  706. {
  707. const string input = @"<html><head></head><body>Hello there @Context.Name;, nice to see you @Context.Name;</body></html>";
  708. dynamic context = new ExpandoObject();
  709. context.Name = "Frank";
  710. ((FakeViewEngineHost)this.fakeHost).Context = context;
  711. var output = viewEngine.Render(input, null, this.fakeHost);
  712. Assert.Equal(@"<html><head></head><body>Hello there Frank, nice to see you Frank</body></html>", output);
  713. }
  714. [Fact]
  715. public void Should_replace_invalid_context_properties_with_error_string()
  716. {
  717. const string input = @"<html><head></head><body>Hello there @Context.Wrong;</body></html>";
  718. dynamic context = new ExpandoObject();
  719. context.Name = "Frank";
  720. ((FakeViewEngineHost)this.fakeHost).Context = context;
  721. var output = viewEngine.Render(input, null, this.fakeHost);
  722. Assert.Equal(@"<html><head></head><body>Hello there [ERR!]</body></html>", output);
  723. }
  724. [Fact]
  725. public void Should_not_replace_context_properties_if_case_is_incorrect()
  726. {
  727. const string input = @"<html><head></head><body>Hello there @Context.name;</body></html>";
  728. dynamic context = new ExpandoObject();
  729. context.Name = "Franke";
  730. ((FakeViewEngineHost)this.fakeHost).Context = context;
  731. var output = viewEngine.Render(input, null, this.fakeHost);
  732. Assert.Equal(@"<html><head></head><body>Hello there [ERR!]</body></html>", output);
  733. }
  734. [Fact]
  735. public void Should_replace_multiple_context_properties_from_dictionary()
  736. {
  737. const string input = @"<html><head></head><body>Hello there @Context.Name; - welcome to @Context.SiteName;</body></html>";
  738. dynamic context = new ExpandoObject();
  739. context.Name = "Frank";
  740. context.SiteName = "Cool Site!";
  741. ((FakeViewEngineHost)this.fakeHost).Context = context;
  742. var output = viewEngine.Render(input, null, this.fakeHost);
  743. Assert.Equal(@"<html><head></head><body>Hello there Frank - welcome to Cool Site!</body></html>", output);
  744. }
  745. [Fact]
  746. public void Should_allow_context_statement_to_be_followed_by_a_newline()
  747. {
  748. const string input = "<html><head></head><body>Hello there @Context.Name;\n</body></html>";
  749. var output = viewEngine.Render(input, null, this.fakeHost);
  750. Assert.Equal("<html><head></head><body>Hello there Frank\n</body></html>", output);
  751. }
  752. [Fact]
  753. public void Context_substitutions_work_with_standard_anonymous_type_objects()
  754. {
  755. const string input = @"<html><head></head><body>Hello there @Context.Name; - welcome to @Context.SiteName;</body></html>";
  756. var context = new { Name = "Bob", SiteName = "Cool Site!" };
  757. ((FakeViewEngineHost)this.fakeHost).Context = context;
  758. var output = viewEngine.Render(input, null, this.fakeHost);
  759. Assert.Equal(@"<html><head></head><body>Hello there Bob - welcome to Cool Site!</body></html>", output);
  760. }
  761. [Fact]
  762. public void Should_allow_sub_properties_using_context_statement()
  763. {
  764. const string input = @"<h1>Hello @Context.User.Username;</h1>";
  765. var output = viewEngine.Render(input, null, this.fakeHost);
  766. Assert.Equal(@"<h1>Hello Frank123</h1>", output);
  767. }
  768. [Fact]
  769. public void Should_allow_Context_substitutions_wihout_semi_colon()
  770. {
  771. const string input = @"<html><head></head><body>Hello there @Context.Name</body></html>";
  772. var output = viewEngine.Render(input, null, this.fakeHost);
  773. Assert.Equal(@"<html><head></head><body>Hello there Frank</body></html>", output);
  774. }
  775. [Fact]
  776. public void Should_expand_partial_content_with_context()
  777. {
  778. const string input = @"<html><head></head><body>@Partial['testing'];</body></html>";
  779. var fakeViewEngineHost = new FakeViewEngineHost();
  780. fakeViewEngineHost.GetTemplateCallback = (s, m) => "Hello @Context.Name";
  781. var result = viewEngine.Render(input, null, fakeViewEngineHost);
  782. Assert.Equal(@"<html><head></head><body>Hello Frank</body></html>", result);
  783. }
  784. [Fact]
  785. public void Should_also_expand_master_page_with_same_context()
  786. {
  787. const string input = "@Master['myMaster']\r\n@Section['Header'];\r\nHeader\r\n@EndSection\r\n@Section['Footer']\r\nFooter\r\n@EndSection";
  788. const string master = @"Hello @Context.Name!<div id='header'>@Section['Header'];</div><div id='footer'>@Section['Footer'];</div>";
  789. var fakeViewEngineHost = new FakeViewEngineHost();
  790. fakeViewEngineHost.GetTemplateCallback = (s, m) => master;
  791. var result = viewEngine.Render(input, null, fakeViewEngineHost);
  792. Assert.Equal("Hello Frank!<div id='header'>\r\nHeader\r\n</div><div id='footer'>\r\nFooter\r\n</div>", result);
  793. }
  794. [Fact]
  795. public void Should_stuffrender_block_when_ifnot_statement_returns_false()
  796. {
  797. const string input = @"<html><head></head><body>@IfNot.Context.HasUsers;<p>No users found!</p>@EndIf;</body></html>";
  798. var model = new FakeModel("Nancy", new List<string>() { "Nancy " });
  799. ((FakeViewEngineHost)this.fakeHost).Context = new FakeModel("NancyContext", new List<string>());
  800. var output = viewEngine.Render(input, model, this.fakeHost);
  801. Assert.Equal(@"<html><head></head><body><p>No users found!</p></body></html>", output);
  802. }
  803. [Fact]
  804. public void Should_allow_ifnot_and_endif_and_context_model_source()
  805. {
  806. const string input = @"<html><head></head><body>@IfNot.Context.HasUsers;No users found!@EndIf</body></html>";
  807. var model = new FakeModel("Nancy", new List<string>() { "Bob", "Jim", "Bill" });
  808. ((FakeViewEngineHost)this.fakeHost).Context = new FakeModel("NancyContext", new List<string>());
  809. var output = viewEngine.Render(input, model, this.fakeHost);
  810. Assert.Equal(@"<html><head></head><body>No users found!</body></html>", output);
  811. }
  812. [Fact]
  813. public void Should_allow_ifnot_and_endif_and_model_model_source()
  814. {
  815. const string input = @"<html><head></head><body>@IfNot.Model.HasUsers;No users found!@EndIf</body></html>";
  816. var model = new FakeModel("Nancy", new List<string>());
  817. ((FakeViewEngineHost)this.fakeHost).Context = new FakeModel("NancyContext", new List<string>() { "Bob", "Jim", "Bill" });
  818. var output = viewEngine.Render(input, model, this.fakeHost);
  819. Assert.Equal(@"<html><head></head><body>No users found!</body></html>", output);
  820. }
  821. [Fact]
  822. public void Should_allow_ifnot_and_endif_and_implicit_model_model_source()
  823. {
  824. const string input = @"<html><head></head><body>@IfNot.HasUsers;No…

Large files files are truncated, but you can click here to view the full file