PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/NancyFx/Nancy
C# | 1494 lines | 1081 code | 371 blank | 42 comment | 3 complexity | bf70d63286a45465ef96361a921dcb43 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-3.0, Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.0, BSD-3-Clause

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

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