Guest User

Untitled

a guest
Jul 23rd, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.50 KB | None | 0 0
  1. namespace TinyTemplates.Tests.ViewEngine
  2. {
  3. using System.Dynamic;
  4. using System.Linq;
  5. using TinyTemplates.ViewEngine;
  6. using Xunit;
  7.  
  8. using System.Collections.Generic;
  9.  
  10. public class SuperSimpleViewEngineTests
  11. {
  12. public class FakeModel
  13. {
  14. public FakeModel(string name, List<string> users)
  15. {
  16. this.Name = name;
  17. this.Users = users;
  18. }
  19.  
  20. public List<string> Users { get; private set; }
  21.  
  22. public string Name { get; private set; }
  23.  
  24. public bool HasUsers
  25. {
  26. get
  27. {
  28. return this.Users.Any();
  29. }
  30. }
  31. }
  32.  
  33. private SuperSimpleViewEngine viewEngine;
  34.  
  35. public SuperSimpleViewEngineTests()
  36. {
  37. this.viewEngine = new SuperSimpleViewEngine();
  38. }
  39.  
  40. [Fact]
  41. public void Replaces_valid_property_when_followed_by_closing_tag()
  42. {
  43. var input = @"<html><head></head><body>Hello there @Model.Name</body></html>";
  44. dynamic model = new ExpandoObject();
  45. model.Name = "Bob";
  46.  
  47. var output = viewEngine.Render(input, model);
  48.  
  49. Assert.Equal(@"<html><head></head><body>Hello there Bob</body></html>", output);
  50. }
  51.  
  52. [Fact]
  53. public void Replaces_multiple_properties_with_the_same_name()
  54. {
  55. var input = @"<html><head></head><body>Hello there @Model.Name, nice to see you @Model.Name</body></html>";
  56. dynamic model = new ExpandoObject();
  57. model.Name = "Bob";
  58.  
  59. var output = viewEngine.Render(input, model);
  60.  
  61. Assert.Equal(@"<html><head></head><body>Hello there Bob, nice to see you Bob</body></html>", output);
  62. }
  63.  
  64. [Fact]
  65. public void Replaces_invalid_properties_with_error_string()
  66. {
  67. var input = @"<html><head></head><body>Hello there @Model.Wrong</body></html>";
  68. dynamic model = new ExpandoObject();
  69. model.Name = "Bob";
  70.  
  71. var output = viewEngine.Render(input, model);
  72.  
  73. Assert.Equal(@"<html><head></head><body>Hello there [ERR!]</body></html>", output);
  74. }
  75.  
  76. [Fact]
  77. public void Does_not_replace_properties_if_case_is_incorrect()
  78. {
  79. var input = @"<html><head></head><body>Hello there @Model.name</body></html>";
  80. dynamic model = new ExpandoObject();
  81. model.Name = "Bob";
  82.  
  83. var output = viewEngine.Render(input, model);
  84.  
  85. Assert.Equal(@"<html><head></head><body>Hello there [ERR!]</body></html>", output);
  86. }
  87.  
  88. [Fact]
  89. public void Replaces_multiple_properties_from_dictionary()
  90. {
  91. var input = @"<html><head></head><body>Hello there @Model.Name - welcome to @Model.SiteName</body></html>";
  92. dynamic model = new ExpandoObject();
  93. model.Name = "Bob";
  94. model.SiteName = "Cool Site!";
  95.  
  96. var output = viewEngine.Render(input, model);
  97.  
  98. Assert.Equal(@"<html><head></head><body>Hello there Bob - welcome to Cool Site!</body></html>", output);
  99. }
  100.  
  101. [Fact]
  102. public void Creates_multiple_entries_for_each_statements()
  103. {
  104. var input = @"<html><head></head><body><ul>@Each.Users<li>@Current</li>@EndEach</ul></body></html>";
  105. dynamic model = new ExpandoObject();
  106. model.Users = new List<string>() { "Bob", "Jim", "Bill" };
  107.  
  108. var output = viewEngine.Render(input, model);
  109.  
  110. Assert.Equal(@"<html><head></head><body><ul><li>Bob</li><li>Jim</li><li>Bill</li></ul></body></html>", output);
  111. }
  112.  
  113. [Fact]
  114. public void Can_use_multiple_current_statements_inside_each()
  115. {
  116. var input = @"<html><head></head><body><ul>@Each.Users<li id=""@Current"">@Current</li>@EndEach</ul></body></html>";
  117. dynamic model = new ExpandoObject();
  118. model.Users = new List<string>() { "Bob", "Jim", "Bill" };
  119.  
  120. var output = viewEngine.Render(input, model);
  121.  
  122. 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);
  123. }
  124.  
  125. [Fact]
  126. public void Trying_to_use_non_enumerable_in_each_shows_error()
  127. {
  128. var input = @"<html><head></head><body><ul>@Each.Users<li id=""@Current"">@Current</li>@EndEach</ul></body></html>";
  129. dynamic model = new ExpandoObject();
  130. model.Users = new object();
  131.  
  132. var output = viewEngine.Render(input, model);
  133.  
  134. Assert.Equal(@"<html><head></head><body><ul>[ERR!]</ul></body></html>", output);
  135. }
  136.  
  137. [Fact]
  138. public void Can_combine_single_substitutions_and_each_substitutions()
  139. {
  140. var input = @"<html><head></head><body><ul>@Each.Users<li>Hello @Current, @Model.Name says hello!</li>@EndEach</ul></body></html>";
  141. dynamic model = new ExpandoObject();
  142. model.Name = "Nancy";
  143. model.Users = new List<string>() { "Bob", "Jim", "Bill" };
  144.  
  145. var output = viewEngine.Render(input, model);
  146.  
  147. 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);
  148. }
  149.  
  150. [Fact]
  151. public void Model_statement_can_be_followed_by_a_newline()
  152. {
  153. var input = "<html><head></head><body>Hello there @Model.Name\n</body></html>";
  154. dynamic model = new ExpandoObject();
  155. model.Name = "Bob";
  156.  
  157. var output = viewEngine.Render(input, model);
  158.  
  159. Assert.Equal("<html><head></head><body>Hello there Bob\n</body></html>", output);
  160. }
  161.  
  162. [Fact]
  163. public void Each_statements_should_work_over_multiple_lines()
  164. {
  165. var 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>";
  166. dynamic model = new ExpandoObject();
  167. model.Users = new List<string>() { "Bob", "Jim", "Bill" };
  168.  
  169. var output = viewEngine.Render(input, model);
  170.  
  171. 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);
  172. }
  173.  
  174. [Fact]
  175. public void Single_substitutions_work_with_standard_anonymous_type_objects()
  176. {
  177. var input = @"<html><head></head><body>Hello there @Model.Name - welcome to @Model.SiteName</body></html>";
  178. var model = new { Name = "Bob", SiteName = "Cool Site!" };
  179.  
  180. var output = viewEngine.Render(input, model);
  181.  
  182. Assert.Equal(@"<html><head></head><body>Hello there Bob - welcome to Cool Site!</body></html>", output);
  183. }
  184.  
  185. [Fact]
  186. public void Each_substitutions_work_with_standard_anonymous_type_objects()
  187. {
  188. var input = @"<html><head></head><body><ul>@Each.Users<li id=""@Current"">@Current</li>@EndEach</ul></body></html>";
  189. var model = new { Users = new List<string>() { "Bob", "Jim", "Bill" } };
  190.  
  191. var output = viewEngine.Render(input, model);
  192.  
  193. 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);
  194. }
  195.  
  196. [Fact]
  197. public void Substitutions_work_with_standard_objects()
  198. {
  199. var input = @"<html><head></head><body><ul>@Each.Users<li>Hello @Current, @Model.Name says hello!</li>@EndEach</ul></body></html>";
  200. var model = new FakeModel("Nancy", new List<string>() { "Bob", "Jim", "Bill" });
  201.  
  202. var output = viewEngine.Render(input, model);
  203.  
  204. 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);
  205. }
  206.  
  207. [Fact]
  208. public void If_statement_with_true_returned_renders_block()
  209. {
  210. var input = @"<html><head></head><body>@If.HasUsers<ul>@Each.Users<li>Hello @Current, @Model.Name says hello!</li>@EndEach</ul>@EndIf</body></html>";
  211. var model = new FakeModel("Nancy", new List<string>() { "Bob", "Jim", "Bill" });
  212.  
  213. var output = viewEngine.Render(input, model);
  214.  
  215. 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);
  216. }
  217.  
  218. [Fact]
  219. public void If_statement_with_false_returned_does_not_render_block()
  220. {
  221. var input = @"<html><head></head><body>@If.HasUsers<ul>@Each.Users<li>Hello @Current, @Model.Name says hello!</li>@EndEach</ul>@EndIf</body></html>";
  222. var model = new FakeModel("Nancy", new List<string>());
  223.  
  224. var output = viewEngine.Render(input, model);
  225.  
  226. Assert.Equal(@"<html><head></head><body></body></html>", output);
  227. }
  228.  
  229. [Fact]
  230. public void IfNot_statement_with_true_returned_does_not_renders_block()
  231. {
  232. var 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>";
  233. var model = new FakeModel("Nancy", new List<string>() { "Bob", "Jim", "Bill" });
  234.  
  235. var output = viewEngine.Render(input, model);
  236.  
  237. 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);
  238. }
  239.  
  240. [Fact]
  241. public void IfNot_statement_with_false_returned_renders_block()
  242. {
  243. var 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>";
  244. var model = new FakeModel("Nancy", new List<string>());
  245.  
  246. var output = viewEngine.Render(input, model);
  247.  
  248. Assert.Equal(@"<html><head></head><body><p>No users found!</p><ul></ul></body></html>", output);
  249. }
  250.  
  251. [Fact]
  252. public void If_and_IfNot_statements_combined_but_not_nested_do_not_conflict()
  253. {
  254. var 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>";
  255. var model = new FakeModel("Nancy", new List<string>());
  256.  
  257. var output = viewEngine.Render(input, model);
  258.  
  259. Assert.Equal(@"<html><head></head><body><p>No users found!</p></body></html>", output);
  260. }
  261.  
  262. [Fact]
  263. public void Multiple_if_statements_match_correctly()
  264. {
  265. var input = "@If.One<p>One</p>@EndIf @If.Two<p>Two</p>@EndIf";
  266. var model = new { One = true, Two = true };
  267.  
  268. var output = viewEngine.Render(input, model);
  269.  
  270. Assert.Equal(@"<p>One</p> <p>Two</p>", output);
  271. }
  272.  
  273. [Fact]
  274. public void Multiple_each_statements_match_correctly()
  275. {
  276. var input = "@Each.Users<li>@Current</li>@EndEach @Each.Admins<li>@Current</li>@EndEach";
  277. var model = new { Users = new List<string> { "1", "2" }, Admins = new List<string> { "3", "4" } };
  278.  
  279. var output = viewEngine.Render(input, model);
  280.  
  281. Assert.Equal(@"<li>1</li><li>2</li> <li>3</li><li>4</li>", output);
  282. }
  283. }
  284. }
Add Comment
Please, Sign In to add comment