erlydtl_unittests.erl 54 KB

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