erlydtl_unittests.erl 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. -module(erlydtl_unittests).
  2. -export([run_tests/0]).
  3. tests() ->
  4. [
  5. {"vars", [
  6. {"string",
  7. <<"String value is: {{ var1 }}">>,
  8. [{var1, "foo"}], <<"String value is: foo">>},
  9. {"int",
  10. <<"The magic number is: {{ var1 }}">>,
  11. [{var1, 42}], <<"The magic number is: 42">>},
  12. {"float",
  13. <<"The price of milk is: {{ var1 }}">>,
  14. [{var1, 0.42}], <<"The price of milk is: 0.42">>},
  15. {"No spaces",
  16. <<"{{var1}}">>,
  17. [{var1, "foo"}], <<"foo">>},
  18. {"Variable name is a tag name",
  19. <<"{{ comment }}">>,
  20. [{comment, "Nice work!"}], <<"Nice work!">>}
  21. ]},
  22. {"comment", [
  23. {"comment block is excised",
  24. <<"bob {% comment %}(moron){% endcomment %} loblaw">>,
  25. [], <<"bob loblaw">>},
  26. {"inline comment is excised",
  27. <<"you're {# not #} a very nice person">>,
  28. [], <<"you're a very nice person">>}
  29. ]},
  30. {"autoescape", [
  31. {"Autoescape works",
  32. <<"{% autoescape on %}{{ var1 }}{% endautoescape %}">>,
  33. [{var1, "<b>bold</b>"}], <<"&lt;b&gt;bold&lt;/b&gt;">>},
  34. {"Nested autoescape",
  35. <<"{% autoescape on %}{{ var1 }}{% autoescape off %}{{ var1 }}{% endautoescape %}{% endautoescape %}">>,
  36. [{var1, "<b>"}], <<"&lt;b&gt;<b>">>}
  37. ]},
  38. {"string literal", [
  39. {"Render literal",
  40. <<"{{ \"foo\" }} is my name">>, [], <<"foo is my name">>},
  41. {"Newlines are escaped",
  42. <<"{{ \"foo\\n\" }}">>, [], <<"foo\n">>}
  43. ]},
  44. {"cycle", [
  45. {"Cycling through quoted strings",
  46. <<"{% for i in test %}{% cycle 'a' 'b' %}{{ i }},{% endfor %}">>,
  47. [{test, ["0", "1", "2", "3", "4"]}], <<"a0,b1,a2,b3,a4,">>},
  48. {"Cycling through normal variables",
  49. <<"{% for i in test %}{% cycle aye bee %}{{ i }},{% endfor %}">>,
  50. [{test, ["0", "1", "2", "3", "4"]}, {aye, "a"}, {bee, "b"}],
  51. <<"a0,b1,a2,b3,a4,">>}
  52. ]},
  53. {"number literal", [
  54. {"Render integer",
  55. <<"{{ 5 }}">>, [], <<"5">>}
  56. ]},
  57. {"variable", [
  58. {"Render variable",
  59. <<"{{ var1 }} is my game">>, [{var1, "bar"}], <<"bar is my game">>},
  60. {"Render variable with attribute",
  61. <<"I enjoy {{ var1.game }}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
  62. {"Render variable with string-key attribute",
  63. <<"I also enjoy {{ var1.game }}">>, [{var1, [{"game", "Parcheesi"}]}], <<"I also enjoy Parcheesi">>},
  64. {"Render variable with binary-key attribute",
  65. <<"I also enjoy {{ var1.game }}">>, [{var1, [{<<"game">>, "Parcheesi"}]}], <<"I also enjoy Parcheesi">>},
  66. {"Render variable in dict",
  67. <<"{{ var1 }}">>, dict:store(var1, "bar", dict:new()), <<"bar">>},
  68. {"Render variable in gb_tree",
  69. <<"{{ var1 }}">>, gb_trees:insert(var1, "bar", gb_trees:empty()), <<"bar">>},
  70. {"Render variable in arity-1 func",
  71. <<"I enjoy {{ var1 }}">>, fun (var1) -> "Othello" end, <<"I enjoy Othello">>},
  72. {"Render variable with attribute in dict",
  73. <<"{{ var1.attr }}">>, [{var1, dict:store(attr, "Othello", dict:new())}], <<"Othello">>},
  74. {"Render variable with attribute in gb_tree",
  75. <<"{{ var1.attr }}">>, [{var1, gb_trees:insert(attr, "Othello", gb_trees:empty())}], <<"Othello">>},
  76. {"Render variable with attribute in arity-1 func",
  77. <<"I enjoy {{ var1.game }}">>, [{var1, fun (game) -> "Othello" end}], <<"I enjoy Othello">>},
  78. {"Render variable in parameterized module",
  79. <<"{{ var1.some_var }}">>, [{var1, erlydtl_example_variable_storage:new("foo")}], <<"foo">>},
  80. {"Nested attributes",
  81. <<"{{ person.city.state.country }}">>, [{person, [{city, [{state, [{country, "Italy"}]}]}]}],
  82. <<"Italy">>}
  83. ]},
  84. {"now", [
  85. {"now functional",
  86. <<"It is the {% now \"jS o\\f F Y\" %}.">>, [{var1, ""}], generate_test_date()}
  87. ]},
  88. {"if", [
  89. {"If/else",
  90. <<"{% if var1 %}boo{% else %}yay{% endif %}">>, [{var1, ""}], <<"yay">>},
  91. {"If elif",
  92. <<"{% if var1 %}boo{% elif var2 %}yay{% endif %}">>, [{var1, ""}, {var2, "happy"}], <<"yay">>},
  93. {"If elif/else",
  94. <<"{% if var1 %}boo{% elif var2 %}sad{% else %}yay{% endif %}">>, [{var1, ""}, {var2, ""}], <<"yay">>},
  95. {"If elif/elif/else",
  96. <<"{% if var1 %}boo{% elif var2 %}yay{% elif var3 %}sad{% else %}noo{% endif %}">>, [{var1, ""},
  97. {var2, "happy"}, {var3, "not_taken"}], <<"yay">>},
  98. {"If",
  99. <<"{% if var1 %}boo{% endif %}">>, [{var1, ""}], <<>>},
  100. {"If not",
  101. <<"{% if not var1 %}yay{% endif %}">>, [{var1, ""}], <<"yay">>},
  102. {"If \"0\"",
  103. <<"{% if var1 %}boo{% endif %}">>, [{var1, "0"}], <<>>},
  104. {"If false",
  105. <<"{% if var1 %}boo{% endif %}">>, [{var1, false}], <<>>},
  106. {"If false string",
  107. <<"{% if var1 %}boo{% endif %}">>, [{var1, "false"}], <<"boo">>},
  108. {"If undefined",
  109. <<"{% if var1 %}boo{% endif %}">>, [{var1, undefined}], <<>>},
  110. {"If other atom",
  111. <<"{% if var1 %}yay{% endif %}">>, [{var1, foobar}], <<"yay">>},
  112. {"If non-empty string",
  113. <<"{% if var1 %}yay{% endif %}">>, [{var1, "hello"}], <<"yay">>},
  114. {"If proplist",
  115. <<"{% if var1 %}yay{% endif %}">>, [{var1, [{foo, "bar"}]}], <<"yay">>},
  116. {"If complex",
  117. <<"{% if foo.bar.baz %}omgwtfbbq{% endif %}">>, [], <<"">>}
  118. ]},
  119. {"if .. in ..", [
  120. {"If substring in string",
  121. <<"{% if var1 in var2 %}yay{% endif %}">>, [{var1, "rook"}, {var2, "Crooks"}], <<"yay">>},
  122. {"If substring in string (false)",
  123. <<"{% if var1 in var2 %}boo{% endif %}">>, [{var1, "Cook"}, {var2, "Crooks"}], <<>>},
  124. {"If substring not in string",
  125. <<"{% if var1 not in var2 %}yay{% endif %}">>, [{var1, "Cook"}, {var2, "Crooks"}], <<"yay">>},
  126. {"If substring not in string (false)",
  127. <<"{% if var1 not in var2 %}boo{% endif %}">>, [{var1, "rook"}, {var2, "Crooks"}], <<>>},
  128. {"If literal substring in string",
  129. <<"{% if \"man\" in \"Ottoman\" %}yay{% endif %}">>, [], <<"yay">>},
  130. {"If literal substring in string (false)",
  131. <<"{% if \"woman\" in \"Ottoman\" %}boo{% endif %}">>, [], <<>>},
  132. {"If element in list",
  133. <<"{% if var1 in var2 %}yay{% endif %}">>, [{var1, "foo"}, {var2, ["bar", "foo", "baz"]}], <<"yay">>},
  134. {"If element in list (false)",
  135. <<"{% if var1 in var2 %}boo{% endif %}">>, [{var1, "FOO"}, {var2, ["bar", "foo", "baz"]}], <<>>}
  136. ]},
  137. {"if .. and ..", [
  138. {"If true and true",
  139. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, true}, {var2, true}], <<"yay">>},
  140. {"If true and false",
  141. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, true}, {var2, false}], <<"">>},
  142. {"If false and true",
  143. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, false}, {var2, true}], <<"">>},
  144. {"If false and false ",
  145. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, false}, {var2, false}], <<"">>}
  146. ]},
  147. {"if .. or ..", [
  148. {"If true or true",
  149. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, true}, {var2, true}], <<"yay">>},
  150. {"If true or false",
  151. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, true}, {var2, false}], <<"yay">>},
  152. {"If false or true",
  153. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, false}, {var2, true}], <<"yay">>},
  154. {"If false or false ",
  155. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, false}, {var2, false}], <<"">>}
  156. ]},
  157. {"if equality", [
  158. {"If int equals number literal",
  159. <<"{% if var1 == 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
  160. {"If int equals number literal (false)",
  161. <<"{% if var1 == 2 %}yay{% endif %}">>, [{var1, 3}], <<"">>},
  162. {"If string equals string literal",
  163. <<"{% if var1 == \"2\" %}yay{% endif %}">>, [{var1, "2"}], <<"yay">>},
  164. {"If string equals string literal (false)",
  165. <<"{% if var1 == \"2\" %}yay{% endif %}">>, [{var1, "3"}], <<"">>},
  166. {"If int not equals number literal",
  167. <<"{% if var1 != 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
  168. {"If string not equals string literal",
  169. <<"{% if var1 != \"2\" %}yay{% endif %}">>, [{var1, "3"}], <<"yay">>},
  170. {"If filter result equals number literal",
  171. <<"{% if var1|length == 2 %}yay{% endif %}">>, [{var1, ["fo", "bo"]}], <<"yay">>},
  172. {"If filter result equals string literal",
  173. <<"{% if var1|capfirst == \"Foo\" %}yay{% endif %}">>, [{var1, "foo"}], <<"yay">>}
  174. ]},
  175. {"if size comparison", [
  176. {"If int greater than number literal",
  177. <<"{% if var1 > 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
  178. {"If int greater than negative number literal",
  179. <<"{% if var1 > -2 %}yay{% endif %}">>, [{var1, -1}], <<"yay">>},
  180. {"If int greater than number literal (false)",
  181. <<"{% if var1 > 2 %}yay{% endif %}">>, [{var1, 2}], <<"">>},
  182. {"If int greater than or equal to number literal",
  183. <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
  184. {"If int greater than or equal to number literal (2)",
  185. <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
  186. {"If int greater than or equal to number literal (false)",
  187. <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 1}], <<"">>},
  188. {"If int less than number literal",
  189. <<"{% if var1 < 2 %}yay{% endif %}">>, [{var1, 1}], <<"yay">>},
  190. {"If int less than number literal (false)",
  191. <<"{% if var1 < 2 %}yay{% endif %}">>, [{var1, 2}], <<"">>},
  192. {"If int less than or equal to number literal",
  193. <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 1}], <<"yay">>},
  194. {"If int less than or equal to number literal",
  195. <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
  196. {"If int less than or equal to number literal (false)",
  197. <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 3}], <<"">>}
  198. ]},
  199. {"if complex bool", [
  200. {"If (true or false) and true",
  201. <<"{% if (var1 or var2) and var3 %}yay{% endif %}">>,
  202. [{var1, true}, {var2, false}, {var3, true}], <<"yay">>},
  203. {"If true or (false and true)",
  204. <<"{% if var1 or (var2 and var3) %}yay{% endif %}">>,
  205. [{var1, true}, {var2, false}, {var3, true}], <<"yay">>}
  206. ]},
  207. {"for", [
  208. {"Simple loop",
  209. <<"{% for x in list %}{{ x }}{% endfor %}">>, [{'list', ["1", "2", "3"]}],
  210. <<"123">>},
  211. {"Reversed loop",
  212. <<"{% for x in list reversed %}{{ x }}{% endfor %}">>, [{'list', ["1", "2", "3"]}],
  213. <<"321">>},
  214. {"Expand list",
  215. <<"{% for x, y in list %}{{ x }},{{ y }}\n{% endfor %}">>, [{'list', [["X", "1"], ["X", "2"]]}],
  216. <<"X,1\nX,2\n">>},
  217. {"Expand tuple",
  218. <<"{% for x, y in list %}{{ x }},{{ y }}\n{% endfor %}">>, [{'list', [{"X", "1"}, {"X", "2"}]}],
  219. <<"X,1\nX,2\n">>},
  220. {"Resolve variable attribute",
  221. <<"{% for number in person.numbers %}{{ number }}\n{% endfor %}">>, [{person, [{numbers, ["411", "911"]}]}],
  222. <<"411\n911\n">>},
  223. {"Resolve nested variable attribute",
  224. <<"{% for number in person.home.numbers %}{{ number }}\n{% endfor %}">>, [{person, [{home, [{numbers, ["411", "911"]}]}]}],
  225. <<"411\n911\n">>},
  226. {"Counter0",
  227. <<"{% for number in numbers %}{{ forloop.counter0 }}. {{ number }}\n{% endfor %}">>,
  228. [{numbers, ["Zero", "One", "Two"]}], <<"0. Zero\n1. One\n2. Two\n">>},
  229. {"Counter",
  230. <<"{% for number in numbers %}{{ forloop.counter }}. {{ number }}\n{% endfor %}">>,
  231. [{numbers, ["One", "Two", "Three"]}], <<"1. One\n2. Two\n3. Three\n">>},
  232. {"Reverse Counter0",
  233. <<"{% for number in numbers %}{{ forloop.revcounter0 }}. {{ number }}\n{% endfor %}">>,
  234. [{numbers, ["Two", "One", "Zero"]}], <<"2. Two\n1. One\n0. Zero\n">>},
  235. {"Reverse Counter",
  236. <<"{% for number in numbers %}{{ forloop.revcounter }}. {{ number }}\n{% endfor %}">>,
  237. [{numbers, ["Three", "Two", "One"]}], <<"3. Three\n2. Two\n1. One\n">>},
  238. {"Counter \"first\"",
  239. <<"{% for number in numbers %}{% if forloop.first %}{{ number }}{% endif %}{% endfor %}">>,
  240. [{numbers, ["One", "Two", "Three"]}], <<"One">>},
  241. {"Counter \"last\"",
  242. <<"{% for number in numbers %}{% if forloop.last %}{{ number }}{% endif %}{% endfor %}">>,
  243. [{numbers, ["One", "Two", "Three"]}], <<"Three">>},
  244. {"Nested for loop",
  245. <<"{% for outer in list %}{% for inner in outer %}{{ inner }}\n{% endfor %}{% endfor %}">>,
  246. [{'list', [["Al", "Albert"], ["Jo", "Joseph"]]}],
  247. <<"Al\nAlbert\nJo\nJoseph\n">>},
  248. {"Access parent loop counters",
  249. <<"{% for outer in list %}{% for inner in outer %}({{ forloop.parentloop.counter0 }}, {{ forloop.counter0 }})\n{% endfor %}{% endfor %}">>,
  250. [{'list', [["One", "two"], ["One", "two"]]}], <<"(0, 0)\n(0, 1)\n(1, 0)\n(1, 1)\n">>},
  251. {"If changed",
  252. <<"{% for x in list %}{% ifchanged %}{{ x }}\n{% endifchanged %}{% endfor %}">>,
  253. [{'list', ["one", "two", "two", "three", "three", "three"]}], <<"one\ntwo\nthree\n">>},
  254. {"If changed/2",
  255. <<"{% for x, y in list %}{% ifchanged %}{{ x|upper }}{% endifchanged %}{% ifchanged %}{{ y|lower }}{% endifchanged %}\n{% endfor %}">>,
  256. [{'list', [["one", "a"], ["two", "A"], ["two", "B"], ["three", "b"], ["three", "c"], ["Three", "b"]]}], <<"ONEa\nTWO\nb\nTHREE\nc\nb\n">>},
  257. {"If changed/else",
  258. <<"{% for x in list %}{% ifchanged %}{{ x }}\n{% else %}foo\n{% endifchanged %}{% endfor %}">>,
  259. [{'list', ["one", "two", "two", "three", "three", "three"]}], <<"one\ntwo\nfoo\nthree\nfoo\nfoo\n">>},
  260. {"If changed/param",
  261. <<"{% for date in list %}{% ifchanged date.month %} {{ date.month }}:{{ date.day }}{% else %},{{ date.day }}{% endifchanged %}{% endfor %}\n">>,
  262. [{'list', [[{month,"Jan"},{day,1}],[{month,"Jan"},{day,2}],[{month,"Apr"},{day,10}],
  263. [{month,"Apr"},{day,11}],[{month,"May"},{day,4}]]}],
  264. <<" Jan:1,2 Apr:10,11 May:4\n">>},
  265. {"If changed/param2",
  266. <<"{% for x, y in list %}{% ifchanged y|upper %}{{ x|upper }}{% endifchanged %}\n{% endfor %}">>,
  267. [{'list', [["one", "a"], ["two", "A"], ["two", "B"], ["three", "b"], ["three", "c"], ["Three", "b"]]}], <<"ONE\n\nTWO\n\nTHREE\nTHREE\n">>},
  268. {"If changed/param2 combined",
  269. <<"{% for x, y in list %}{% ifchanged x y|upper %}{{ x }}{% endifchanged %}\n{% endfor %}">>,
  270. [{'list', [["one", "a"], ["two", "A"], ["two", "B"], ["three", "b"], ["three", "B"], ["three", "c"]]}], <<"one\ntwo\ntwo\nthree\n\nthree\n">>},
  271. {"If changed/resolve",
  272. <<"{% for x in list %}{% ifchanged x.name|first %}{{ x.value }}{% endifchanged %}\n{% endfor %}">>,
  273. [{'list', [[{"name", ["nA","nB"]},{"value","1"}],[{"name", ["nA","nC"]},{"value","2"}],
  274. [{"name", ["nB","nC"]},{"value","3"}],[{"name", ["nB","nA"]},{"value","4"}]]}],
  275. <<"1\n\n3\n\n">>}
  276. ]},
  277. {"for/empty", [
  278. {"Simple loop",
  279. <<"{% for x in list %}{{ x }}{% empty %}shucks{% endfor %}">>, [{'list', ["1", "2", "3"]}],
  280. <<"123">>},
  281. {"Simple loop (empty)",
  282. <<"{% for x in list %}{{ x }}{% empty %}shucks{% endfor %}">>, [{'list', []}],
  283. <<"shucks">>}
  284. ]},
  285. {"ifequal", [
  286. {"Compare variable to literal",
  287. <<"{% ifequal var1 \"foo\" %}yay{% endifequal %}">>,
  288. [{var1, "foo"}], <<"yay">>},
  289. {"Compare variable to unequal literal",
  290. <<"{% ifequal var1 \"foo\" %}boo{% endifequal %}">>,
  291. [{var1, "bar"}], <<>>},
  292. {"Compare literal to variable",
  293. <<"{% ifequal \"foo\" var1 %}yay{% endifequal %}">>,
  294. [{var1, "foo"}], <<"yay">>},
  295. {"Compare literal to unequal variable",
  296. <<"{% ifequal \"foo\" var1 %}boo{% endifequal %}">>,
  297. [{var1, "bar"}], <<>>},
  298. {"Compare variable to literal (int string)",
  299. <<"{% ifequal var1 \"2\" %}yay{% else %}boo{% endifequal %}">>,
  300. [{var1, "2"}], <<"yay">>},
  301. {"Compare variable to literal (int)",
  302. <<"{% ifequal var1 2 %}yay{% else %}boo{% endifequal %}">>,
  303. [{var1, 2}], <<"yay">>},
  304. {"Compare variable to unequal literal (int)",
  305. <<"{% ifequal var1 2 %}boo{% else %}yay{% endifequal %}">>,
  306. [{var1, 3}], <<"yay">>},
  307. {"Compare variable to equal literal (atom)",
  308. <<"{% ifequal var1 \"foo\"%}yay{% endifequal %}">>,
  309. [{var1, foo}], <<"yay">>},
  310. {"Compare variable to unequal literal (atom)",
  311. <<"{% ifequal var1 \"foo\"%}yay{% else %}boo{% endifequal %}">>,
  312. [{var1, bar}], <<"boo">>}
  313. ]},
  314. {"ifequal/else", [
  315. {"Compare variable to literal",
  316. <<"{% ifequal var1 \"foo\" %}yay{% else %}boo{% endifequal %}">>,
  317. [{var1, "foo"}], <<"yay">>},
  318. {"Compare variable to unequal literal",
  319. <<"{% ifequal var1 \"foo\" %}boo{% else %}yay{% endifequal %}">>,
  320. [{var1, "bar"}], <<"yay">>},
  321. {"Compare literal to variable",
  322. <<"{% ifequal \"foo\" var1 %}yay{% else %}boo{% endifequal %}">>,
  323. [{var1, "foo"}], <<"yay">>},
  324. {"Compare literal to unequal variable",
  325. <<"{% ifequal \"foo\" var1 %}boo{% else %}yay{% endifequal %}">>,
  326. [{var1, "bar"}], <<"yay">>}
  327. ]},
  328. {"ifnotequal", [
  329. {"Compare variable to literal",
  330. <<"{% ifnotequal var1 \"foo\" %}boo{% endifnotequal %}">>,
  331. [{var1, "foo"}], <<>>},
  332. {"Compare variable to unequal literal",
  333. <<"{% ifnotequal var1 \"foo\" %}yay{% endifnotequal %}">>,
  334. [{var1, "bar"}], <<"yay">>},
  335. {"Compare literal to variable",
  336. <<"{% ifnotequal \"foo\" var1 %}boo{% endifnotequal %}">>,
  337. [{var1, "foo"}], <<>>},
  338. {"Compare literal to unequal variable",
  339. <<"{% ifnotequal \"foo\" var1 %}yay{% endifnotequal %}">>,
  340. [{var1, "bar"}], <<"yay">>}
  341. ]},
  342. {"ifnotequal/else", [
  343. {"Compare variable to literal",
  344. <<"{% ifnotequal var1 \"foo\" %}boo{% else %}yay{% endifnotequal %}">>,
  345. [{var1, "foo"}], <<"yay">>},
  346. {"Compare variable to unequal literal",
  347. <<"{% ifnotequal var1 \"foo\" %}yay{% else %}boo{% endifnotequal %}">>,
  348. [{var1, "bar"}], <<"yay">>},
  349. {"Compare literal to variable",
  350. <<"{% ifnotequal \"foo\" var1 %}boo{% else %}yay{% endifnotequal %}">>,
  351. [{var1, "foo"}], <<"yay">>},
  352. {"Compare literal to unequal variable",
  353. <<"{% ifnotequal \"foo\" var1 %}yay{% else %}boo{% endifnotequal %}">>,
  354. [{var1, "bar"}], <<"yay">>}
  355. ]},
  356. {"filter tag", [
  357. {"Apply a filter",
  358. <<"{% filter escape %}&{% endfilter %}">>, [], <<"&amp;">>},
  359. {"Chained filters",
  360. <<"{% filter linebreaksbr|escape %}\n{% endfilter %}">>, [], <<"&lt;br /&gt;">>}
  361. ]},
  362. {"filters", [
  363. {"Filter a literal",
  364. <<"{{ \"pop\"|capfirst }}">>, [],
  365. <<"Pop">>},
  366. {"Filters applied in order",
  367. <<"{{ var1|force_escape|length }}">>, [{var1, <<"&">>}],
  368. <<"5">>},
  369. {"Escape is applied last",
  370. <<"{{ var1|escape|linebreaksbr }}">>, [{var1, <<"\n">>}],
  371. <<"&lt;br /&gt;">>},
  372. {"add; lhs number, rhs number",
  373. <<"{{ one|add:4}}">>, [{one, 1}],
  374. <<"5">>},
  375. {"add; lhs numeric string, rhs number",
  376. <<"{{ one|add:4}}">>, [{one, "1"}],
  377. <<"5">>},
  378. {"add; lhs number, rhs numeric string",
  379. <<"{{ one|add:'4'}}">>, [{one, 1}],
  380. <<"5">>},
  381. {"add; lhs non-numeric string, rhs number",
  382. <<"{{ one|add:4}}">>, [{one, "foo"}],
  383. <<"foo4">>},
  384. {"add; lhs number, rhs non-numeric string",
  385. <<"{{ one|add:'foo'}}">>, [{one, 1}],
  386. <<"1foo">>},
  387. {"add; lhs non-numeric string, rhs non-numeric string",
  388. <<"{{ one|add:'bar'}}">>, [{one, "foo"}],
  389. <<"foobar">>},
  390. {"add; lhs numeric string, rhs numeric string",
  391. <<"{{ one|add:'4'}}">>, [{one, "1"}],
  392. <<"5">>},
  393. {"|addslashes",
  394. <<"{{ var1|addslashes }}">>, [{var1, "Jimmy's \"great\" meats'n'things"}],
  395. <<"Jimmy\\'s \\\"great\\\" meats\\'n\\'things">>},
  396. {"|capfirst",
  397. <<"{{ var1|capfirst }}">>, [{var1, "dana boyd"}],
  398. <<"Dana boyd">>},
  399. {"|center:10",
  400. <<"{{ var1|center:10 }}">>, [{var1, "MB"}],
  401. <<" MB ">>},
  402. {"|center:1",
  403. <<"{{ var1|center:1 }}">>, [{var1, "KBR"}],
  404. <<"B">>},
  405. {"|cut:\" \"",
  406. <<"{{ var1|cut:\" \" }}">>, [{var1, "String with spaces"}],
  407. <<"Stringwithspaces">>},
  408. {"|date 1",
  409. <<"{{ var1|date:\"jS F Y H:i\" }}">>,
  410. [{var1, {1975,7,24}}],
  411. <<"24th July 1975 00:00">>},
  412. {"|date 2",
  413. <<"{{ var1|date:\"jS F Y H:i\" }}">>,
  414. [{var1, {{1975,7,24}, {7,13,1}}}],
  415. <<"24th July 1975 07:13">>},
  416. {"|date 3",
  417. <<"{{ var1|date }}">>,
  418. [{var1, {{1975,7,24}, {7,13,1}}}],
  419. <<"July 24, 1975">>},
  420. {"|default:\"foo\" 1",
  421. <<"{{ var1|default:\"foo\" }}">>, [], <<"foo">>},
  422. {"|default:\"foo\" 2",
  423. <<"{{ var1|default:\"foo\" }}">>, [{var1, "bar"}], <<"bar">>},
  424. {"|default:\"foo\" 3",
  425. <<"{{ var1|default:\"foo\" }}">>, [{var1, "0"}], <<"foo">>},
  426. {"|default_if_none:\"foo\"",
  427. <<"{{ var1|default_if_none:\"foo\" }}">>, [], <<"foo">>},
  428. {"|default_if_none:\"foo\" 2",
  429. <<"{{ var1|default_if_none:\"foo\" }}">>, [{var1, "bar"}], <<"bar">>},
  430. {"|dictsort 1",
  431. <<"{{ var1|dictsort:\"foo\" }}">>,
  432. [{var1,[[{foo,2}],[{foo,1}]]}], <<"{foo,1}{foo,2}">>},
  433. {"|dictsort 2",
  434. <<"{{ var1|dictsort:\"foo.bar\" }}">>,
  435. [{var1,[[{foo,[{bar,2}]}],[{foo,[{bar,1}]}]]}],
  436. <<"{foo,[{bar,1}]}{foo,[{bar,2}]}">>},
  437. {"|divisibleby:\"3\"",
  438. <<"{% if var1|divisibleby:\"3\" %}yay{% endif %}">>, [{var1, 21}], <<"yay">>},
  439. {"|divisibleby:\"3\"",
  440. <<"{% if var1|divisibleby:\"3\" %}yay{% endif %}">>, [{var1, 22}], <<"">>},
  441. {"|escape",
  442. <<"{% autoescape on %}{{ var1|escape|escape|escape }}{% endautoescape %}">>, [{var1, ">&1"}], <<"&gt;&amp;1">>},
  443. {"|escapejs",
  444. <<"{{ var1|escapejs }}">>, [{var1, "testing\r\njavascript 'string\" <b>escaping</b>"}],
  445. <<"testing\\u000D\\u000Ajavascript \\u0027string\\u0022 \\u003Cb\\u003Eescaping\\u003C/b\\u003E">>},
  446. {"|filesizeformat (bytes)",
  447. <<"{{ var1|filesizeformat }}">>, [{var1, 1023}], <<"1023 bytes">>},
  448. {"|filesizeformat (KB)",
  449. <<"{{ var1|filesizeformat }}">>, [{var1, 3487}], <<"3.4 KB">>},
  450. {"|filesizeformat (MB)",
  451. <<"{{ var1|filesizeformat }}">>, [{var1, 6277098}], <<"6.0 MB">>},
  452. {"|filesizeformat (GB)",
  453. <<"{{ var1|filesizeformat }}">>, [{var1, 1024 * 1024 * 1024}], <<"1.0 GB">>},
  454. {"|first",
  455. <<"{{ var1|first }}">>, [{var1, "James"}],
  456. <<"J">>},
  457. {"|fix_ampersands",
  458. <<"{{ var1|fix_ampersands }}">>, [{var1, "Ben & Jerry's"}],
  459. <<"Ben &amp; Jerry's">>},
  460. {"|floatformat:\"-1\"",
  461. <<"{{ var1|floatformat:\"-1\" }}">>, [{var1, 34.23234}],
  462. <<"34.2">>},
  463. %% ?assertEqual( "", erlydtl_filters:floatformat(,)),
  464. %% ?assertEqual( "34", erlydtl_filters:floatformat(34.00000,-1)),
  465. %% ?assertEqual( "34.3", erlydtl_filters:floatformat(34.26000,-1)),
  466. %% ?assertEqual( "34.232", erlydtl_filters:floatformat(34.23234,3)),
  467. %% ?assertEqual( "34.000", erlydtl_filters:floatformat(34.00000,3)),
  468. %% ?assertEqual( "34.260", erlydtl_filters:floatformat(34.26000,3)),
  469. %% ?assertEqual( "34.232", erlydtl_filters:floatformat(34.23234,-3)),
  470. %% ?assertEqual( "34", erlydtl_filters:floatformat(34.00000,-3)),
  471. %% ?assertEqual( "34.260", erlydtl_filters:floatformat(34.26000,-3)).
  472. {"|force_escape",
  473. <<"{{ var1|force_escape }}">>, [{var1, "Ben & Jerry's <=> \"The World's Best Ice Cream\""}],
  474. <<"Ben &amp; Jerry&#039;s &lt;=&gt; &quot;The World&#039;s Best Ice Cream&quot;">>},
  475. {"|format_integer",
  476. <<"{{ var1|format_integer }}">>, [{var1, 28}], <<"28">>},
  477. {"|format_number 1",
  478. <<"{{ var1|format_number }}">>, [{var1, 28}], <<"28">>},
  479. {"|format_number 2",
  480. <<"{{ var1|format_number }}">>, [{var1, 23.77}], <<"23.77">>},
  481. {"|format_number 3",
  482. <<"{{ var1|format_number }}">>, [{var1, "28.77"}], <<"28.77">>},
  483. {"|format_number 4",
  484. <<"{{ var1|format_number }}">>, [{var1, "23.77"}], <<"23.77">>},
  485. {"|format_number 5",
  486. <<"{{ var1|format_number }}">>, [{var1, fun() -> 29 end}], <<"29">>},
  487. {"|format_number 6",
  488. <<"{{ var1|format_number }}">>, [{var1, fun() -> fun() -> 31 end end}], <<"31">>},
  489. {"|get_digit:\"2\"",
  490. <<"{{ var1|get_digit:\"2\" }}">>, [{var1, 42}], <<"4">>},
  491. {"|iriencode",
  492. <<"{{ url|iriencode }}">>, [{url, "You #$*@!!"}], <<"You+#$*@!!">>},
  493. {"|join:\", \" (list)",
  494. <<"{{ var1|join:\", \" }}">>, [{var1, ["Liberte", "Egalite", "Fraternite"]}],
  495. <<"Liberte, Egalite, Fraternite">>},
  496. {"|join:\", \" (binary)",
  497. <<"{{ var1|join:\", \" }}">>, [{var1, [<<"Liberte">>, "Egalite", <<"Fraternite">>]}],
  498. <<"Liberte, Egalite, Fraternite">>},
  499. {"|last",
  500. <<"{{ var1|last }}">>, [{var1, "XYZ"}],
  501. <<"Z">>},
  502. {"|length",
  503. <<"{{ var1|length }}">>, [{var1, "antidisestablishmentarianism"}],
  504. <<"28">>},
  505. {"|linebreaks",
  506. <<"{{ var1|linebreaks }}">>, [{var1, "Joel\nis a slug"}],
  507. <<"<p>Joel<br />is a slug</p>">>},
  508. {"|linebreaks",
  509. <<"{{ var1|linebreaks }}">>, [{var1, "Joel\n\n\n\nis a slug"}],
  510. <<"<p>Joel</p><p>is a slug</p>">>},
  511. {"|linebreaks",
  512. <<"{{ var1|linebreaks }}">>, [{var1, "Joel\n\nis a \nslug"}],
  513. <<"<p>Joel</p><p>is a <br />slug</p>">>},
  514. {"|linebreaksbr",
  515. <<"{{ var1|linebreaksbr }}">>, [{var1, "One\nTwo\n\nThree\n\n\n"}],
  516. <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
  517. {"|linebreaksbr",
  518. <<"{{ \"One\\nTwo\\n\\nThree\\n\\n\\n\"|linebreaksbr }}">>, [],
  519. <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
  520. {"|linenumbers",
  521. <<"{{ var1|linenumbers }}">>, [{var1, "a\nb\nc"}],
  522. <<"1. a\n2. b\n3. c">>},
  523. {"|linenumbers",
  524. <<"{{ var1|linenumbers }}">>, [{var1, "a"}],
  525. <<"1. a">>},
  526. {"|linenumbers",
  527. <<"{{ var1|linenumbers }}">>, [{var1, "a\n"}],
  528. <<"1. a\n2. ">>},
  529. {"|ljust:10",
  530. <<"{{ var1|ljust:10 }}">>, [{var1, "Gore"}],
  531. <<"Gore ">>},
  532. {"|lower",
  533. <<"{{ var1|lower }}">>, [{var1, "E. E. Cummings"}],
  534. <<"e. e. cummings">>},
  535. {"|makelist",
  536. <<"{{ list|make_list }}">>, [{list, "Joel"}],
  537. <<"J","o","e","l">>},
  538. {"|pluralize",
  539. <<"{{ num|pluralize }}">>, [{num, 1}],
  540. <<"">>},
  541. {"|pluralize",
  542. <<"{{ num|pluralize }}">>, [{num, 2}],
  543. <<"s">>},
  544. {"|pluralize:\"s\"",
  545. <<"{{ num|pluralize }}">>, [{num, 1}],
  546. <<"">>},
  547. {"|pluralize:\"s\"",
  548. <<"{{ num|pluralize }}">>, [{num, 2}],
  549. <<"s">>},
  550. {"|pluralize:\"y,es\" (list)",
  551. <<"{{ num|pluralize:\"y,es\" }}">>, [{num, 1}],
  552. <<"y">>},
  553. {"|pluralize:\"y,es\" (list)",
  554. <<"{{ num|pluralize:\"y,es\" }}">>, [{num, 2}],
  555. <<"es">>},
  556. {"|random",
  557. <<"{{ var1|random }}">>, [{var1, ["foo", "foo", "foo"]}],
  558. <<"foo">>},
  559. {"|removetags:\"b span\"",
  560. <<"{{ var1|removetags:\"b span\" }}">>, [{var1, "<B>Joel</B> <button>is</button> a <span>slug</span>"}],
  561. <<"<B>Joel</B> <button>is</button> a slug">>},
  562. {"|rjust:10",
  563. <<"{{ var1|rjust:10 }}">>, [{var1, "Bush"}],
  564. <<" Bush">>},
  565. {"|safe",
  566. <<"{% autoescape on %}{{ var1|safe|escape }}{% endautoescape %}">>, [{var1, "&"}],
  567. <<"&">>},
  568. %%python/django slice is zero based, erlang lists are 1 based
  569. %%first number included, second number not
  570. %%negative numbers are allowed
  571. %%regex to convert from erlydtl_filters_tests:
  572. % for slice: \?assert.*\( \[(.*)\], erlydtl_filters:(.*)\((.*),"(.*)"\)\),
  573. % {"|slice:\"$4\"", <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],<<$1>>},
  574. % \t\t{"|slice:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>},
  575. %
  576. % for stringformat:
  577. % \?assert.*\( (.*), erlydtl_filters:(.*)\((.*), "(.*)"\) \)
  578. % \t\t{"|stringformat:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>}
  579. {"|slice:\":\"",
  580. <<"{{ var|slice:\":\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  581. <<1,2,3,4,5,6,7,8,9>>},
  582. {"|slice:\"1\"",
  583. <<"{{ var|slice:\"1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  584. <<"2">>},
  585. {"|slice:\"100\"",
  586. <<"{{ var|slice:\"100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  587. <<"indexError">>},
  588. {"|slice:\"-1\"",
  589. <<"{{ var|slice:\"-1\" }}">>, [{var, ["a","b","c","d","e","f","g","h","i"]}],
  590. <<"i">>},
  591. {"|slice:\"-1\"",
  592. <<"{{ var|slice:\"-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  593. <<"9">>},
  594. {"|slice:\"-100\"",
  595. <<"{{ var|slice:\"-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  596. <<"indexError">>},
  597. {"|slice:\"1:\"",
  598. <<"{{ var|slice:\"1:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  599. <<2,3,4,5,6,7,8,9>>},
  600. {"|slice:\"100:\"",
  601. <<"{{ var|slice:\"100:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  602. <<>>},
  603. {"|slice:\"-1:\"",
  604. <<"{{ var|slice:\"-1:\" }}">>, [{var, ["a","b","c","d","e","f","h","i","j"]}],
  605. <<"j">>},
  606. {"|slice:\"-1:\"",
  607. <<"{{ var|slice:\"-1:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  608. <<9>>},
  609. {"|slice:\"-100:\"",
  610. <<"{{ var|slice:\"-100:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  611. <<1,2,3,4,5,6,7,8,9>>},
  612. {"|slice:\":1\"",
  613. <<"{{ var|slice:\":1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  614. <<1>>},
  615. {"|slice:\":100\"",
  616. <<"{{ var|slice:\":100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  617. <<1,2,3,4,5,6,7,8,9>>},
  618. {"|slice:\":-1\"",
  619. <<"{{ var|slice:\":-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  620. <<1,2,3,4,5,6,7,8>>},
  621. {"|slice:\":-100\"",
  622. <<"{{ var|slice:\":-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  623. <<>>},
  624. {"|slice:\"-1:-1\"",
  625. <<"{{ var|slice:\"-1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  626. <<>>},
  627. {"|slice:\"1:1\"",
  628. <<"{{ var|slice:\"1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  629. <<>>},
  630. {"|slice:\"1:-1\"",
  631. <<"{{ var|slice:\"1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  632. <<2,3,4,5,6,7,8>>},
  633. {"|slice:\"-1:1\"",
  634. <<"{{ var|slice:\"-1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  635. <<>>},
  636. {"|slice:\"-100:-100\"",
  637. <<"{{ var|slice:\"-100:-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  638. <<>>},
  639. {"|slice:\"100:100\"",
  640. <<"{{ var|slice:\"100:100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  641. <<>>},
  642. {"|slice:\"100:-100\"",
  643. <<"{{ var|slice:\"100:-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  644. <<>>},
  645. {"|slice:\"-100:100\"",
  646. <<"{{ var|slice:\"-100:100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  647. <<1,2,3,4,5,6,7,8,9>>},
  648. {"|slice:\"1:3\"",
  649. <<"{{ var|slice:\"1:3\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  650. <<2,3>>},
  651. {"|slice:\"::\"",
  652. <<"{{ var|slice:\"::\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  653. <<1,2,3,4,5,6,7,8,9>>},
  654. {"|slice:\"1:9:1\"",
  655. <<"{{ var|slice:\"1:9:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  656. <<2,3,4,5,6,7,8,9>>},
  657. {"|slice:\"10:1:-1\"",
  658. <<"{{ var|slice:\"10:1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  659. <<9,8,7,6,5,4,3>>},
  660. {"|slice:\"-111:-1:1\"",
  661. <<"{{ var|slice:\"-111:-1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  662. <<1,2,3,4,5,6,7,8>>},
  663. {"|slice:\"-111:-111:1\"",
  664. <<"{{ var|slice:\"-111:-111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  665. <<>>},
  666. {"|slice:\"111:111:1\"",
  667. <<"{{ var|slice:\"111:111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  668. <<>>},
  669. {"|slice:\"-111:111:1\"",
  670. <<"{{ var|slice:\"-111:111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  671. <<1,2,3,4,5,6,7,8,9>>},
  672. {"|slice:\"111:-111:1\"",
  673. <<"{{ var|slice:\"111:-111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  674. <<>>},
  675. {"|slice:\"-111:-111:-1\"",
  676. <<"{{ var|slice:\"-111:-111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  677. <<>>},
  678. {"|slice:\"111:111:-1\"",
  679. <<"{{ var|slice:\"111:111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  680. <<>>},
  681. {"|slice:\"-111:111:-1\"",
  682. <<"{{ var|slice:\"-111:111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  683. <<>>},
  684. {"|slice:\"111:-111:-1\"",
  685. <<"{{ var|slice:\"111:-111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  686. <<9,8,7,6,5,4,3,2,1>>}, {"|phone2numeric",
  687. <<"{{ var1|phone2numeric }}">>, [{var1, "1-800-COLLECT"}],
  688. <<"1-800-2655328">>},
  689. {"|slugify",
  690. <<"{{ var1|slugify }}">>, [{var1, "What The $#_! Was He Thinking?"}],
  691. <<"what-the-_-was-he-thinking">>},
  692. {"|slice:\"s\"",
  693. <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
  694. <<"test">>},
  695. {"|stringformat:\"s\"",
  696. <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
  697. <<"test">>},
  698. {"|stringformat:\"s\"",
  699. <<"{{ var|stringformat:\"s\" }}">>, [{var, "1"}],
  700. <<"1">>},
  701. {"|stringformat:\"s\"",
  702. <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
  703. <<"test">>},
  704. {"|stringformat:\"10s\"",
  705. <<"{{ var|stringformat:\"10s\" }}">>, [{var, "test"}],
  706. <<" test">>},
  707. {"|stringformat:\"-10s\"",
  708. <<"{{ var|stringformat:\"-10s\" }}">>, [{var, "test"}],
  709. <<"test ">>},
  710. {"|stringformat:\"d\"",
  711. <<"{{ var|stringformat:\"d\" }}">>, [{var, "90"}],
  712. <<"90">>},
  713. {"|stringformat:\"10d\"",
  714. <<"{{ var|stringformat:\"10d\" }}">>, [{var, "90"}],
  715. <<" 90">>},
  716. {"|stringformat:\"-10d\"",
  717. <<"{{ var|stringformat:\"-10d\" }}">>, [{var, "90"}],
  718. <<"90 ">>},
  719. {"|stringformat:\"i\"",
  720. <<"{{ var|stringformat:\"i\" }}">>, [{var, "90"}],
  721. <<"90">>},
  722. {"|stringformat:\"10i\"",
  723. <<"{{ var|stringformat:\"10i\" }}">>, [{var, "90"}],
  724. <<" 90">>},
  725. {"|stringformat:\"-10i\"",
  726. <<"{{ var|stringformat:\"-10i\" }}">>, [{var, "90"}],
  727. <<"90 ">>},
  728. {"|stringformat:\"0.2d\"",
  729. <<"{{ var|stringformat:\"0.2d\" }}">>, [{var, "9"}],
  730. <<"09">>},
  731. {"|stringformat:\"10.4d\"",
  732. <<"{{ var|stringformat:\"10.4d\" }}">>, [{var, "9"}],
  733. <<" 0009">>},
  734. {"|stringformat:\"-10.4d\"",
  735. <<"{{ var|stringformat:\"-10.4d\" }}">>, [{var, "9"}],
  736. <<"0009 ">>},
  737. {"|stringformat:\"f\"",
  738. <<"{{ var|stringformat:\"f\" }}">>, [{var, "1"}],
  739. <<"1.000000">>},
  740. {"|stringformat:\".2f\"",
  741. <<"{{ var|stringformat:\".2f\" }}">>, [{var, "1"}],
  742. <<"1.00">>},
  743. {"|stringformat:\"0.2f\"",
  744. <<"{{ var|stringformat:\"0.2f\" }}">>, [{var, "1"}],
  745. <<"1.00">>},
  746. {"|stringformat:\"-0.2f\"",
  747. <<"{{ var|stringformat:\"-0.2f\" }}">>, [{var, "1"}],
  748. <<"1.00">>},
  749. {"|stringformat:\"10.2f\"",
  750. <<"{{ var|stringformat:\"10.2f\" }}">>, [{var, "1"}],
  751. <<" 1.00">>},
  752. {"|stringformat:\"-10.2f\"",
  753. <<"{{ var|stringformat:\"-10.2f\" }}">>, [{var, "1"}],
  754. <<"1.00 ">>},
  755. {"|stringformat:\".2f\"",
  756. <<"{{ var|stringformat:\".2f\" }}">>, [{var, "1"}],
  757. <<"1.00">>},
  758. {"|stringformat:\"x\"",
  759. <<"{{ var|stringformat:\"x\" }}">>, [{var, "90"}],
  760. <<"5a">>},
  761. {"|stringformat:\"X\"",
  762. <<"{{ var|stringformat:\"X\" }}">>, [{var, "90"}],
  763. <<"5A">>},
  764. {"|stringformat:\"o\"",
  765. <<"{{ var|stringformat:\"o\" }}">>, [{var, "90"}],
  766. <<"132">>},
  767. {"|stringformat:\"e\"",
  768. <<"{{ var|stringformat:\"e\" }}">>, [{var, "90"}],
  769. <<"9.000000e+01">>},
  770. {"|stringformat:\"e\"",
  771. <<"{{ var|stringformat:\"e\" }}">>, [{var, "90000000000"}],
  772. <<"9.000000e+10">>},
  773. {"|stringformat:\"E\"",
  774. <<"{{ var|stringformat:\"E\" }}">>, [{var, "90"}],
  775. <<"9.000000E+01">>},
  776. {"|striptags",
  777. <<"{{ var|striptags }}">>, [{var, "<b>Joel</b> <button>is</button> a <span>slug</span>"}],
  778. <<"Joel is a slug">>},
  779. {"|striptags",
  780. <<"{{ var|striptags }}">>, [{var, "<B>Joel</B> <button>is</button> a <span>slug</Span>"}],
  781. <<"Joel is a slug">>},
  782. {"|striptags",
  783. <<"{{ var|striptags }}">>, [{var, "Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>"}],
  784. <<"Check out http://www.djangoproject.com">>},
  785. {"|time:\"H:i\"",
  786. <<"{{ var|time:\"H:i\" }}">>, [{var, {{2010,12,1}, {10,11,12}} }],
  787. <<"10:11">>},
  788. {"|time",
  789. <<"{{ var|time }}">>, [{var, {{2010,12,1}, {10,11,12}} }],
  790. <<"10:11 a.m.">>},
  791. {"|timesince:from_date",
  792. <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2006,6,1},{8,0,0}} }, {from_date, {{2006,6,1},{0,0,0}} }],
  793. <<"8 hours">>},
  794. {"|timesince:from_date",
  795. <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2010,6,1},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  796. <<"4 years, 1 day">>}, % leap year
  797. {"|timesince:from_date",
  798. <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2006,7,15},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  799. <<"1 month, 2 weeks">>},
  800. {"|timeuntil:from_date",
  801. <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2006,6,1},{8,0,0}} }, {from_date, {{2006,6,1},{0,0,0}} }],
  802. <<"8 hours">>},
  803. {"|timeuntil:from_date",
  804. <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2010,6,1},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  805. <<"4 years, 1 day">>},
  806. {"|timeuntil:from_date",
  807. <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2006,7,15},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  808. <<"1 month, 2 weeks">>},
  809. {"|title",
  810. <<"{{ \"my title case\"|title }}">>, [],
  811. <<"My Title Case">>},
  812. {"|title (pre-formatted)",
  813. <<"{{ \"My Title Case\"|title }}">>, [],
  814. <<"My Title Case">>},
  815. {"|title (wacky separators)",
  816. <<"{{ \"my-title!case\"|title }}">>, [],
  817. <<"My-Title!Case">>},
  818. {"|title (numbers)",
  819. <<"{{ \"my-title123CaSe\"|title }}">>, [],
  820. <<"My-Title123case">>},
  821. {"|title (Irish names)",
  822. <<"{{ \"who's o'malley?\"|title }}">>, [],
  823. <<"Who's O'Malley?">>},
  824. {"|truncatechars:0",
  825. <<"{{ var1|truncatechars:0 }}">>, [{var1, "Empty Me"}],
  826. <<"">>},
  827. {"|truncatechars:11",
  828. <<"{{ var1|truncatechars:11 }}">>, [{var1, "Truncate Me Please"}],
  829. <<"Truncate Me...">>},
  830. {"|truncatechars:17",
  831. <<"{{ var1|truncatechars:17 }}">>, [{var1, "Don't Truncate Me"}],
  832. <<"Don't Truncate Me">>},
  833. {"|truncatechars:1 (UTF-8)",
  834. <<"{{ var1|truncatechars:1 }}">>, [{var1, "\x{E2}\x{82}\x{AC}1.99"}],
  835. <<"\x{E2}\x{82}\x{AC}...">>},
  836. {"|truncatechars:2 (UTF-8)",
  837. <<"{{ var1|truncatechars:2 }}">>, [{var1, "\x{E2}\x{82}\x{AC}1.99"}],
  838. <<"\x{E2}\x{82}\x{AC}1...">>},
  839. {"|truncatewords:0",
  840. <<"{{ var1|truncatewords:0 }}">>, [{var1, "Empty Me"}],
  841. <<"">>},
  842. {"|truncatewords:2",
  843. <<"{{ var1|truncatewords:2 }}">>, [{var1, "Truncate Me Please"}],
  844. <<"Truncate Me...">>},
  845. {"|truncatewords:3",
  846. <<"{{ var1|truncatewords:3 }}">>, [{var1, "Don't Truncate Me"}],
  847. <<"Don't Truncate Me">>},
  848. {"|truncatewords_html:4",
  849. <<"{{ var1|truncatewords_html:4 }}">>, [{var1, "<p>The <strong>Long and <em>Winding</em> Road</strong> is too long</p>"}],
  850. <<"<p>The <strong>Long and <em>Winding</em>...</strong></p>">>},
  851. {"|unordered_list",
  852. <<"{{ var1|unordered_list }}">>, [{var1, ["States", ["Kansas", ["Lawrence", "Topeka"], "Illinois"]]}],
  853. <<"<li>States<ul><li>Kansas<ul><li>Lawrence</li><li>Topeka</li></ul></li><li>Illinois</li></ul></li>">>},
  854. {"|upper",
  855. <<"{{ message|upper }}">>, [{message, "That man has a gun."}],
  856. <<"THAT MAN HAS A GUN.">>},
  857. {"|urlencode",
  858. <<"{{ url|urlencode }}">>, [{url, "You #$*@!!"}],
  859. <<"You+%23%24%2A%40%21%21">>},
  860. {"|urlize",
  861. <<"{{ var|urlize }}">>, [{var, "Check out www.djangoproject.com"}],
  862. <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">www.djangoproject.com</a>">>},
  863. {"|urlize",
  864. <<"{{ var|urlize }}">>, [{var, "Check out http://www.djangoproject.com"}],
  865. <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>">>},
  866. {"|urlize",
  867. <<"{{ var|urlize }}">>, [{var, "Check out \"http://www.djangoproject.com\""}],
  868. <<"Check out \"<a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>\"">>},
  869. {"|urlizetrunc:15",
  870. <<"{{ var|urlizetrunc:15 }}">>, [{var, "Check out www.djangoproject.com"}],
  871. <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">www.djangopr...</a>">>},
  872. {"|wordcount",
  873. <<"{{ words|wordcount }}">>, [{words, "Why Hello There!"}],
  874. <<"3">>},
  875. {"|wordwrap:2",
  876. <<"{{ words|wordwrap:2 }}">>, [{words, "this is"}],
  877. <<"this \nis">>},
  878. {"|wordwrap:100",
  879. <<"{{ words|wordwrap:100 }}">>, [{words, "testing testing"}],
  880. <<"testing testing">>},
  881. {"|wordwrap:10",
  882. <<"{{ words|wordwrap:10 }}">>, [{words, ""}],
  883. <<"">>},
  884. {"|wordwrap:1",
  885. <<"{{ words|wordwrap:1 }}">>, [{words, "two"}],
  886. <<"two">>},
  887. % yesno match: \?assert.*\( (.*), erlydtl_filters:(.*)\((.*), "(.*)"\)\)
  888. % yesno replace: \t\t{"|$2:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>}
  889. {"|yesno:\"yeah,no,maybe\"",
  890. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, true}],
  891. <<"yeah">>},
  892. {"|yesno:\"yeah,no,maybe\"",
  893. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, false}],
  894. <<"no">>},
  895. {"|yesno:\"yeah,no\"",
  896. <<"{{ var|yesno:\"yeah,no\" }}">>, [{var, undefined}],
  897. <<"no">>},
  898. {"|yesno:\"yeah,no,maybe\"",
  899. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, undefined}],
  900. <<"maybe">>}
  901. ]},
  902. {"filters_if", [
  903. {"Filter if 1.1",
  904. <<"{% if var1|length_is:0 %}Y{% else %}N{% endif %}">>,
  905. [{var1, []}],
  906. <<"Y">>},
  907. {"Filter if 1.2",
  908. <<"{% if var1|length_is:1 %}Y{% else %}N{% endif %}">>,
  909. [{var1, []}],
  910. <<"N">>},
  911. {"Filter if 1.3",
  912. <<"{% if var1|length_is:7 %}Y{% else %}N{% endif %}">>,
  913. [{var1, []}],
  914. <<"N">>},
  915. {"Filter if 2.1",
  916. <<"{% if var1|length_is:0 %}Y{% else %}N{% endif %}">>,
  917. [{var1, ["foo"]}],
  918. <<"N">>},
  919. {"Filter if 2.2",
  920. <<"{% if var1|length_is:1 %}Y{% else %}N{% endif %}">>,
  921. [{var1, ["foo"]}],
  922. <<"Y">>},
  923. {"Filter if 2.3",
  924. <<"{% if var1|length_is:7 %}Y{% else %}N{% endif %}">>,
  925. [{var1, ["foo"]}],
  926. <<"N">>},
  927. {"Filter if 3.1",
  928. <<"{% ifequal var1|length 0 %}Y{% else %}N{% endifequal %}">>,
  929. [{var1, []}],
  930. <<"Y">>},
  931. {"Filter if 3.2",
  932. <<"{% ifequal var1|length 1 %}Y{% else %}N{% endifequal %}">>,
  933. [{var1, []}],
  934. <<"N">>},
  935. {"Filter if 4.1",
  936. <<"{% ifequal var1|length 3 %}Y{% else %}N{% endifequal %}">>,
  937. [{var1, ["foo", "bar", "baz"]}],
  938. <<"Y">>},
  939. {"Filter if 4.2",
  940. <<"{% ifequal var1|length 0 %}Y{% else %}N{% endifequal %}">>,
  941. [{var1, ["foo", "bar", "baz"]}],
  942. <<"N">>},
  943. {"Filter if 4.3",
  944. <<"{% ifequal var1|length 1 %}Y{% else %}N{% endifequal %}">>,
  945. [{var1, ["foo", "bar", "baz"]}],
  946. <<"N">>}
  947. ]},
  948. {"firstof", [
  949. {"Firstof first",
  950. <<"{% firstof foo bar baz %}">>,
  951. [{foo, "1"},{bar, "2"}],
  952. <<"1">>},
  953. {"Firstof second",
  954. <<"{% firstof foo bar baz %}">>,
  955. [{bar, "2"}],
  956. <<"2">>},
  957. {"Firstof none",
  958. <<"{% firstof foo bar baz %}">>,
  959. [],
  960. <<"">>},
  961. {"Firstof complex",
  962. <<"{% firstof foo.bar.baz bar %}">>,
  963. [{foo, [{bar, [{baz, "quux"}]}]}],
  964. <<"quux">>},
  965. {"Firstof undefined complex",
  966. <<"{% firstof foo.bar.baz bar %}">>,
  967. [{bar, "bar"}],
  968. <<"bar">>},
  969. {"Firstof literal",
  970. <<"{% firstof foo bar \"baz\" %}">>,
  971. [],
  972. <<"baz">>}
  973. ]},
  974. {"regroup", [
  975. {"Ordered", <<"{% regroup people by gender as gender_list %}{% for gender in gender_list %}{{ gender.grouper }}\n{% for item in gender.list %}{{ item.first_name }}\n{% endfor %}{% endfor %}{% endregroup %}">>,
  976. [{people, [[{first_name, "George"}, {gender, "Male"}], [{first_name, "Bill"}, {gender, "Male"}],
  977. [{first_name, "Margaret"}, {gender, "Female"}], [{first_name, "Condi"}, {gender, "Female"}]]}],
  978. <<"Male\nGeorge\nBill\nFemale\nMargaret\nCondi\n">>},
  979. {"Unordered", <<"{% regroup people by gender as gender_list %}{% for gender in gender_list %}{{ gender.grouper }}\n{% for item in gender.list %}{{ item.first_name }}\n{% endfor %}{% endfor %}{% endregroup %}">>,
  980. [{people, [[{first_name, "George"}, {gender, "Male"}],
  981. [{first_name, "Margaret"}, {gender, "Female"}],
  982. [{first_name, "Condi"}, {gender, "Female"}],
  983. [{first_name, "Bill"}, {gender, "Male"}]
  984. ]}],
  985. <<"Male\nGeorge\nFemale\nMargaret\nCondi\nMale\nBill\n">>},
  986. {"NestedOrdered", <<"{% regroup people by name.last as lastname_list %}{% for lastname in lastname_list %}{{ lastname.grouper }}\n{% for item in lastname.list %}{{ item.name.first }}\n{% endfor %}{% endfor %}{% endregroup %}">>,
  987. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  988. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  989. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  990. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  991. <<"Costanza\nGeorge\nMargaret\nBuffalo\nBill\nCondi\n">>},
  992. {"NestedUnordered", <<"{% regroup people by name.last as lastname_list %}{% for lastname in lastname_list %}{{ lastname.grouper }}\n{% for item in lastname.list %}{{ item.name.first }}\n{% endfor %}{% endfor %}{% endregroup %}">>,
  993. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  994. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  995. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  996. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  997. <<"Costanza\nGeorge\nBuffalo\nBill\nCostanza\nMargaret\nBuffalo\nCondi\n">>},
  998. {"Filter", <<"{% regroup people|dictsort:\"name.last\" by name.last as lastname_list %}{% for lastname in lastname_list %}{{ lastname.grouper }}\n{% for item in lastname.list %}{{ item.name.first }}\n{% endfor %}{% endfor %}{% endregroup %}">>,
  999. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1000. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1001. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1002. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1003. <<"Buffalo\nBill\nCondi\nCostanza\nGeorge\nMargaret\n">>}
  1004. ]},
  1005. {"spaceless", [
  1006. {"Beginning", <<"{% spaceless %} <b>foo</b>{% endspaceless %}">>, [], <<"<b>foo</b>">>},
  1007. {"Middle", <<"{% spaceless %}<b>foo</b> <b>bar</b>{% endspaceless %}">>, [], <<"<b>foo</b><b>bar</b>">>},
  1008. {"End", <<"{% spaceless %}<b>foo</b> {% endspaceless %}">>, [], <<"<b>foo</b>">>},
  1009. {"NewLine", <<"{% spaceless %}\n<div> \n <b>foo</b> \n </div>\n {% endspaceless %}">>, [], <<"<div><b>foo</b></div>">>}
  1010. ]},
  1011. {"templatetag", [
  1012. {"openblock", <<"{% templatetag openblock %}">>, [], <<"{%">>},
  1013. {"closeblock", <<"{% templatetag closeblock %}">>, [], <<"%}">>},
  1014. {"openvariable", <<"{% templatetag openvariable %}">>, [], <<"{{">>},
  1015. {"closevariable", <<"{% templatetag closevariable %}">>, [], <<"}}">>},
  1016. {"openbrace", <<"{% templatetag openbrace %}">>, [], <<"{">>},
  1017. {"closebrace", <<"{% templatetag closebrace %}">>, [], <<"}">>},
  1018. {"opencomment", <<"{% templatetag opencomment %}">>, [], <<"{#">>},
  1019. {"closecomment", <<"{% templatetag closecomment %}">>, [], <<"#}">>}
  1020. ]},
  1021. {"trans",
  1022. [
  1023. {"trans functional default locale",
  1024. <<"Hello {% trans \"Hi\" %}">>, [], <<"Hello Hi">>
  1025. },
  1026. {"trans functional reverse locale",
  1027. <<"Hello {% trans \"Hi\" %}">>, [], [], [{locale, "reverse"}], <<"Hello iH">>
  1028. },
  1029. {"trans literal at run-time",
  1030. <<"Hello {% trans \"Hi\" %}">>, [], [{translation_fun, fun("Hi") -> "Konichiwa" end}], [],
  1031. <<"Hello Konichiwa">>},
  1032. {"trans variable at run-time",
  1033. <<"Hello {% trans var1 %}">>, [{var1, <<"Hi">>}], [{translation_fun, fun(<<"Hi">>) -> <<"Konichiwa">> end}], [],
  1034. <<"Hello Konichiwa">>},
  1035. {"trans literal at run-time: No-op",
  1036. <<"Hello {% trans \"Hi\" noop %}">>, [], [{translation_fun, fun("Hi") -> <<"Konichiwa">> end}], [],
  1037. <<"Hello Hi">>},
  1038. {"trans variable at run-time: No-op",
  1039. <<"Hello {% trans var1 noop %}">>, [{var1, <<"Hi">>}], [{translation_fun, fun(<<"Hi">>) -> <<"Konichiwa">> end}], [],
  1040. <<"Hello Hi">>}
  1041. ]},
  1042. {"blocktrans",
  1043. [
  1044. {"blocktrans default locale",
  1045. <<"{% blocktrans %}Hello{% endblocktrans %}">>, [], <<"Hello">>},
  1046. {"blocktrans choose locale",
  1047. <<"{% blocktrans %}Hello, {{ name }}{% endblocktrans %}">>, [{name, "Mr. President"}], [{locale, "de"}],
  1048. [{blocktrans_locales, ["de"]}, {blocktrans_fun, fun("Hello, {{ name }}", "de") -> <<"Guten tag, {{ name }}">> end}], <<"Guten tag, Mr. President">>},
  1049. {"blocktrans with args",
  1050. <<"{% blocktrans with var1=foo %}{{ var1 }}{% endblocktrans %}">>, [{foo, "Hello"}], <<"Hello">>}
  1051. ]},
  1052. {"verbatim", [
  1053. {"Plain verbatim",
  1054. <<"{% verbatim %}{{ oh no{% foobar %}{% endverbatim %}">>, [],
  1055. <<"{{ oh no{% foobar %}">>},
  1056. {"Named verbatim",
  1057. <<"{% verbatim foobar %}{% verbatim %}{% endverbatim foobar2 %}{% endverbatim foobar %}">>, [],
  1058. <<"{% verbatim %}{% endverbatim foobar2 %}">>}
  1059. ]},
  1060. {"widthratio", [
  1061. {"Literals", <<"{% widthratio 5 10 100 %}">>, [], <<"50">>},
  1062. {"Rounds up", <<"{% widthratio a b 100 %}">>, [{a, 175}, {b, 200}], <<"88">>}
  1063. ]},
  1064. {"with", [
  1065. {"Cache literal",
  1066. <<"{% with a=1 %}{{ a }}{% endwith %}">>, [], <<"1">>},
  1067. {"Cache variable",
  1068. <<"{% with a=b %}{{ a }}{% endwith %}">>, [{b, "foo"}], <<"foo">>},
  1069. {"Cache variable with attribute",
  1070. <<"I enjoy {% with a = var1 %}{{ a.game }}{% endwith %}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
  1071. {"Cache variable attribute",
  1072. <<"I enjoy {% with a = var1.game %}{{ a }}{% endwith %}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
  1073. {"Cache multiple",
  1074. <<"{% with alpha=1 beta=b %}{{ alpha }}/{{ beta }}{% endwith %}">>, [{b, 2}], <<"1/2">>}
  1075. ]},
  1076. {"unicode", [
  1077. {"(tm) somewhere",
  1078. <<"™">>, [], <<"™">>}
  1079. ]},
  1080. {"contrib_humanize", [
  1081. {"intcomma",
  1082. <<"{{ a|intcomma }} {{ b|intcomma }} {{ c|intcomma }} {{ d|intcomma }}">>,
  1083. [{a, 999}, {b, 123456789}, {c, 12345}, {d, 1234567890}],
  1084. <<"999 123,456,789 12,345 1,234,567,890">>}
  1085. ]}
  1086. ].
  1087. run_tests() ->
  1088. io:format("Running unit tests...~n"),
  1089. DefaultOptions = [{custom_filters_modules, [erlydtl_contrib_humanize]}],
  1090. Failures = lists:foldl(
  1091. fun({Group, Assertions}, GroupAcc) ->
  1092. io:format(" Test group ~p...~n", [Group]),
  1093. lists:foldl(fun
  1094. ({Name, DTL, Vars, Output}, Acc) ->
  1095. process_unit_test(erlydtl:compile(DTL, erlydtl_running_test, DefaultOptions),
  1096. Vars, [], Output, Acc, Group, Name);
  1097. ({Name, DTL, Vars, RenderOpts, Output}, Acc) ->
  1098. process_unit_test(erlydtl:compile(DTL, erlydtl_running_test, DefaultOptions),
  1099. Vars, RenderOpts, Output, Acc, Group, Name);
  1100. ({Name, DTL, Vars, RenderOpts, CompilerOpts, Output}, Acc) ->
  1101. process_unit_test(erlydtl:compile(DTL, erlydtl_running_test, CompilerOpts ++ DefaultOptions),
  1102. Vars, RenderOpts, Output, Acc, Group, Name)
  1103. end, GroupAcc, Assertions)
  1104. end, [], tests()),
  1105. io:format("Unit test failures: ~p~n", [lists:reverse(Failures)]).
  1106. process_unit_test(CompiledTemplate, Vars, RenderOpts, Output,Acc, Group, Name) ->
  1107. case CompiledTemplate of
  1108. {ok, _} ->
  1109. {ok, IOList} = erlydtl_running_test:render(Vars, RenderOpts),
  1110. {ok, IOListBin} = erlydtl_running_test:render(vars_to_binary(Vars), RenderOpts),
  1111. case {iolist_to_binary(IOList), iolist_to_binary(IOListBin)} of
  1112. {Output, Output} ->
  1113. Acc;
  1114. {Output, Unexpected} ->
  1115. [{Group, Name, 'binary', Unexpected, Output} | Acc];
  1116. {Unexpected, Output} ->
  1117. [{Group, Name, 'list', Unexpected, Output} | Acc];
  1118. {Unexpected1, Unexpected2} ->
  1119. [{Group, Name, 'list', Unexpected1, Output},
  1120. {Group, Name, 'binary', Unexpected2, Output} | Acc]
  1121. end;
  1122. Err ->
  1123. [{Group, Name, Err} | Acc]
  1124. end.
  1125. vars_to_binary(Vars) when is_list(Vars) ->
  1126. lists:map(fun
  1127. ({Key, [H|_] = Value}) when is_tuple(H) ->
  1128. {Key, vars_to_binary(Value)};
  1129. ({Key, [H|_] = Value}) when is_integer(H) ->
  1130. {Key, list_to_binary(Value)};
  1131. ({Key, Value}) ->
  1132. {Key, Value}
  1133. end, Vars);
  1134. vars_to_binary(Vars) ->
  1135. Vars.
  1136. generate_test_date() ->
  1137. {{Y,M,D}, _} = erlang:localtime(),
  1138. MonthName = [
  1139. "January", "February", "March", "April",
  1140. "May", "June", "July", "August", "September",
  1141. "October", "November", "December"
  1142. ],
  1143. OrdinalSuffix = [
  1144. "st","nd","rd","th","th","th","th","th","th","th", % 1-10
  1145. "th","th","th","th","th","th","th","th","th","th", % 10-20
  1146. "st","nd","rd","th","th","th","th","th","th","th", % 20-30
  1147. "st"
  1148. ],
  1149. list_to_binary([
  1150. "It is the ",
  1151. integer_to_list(D),
  1152. lists:nth(D, OrdinalSuffix),
  1153. " of ", lists:nth(M, MonthName),
  1154. " ", integer_to_list(Y), "."
  1155. ]).