PageRenderTime 67ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/SharpTAL.Tests/TALTests/TALRepeatTests.cs

https://bitbucket.org/rlacko/sharptal
C# | 295 lines | 263 code | 32 blank | 0 comment | 2 complexity | 47b83879a49f30a36f64c0c24a0e01f6 MD5 | raw file
Possible License(s): Apache-2.0
  1. namespace SharpTAL.SharpTALTests.TALTests
  2. {
  3. using System;
  4. using System.Text;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.IO;
  9. using System.Reflection;
  10. using NUnit.Framework;
  11. using SharpTAL.TemplateCache;
  12. [TestFixture]
  13. public class TALRepeatTests
  14. {
  15. public static Dictionary<string, object> globals;
  16. [TestFixtureSetUp]
  17. public void SetUpClass()
  18. {
  19. }
  20. [TestFixtureTearDown]
  21. public void CleanupClass()
  22. {
  23. }
  24. [SetUp]
  25. public void SetUp()
  26. {
  27. globals = new Dictionary<string, object>();
  28. globals.Add("test", "testing"); ;
  29. globals.Add("one", new List<int>() { 1 });
  30. globals.Add("two", new List<string>() { "one", "two" });
  31. globals.Add("three", new List<object>() { 1, "Two", 3 });
  32. globals.Add("emptyList", new List<string>());
  33. List<int> bigList = new List<int>();
  34. for (int i = 1; i < 100; i++)
  35. bigList.Add(i);
  36. globals.Add("bigList", bigList);
  37. globals.Add("fourList", new List<string>() { "zero", "one", "two", "three" });
  38. globals.Add("nested", new List<Dictionary<string, IEnumerable>>()
  39. {
  40. {
  41. new Dictionary<string, IEnumerable>()
  42. {
  43. { "title", "Image 1"}, { "catList", new List<int>() { 1, 2, 3} }
  44. }
  45. },
  46. {
  47. new Dictionary<string, IEnumerable>()
  48. {
  49. { "title", "Image 2"}, { "catList", new List<int>() { 5, 2, 3} }, { "selected", Constants.DEFAULT_VALUE }
  50. }
  51. },
  52. {
  53. new Dictionary<string, IEnumerable>()
  54. {
  55. { "title", "Image 3"}, { "catList", new List<int>() { 8, 9, 1} }
  56. }
  57. },
  58. });
  59. globals.Add("defList", new List<string>() { "Hello", Constants.DEFAULT_VALUE, "World" });
  60. globals.Add("testString", "ABC"); ;
  61. globals.Add("testDict", new Dictionary<string, string>() { { "KeyA", "A" }, { "KeyB", "B" } }); ;
  62. }
  63. public static void RunTest(string template, string expected, string errMsg)
  64. {
  65. string actual = new Template(template).Render(globals);
  66. actual = actual.Replace("{", "{{").Replace("}", "}}");
  67. Assert.AreEqual(expected, actual, "{1} - {0}template: {2}{0}actual: {3}{0}expected: {4}",
  68. Environment.NewLine, errMsg, template, actual, expected);
  69. }
  70. [Test]
  71. [ExpectedException(typeof(CompileSourceException))]
  72. public void TestInvalidPath()
  73. {
  74. RunTest(@"<html><p tal:repeat=""entry wibble"">Hello</p></html>",
  75. "<html></html>",
  76. "Repeat of non-existant element failed");
  77. }
  78. [Test]
  79. public void TestDefaultValue()
  80. {
  81. RunTest(@"<html><p tal:repeat=""entry default"">Default Only</p></html>",
  82. "<html><p>Default Only</p></html>",
  83. "Default did not keep existing structure intact");
  84. }
  85. [Test]
  86. public void TestStringRepeat()
  87. {
  88. RunTest(@"<html><p tal:omit-tag="""" tal:repeat=""letter test""><b tal:replace=""letter""></b></p></html>",
  89. "<html>testing</html>",
  90. "Itteration over string failed.");
  91. }
  92. [Test]
  93. public void TestEmptyList()
  94. {
  95. RunTest(@"<html><p tal:omit-tag="""" tal:repeat=""empty emptyList""><b tal:replace=""empty.Length"">Empty</b></p></html>",
  96. "<html></html>",
  97. "Empty list repeat failed.");
  98. }
  99. [Test]
  100. public void TestListRepeat()
  101. {
  102. RunTest(@"<html><p tal:repeat=""word two""><b tal:replace=""word""></b></p></html>",
  103. "<html><p>one</p><p>two</p></html>",
  104. "Itteration over list failed.");
  105. }
  106. [Test]
  107. public void TestTwoCmndsOneTagListRepeat()
  108. {
  109. RunTest(@"<html><p tal:repeat=""word two"" tal:content=""word""></p></html>",
  110. "<html><p>one</p><p>two</p></html>",
  111. "Itteration over list with both content and repeat on same element failed.");
  112. }
  113. [Test]
  114. public void TestAttributesInRepeat()
  115. {
  116. RunTest(
  117. @"<html><p tal:repeat=""words nested"" tal:content='words[""title""]' tal:attributes='selected words[""selected""]' selected=""selected""></p></html>",
  118. @"<html><p>Image 1</p><p selected=""selected"">Image 2</p><p>Image 3</p></html>",
  119. "Accessing attributes in repeat loop failed.");
  120. }
  121. [Test]
  122. public void TestDefaultInContentInRepeat()
  123. {
  124. RunTest(@"<html><p tal:repeat=""words defList"" tal:content=""words"">Default Word</p></html>",
  125. "<html><p>Hello</p><p>Default Word</p><p>World</p></html>",
  126. "Using default content in repeat loop failed.");
  127. }
  128. [Test]
  129. public void TestDefaultInReplaceInRepeat()
  130. {
  131. RunTest(@"<html><p tal:repeat=""words defList"" tal:replace=""words"">Default Word</p></html>",
  132. "<html>Hello<p>Default Word</p>World</html>",
  133. "Using default content in repeat loop failed.");
  134. }
  135. [Test]
  136. public void TestNestedRepeat()
  137. {
  138. RunTest(
  139. @"<html><p tal:repeat=""image nested""><h2 tal:content='image[""title""]'></h2><b tal:omit-tag="""" tal:repeat='category image[""catList""]'><i tal:content=""category""></i></b></p></html>",
  140. "<html><p><h2>Image 1</h2><i>1</i><i>2</i><i>3</i></p><p><h2>Image 2</h2><i>5</i><i>2</i><i>3</i></p><p><h2>Image 3</h2><i>8</i><i>9</i><i>1</i></p></html>",
  141. "Nested repeat did not create expected outcome.");
  142. }
  143. [Test]
  144. public void TestNestedRepeatClasses()
  145. {
  146. RunTest(
  147. @"<html><p class=""outerClass"" tal:repeat=""image nested""><div class=""innerClass"" tal:repeat='category image[""catList""]'><i tal:content=""category""></i></div></p></html>",
  148. @"<html><p class=""outerClass""><div class=""innerClass""><i>1</i></div><div class=""innerClass""><i>2</i></div><div class=""innerClass""><i>3</i></div></p><p class=""outerClass""><div class=""innerClass""><i>5</i></div><div class=""innerClass""><i>2</i></div><div class=""innerClass""><i>3</i></div></p><p class=""outerClass""><div class=""innerClass""><i>8</i></div><div class=""innerClass""><i>9</i></div><div class=""innerClass""><i>1</i></div></p></html>",
  149. "Nested repeat with classes did not create expected outcome.");
  150. }
  151. [Test]
  152. [ExpectedException(typeof(CompileSourceException))]
  153. public void TestNestedRepeatScope()
  154. {
  155. RunTest(
  156. @"<html><p tal:repeat=""image nested""><h2 tal:content='image[""title""]'></h2><b tal:omit-tag="""" tal:repeat='image image[""catList""]'><i tal:content=""image""></i></b></p></html>",
  157. @"<html><p><h2>Image 1</h2><i>1</i><i>2</i><i>3</i></p><p><h2>Image 2</h2><i>5</i><i>2</i><i>3</i></p><p><h2>Image 3</h2><i>8</i><i>9</i><i>1</i></p></html>",
  158. "Nested repeat did not create expected outcome.");
  159. }
  160. [Test]
  161. public void TestRepeatVarIndex()
  162. {
  163. string expectedResult = "<html>";
  164. for (int num = 0; num < 99; num++)
  165. {
  166. expectedResult += num.ToString();
  167. }
  168. expectedResult += "</html>";
  169. RunTest(
  170. @"<html><p tal:repeat=""val bigList"" tal:omit-tag=""""><b tal:replace='repeat[""val""].index'>Index</b></p></html>",
  171. expectedResult,
  172. "Repeat variable index failed.");
  173. }
  174. [Test]
  175. public void TestRepeatVarNumber()
  176. {
  177. RunTest(
  178. @"<html><p tal:repeat=""val bigList"" tal:omit-tag=""""><b tal:replace='repeat[""val""].number'>Index</b></p></html>",
  179. "<html>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899</html>",
  180. "Repeat variable number failed.");
  181. }
  182. [Test]
  183. public void TestRepeatVarEvenOdd()
  184. {
  185. RunTest(
  186. @"<html><p tal:repeat=""val fourList""><i tal:replace=""val""></i> - <b tal:condition='repeat[""val""].odd'>Odd</b><b tal:condition='repeat[""val""].even'>Even</b></p></html>",
  187. "<html><p>zero - <b>Even</b></p><p>one - <b>Odd</b></p><p>two - <b>Even</b></p><p>three - <b>Odd</b></p></html>",
  188. "Repeat variables odd and even failed.");
  189. }
  190. [Test]
  191. public void TestRepeatVarStartEnd()
  192. {
  193. RunTest(
  194. @"<html><p tal:repeat=""val fourList""><b tal:condition='repeat[""val""].start'>Start</b><i tal:replace=""val""></i><b tal:condition='repeat[""val""].end'>End</b></p></html>",
  195. "<html><p><b>Start</b>zero</p><p>one</p><p>two</p><p>three<b>End</b></p></html>",
  196. "Repeat variables start and end failed.");
  197. }
  198. [Test]
  199. public void TestRepeatVarStartEndString()
  200. {
  201. RunTest(
  202. @"<html><p tal:repeat=""val testString""><b tal:condition='repeat[""val""].start'>Start</b><i tal:replace=""val""></i><b tal:condition='repeat[""val""].end'>End</b></p></html>",
  203. "<html><p><b>Start</b>A</p><p>B</p><p>C<b>End</b></p></html>",
  204. "Repeat variables start and end failed.");
  205. }
  206. [Test]
  207. public void TestRepeatVarStartEndDict()
  208. {
  209. RunTest(
  210. @"<html><p tal:repeat=""val testDict""><b tal:condition='repeat[""val""].start'>Start</b><i tal:replace='val.Value'></i><b tal:condition='repeat[""val""].end'>End</b></p></html>",
  211. "<html><p><b>Start</b>A</p><p>B<b>End</b></p></html>",
  212. "Repeat variables start and end failed.");
  213. }
  214. [Test]
  215. public void TestRepeatVarLength()
  216. {
  217. RunTest(
  218. @"<html><p tal:repeat=""val fourList""><b tal:condition='repeat[""val""].start'>Len: <i tal:replace='repeat[""val""].length'>length</i></b>Entry: <i tal:replace=""val""></i></p></html>",
  219. "<html><p><b>Len: 4</b>Entry: zero</p><p>Entry: one</p><p>Entry: two</p><p>Entry: three</p></html>",
  220. "Repeat variable length failed.");
  221. }
  222. [Test]
  223. public void TestRepeatVarLowerLetter()
  224. {
  225. RunTest(
  226. @"<html><p tal:repeat=""val fourList""><i tal:replace='repeat[""val""].letter'>a,b,c,etc</i>: <i tal:replace=""val""></i></p></html>",
  227. "<html><p>a: zero</p><p>b: one</p><p>c: two</p><p>d: three</p></html>",
  228. "Repeat variable letter failed.");
  229. }
  230. [Test]
  231. public void TestRepeatVarLowerLetterLarge()
  232. {
  233. RunTest(
  234. @"<html><p tal:repeat=""val bigList""><i tal:replace='repeat[""val""].letter'>a,b,c,etc</i>: <i tal:replace=""val""></i></p></html>",
  235. "<html><p>a: 1</p><p>b: 2</p><p>c: 3</p><p>d: 4</p><p>e: 5</p><p>f: 6</p><p>g: 7</p><p>h: 8</p><p>i: 9</p><p>j: 10</p><p>k: 11</p><p>l: 12</p><p>m: 13</p><p>n: 14</p><p>o: 15</p><p>p: 16</p><p>q: 17</p><p>r: 18</p><p>s: 19</p><p>t: 20</p><p>u: 21</p><p>v: 22</p><p>w: 23</p><p>x: 24</p><p>y: 25</p><p>z: 26</p><p>ba: 27</p><p>bb: 28</p><p>bc: 29</p><p>bd: 30</p><p>be: 31</p><p>bf: 32</p><p>bg: 33</p><p>bh: 34</p><p>bi: 35</p><p>bj: 36</p><p>bk: 37</p><p>bl: 38</p><p>bm: 39</p><p>bn: 40</p><p>bo: 41</p><p>bp: 42</p><p>bq: 43</p><p>br: 44</p><p>bs: 45</p><p>bt: 46</p><p>bu: 47</p><p>bv: 48</p><p>bw: 49</p><p>bx: 50</p><p>by: 51</p><p>bz: 52</p><p>ca: 53</p><p>cb: 54</p><p>cc: 55</p><p>cd: 56</p><p>ce: 57</p><p>cf: 58</p><p>cg: 59</p><p>ch: 60</p><p>ci: 61</p><p>cj: 62</p><p>ck: 63</p><p>cl: 64</p><p>cm: 65</p><p>cn: 66</p><p>co: 67</p><p>cp: 68</p><p>cq: 69</p><p>cr: 70</p><p>cs: 71</p><p>ct: 72</p><p>cu: 73</p><p>cv: 74</p><p>cw: 75</p><p>cx: 76</p><p>cy: 77</p><p>cz: 78</p><p>da: 79</p><p>db: 80</p><p>dc: 81</p><p>dd: 82</p><p>de: 83</p><p>df: 84</p><p>dg: 85</p><p>dh: 86</p><p>di: 87</p><p>dj: 88</p><p>dk: 89</p><p>dl: 90</p><p>dm: 91</p><p>dn: 92</p><p>do: 93</p><p>dp: 94</p><p>dq: 95</p><p>dr: 96</p><p>ds: 97</p><p>dt: 98</p><p>du: 99</p></html>",
  236. "Repeat variable letter failed on a large list.");
  237. }
  238. [Test]
  239. public void TestRepeatVarUpperLetter()
  240. {
  241. RunTest(
  242. @"<html><p tal:repeat=""val fourList""><i tal:replace='repeat[""val""].Letter'>A,B,C,etc</i>: <i tal:replace=""val""></i></p></html>",
  243. "<html><p>A: zero</p><p>B: one</p><p>C: two</p><p>D: three</p></html>",
  244. "Repeat variable Letter failed.");
  245. }
  246. [Test]
  247. public void TestRepeatVarLowerRoman()
  248. {
  249. RunTest(
  250. @"<html><p tal:repeat=""val bigList""><i tal:replace='repeat[""val""].roman'>i,ii,iii,etc</i>: <i tal:replace=""val""></i></p></html>",
  251. "<html><p>i: 1</p><p>ii: 2</p><p>iii: 3</p><p>iv: 4</p><p>v: 5</p><p>vi: 6</p><p>vii: 7</p><p>viii: 8</p><p>ix: 9</p><p>x: 10</p><p>xi: 11</p><p>xii: 12</p><p>xiii: 13</p><p>xiv: 14</p><p>xv: 15</p><p>xvi: 16</p><p>xvii: 17</p><p>xviii: 18</p><p>xix: 19</p><p>xx: 20</p><p>xxi: 21</p><p>xxii: 22</p><p>xxiii: 23</p><p>xxiv: 24</p><p>xxv: 25</p><p>xxvi: 26</p><p>xxvii: 27</p><p>xxviii: 28</p><p>xxix: 29</p><p>xxx: 30</p><p>xxxi: 31</p><p>xxxii: 32</p><p>xxxiii: 33</p><p>xxxiv: 34</p><p>xxxv: 35</p><p>xxxvi: 36</p><p>xxxvii: 37</p><p>xxxviii: 38</p><p>xxxix: 39</p><p>xl: 40</p><p>xli: 41</p><p>xlii: 42</p><p>xliii: 43</p><p>xliv: 44</p><p>xlv: 45</p><p>xlvi: 46</p><p>xlvii: 47</p><p>xlviii: 48</p><p>xlix: 49</p><p>l: 50</p><p>li: 51</p><p>lii: 52</p><p>liii: 53</p><p>liv: 54</p><p>lv: 55</p><p>lvi: 56</p><p>lvii: 57</p><p>lviii: 58</p><p>lix: 59</p><p>lx: 60</p><p>lxi: 61</p><p>lxii: 62</p><p>lxiii: 63</p><p>lxiv: 64</p><p>lxv: 65</p><p>lxvi: 66</p><p>lxvii: 67</p><p>lxviii: 68</p><p>lxix: 69</p><p>lxx: 70</p><p>lxxi: 71</p><p>lxxii: 72</p><p>lxxiii: 73</p><p>lxxiv: 74</p><p>lxxv: 75</p><p>lxxvi: 76</p><p>lxxvii: 77</p><p>lxxviii: 78</p><p>lxxix: 79</p><p>lxxx: 80</p><p>lxxxi: 81</p><p>lxxxii: 82</p><p>lxxxiii: 83</p><p>lxxxiv: 84</p><p>lxxxv: 85</p><p>lxxxvi: 86</p><p>lxxxvii: 87</p><p>lxxxviii: 88</p><p>lxxxix: 89</p><p>xc: 90</p><p>xci: 91</p><p>xcii: 92</p><p>xciii: 93</p><p>xciv: 94</p><p>xcv: 95</p><p>xcvi: 96</p><p>xcvii: 97</p><p>xcviii: 98</p><p>xcix: 99</p></html>",
  252. "Repeat variable roman failed.");
  253. }
  254. [Test]
  255. public void TestRepeatVarUpperRoman()
  256. {
  257. RunTest(
  258. @"<html><p tal:repeat=""val bigList""><i tal:replace='repeat[""val""].Roman'>I,II,III,etc</i>: <i tal:replace=""val""></i></p></html>",
  259. "<html><p>I: 1</p><p>II: 2</p><p>III: 3</p><p>IV: 4</p><p>V: 5</p><p>VI: 6</p><p>VII: 7</p><p>VIII: 8</p><p>IX: 9</p><p>X: 10</p><p>XI: 11</p><p>XII: 12</p><p>XIII: 13</p><p>XIV: 14</p><p>XV: 15</p><p>XVI: 16</p><p>XVII: 17</p><p>XVIII: 18</p><p>XIX: 19</p><p>XX: 20</p><p>XXI: 21</p><p>XXII: 22</p><p>XXIII: 23</p><p>XXIV: 24</p><p>XXV: 25</p><p>XXVI: 26</p><p>XXVII: 27</p><p>XXVIII: 28</p><p>XXIX: 29</p><p>XXX: 30</p><p>XXXI: 31</p><p>XXXII: 32</p><p>XXXIII: 33</p><p>XXXIV: 34</p><p>XXXV: 35</p><p>XXXVI: 36</p><p>XXXVII: 37</p><p>XXXVIII: 38</p><p>XXXIX: 39</p><p>XL: 40</p><p>XLI: 41</p><p>XLII: 42</p><p>XLIII: 43</p><p>XLIV: 44</p><p>XLV: 45</p><p>XLVI: 46</p><p>XLVII: 47</p><p>XLVIII: 48</p><p>XLIX: 49</p><p>L: 50</p><p>LI: 51</p><p>LII: 52</p><p>LIII: 53</p><p>LIV: 54</p><p>LV: 55</p><p>LVI: 56</p><p>LVII: 57</p><p>LVIII: 58</p><p>LIX: 59</p><p>LX: 60</p><p>LXI: 61</p><p>LXII: 62</p><p>LXIII: 63</p><p>LXIV: 64</p><p>LXV: 65</p><p>LXVI: 66</p><p>LXVII: 67</p><p>LXVIII: 68</p><p>LXIX: 69</p><p>LXX: 70</p><p>LXXI: 71</p><p>LXXII: 72</p><p>LXXIII: 73</p><p>LXXIV: 74</p><p>LXXV: 75</p><p>LXXVI: 76</p><p>LXXVII: 77</p><p>LXXVIII: 78</p><p>LXXIX: 79</p><p>LXXX: 80</p><p>LXXXI: 81</p><p>LXXXII: 82</p><p>LXXXIII: 83</p><p>LXXXIV: 84</p><p>LXXXV: 85</p><p>LXXXVI: 86</p><p>LXXXVII: 87</p><p>LXXXVIII: 88</p><p>LXXXIX: 89</p><p>XC: 90</p><p>XCI: 91</p><p>XCII: 92</p><p>XCIII: 93</p><p>XCIV: 94</p><p>XCV: 95</p><p>XCVI: 96</p><p>XCVII: 97</p><p>XCVIII: 98</p><p>XCIX: 99</p></html>",
  260. "Repeat variable Roman failed.");
  261. }
  262. }
  263. }