erlydtl_test_defs.erl 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  1. -module(erlydtl_test_defs).
  2. -export([tests/0]).
  3. -include("testrunner.hrl").
  4. -record(testrec, {foo, bar, baz}).
  5. %% {Name, DTL, Vars, Output}
  6. %% {Name, DTL, Vars, RenderOpts, Output}
  7. %% {Name, DTL, Vars, RenderOpts, CompilerOpts, Output}
  8. %% {Name, DTL, Vars, RenderOpts, CompilerOpts, Output, Warnings}
  9. tests() ->
  10. [def_to_test(G, D) || {G, Ds} <- all_test_defs(), D <- Ds].
  11. all_test_defs() ->
  12. [{"vars",
  13. [{"string",
  14. <<"String value is: {{ var1 }}">>,
  15. [{var1, "foo"}], <<"String value is: foo">>},
  16. {"int",
  17. <<"The magic number is: {{ var1 }}">>,
  18. [{var1, 42}], <<"The magic number is: 42">>},
  19. {"float",
  20. <<"The price of milk is: {{ var1 }}">>,
  21. [{var1, 0.42}], <<"The price of milk is: 0.42">>},
  22. {"No spaces",
  23. <<"{{var1}}">>,
  24. [{var1, "foo"}], <<"foo">>},
  25. {"Variable name is a tag name",
  26. <<"{{ comment }}">>,
  27. [{comment, "Nice work!"}], <<"Nice work!">>}
  28. ]},
  29. {"comment",
  30. [{"comment block is excised",
  31. <<"bob {% comment %}(moron){% endcomment %} loblaw">>,
  32. [], <<"bob loblaw">>},
  33. {"inline comment is excised",
  34. <<"you're {# not #} a very nice person">>,
  35. [], <<"you're a very nice person">>}
  36. ]},
  37. {"autoescape",
  38. [{"Autoescape works",
  39. <<"{% autoescape on %}{{ var1 }}{% endautoescape %}">>,
  40. [{var1, "<b>bold</b>"}], <<"&lt;b&gt;bold&lt;/b&gt;">>},
  41. {"Nested autoescape",
  42. <<"{% autoescape on %}{{ var1 }}{% autoescape off %}{{ var1 }}{% endautoescape %}{% endautoescape %}">>,
  43. [{var1, "<b>"}], <<"&lt;b&gt;<b>">>},
  44. {"default auto escape",
  45. <<"{{ var1 }}">>, [{var1, "&"}], [], [auto_escape],
  46. <<"&amp;">>},
  47. {"intermixed autoescape",
  48. <<"{% autoescape on %}1:{{ var1 }}{% endautoescape %} 2:{{ var1 }}{% autoescape on %} 3:{{ var1 }}{% endautoescape %}">>,
  49. [{var1, "&"}],
  50. <<"1:&amp; 2:& 3:&amp;">>}
  51. ]},
  52. {"string literal",
  53. [{"Render literal",
  54. <<"{{ \"foo\" }} is my name">>, [], <<"foo is my name">>},
  55. {"Newlines are escaped",
  56. <<"{{ \"foo\\n\" }}">>, [], <<"foo\n">>},
  57. {"strip quotes",
  58. <<"{{ \"foo\"|add:\"\\\"\" }}">>, [], <<"foo\"">>}
  59. ]},
  60. {"cycle",
  61. [{"Cycling through quoted strings",
  62. <<"{% for i in test %}{% cycle 'a' 'b' %}{{ i }},{% endfor %}">>,
  63. [{test, ["0", "1", "2", "3", "4"]}], <<"a0,b1,a2,b3,a4,">>},
  64. {"Cycling through normal variables",
  65. <<"{% for i in test %}{% cycle aye bee %}{{ i }},{% endfor %}">>,
  66. [{test, ["0", "1", "2", "3", "4"]}, {aye, "a"}, {bee, "b"}],
  67. <<"a0,b1,a2,b3,a4,">>}
  68. ]},
  69. {"number literal",
  70. [{"Render integer",
  71. <<"{{ 5 }}">>, [], <<"5">>}
  72. ]},
  73. {"variable",
  74. [{"Render variable",
  75. <<"{{ var1 }} is my game">>, [{var1, "bar"}], <<"bar is my game">>},
  76. {"Render variable with attribute",
  77. <<"I enjoy {{ var1.game }}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
  78. {"Render variable with string-key attribute",
  79. <<"I also enjoy {{ var1.game }}">>, [{var1, [{"game", "Parcheesi"}]}], <<"I also enjoy Parcheesi">>},
  80. {"Render variable with binary-key attribute",
  81. <<"I also enjoy {{ var1.game }}">>, [{var1, [{<<"game">>, "Parcheesi"}]}], <<"I also enjoy Parcheesi">>},
  82. {"Render variable in dict",
  83. <<"{{ var1 }}">>, dict:store(var1, "bar", dict:new()), <<"bar">>},
  84. {"Render variable with missing attribute in dict",
  85. <<"{{ var1.foo }}">>, [{var1, dict:store(bar, "Othello", dict:new())}], <<"">>},
  86. {"Render variable in gb_tree",
  87. <<"{{ var1 }}">>, gb_trees:insert(var1, "bar", gb_trees:empty()), <<"bar">>},
  88. {"Render variable in arity-1 func",
  89. <<"I enjoy {{ var1 }}">>, fun (var1) -> "Othello" end, <<"I enjoy Othello">>},
  90. {"Render variable with attribute in dict",
  91. <<"{{ var1.attr }}">>, [{var1, dict:store(attr, "Othello", dict:new())}], <<"Othello">>},
  92. {"Render variable with attribute in gb_tree",
  93. <<"{{ var1.attr }}">>, [{var1, gb_trees:insert(attr, "Othello", gb_trees:empty())}], <<"Othello">>},
  94. {"Render variable with attribute in arity-1 func",
  95. <<"I enjoy {{ var1.game }}">>, [{var1, fun (game) -> "Othello" end}], <<"I enjoy Othello">>},
  96. %% {"Render variable in parameterized module",
  97. %% <<"{{ var1.some_var }}">>, [{var1, erlydtl_example_variable_storage:new("foo")}], <<"foo">>},
  98. {"Nested attributes",
  99. <<"{{ person.city.state.country }}">>, [{person, [{city, [{state, [{country, "Italy"}]}]}]}],
  100. <<"Italy">>},
  101. {"Index list variable",
  102. <<"{{ var1.2 }}">>, [{var1, [a, b, c]}],
  103. <<"b">>},
  104. {"Index tuple variable",
  105. <<"{{ var1.2 }}">>, [{var1, {a, b, c}}],
  106. <<"b">>},
  107. {"Index all elements of list",
  108. <<"{{ var1.1 }},{{ var1.2 }},{{ var1.3 }}.">>,
  109. [{var1, [a, b, c]}],
  110. <<"a,b,c.">>}
  111. ]},
  112. {"now",
  113. [{"now functional",
  114. <<"It is the {% now \"jS \\o\\f F Y\" %}.">>, [{var1, ""}], generate_test_date()}
  115. ]},
  116. {"if",
  117. [{"If/else",
  118. <<"{% if var1 %}boo{% else %}yay{% endif %}">>, [{var1, ""}], <<"yay">>},
  119. {"If elif",
  120. <<"{% if var1 %}boo{% elif var2 %}yay{% endif %}">>, [{var1, ""}, {var2, "happy"}], <<"yay">>},
  121. {"If elif/else",
  122. <<"{% if var1 %}boo{% elif var2 %}sad{% else %}yay{% endif %}">>, [{var1, ""}, {var2, ""}], <<"yay">>},
  123. {"If elif/elif/else",
  124. <<"{% if var1 %}boo{% elif var2 %}yay{% elif var3 %}sad{% else %}noo{% endif %}">>,
  125. [{var1, ""}, {var2, "happy"}, {var3, "not_taken"}],
  126. <<"yay">>},
  127. {"If",
  128. <<"{% if var1 %}boo{% endif %}">>, [{var1, ""}], <<>>},
  129. {"If not",
  130. <<"{% if not var1 %}yay{% endif %}">>, [{var1, ""}], <<"yay">>},
  131. {"If \"0\"",
  132. <<"{% if var1 %}boo{% endif %}">>, [{var1, "0"}], <<>>},
  133. {"If 0",
  134. <<"{% if var1 %}boo{% endif %}">>, [{var1, 0}], <<>>},
  135. {"If false",
  136. <<"{% if var1 %}boo{% endif %}">>, [{var1, false}], <<>>},
  137. {"If false string",
  138. <<"{% if var1 %}boo{% endif %}">>, [{var1, "false"}], <<"boo">>},
  139. {"If undefined",
  140. <<"{% if var1 %}boo{% endif %}">>, [{var1, undefined}], <<>>},
  141. {"If other atom",
  142. <<"{% if var1 %}yay{% endif %}">>, [{var1, foobar}], <<"yay">>},
  143. {"If non-empty string",
  144. <<"{% if var1 %}yay{% endif %}">>, [{var1, "hello"}], <<"yay">>},
  145. {"If proplist",
  146. <<"{% if var1 %}yay{% endif %}">>, [{var1, [{foo, "bar"}]}], <<"yay">>},
  147. {"If complex",
  148. <<"{% if foo.bar.baz %}omgwtfbbq{% endif %}">>, [], <<"">>}
  149. ]},
  150. {"if .. in ..",
  151. [{"If substring in string",
  152. <<"{% if var1 in var2 %}yay{% endif %}">>, [{var1, "rook"}, {var2, "Crooks"}], <<"yay">>},
  153. {"If substring in string (false)",
  154. <<"{% if var1 in var2 %}boo{% endif %}">>, [{var1, "Cook"}, {var2, "Crooks"}], <<>>},
  155. {"If substring not in string",
  156. <<"{% if var1 not in var2 %}yay{% endif %}">>, [{var1, "Cook"}, {var2, "Crooks"}], <<"yay">>},
  157. {"If substring not in string (false)",
  158. <<"{% if var1 not in var2 %}boo{% endif %}">>, [{var1, "rook"}, {var2, "Crooks"}], <<>>},
  159. {"If literal substring in string",
  160. <<"{% if \"man\" in \"Ottoman\" %}yay{% endif %}">>, [], <<"yay">>},
  161. {"If literal substring in string (false)",
  162. <<"{% if \"woman\" in \"Ottoman\" %}boo{% endif %}">>, [], <<>>},
  163. {"If element in list",
  164. <<"{% if var1 in var2 %}yay{% endif %}">>, [{var1, "foo"}, {var2, ["bar", "foo", "baz"]}], <<"yay">>},
  165. {"If element in list (false)",
  166. <<"{% if var1 in var2 %}boo{% endif %}">>, [{var1, "FOO"}, {var2, ["bar", "foo", "baz"]}], <<>>}
  167. ]},
  168. {"if .. and ..",
  169. [{"If true and true",
  170. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, true}, {var2, true}], <<"yay">>},
  171. {"If true and false",
  172. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, true}, {var2, false}], <<"">>},
  173. {"If false and true",
  174. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, false}, {var2, true}], <<"">>},
  175. {"If false and false ",
  176. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, false}, {var2, false}], <<"">>}
  177. ]},
  178. {"if .. or ..",
  179. [{"If true or true",
  180. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, true}, {var2, true}], <<"yay">>},
  181. {"If true or false",
  182. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, true}, {var2, false}], <<"yay">>},
  183. {"If false or true",
  184. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, false}, {var2, true}], <<"yay">>},
  185. {"If false or false ",
  186. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, false}, {var2, false}], <<"">>}
  187. ]},
  188. {"if equality",
  189. [{"If int equals number literal",
  190. <<"{% if var1 == 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
  191. {"If int equals number literal (false)",
  192. <<"{% if var1 == 2 %}yay{% endif %}">>, [{var1, 3}], <<"">>},
  193. {"If string equals string literal",
  194. <<"{% if var1 == \"2\" %}yay{% endif %}">>, [{var1, "2"}], <<"yay">>},
  195. {"If string equals string literal (false)",
  196. <<"{% if var1 == \"2\" %}yay{% endif %}">>, [{var1, "3"}], <<"">>},
  197. {"If int not equals number literal",
  198. <<"{% if var1 != 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
  199. {"If string not equals string literal",
  200. <<"{% if var1 != \"2\" %}yay{% endif %}">>, [{var1, "3"}], <<"yay">>},
  201. {"If filter result equals number literal",
  202. <<"{% if var1|length == 2 %}yay{% endif %}">>, [{var1, ["fo", "bo"]}], <<"yay">>},
  203. {"If filter result equals string literal",
  204. <<"{% if var1|capfirst == \"Foo\" %}yay{% endif %}">>, [{var1, "foo"}], <<"yay">>}
  205. ]},
  206. {"if size comparison",
  207. [{"If int greater than number literal",
  208. <<"{% if var1 > 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
  209. {"If int greater than negative number literal",
  210. <<"{% if var1 > -2 %}yay{% endif %}">>, [{var1, -1}], <<"yay">>},
  211. {"If int greater than number literal (false)",
  212. <<"{% if var1 > 2 %}yay{% endif %}">>, [{var1, 2}], <<"">>},
  213. {"If int greater than or equal to number literal",
  214. <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
  215. {"If int greater than or equal to number literal (2)",
  216. <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
  217. {"If int greater than or equal to number literal (false)",
  218. <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 1}], <<"">>},
  219. {"If int less than number literal",
  220. <<"{% if var1 < 2 %}yay{% endif %}">>, [{var1, 1}], <<"yay">>},
  221. {"If int less than number literal (false)",
  222. <<"{% if var1 < 2 %}yay{% endif %}">>, [{var1, 2}], <<"">>},
  223. {"If int less than or equal to number literal",
  224. <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 1}], <<"yay">>},
  225. {"If int less than or equal to number literal",
  226. <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
  227. {"If int less than or equal to number literal (false)",
  228. <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 3}], <<"">>}
  229. ]},
  230. {"if complex bool",
  231. [{"If (true or false) and true",
  232. <<"{% if (var1 or var2) and var3 %}yay{% endif %}">>,
  233. [{var1, true}, {var2, false}, {var3, true}], <<"yay">>},
  234. {"If true or (false and true)",
  235. <<"{% if var1 or (var2 and var3) %}yay{% endif %}">>,
  236. [{var1, true}, {var2, false}, {var3, true}], <<"yay">>}
  237. ]},
  238. {"for",
  239. [{"Simple loop",
  240. <<"{% for x in list %}{{ x }}{% endfor %}">>, [{'list', ["1", "2", "3"]}],
  241. <<"123">>},
  242. {"Reversed loop",
  243. <<"{% for x in list reversed %}{{ x }}{% endfor %}">>, [{'list', ["1", "2", "3"]}],
  244. <<"321">>},
  245. {"Expand list",
  246. <<"{% for x, y in list %}{{ x }},{{ y }}\n{% endfor %}">>, [{'list', [["X", "1"], ["X", "2"]]}],
  247. <<"X,1\nX,2\n">>},
  248. {"Expand tuple",
  249. <<"{% for x, y in list %}{{ x }},{{ y }}\n{% endfor %}">>, [{'list', [{"X", "1"}, {"X", "2"}]}],
  250. <<"X,1\nX,2\n">>},
  251. {"Resolve variable attribute",
  252. <<"{% for number in person.numbers %}{{ number }}\n{% endfor %}">>, [{person, [{numbers, ["411", "911"]}]}],
  253. <<"411\n911\n">>},
  254. {"Resolve nested variable attribute",
  255. <<"{% for number in person.home.numbers %}{{ number }}\n{% endfor %}">>, [{person, [{home, [{numbers, ["411", "911"]}]}]}],
  256. <<"411\n911\n">>},
  257. {"Counter0",
  258. <<"{% for number in numbers %}{{ forloop.counter0 }}. {{ number }}\n{% endfor %}">>,
  259. [{numbers, ["Zero", "One", "Two"]}], <<"0. Zero\n1. One\n2. Two\n">>},
  260. {"Counter",
  261. <<"{% for number in numbers %}{{ forloop.counter }}. {{ number }}\n{% endfor %}">>,
  262. [{numbers, ["One", "Two", "Three"]}], <<"1. One\n2. Two\n3. Three\n">>},
  263. {"Reverse Counter0",
  264. <<"{% for number in numbers %}{{ forloop.revcounter0 }}. {{ number }}\n{% endfor %}">>,
  265. [{numbers, ["Two", "One", "Zero"]}], <<"2. Two\n1. One\n0. Zero\n">>},
  266. {"Reverse Counter",
  267. <<"{% for number in numbers %}{{ forloop.revcounter }}. {{ number }}\n{% endfor %}">>,
  268. [{numbers, ["Three", "Two", "One"]}], <<"3. Three\n2. Two\n1. One\n">>},
  269. {"Counter \"first\"",
  270. <<"{% for number in numbers %}{% if forloop.first %}{{ number }}{% endif %}{% endfor %}">>,
  271. [{numbers, ["One", "Two", "Three"]}], <<"One">>},
  272. {"Counter \"last\"",
  273. <<"{% for number in numbers %}{% if forloop.last %}{{ number }}{% endif %}{% endfor %}">>,
  274. [{numbers, ["One", "Two", "Three"]}], <<"Three">>},
  275. {"Nested for loop",
  276. <<"{% for outer in list %}{% for inner in outer %}{{ inner }}\n{% endfor %}{% endfor %}">>,
  277. [{'list', [["Al", "Albert"], ["Jo", "Joseph"]]}],
  278. <<"Al\nAlbert\nJo\nJoseph\n">>},
  279. {"Access parent loop counters",
  280. <<"{% for outer in list %}{% for inner in outer %}({{ forloop.parentloop.counter0 }}, {{ forloop.counter0 }})\n{% endfor %}{% endfor %}">>,
  281. [{'list', [["One", "two"], ["One", "two"]]}], [], [], <<"(0, 0)\n(0, 1)\n(1, 0)\n(1, 1)\n">>,
  282. %% the warnings we get from the erlang compiler still needs some care..
  283. [error_info([{0, erl_lint, {unused_var, 'Var_inner/3_1:31'}}])]},
  284. {"If changed",
  285. <<"{% for x in list %}{% ifchanged %}{{ x }}\n{% endifchanged %}{% endfor %}">>,
  286. [{'list', ["one", "two", "two", "three", "three", "three"]}], <<"one\ntwo\nthree\n">>},
  287. {"If changed/2",
  288. <<"{% for x, y in list %}{% ifchanged %}{{ x|upper }}{% endifchanged %}{% ifchanged %}{{ y|lower }}{% endifchanged %}\n{% endfor %}">>,
  289. [{'list', [["one", "a"], ["two", "A"], ["two", "B"], ["three", "b"], ["three", "c"], ["Three", "b"]]}], <<"ONEa\nTWO\nb\nTHREE\nc\nb\n">>},
  290. {"If changed/else",
  291. <<"{% for x in list %}{% ifchanged %}{{ x }}\n{% else %}foo\n{% endifchanged %}{% endfor %}">>,
  292. [{'list', ["one", "two", "two", "three", "three", "three"]}], <<"one\ntwo\nfoo\nthree\nfoo\nfoo\n">>},
  293. {"If changed/param",
  294. <<"{% for date in list %}{% ifchanged date.month %} {{ date.month }}:{{ date.day }}{% else %},{{ date.day }}{% endifchanged %}{% endfor %}\n">>,
  295. [{'list', [[{month,"Jan"},{day,1}],[{month,"Jan"},{day,2}],[{month,"Apr"},{day,10}],
  296. [{month,"Apr"},{day,11}],[{month,"May"},{day,4}]]}],
  297. <<" Jan:1,2 Apr:10,11 May:4\n">>},
  298. {"If changed/param2",
  299. <<"{% for x, y in list %}{% ifchanged y|upper %}{{ x|upper }}{% endifchanged %}\n{% endfor %}">>,
  300. [{'list', [["one", "a"], ["two", "A"], ["two", "B"], ["three", "b"], ["three", "c"], ["Three", "b"]]}], <<"ONE\n\nTWO\n\nTHREE\nTHREE\n">>},
  301. {"If changed/param2 combined",
  302. <<"{% for x, y in list %}{% ifchanged x y|upper %}{{ x }}{% endifchanged %}\n{% endfor %}">>,
  303. [{'list', [["one", "a"], ["two", "A"], ["two", "B"], ["three", "b"], ["three", "B"], ["three", "c"]]}], <<"one\ntwo\ntwo\nthree\n\nthree\n">>},
  304. {"If changed/resolve",
  305. <<"{% for x in list %}{% ifchanged x.name|first %}{{ x.value }}{% endifchanged %}\n{% endfor %}">>,
  306. [{'list', [[{"name", ["nA","nB"]},{"value","1"}],[{"name", ["nA","nC"]},{"value","2"}],
  307. [{"name", ["nB","nC"]},{"value","3"}],[{"name", ["nB","nA"]},{"value","4"}]]}],
  308. <<"1\n\n3\n\n">>},
  309. {"Loop undefined var",
  310. <<"{% for i in undef %}i = {{ i }}.\n{% endfor %}">>,
  311. [],
  312. <<"">>},
  313. {"Loop filtered value rather than variable",
  314. <<"{% for x in 123|make_list %}{% if not forloop.first %}, {% endif %}{{ x }}{% endfor %}">>,
  315. [],
  316. <<"1, 2, 3">>}
  317. ]},
  318. {"for/empty",
  319. [{"Simple loop",
  320. <<"{% for x in list %}{{ x }}{% empty %}shucks{% endfor %}">>, [{'list', ["1", "2", "3"]}],
  321. <<"123">>},
  322. {"Simple loop (empty)",
  323. <<"{% for x in list %}{{ x }}{% empty %}shucks{% endfor %}">>, [{'list', []}],
  324. <<"shucks">>}
  325. ]},
  326. {"ifequal",
  327. [{"Compare variable to literal",
  328. <<"{% ifequal var1 \"foo\" %}yay{% endifequal %}">>,
  329. [{var1, "foo"}], <<"yay">>},
  330. {"Compare variable to unequal literal",
  331. <<"{% ifequal var1 \"foo\" %}boo{% endifequal %}">>,
  332. [{var1, "bar"}], <<>>},
  333. {"Compare literal to variable",
  334. <<"{% ifequal \"foo\" var1 %}yay{% endifequal %}">>,
  335. [{var1, "foo"}], <<"yay">>},
  336. {"Compare literal to unequal variable",
  337. <<"{% ifequal \"foo\" var1 %}boo{% endifequal %}">>,
  338. [{var1, "bar"}], <<>>},
  339. {"Compare variable to literal (int string)",
  340. <<"{% ifequal var1 \"2\" %}yay{% else %}boo{% endifequal %}">>,
  341. [{var1, "2"}], <<"yay">>},
  342. {"Compare variable to literal (int)",
  343. <<"{% ifequal var1 2 %}yay{% else %}boo{% endifequal %}">>,
  344. [{var1, 2}], <<"yay">>},
  345. {"Compare variable to unequal literal (int)",
  346. <<"{% ifequal var1 2 %}boo{% else %}yay{% endifequal %}">>,
  347. [{var1, 3}], <<"yay">>},
  348. {"Compare variable to equal literal (atom)",
  349. <<"{% ifequal var1 \"foo\"%}yay{% endifequal %}">>,
  350. [{var1, foo}], <<"yay">>},
  351. {"Compare variable to unequal literal (atom)",
  352. <<"{% ifequal var1 \"foo\"%}yay{% else %}boo{% endifequal %}">>,
  353. [{var1, bar}], <<"boo">>}
  354. ]},
  355. {"ifequal/else",
  356. [{"Compare variable to literal",
  357. <<"{% ifequal var1 \"foo\" %}yay{% else %}boo{% endifequal %}">>,
  358. [{var1, "foo"}], <<"yay">>},
  359. {"Compare variable to unequal literal",
  360. <<"{% ifequal var1 \"foo\" %}boo{% else %}yay{% endifequal %}">>,
  361. [{var1, "bar"}], <<"yay">>},
  362. {"Compare literal to variable",
  363. <<"{% ifequal \"foo\" var1 %}yay{% else %}boo{% endifequal %}">>,
  364. [{var1, "foo"}], <<"yay">>},
  365. {"Compare literal to unequal variable",
  366. <<"{% ifequal \"foo\" var1 %}boo{% else %}yay{% endifequal %}">>,
  367. [{var1, "bar"}], <<"yay">>}
  368. ]},
  369. {"ifnotequal",
  370. [{"Compare variable to literal",
  371. <<"{% ifnotequal var1 \"foo\" %}boo{% endifnotequal %}">>,
  372. [{var1, "foo"}], <<>>},
  373. {"Compare variable to unequal literal",
  374. <<"{% ifnotequal var1 \"foo\" %}yay{% endifnotequal %}">>,
  375. [{var1, "bar"}], <<"yay">>},
  376. {"Compare literal to variable",
  377. <<"{% ifnotequal \"foo\" var1 %}boo{% endifnotequal %}">>,
  378. [{var1, "foo"}], <<>>},
  379. {"Compare literal to unequal variable",
  380. <<"{% ifnotequal \"foo\" var1 %}yay{% endifnotequal %}">>,
  381. [{var1, "bar"}], <<"yay">>}
  382. ]},
  383. {"ifnotequal/else",
  384. [{"Compare variable to literal",
  385. <<"{% ifnotequal var1 \"foo\" %}boo{% else %}yay{% endifnotequal %}">>,
  386. [{var1, "foo"}], <<"yay">>},
  387. {"Compare variable to unequal literal",
  388. <<"{% ifnotequal var1 \"foo\" %}yay{% else %}boo{% endifnotequal %}">>,
  389. [{var1, "bar"}], <<"yay">>},
  390. {"Compare literal to variable",
  391. <<"{% ifnotequal \"foo\" var1 %}boo{% else %}yay{% endifnotequal %}">>,
  392. [{var1, "foo"}], <<"yay">>},
  393. {"Compare literal to unequal variable",
  394. <<"{% ifnotequal \"foo\" var1 %}yay{% else %}boo{% endifnotequal %}">>,
  395. [{var1, "bar"}], <<"yay">>}
  396. ]},
  397. {"filter tag",
  398. [{"Apply a filter",
  399. <<"{% filter escape %}&{% endfilter %}">>, [], <<"&amp;">>},
  400. {"Chained filters",
  401. <<"{% filter linebreaksbr|escape %}\n{% endfilter %}">>, [], <<"&lt;br /&gt;">>}
  402. ]},
  403. {"filters",
  404. [{"Filter a literal",
  405. <<"{{ \"pop\"|capfirst }}">>, [],
  406. <<"Pop">>},
  407. {"Filters applied in order",
  408. <<"{{ var1|force_escape|length }}">>, [{var1, <<"&">>}],
  409. <<"5">>},
  410. {"Escape is applied last",
  411. <<"{{ var1|escape|linebreaksbr }}">>, [{var1, <<"\n">>}],
  412. <<"&lt;br /&gt;">>},
  413. {"add; lhs number, rhs number",
  414. <<"{{ one|add:4}}">>, [{one, 1}],
  415. <<"5">>},
  416. {"add; lhs numeric string, rhs number",
  417. <<"{{ one|add:4}}">>, [{one, "1"}],
  418. <<"5">>},
  419. {"add; lhs number, rhs numeric string",
  420. <<"{{ one|add:'4'}}">>, [{one, 1}],
  421. <<"5">>},
  422. {"add; lhs non-numeric string, rhs number",
  423. <<"{{ one|add:4}}">>, [{one, "foo"}],
  424. <<"foo4">>},
  425. {"add; lhs number, rhs non-numeric string",
  426. <<"{{ one|add:'foo'}}">>, [{one, 1}],
  427. <<"1foo">>},
  428. {"add; lhs non-numeric string, rhs non-numeric string",
  429. <<"{{ one|add:'bar'}}">>, [{one, "foo"}],
  430. <<"foobar">>},
  431. {"add; lhs numeric string, rhs numeric string",
  432. <<"{{ one|add:'4'}}">>, [{one, "1"}],
  433. <<"5">>},
  434. {"|addslashes",
  435. <<"{{ var1|addslashes }}">>, [{var1, "Jimmy's \"great\" meats'n'things"}],
  436. <<"Jimmy\\'s \\\"great\\\" meats\\'n\\'things">>},
  437. {"|capfirst",
  438. <<"{{ var1|capfirst }}">>, [{var1, "dana boyd"}],
  439. <<"Dana boyd">>},
  440. {"|center:10",
  441. <<"{{ var1|center:10 }}">>, [{var1, "MB"}],
  442. <<" MB ">>},
  443. {"|center:1",
  444. <<"{{ var1|center:1 }}">>, [{var1, "KBR"}],
  445. <<"B">>},
  446. {"|cut:\" \"",
  447. <<"{{ var1|cut:\" \" }}">>, [{var1, "String with spaces"}],
  448. <<"Stringwithspaces">>},
  449. {"|date 1",
  450. <<"{{ var1|date:\"jS F Y H:i\" }}">>,
  451. [{var1, {1975,7,24}}],
  452. <<"24th July 1975 00:00">>},
  453. {"|date 2",
  454. <<"{{ var1|date:\"jS F Y H:i\" }}">>,
  455. [{var1, {{1975,7,24}, {7,13,1}}}],
  456. <<"24th July 1975 07:13">>},
  457. {"|date 3",
  458. <<"{{ var1|date }}">>,
  459. [{var1, {{1975,7,24}, {7,13,1}}}],
  460. <<"July 24, 1975">>},
  461. {"|default:\"foo\" 1",
  462. <<"{{ var1|default:\"foo\" }}">>, [], <<"foo">>},
  463. {"|default:\"foo\" 2",
  464. <<"{{ var1|default:\"foo\" }}">>, [{var1, "bar"}], <<"bar">>},
  465. {"|default:\"foo\" 3",
  466. <<"{{ var1|default:\"foo\" }}">>, [{var1, "0"}], <<"foo">>},
  467. {"|default_if_none:\"foo\"",
  468. <<"{{ var1|default_if_none:\"foo\" }}">>, [], <<"foo">>},
  469. {"|default_if_none:\"foo\" 2",
  470. <<"{{ var1|default_if_none:\"foo\" }}">>, [{var1, "bar"}], <<"bar">>},
  471. {"|dictsort 1",
  472. <<"{{ var1|dictsort:\"foo\" }}">>,
  473. [{var1,[[{foo,2}],[{foo,1}]]}], <<"{foo,1}{foo,2}">>},
  474. {"|dictsort 2",
  475. <<"{{ var1|dictsort:\"foo.bar\" }}">>,
  476. [{var1,[[{foo,[{bar,2}]}],[{foo,[{bar,1}]}]]}],
  477. <<"{foo,[{bar,1}]}{foo,[{bar,2}]}">>},
  478. {"|divisibleby:\"3\"",
  479. <<"{% if var1|divisibleby:\"3\" %}yay{% endif %}">>, [{var1, 21}], <<"yay">>},
  480. {"|divisibleby:\"3\"",
  481. <<"{% if var1|divisibleby:\"3\" %}yay{% endif %}">>, [{var1, 22}], <<"">>},
  482. {"|escape",
  483. <<"{% autoescape on %}{{ var1|escape|escape|escape }}{% endautoescape %}">>, [{var1, ">&1"}], <<"&gt;&amp;1">>},
  484. {"|escapejs",
  485. <<"{{ var1|escapejs }}">>, [{var1, "testing\r\njavascript 'string\" <b>escaping</b>"}],
  486. <<"testing\\u000D\\u000Ajavascript \\u0027string\\u0022 \\u003Cb\\u003Eescaping\\u003C/b\\u003E">>},
  487. {"|filesizeformat (bytes)",
  488. <<"{{ var1|filesizeformat }}">>, [{var1, 1023}], <<"1023 bytes">>},
  489. {"|filesizeformat (KB)",
  490. <<"{{ var1|filesizeformat }}">>, [{var1, 3487}], <<"3.4 KB">>},
  491. {"|filesizeformat (MB)",
  492. <<"{{ var1|filesizeformat }}">>, [{var1, 6277098}], <<"6.0 MB">>},
  493. {"|filesizeformat (GB)",
  494. <<"{{ var1|filesizeformat }}">>, [{var1, 1024 * 1024 * 1024}], <<"1.0 GB">>},
  495. {"|first",
  496. <<"{{ var1|first }}">>, [{var1, "James"}],
  497. <<"J">>},
  498. {"|fix_ampersands",
  499. <<"{{ var1|fix_ampersands }}">>, [{var1, "Ben & Jerry's"}],
  500. <<"Ben &amp; Jerry's">>},
  501. {"|floatformat:\"-1\"",
  502. <<"{{ var1|floatformat:\"-1\" }}">>, [{var1, 34.23234}],
  503. <<"34.2">>},
  504. {"int |floatformat",
  505. <<"{{ var1|floatformat:\"-1\" }}">>, [{var1, 123}],
  506. <<"123">>},
  507. {"string |floatformat",
  508. <<"{{ var1|floatformat:\"-1\" }}">>, [{var1, "123.321"}],
  509. <<"123.3">>},
  510. {"binary |floatformat",
  511. <<"{{ var1|floatformat:\"-1\" }}">>, [{var1, <<"123.321">>}],
  512. <<"123.3">>},
  513. %% from: https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#floatformat
  514. {"1.a) |floatformat",
  515. <<"{{ var1|floatformat }}">>, [{var1, 34.23234}],
  516. <<"34.2">>},
  517. {"1.b) |floatformat",
  518. <<"{{ var1|floatformat }}">>, [{var1, 34.00000}],
  519. <<"34">>},
  520. {"1.c) |floatformat",
  521. <<"{{ var1|floatformat }}">>, [{var1, 34.26000}],
  522. <<"34.3">>},
  523. {"2.a) |floatformat:\"3\"",
  524. <<"{{ var1|floatformat:\"3\" }}">>, [{var1, 34.23234}],
  525. <<"34.232">>},
  526. {"2.b) |floatformat:\"3\"",
  527. <<"{{ var1|floatformat:\"3\" }}">>, [{var1, 34.00000}],
  528. <<"34.000">>},
  529. {"2.c) |floatformat:\"3\"",
  530. <<"{{ var1|floatformat:\"3\" }}">>, [{var1, 34.26000}],
  531. <<"34.260">>},
  532. {"3.a) |floatformat:\"0\"",
  533. <<"{{ var1|floatformat:\"0\" }}">>, [{var1, 34.23234}],
  534. <<"34">>},
  535. {"3.b) |floatformat:\"0\"",
  536. <<"{{ var1|floatformat:\"0\" }}">>, [{var1, 34.00000}],
  537. <<"34">>},
  538. {"3.c) |floatformat:\"0\"",
  539. <<"{{ var1|floatformat:\"0\" }}">>, [{var1, 39.56000}],
  540. <<"40">>},
  541. {"4.a) |floatformat:\"-3\"",
  542. <<"{{ var1|floatformat:\"-3\" }}">>, [{var1, 34.23234}],
  543. <<"34.232">>},
  544. {"4.b) |floatformat:\"-3\"",
  545. <<"{{ var1|floatformat:\"-3\" }}">>, [{var1, 34.00000}],
  546. <<"34">>},
  547. {"4.c) |floatformat:\"-3\"",
  548. <<"{{ var1|floatformat:\"-3\" }}">>, [{var1, 34.26000}],
  549. <<"34.260">>},
  550. {"|force_escape",
  551. <<"{{ var1|force_escape }}">>, [{var1, "Ben & Jerry's <=> \"The World's Best Ice Cream\""}],
  552. <<"Ben &amp; Jerry&#039;s &lt;=&gt; &quot;The World&#039;s Best Ice Cream&quot;">>},
  553. {"iolist |force_escape",
  554. <<"{{ var1|force_escape }}">>, [{var1, ["'a'"]}],
  555. <<"&#039;a&#039;">>},
  556. {"nested iolist |force_escape",
  557. <<"{{ var1|force_escape }}">>, [{var1, ["a'", <<"b">>, [<<"<c">>, "d", ["e>"]]]}],
  558. <<"a&#039;b&lt;cde&gt;">>},
  559. {"|format_integer",
  560. <<"{{ var1|format_integer }}">>, [{var1, 28}], <<"28">>},
  561. {"|format_number 1",
  562. <<"{{ var1|format_number }}">>, [{var1, 28}], <<"28">>},
  563. {"|format_number 2",
  564. <<"{{ var1|format_number }}">>, [{var1, 23.77}], <<"23.77">>},
  565. {"|format_number 3",
  566. <<"{{ var1|format_number }}">>, [{var1, "28.77"}], <<"28.77">>},
  567. {"|format_number 4",
  568. <<"{{ var1|format_number }}">>, [{var1, "23.77"}], <<"23.77">>},
  569. {"|format_number 5",
  570. <<"{{ var1|format_number }}">>, [{var1, fun() -> 29 end}], <<"29">>},
  571. {"|format_number 6",
  572. <<"{{ var1|format_number }}">>, [{var1, fun() -> fun() -> 31 end end}], <<"31">>},
  573. {"|get_digit:\"2\"",
  574. <<"{{ var1|get_digit:\"2\" }}">>, [{var1, 42}], <<"4">>},
  575. {"|iriencode",
  576. <<"{{ url|iriencode }}">>, [{url, "You #$*@!!"}], <<"You+#$*@!!">>},
  577. {"|join:\", \" (list)",
  578. <<"{{ var1|join:\", \" }}">>, [{var1, ["Liberte", "Egalite", "Fraternite"]}],
  579. <<"Liberte, Egalite, Fraternite">>},
  580. {"|join:\", \" (binary)",
  581. <<"{{ var1|join:\", \" }}">>, [{var1, [<<"Liberte">>, "Egalite", <<"Fraternite">>]}],
  582. <<"Liberte, Egalite, Fraternite">>},
  583. {"|last",
  584. <<"{{ var1|last }}">>, [{var1, "XYZ"}],
  585. <<"Z">>},
  586. {"|length",
  587. <<"{{ var1|length }}">>, [{var1, "antidisestablishmentarianism"}],
  588. <<"28">>},
  589. {"|linebreaks",
  590. <<"{{ var1|linebreaks }}">>, [{var1, "Joel\nis a slug"}],
  591. <<"<p>Joel<br />is a slug</p>">>},
  592. {"|linebreaks",
  593. <<"{{ var1|linebreaks }}">>, [{var1, "Joel\n\n\n\nis a slug"}],
  594. <<"<p>Joel</p><p>is a slug</p>">>},
  595. {"|linebreaks",
  596. <<"{{ var1|linebreaks }}">>, [{var1, "Joel\n\nis a \nslug"}],
  597. <<"<p>Joel</p><p>is a <br />slug</p>">>},
  598. {"|linebreaksbr",
  599. <<"{{ var1|linebreaksbr }}">>, [{var1, "One\nTwo\n\nThree\n\n\n"}],
  600. <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
  601. {"|linebreaksbr",
  602. <<"{{ \"One\\nTwo\\n\\nThree\\n\\n\\n\"|linebreaksbr }}">>, [],
  603. <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
  604. {"|linenumbers",
  605. <<"{{ var1|linenumbers }}">>, [{var1, "a\nb\nc"}],
  606. <<"1. a\n2. b\n3. c">>},
  607. {"|linenumbers",
  608. <<"{{ var1|linenumbers }}">>, [{var1, "a"}],
  609. <<"1. a">>},
  610. {"|linenumbers",
  611. <<"{{ var1|linenumbers }}">>, [{var1, "a\n"}],
  612. <<"1. a\n2. ">>},
  613. {"|ljust:10",
  614. <<"{{ var1|ljust:10 }}">>, [{var1, "Gore"}],
  615. <<"Gore ">>},
  616. {"|lower",
  617. <<"{{ var1|lower }}">>, [{var1, "E. E. Cummings"}],
  618. <<"e. e. cummings">>},
  619. {"|makelist",
  620. <<"{{ list|make_list }}">>, [{list, "Joel"}],
  621. <<"J","o","e","l">>},
  622. {"|pluralize",
  623. <<"{{ num|pluralize }}">>, [{num, 1}],
  624. <<"">>},
  625. {"|pluralize",
  626. <<"{{ num|pluralize }}">>, [{num, 2}],
  627. <<"s">>},
  628. {"|pluralize:\"s\"",
  629. <<"{{ num|pluralize }}">>, [{num, 1}],
  630. <<"">>},
  631. {"|pluralize:\"s\"",
  632. <<"{{ num|pluralize }}">>, [{num, 2}],
  633. <<"s">>},
  634. {"|pluralize:\"y,es\" (list)",
  635. <<"{{ num|pluralize:\"y,es\" }}">>, [{num, 1}],
  636. <<"y">>},
  637. {"|pluralize:\"y,es\" (list)",
  638. <<"{{ num|pluralize:\"y,es\" }}">>, [{num, 2}],
  639. <<"es">>},
  640. {"|random",
  641. <<"{{ var1|random }}">>, [{var1, ["foo", "foo", "foo"]}],
  642. <<"foo">>},
  643. {"|removetags:\"b span\"",
  644. <<"{{ var1|removetags:\"b span\" }}">>, [{var1, "<B>Joel</B> <button>is</button> a <span>slug</span>"}],
  645. <<"<B>Joel</B> <button>is</button> a slug">>},
  646. {"|rjust:10",
  647. <<"{{ var1|rjust:10 }}">>, [{var1, "Bush"}],
  648. <<" Bush">>},
  649. {"|safe",
  650. <<"{% autoescape on %}{{ var1|safe|escape }}{% endautoescape %}">>, [{var1, "&"}],
  651. <<"&">>},
  652. {"|safe is local",
  653. <<"{{ var1 }}{{ var1|safe }}{{ var1 }}">>, [{var1, "&"}], [], [auto_escape],
  654. <<"&amp;&&amp;">>},
  655. %%python/django slice is zero based, erlang lists are 1 based
  656. %%first number included, second number not
  657. %%negative numbers are allowed
  658. %%regex to convert from erlydtl_filters_tests:
  659. % for slice: \?assert.*\( \[(.*)\], erlydtl_filters:(.*)\((.*),"(.*)"\)\),
  660. % {"|slice:\"$4\"", <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],<<$1>>},
  661. % \t\t{"|slice:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>},
  662. %
  663. % for stringformat:
  664. % \?assert.*\( (.*), erlydtl_filters:(.*)\((.*), "(.*)"\) \)
  665. % \t\t{"|stringformat:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>}
  666. {"|slice:\":\"",
  667. <<"{{ var|slice:\":\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  668. <<1,2,3,4,5,6,7,8,9>>},
  669. {"|slice:\"1\"",
  670. <<"{{ var|slice:\"1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  671. <<"2">>},
  672. {"|slice:\"100\"",
  673. <<"{{ var|slice:\"100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  674. <<"indexError">>},
  675. {"|slice:\"-1\"",
  676. <<"{{ var|slice:\"-1\" }}">>, [{var, ["a","b","c","d","e","f","g","h","i"]}],
  677. <<"i">>},
  678. {"|slice:\"-1\"",
  679. <<"{{ var|slice:\"-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  680. <<"9">>},
  681. {"|slice:\"-100\"",
  682. <<"{{ var|slice:\"-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  683. <<"indexError">>},
  684. {"|slice:\"1:\"",
  685. <<"{{ var|slice:\"1:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  686. <<2,3,4,5,6,7,8,9>>},
  687. {"|slice:\"100:\"",
  688. <<"{{ var|slice:\"100:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  689. <<>>},
  690. {"|slice:\"-1:\"",
  691. <<"{{ var|slice:\"-1:\" }}">>, [{var, ["a","b","c","d","e","f","h","i","j"]}],
  692. <<"j">>},
  693. {"|slice:\"-1:\"",
  694. <<"{{ var|slice:\"-1:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  695. <<9>>},
  696. {"|slice:\"-100:\"",
  697. <<"{{ var|slice:\"-100:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  698. <<1,2,3,4,5,6,7,8,9>>},
  699. {"|slice:\":1\"",
  700. <<"{{ var|slice:\":1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  701. <<1>>},
  702. {"|slice:\":100\"",
  703. <<"{{ var|slice:\":100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  704. <<1,2,3,4,5,6,7,8,9>>},
  705. {"|slice:\":-1\"",
  706. <<"{{ var|slice:\":-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  707. <<1,2,3,4,5,6,7,8>>},
  708. {"|slice:\":-100\"",
  709. <<"{{ var|slice:\":-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  710. <<>>},
  711. {"|slice:\"-1:-1\"",
  712. <<"{{ var|slice:\"-1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  713. <<>>},
  714. {"|slice:\"1:1\"",
  715. <<"{{ var|slice:\"1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  716. <<>>},
  717. {"|slice:\"1:-1\"",
  718. <<"{{ var|slice:\"1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  719. <<2,3,4,5,6,7,8>>},
  720. {"|slice:\"-1:1\"",
  721. <<"{{ var|slice:\"-1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  722. <<>>},
  723. {"|slice:\"-100:-100\"",
  724. <<"{{ var|slice:\"-100:-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  725. <<>>},
  726. {"|slice:\"100:100\"",
  727. <<"{{ var|slice:\"100:100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  728. <<>>},
  729. {"|slice:\"100:-100\"",
  730. <<"{{ var|slice:\"100:-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  731. <<>>},
  732. {"|slice:\"-100:100\"",
  733. <<"{{ var|slice:\"-100:100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  734. <<1,2,3,4,5,6,7,8,9>>},
  735. {"|slice:\"1:3\"",
  736. <<"{{ var|slice:\"1:3\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  737. <<2,3>>},
  738. {"|slice:\"::\"",
  739. <<"{{ var|slice:\"::\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  740. <<1,2,3,4,5,6,7,8,9>>},
  741. {"|slice:\"1:9:1\"",
  742. <<"{{ var|slice:\"1:9:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  743. <<2,3,4,5,6,7,8,9>>},
  744. {"|slice:\"10:1:-1\"",
  745. <<"{{ var|slice:\"10:1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  746. <<9,8,7,6,5,4,3>>},
  747. {"|slice:\"-111:-1:1\"",
  748. <<"{{ var|slice:\"-111:-1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  749. <<1,2,3,4,5,6,7,8>>},
  750. {"|slice:\"-111:-111:1\"",
  751. <<"{{ var|slice:\"-111:-111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  752. <<>>},
  753. {"|slice:\"111:111:1\"",
  754. <<"{{ var|slice:\"111:111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  755. <<>>},
  756. {"|slice:\"-111:111:1\"",
  757. <<"{{ var|slice:\"-111:111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  758. <<1,2,3,4,5,6,7,8,9>>},
  759. {"|slice:\"111:-111:1\"",
  760. <<"{{ var|slice:\"111:-111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  761. <<>>},
  762. {"|slice:\"-111:-111:-1\"",
  763. <<"{{ var|slice:\"-111:-111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  764. <<>>},
  765. {"|slice:\"111:111:-1\"",
  766. <<"{{ var|slice:\"111:111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  767. <<>>},
  768. {"|slice:\"-111:111:-1\"",
  769. <<"{{ var|slice:\"-111:111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  770. <<>>},
  771. {"|slice:\"111:-111:-1\"",
  772. <<"{{ var|slice:\"111:-111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  773. <<9,8,7,6,5,4,3,2,1>>}, {"|phone2numeric",
  774. <<"{{ var1|phone2numeric }}">>, [{var1, "1-800-COLLECT"}],
  775. <<"1-800-2655328">>},
  776. {"|slugify",
  777. <<"{{ var1|slugify }}">>, [{var1, "What The $#_! Was He Thinking?"}],
  778. <<"what-the-_-was-he-thinking">>},
  779. {"|slice:\"s\"",
  780. <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
  781. <<"test">>},
  782. {"|stringformat:\"s\"",
  783. <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
  784. <<"test">>},
  785. {"|stringformat:\"s\"",
  786. <<"{{ var|stringformat:\"s\" }}">>, [{var, "1"}],
  787. <<"1">>},
  788. {"|stringformat:\"s\"",
  789. <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
  790. <<"test">>},
  791. {"|stringformat:\"10s\"",
  792. <<"{{ var|stringformat:\"10s\" }}">>, [{var, "test"}],
  793. <<" test">>},
  794. {"|stringformat:\"-10s\"",
  795. <<"{{ var|stringformat:\"-10s\" }}">>, [{var, "test"}],
  796. <<"test ">>},
  797. {"|stringformat:\"d\"",
  798. <<"{{ var|stringformat:\"d\" }}">>, [{var, "90"}],
  799. <<"90">>},
  800. {"|stringformat:\"10d\"",
  801. <<"{{ var|stringformat:\"10d\" }}">>, [{var, "90"}],
  802. <<" 90">>},
  803. {"|stringformat:\"-10d\"",
  804. <<"{{ var|stringformat:\"-10d\" }}">>, [{var, "90"}],
  805. <<"90 ">>},
  806. {"|stringformat:\"i\"",
  807. <<"{{ var|stringformat:\"i\" }}">>, [{var, "90"}],
  808. <<"90">>},
  809. {"|stringformat:\"10i\"",
  810. <<"{{ var|stringformat:\"10i\" }}">>, [{var, "90"}],
  811. <<" 90">>},
  812. {"|stringformat:\"-10i\"",
  813. <<"{{ var|stringformat:\"-10i\" }}">>, [{var, "90"}],
  814. <<"90 ">>},
  815. {"|stringformat:\"0.2d\"",
  816. <<"{{ var|stringformat:\"0.2d\" }}">>, [{var, "9"}],
  817. <<"09">>},
  818. {"|stringformat:\"10.4d\"",
  819. <<"{{ var|stringformat:\"10.4d\" }}">>, [{var, "9"}],
  820. <<" 0009">>},
  821. {"|stringformat:\"-10.4d\"",
  822. <<"{{ var|stringformat:\"-10.4d\" }}">>, [{var, "9"}],
  823. <<"0009 ">>},
  824. {"|stringformat:\"f\"",
  825. <<"{{ var|stringformat:\"f\" }}">>, [{var, "1"}],
  826. <<"1.000000">>},
  827. {"|stringformat:\".2f\"",
  828. <<"{{ var|stringformat:\".2f\" }}">>, [{var, "1"}],
  829. <<"1.00">>},
  830. {"|stringformat:\"0.2f\"",
  831. <<"{{ var|stringformat:\"0.2f\" }}">>, [{var, "1"}],
  832. <<"1.00">>},
  833. {"|stringformat:\"-0.2f\"",
  834. <<"{{ var|stringformat:\"-0.2f\" }}">>, [{var, "1"}],
  835. <<"1.00">>},
  836. {"|stringformat:\"10.2f\"",
  837. <<"{{ var|stringformat:\"10.2f\" }}">>, [{var, "1"}],
  838. <<" 1.00">>},
  839. {"|stringformat:\"-10.2f\"",
  840. <<"{{ var|stringformat:\"-10.2f\" }}">>, [{var, "1"}],
  841. <<"1.00 ">>},
  842. {"|stringformat:\".2f\"",
  843. <<"{{ var|stringformat:\".2f\" }}">>, [{var, "1"}],
  844. <<"1.00">>},
  845. {"|stringformat:\"x\"",
  846. <<"{{ var|stringformat:\"x\" }}">>, [{var, "90"}],
  847. <<"5a">>},
  848. {"|stringformat:\"X\"",
  849. <<"{{ var|stringformat:\"X\" }}">>, [{var, "90"}],
  850. <<"5A">>},
  851. {"|stringformat:\"o\"",
  852. <<"{{ var|stringformat:\"o\" }}">>, [{var, "90"}],
  853. <<"132">>},
  854. {"|stringformat:\"e\"",
  855. <<"{{ var|stringformat:\"e\" }}">>, [{var, "90"}],
  856. <<"9.000000e+01">>},
  857. {"|stringformat:\"e\"",
  858. <<"{{ var|stringformat:\"e\" }}">>, [{var, "90000000000"}],
  859. <<"9.000000e+10">>},
  860. {"|stringformat:\"E\"",
  861. <<"{{ var|stringformat:\"E\" }}">>, [{var, "90"}],
  862. <<"9.000000E+01">>},
  863. {"|striptags",
  864. <<"{{ var|striptags }}">>, [{var, "<b>Joel</b> <button>is</button> a <span>slug</span>"}],
  865. <<"Joel is a slug">>},
  866. {"|striptags",
  867. <<"{{ var|striptags }}">>, [{var, "<B>Joel</B> <button>is</button> a <span>slug</Span>"}],
  868. <<"Joel is a slug">>},
  869. {"|striptags",
  870. <<"{{ var|striptags }}">>, [{var, "Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>"}],
  871. <<"Check out http://www.djangoproject.com">>},
  872. {"|time:\"H:i\"",
  873. <<"{{ var|time:\"H:i\" }}">>, [{var, {{2010,12,1}, {10,11,12}} }],
  874. <<"10:11">>},
  875. {"|time",
  876. <<"{{ var|time }}">>, [{var, {{2010,12,1}, {10,11,12}} }],
  877. <<"10:11 a.m.">>},
  878. {"|timesince:from_date",
  879. <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2006,6,1},{8,0,0}} }, {from_date, {{2006,6,1},{0,0,0}} }],
  880. <<"8 hours">>},
  881. {"|timesince:from_date",
  882. <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2010,6,1},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  883. <<"4 years, 1 day">>}, % leap year
  884. {"|timesince:from_date",
  885. <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2006,7,15},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  886. <<"1 month, 2 weeks">>},
  887. {"|timeuntil:from_date",
  888. <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2006,6,1},{8,0,0}} }, {from_date, {{2006,6,1},{0,0,0}} }],
  889. <<"8 hours">>},
  890. {"|timeuntil:from_date",
  891. <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2010,6,1},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  892. <<"4 years, 1 day">>},
  893. {"|timeuntil:from_date",
  894. <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2006,7,15},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  895. <<"1 month, 2 weeks">>},
  896. {"|title",
  897. <<"{{ \"my title case\"|title }}">>, [],
  898. <<"My Title Case">>},
  899. {"|title (pre-formatted)",
  900. <<"{{ \"My Title Case\"|title }}">>, [],
  901. <<"My Title Case">>},
  902. {"|title (wacky separators)",
  903. <<"{{ \"my-title!case\"|title }}">>, [],
  904. <<"My-Title!Case">>},
  905. {"|title (numbers)",
  906. <<"{{ \"my-title123CaSe\"|title }}">>, [],
  907. <<"My-Title123case">>},
  908. {"|title (Irish names)",
  909. <<"{{ \"who's o'malley?\"|title }}">>, [],
  910. <<"Who's O'Malley?">>},
  911. {"|truncatechars:0",
  912. <<"{{ var1|truncatechars:0 }}">>, [{var1, "Empty Me"}],
  913. <<"...">>},
  914. {"|truncatechars:14",
  915. <<"{{ var1|truncatechars:14 }}">>, [{var1, "Truncate Me Please"}],
  916. <<"Truncate Me...">>},
  917. {"|truncatechars:17",
  918. <<"{{ var1|truncatechars:17 }}">>, [{var1, "Don't Truncate Me"}],
  919. <<"Don't Truncate Me">>},
  920. {"|truncatechars:4 (UTF-8)",
  921. <<"{{ var1|truncatechars:4 }}">>, [{var1, "\x{E2}\x{82}\x{AC}1.99"}],
  922. <<"\x{E2}\x{82}\x{AC}...">>},
  923. {"|truncatechars:5 (UTF-8)",
  924. <<"{{ var1|truncatechars:5 }}">>, [{var1, "\x{E2}\x{82}\x{AC} 1.99"}],
  925. <<"\x{E2}\x{82}\x{AC} ...">>},
  926. {"|truncatewords:0",
  927. <<"{{ var1|truncatewords:0 }}">>, [{var1, "Empty Me"}],
  928. <<" ...">>},
  929. {"|truncatewords:2",
  930. <<"{{ var1|truncatewords:2 }}">>, [{var1, "Truncate Me Please"}],
  931. <<"Truncate Me ...">>},
  932. {"|truncatewords:3",
  933. <<"{{ var1|truncatewords:3 }}">>, [{var1, "Don't Truncate Me"}],
  934. <<"Don't Truncate Me">>},
  935. {"|truncatewords_html:4",
  936. <<"{{ var1|truncatewords_html:4 }}">>, [{var1, "<p>The <strong>Long and <em>Winding</em> Road</strong> is too long</p>"}],
  937. <<"<p>The <strong>Long and <em>Winding</em>...</strong></p>">>},
  938. {"|unordered_list",
  939. <<"{{ var1|unordered_list }}">>, [{var1, ["States", ["Kansas", ["Lawrence", "Topeka"], "Illinois"]]}],
  940. <<"<li>States<ul><li>Kansas<ul><li>Lawrence</li><li>Topeka</li></ul></li><li>Illinois</li></ul></li>">>},
  941. {"|upper",
  942. <<"{{ message|upper }}">>, [{message, "That man has a gun."}],
  943. <<"THAT MAN HAS A GUN.">>},
  944. {"|urlencode",
  945. <<"{{ url|urlencode }}">>, [{url, "You #$*@!!"}],
  946. <<"You%20%23%24%2A%40%21%21">>},
  947. {"|urlencode",
  948. <<"{{ url|urlencode }}">>, [{url, "http://www.example.org/foo?a=b&c=d"}],
  949. <<"http%3A//www.example.org/foo%3Fa%3Db%26c%3Dd">>},
  950. {"|urlencode",
  951. <<"{{ url|urlencode:\"\" }}">>, [{url, "http://www.example.org/foo?a=b&c=d"}],
  952. <<"http%3A%2F%2Fwww.example.org%2Ffoo%3Fa%3Db%26c%3Dd">>},
  953. {"|urlencode",
  954. <<"{{ url|urlencode:\":/?=&\" }}">>, [{url, "http://www.example.org/foo?a=b&c=d"}],
  955. <<"http://www.example.org/foo?a=b&c=d">>},
  956. {"|urlize",
  957. <<"{{ var|urlize }}">>, [{var, "Check out www.djangoproject.com"}],
  958. <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">www.djangoproject.com</a>">>},
  959. {"|urlize",
  960. <<"{{ var|urlize }}">>, [{var, "Check out http://www.djangoproject.com"}],
  961. <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>">>},
  962. {"|urlize",
  963. <<"{{ var|urlize }}">>, [{var, "Check out \"http://www.djangoproject.com\""}],
  964. <<"Check out \"<a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>\"">>},
  965. {"|urlizetrunc:15",
  966. <<"{{ var|urlizetrunc:15 }}">>, [{var, "Check out www.djangoproject.com"}],
  967. <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">www.djangopr...</a>">>},
  968. {"|wordcount",
  969. <<"{{ words|wordcount }}">>, [{words, "Why Hello There!"}],
  970. <<"3">>},
  971. {"|wordwrap:2",
  972. <<"{{ words|wordwrap:2 }}">>, [{words, "this is"}],
  973. <<"this \nis">>},
  974. {"|wordwrap:100",
  975. <<"{{ words|wordwrap:100 }}">>, [{words, "testing testing"}],
  976. <<"testing testing">>},
  977. {"|wordwrap:10",
  978. <<"{{ words|wordwrap:10 }}">>, [{words, ""}],
  979. <<"">>},
  980. {"|wordwrap:1",
  981. <<"{{ words|wordwrap:1 }}">>, [{words, "two"}],
  982. <<"two">>},
  983. %% yesno match: \?assert.*\( (.*), erlydtl_filters:(.*)\((.*), "(.*)"\)\)
  984. %% yesno replace: \t\t{"|$2:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>}
  985. {"|yesno:\"yeah,no,maybe\"",
  986. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, true}],
  987. <<"yeah">>},
  988. {"|yesno:\"yeah,no,maybe\"",
  989. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, false}],
  990. <<"no">>},
  991. {"|yesno:\"yeah,no\"",
  992. <<"{{ var|yesno:\"yeah,no\" }}">>, [{var, undefined}],
  993. <<"no">>},
  994. {"|yesno:\"yeah,no,maybe\"",
  995. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, undefined}],
  996. <<"maybe">>},
  997. {"string |yesno:\"yeah,no,maybe\"",
  998. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, "non-empty string"}],
  999. <<"yeah">>},
  1000. {"binary |yesno:\"yeah,no,maybe\"",
  1001. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, <<"non-empty binary">>}],
  1002. <<"yeah">>},
  1003. {"empty string |yesno:\"yeah,no,maybe\"",
  1004. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, ""}],
  1005. <<"no">>},
  1006. {"empty binary |yesno:\"yeah,no\"",
  1007. <<"{{ var|yesno:\",no\" }}">>, [{var, <<"">>}],
  1008. <<"no">>},
  1009. {"term |yesno:\"yeah,,maybe\"",
  1010. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, {my, [term, "test"]}}],
  1011. <<"yeah">>},
  1012. {"|yesno:\"yeah,\"",
  1013. <<"{{ var|yesno:\"yeah,\" }}">>, [{var, false}],
  1014. <<"">>},
  1015. {"|yesno:\"yeah,,maybe\"",
  1016. <<"{{ var|yesno:\"yeah,,maybe\" }}">>, [{var, false}],
  1017. <<"">>},
  1018. #test{
  1019. title = "|yesno:\"missing_false_choice\"",
  1020. source = <<"{{ var|yesno:\"missing_false_choice\" }}">>,
  1021. render_vars = [{var, true}],
  1022. output = {error, {yesno, choices}}
  1023. },
  1024. {"escape only once (#150) - no auto escape",
  1025. %% note that auto_escape is off by default in the test suite
  1026. %% due to how the tests have been written (and it's too much
  1027. %% work for me to rewrite them)
  1028. <<"{{ foo }}{{ foo|add:'bar' }}">>,
  1029. [{foo, "foo&"}],
  1030. <<"foo&foo&bar">>},
  1031. {"escape only once (#150) - auto escape block",
  1032. <<"{% autoescape on %}{{ foo }}{{ foo|add:'bar' }}{% endautoescape %}">>,
  1033. [{foo, "foo&"}],
  1034. <<"foo&amp;foo&amp;bar">>},
  1035. {"escape only once (#150) - auto escape",
  1036. <<"{{ foo }}{{ foo|add:'bar' }}">>,
  1037. [{foo, "foo&"}], [], [auto_escape],
  1038. <<"foo&amp;foo&amp;bar">>},
  1039. {"escape only once (#150) - auto escape, safe",
  1040. <<"{{ foo|safe }}{{ foo|add:'bar'|safe }}&{{ foo|safe|add:'bar' }}">>,
  1041. [{foo, "foo&"}], [], [auto_escape],
  1042. <<"foo&foo&bar&foo&bar">>},
  1043. {"escape only once (#150) - escape filter",
  1044. <<"{{ foo|escape }}{{ foo|add:'bar'|escape }}&{{ foo|escape|add:'bar' }}">>,
  1045. [{foo, "foo&"}],
  1046. <<"foo&amp;foo&amp;bar&foo&amp;bar">>},
  1047. {"escape only once (#150) - auto escape + escape filter",
  1048. <<"{{ foo|escape }}{{ foo|add:'bar'|escape }}&{{ foo|escape|add:'bar' }}">>,
  1049. [{foo, "foo&"}], [], [auto_escape],
  1050. <<"foo&amp;foo&amp;bar&foo&amp;bar">>}
  1051. ]},
  1052. {"filters_if",
  1053. [{"Filter if 1.1",
  1054. <<"{% if var1|length_is:0 %}Y{% else %}N{% endif %}">>,
  1055. [{var1, []}],
  1056. <<"Y">>},
  1057. {"Filter if 1.2",
  1058. <<"{% if var1|length_is:1 %}Y{% else %}N{% endif %}">>,
  1059. [{var1, []}],
  1060. <<"N">>},
  1061. {"Filter if 1.3",
  1062. <<"{% if var1|length_is:7 %}Y{% else %}N{% endif %}">>,
  1063. [{var1, []}],
  1064. <<"N">>},
  1065. {"Filter if 2.1",
  1066. <<"{% if var1|length_is:0 %}Y{% else %}N{% endif %}">>,
  1067. [{var1, ["foo"]}],
  1068. <<"N">>},
  1069. {"Filter if 2.2",
  1070. <<"{% if var1|length_is:1 %}Y{% else %}N{% endif %}">>,
  1071. [{var1, ["foo"]}],
  1072. <<"Y">>},
  1073. {"Filter if 2.3",
  1074. <<"{% if var1|length_is:7 %}Y{% else %}N{% endif %}">>,
  1075. [{var1, ["foo"]}],
  1076. <<"N">>},
  1077. {"Filter if 3.1",
  1078. <<"{% ifequal var1|length 0 %}Y{% else %}N{% endifequal %}">>,
  1079. [{var1, []}],
  1080. <<"Y">>},
  1081. {"Filter if 3.2",
  1082. <<"{% ifequal var1|length 1 %}Y{% else %}N{% endifequal %}">>,
  1083. [{var1, []}],
  1084. <<"N">>},
  1085. {"Filter if 4.1",
  1086. <<"{% ifequal var1|length 3 %}Y{% else %}N{% endifequal %}">>,
  1087. [{var1, ["foo", "bar", "baz"]}],
  1088. <<"Y">>},
  1089. {"Filter if 4.2",
  1090. <<"{% ifequal var1|length 0 %}Y{% else %}N{% endifequal %}">>,
  1091. [{var1, ["foo", "bar", "baz"]}],
  1092. <<"N">>},
  1093. {"Filter if 4.3",
  1094. <<"{% ifequal var1|length 1 %}Y{% else %}N{% endifequal %}">>,
  1095. [{var1, ["foo", "bar", "baz"]}],
  1096. <<"N">>}
  1097. ]},
  1098. {"firstof",
  1099. [{"Firstof first",
  1100. <<"{% firstof foo bar baz %}">>,
  1101. [{foo, "1"},{bar, "2"}],
  1102. <<"1">>},
  1103. {"Firstof second",
  1104. <<"{% firstof foo bar baz %}">>,
  1105. [{bar, "2"}],
  1106. <<"2">>},
  1107. {"Firstof none",
  1108. <<"{% firstof foo bar baz %}">>,
  1109. [],
  1110. <<"">>},
  1111. {"Firstof complex",
  1112. <<"{% firstof foo.bar.baz bar %}">>,
  1113. [{foo, [{bar, [{baz, "quux"}]}]}],
  1114. <<"quux">>},
  1115. {"Firstof undefined complex",
  1116. <<"{% firstof foo.bar.baz bar %}">>,
  1117. [{bar, "bar"}],
  1118. <<"bar">>},
  1119. {"Firstof literal",
  1120. <<"{% firstof foo bar \"baz\" %}">>,
  1121. [],
  1122. <<"baz">>}
  1123. ]},
  1124. {"regroup .. endregroup",
  1125. [{"Ordered",
  1126. <<"{% 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 %}">>,
  1127. [{people, [[{first_name, "George"}, {gender, "Male"}], [{first_name, "Bill"}, {gender, "Male"}],
  1128. [{first_name, "Margaret"}, {gender, "Female"}], [{first_name, "Condi"}, {gender, "Female"}]]}],
  1129. <<"Male\nGeorge\nBill\nFemale\nMargaret\nCondi\n">>},
  1130. {"Unordered",
  1131. <<"{% 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 %}">>,
  1132. [{people, [[{first_name, "George"}, {gender, "Male"}],
  1133. [{first_name, "Margaret"}, {gender, "Female"}],
  1134. [{first_name, "Condi"}, {gender, "Female"}],
  1135. [{first_name, "Bill"}, {gender, "Male"}]
  1136. ]}],
  1137. <<"Male\nGeorge\nFemale\nMargaret\nCondi\nMale\nBill\n">>},
  1138. {"NestedOrdered",
  1139. <<"{% 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 %}">>,
  1140. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1141. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1142. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1143. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1144. <<"Costanza\nGeorge\nMargaret\nBuffalo\nBill\nCondi\n">>},
  1145. {"NestedUnordered",
  1146. <<"{% 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 %}">>,
  1147. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1148. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1149. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1150. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1151. <<"Costanza\nGeorge\nBuffalo\nBill\nCostanza\nMargaret\nBuffalo\nCondi\n">>},
  1152. {"Filter",
  1153. <<"{% 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 %}">>,
  1154. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1155. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1156. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1157. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1158. <<"Buffalo\nBill\nCondi\nCostanza\nGeorge\nMargaret\n">>}
  1159. ]},
  1160. {"regroup",
  1161. [{"Ordered",
  1162. <<"{% 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 %}">>,
  1163. [{people, [[{first_name, "George"}, {gender, "Male"}], [{first_name, "Bill"}, {gender, "Male"}],
  1164. [{first_name, "Margaret"}, {gender, "Female"}], [{first_name, "Condi"}, {gender, "Female"}]]}],
  1165. <<"Male\nGeorge\nBill\nFemale\nMargaret\nCondi\n">>},
  1166. {"Unordered",
  1167. <<"{% 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 %}">>,
  1168. [{people, [[{first_name, "George"}, {gender, "Male"}],
  1169. [{first_name, "Margaret"}, {gender, "Female"}],
  1170. [{first_name, "Condi"}, {gender, "Female"}],
  1171. [{first_name, "Bill"}, {gender, "Male"}]
  1172. ]}],
  1173. <<"Male\nGeorge\nFemale\nMargaret\nCondi\nMale\nBill\n">>},
  1174. {"NestedOrdered",
  1175. <<"{% 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 %}">>,
  1176. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1177. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1178. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1179. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1180. <<"Costanza\nGeorge\nMargaret\nBuffalo\nBill\nCondi\n">>},
  1181. {"NestedUnordered",
  1182. <<"{% 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 %}">>,
  1183. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1184. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1185. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1186. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1187. <<"Costanza\nGeorge\nBuffalo\nBill\nCostanza\nMargaret\nBuffalo\nCondi\n">>},
  1188. {"Filter",
  1189. <<"{% 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 %}">>,
  1190. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1191. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1192. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1193. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1194. <<"Buffalo\nBill\nCondi\nCostanza\nGeorge\nMargaret\n">>},
  1195. {"With surrounding context",
  1196. <<"People: {% 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 %}Done.">>,
  1197. [{people, [[{first_name, "George"}, {gender, "Male"}], [{first_name, "Bill"}, {gender, "Male"}],
  1198. [{first_name, "Margaret"}, {gender, "Female"}], [{first_name, "Condi"}, {gender, "Female"}]]}],
  1199. <<"People: Male\nGeorge\nBill\nFemale\nMargaret\nCondi\nDone.">>}
  1200. ]},
  1201. {"spaceless",
  1202. [{"Beginning", <<"{% spaceless %} <b>foo</b>{% endspaceless %}">>, [], <<"<b>foo</b>">>},
  1203. {"Middle", <<"{% spaceless %}<b>foo</b> <b>bar</b>{% endspaceless %}">>, [], <<"<b>foo</b><b>bar</b>">>},
  1204. {"End", <<"{% spaceless %}<b>foo</b> {% endspaceless %}">>, [], <<"<b>foo</b>">>},
  1205. {"NewLine", <<"{% spaceless %}\n<div> \n <b>foo</b> \n </div>\n {% endspaceless %}">>, [], <<"<div><b>foo</b></div>">>}
  1206. ]},
  1207. {"templatetag",
  1208. [{"openblock", <<"{% templatetag openblock %}">>, [], <<"{%">>},
  1209. {"closeblock", <<"{% templatetag closeblock %}">>, [], <<"%}">>},
  1210. {"openvariable", <<"{% templatetag openvariable %}">>, [], <<"{{">>},
  1211. {"closevariable", <<"{% templatetag closevariable %}">>, [], <<"}}">>},
  1212. {"openbrace", <<"{% templatetag openbrace %}">>, [], <<"{">>},
  1213. {"closebrace", <<"{% templatetag closebrace %}">>, [], <<"}">>},
  1214. {"opencomment", <<"{% templatetag opencomment %}">>, [], <<"{#">>},
  1215. {"closecomment", <<"{% templatetag closecomment %}">>, [], <<"#}">>}
  1216. ]},
  1217. {"trans",
  1218. [{"trans functional default locale",
  1219. <<"Hello {% trans \"Hi\" %}">>, [], <<"Hello Hi">>
  1220. },
  1221. {"trans functional reverse locale",
  1222. <<"Hello {% trans \"Hi\" %}">>, [], [{locale, "reverse"}],
  1223. [{blocktrans_locales, ["reverse"]}, {blocktrans_fun, fun("Hi"=Key, "reverse") -> list_to_binary(lists:reverse(Key)) end}],
  1224. <<"Hello iH">>
  1225. },
  1226. {"trans literal at run-time",
  1227. <<"Hello {% trans \"Hi\" %}">>, [], [{translation_fun, fun("Hi") -> "Konichiwa" end}], [],
  1228. <<"Hello Konichiwa">>},
  1229. {"trans variable at run-time",
  1230. <<"Hello {% trans var1 %}">>, [{var1, <<"Hi">>}], [{translation_fun, fun(<<"Hi">>) -> <<"Konichiwa">> end}], [],
  1231. <<"Hello Konichiwa">>},
  1232. {"trans literal at run-time: No-op",
  1233. <<"Hello {% trans \"Hi\" noop %}">>, [], [{translation_fun, fun("Hi") -> <<"Konichiwa">> end}], [],
  1234. <<"Hello Hi">>},
  1235. {"trans variable at run-time: No-op",
  1236. <<"Hello {% trans var1 noop %}">>, [{var1, <<"Hi">>}], [{translation_fun, fun(<<"Hi">>) -> <<"Konichiwa">> end}], [],
  1237. <<"Hello Hi">>},
  1238. {"trans as",
  1239. <<"{% trans 'Hans' as name %}Hello {{ name }}">>, [], <<"Hello Hans">>
  1240. }
  1241. ]},
  1242. {"blocktrans",
  1243. [{"blocktrans default locale",
  1244. <<"{% blocktrans %}Hello{% endblocktrans %}">>, [], <<"Hello">>},
  1245. {"blocktrans choose locale",
  1246. <<"{% blocktrans %}Hello, {{ name }}{% endblocktrans %}">>, [{name, "Mr. President"}], [{locale, "de"}],
  1247. [{blocktrans_locales, ["de"]}, {blocktrans_fun, fun("Hello, {{ name }}", "de") -> <<"Guten tag, {{ name }}">> end}], <<"Guten tag, Mr. President">>},
  1248. {"blocktrans with args",
  1249. <<"{% blocktrans with var1=foo %}{{ var1 }}{% endblocktrans %}">>, [{foo, "Hello"}], <<"Hello">>},
  1250. #test{
  1251. title = "blocktrans blocks in content not allowed",
  1252. source = <<"{% blocktrans %}Hello{%if name%}, {{ name }}{%endif%}!{% endblocktrans %}">>,
  1253. errors = [error_info([{{1, 24}, erlydtl_parser, ["syntax error before: ",["\"if\""]]}])]
  1254. },
  1255. #test{
  1256. title = "blocktrans nested variables not allowed",
  1257. source = <<"{% blocktrans %}Hello, {{ user.name }}!{% endblocktrans %}">>,
  1258. errors = [error_info([{{1,31}, erlydtl_parser, ["syntax error before: ","'.'"]}])]
  1259. },
  1260. {"blocktrans runtime",
  1261. <<"{% blocktrans with v1=foo%}Hello, {{ name }}! See {{v1}}.{%endblocktrans%}">>,
  1262. [{name, "Mr. President"}, {foo, <<"rubber-duck">>}],
  1263. [{translation_fun, fun("Hello, {{ name }}! See {{ v1 }}.") -> <<"Guten tag, {{name}}! Sehen {{ v1 }}.">> end}],
  1264. [], <<"Guten tag, Mr. President! Sehen rubber-duck.">>}
  1265. ]},
  1266. {"verbatim",
  1267. [{"Plain verbatim",
  1268. <<"{% verbatim %}{{ oh no{% foobar %}{% endverbatim %}">>, [],
  1269. <<"{{ oh no{% foobar %}">>},
  1270. {"Named verbatim",
  1271. <<"{% verbatim foobar %}{% verbatim %}{% endverbatim foobar2 %}{% endverbatim foobar %}">>, [],
  1272. <<"{% verbatim %}{% endverbatim foobar2 %}">>}
  1273. ]},
  1274. {"widthratio",
  1275. [{"Literals", <<"{% widthratio 5 10 100 %}">>, [], <<"50">>},
  1276. {"Rounds up", <<"{% widthratio a b 100 %}">>, [{a, 175}, {b, 200}], <<"88">>}
  1277. ]},
  1278. {"with",
  1279. [{"Cache literal",
  1280. <<"{% with a=1 %}{{ a }}{% endwith %}">>, [], <<"1">>},
  1281. {"Cache variable",
  1282. <<"{% with a=b %}{{ a }}{% endwith %}">>, [{b, "foo"}], <<"foo">>},
  1283. {"Cache variable with attribute",
  1284. <<"I enjoy {% with a = var1 %}{{ a.game }}{% endwith %}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
  1285. {"Cache variable attribute",
  1286. <<"I enjoy {% with a = var1.game %}{{ a }}{% endwith %}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
  1287. {"Cache multiple",
  1288. <<"{% with alpha=1 beta=b %}{{ alpha }}/{{ beta }}{% endwith %}">>, [{b, 2}], <<"1/2">>}
  1289. ]},
  1290. {"unicode",
  1291. [{"(tm) somewhere",
  1292. <<"™">>, [], <<"™">>}
  1293. ]},
  1294. {"contrib_humanize",
  1295. [{"intcomma",
  1296. <<"{{ a|intcomma }} {{ b|intcomma }} {{ c|intcomma }} {{ d|intcomma }}">>,
  1297. [{a, 999}, {b, 123456789}, {c, 12345}, {d, 1234567890}], [],
  1298. [{custom_filters_modules, [erlydtl_contrib_humanize]}],
  1299. <<"999 123,456,789 12,345 1,234,567,890">>}
  1300. ]},
  1301. %% custom syntax stuff
  1302. {"extension_module",
  1303. [ %% the erlydtl_test_extension module replaces a foo identifier with bar when hitting a # following foo.
  1304. {"replace parsed token", <<"{{ foo # }}">>, [{bar, "ok"}], [],
  1305. [{extension_module, erlydtl_test_extension}], <<"ok">>},
  1306. #test{
  1307. title = "proper error message",
  1308. source = <<"{{ bar # }}">>,
  1309. render_vars = [{bar, "ok"}],
  1310. compile_opts = [{extension_module, erlydtl_test_extension},
  1311. report, return, force_recompile, {out_dir, false}],
  1312. errors = [error_info([{{1,8},erlydtl_scanner,{illegal_char, $#}}])]
  1313. },
  1314. %% accept identifiers as expressions (this is a dummy functionality to test the parser extensibility)
  1315. {"identifiers as expressions", <<"{{ foo.bar or baz }}">>, [{baz, "ok"}], [],
  1316. [{extension_module, erlydtl_test_extension}], <<"ok">>}
  1317. ]},
  1318. {"records",
  1319. [{"field access",
  1320. <<"{{ r.baz }}">>, [{r, #testrec{ foo="Foo", bar="Bar", baz="Baz" }}], [],
  1321. [{record_info, [{testrec, record_info(fields, testrec)}]}],
  1322. <<"Baz">>}
  1323. ]},
  1324. {"error reporting",
  1325. [#test{
  1326. title = "no out dir warning",
  1327. source = <<"foo bar">>,
  1328. compile_opts = [report, return, force_recompile],
  1329. output = <<"foo bar">>,
  1330. warnings = [error_info([no_out_dir])]
  1331. },
  1332. #test{
  1333. title = "warnings as errors",
  1334. source = <<"foo bar">>,
  1335. compile_opts = [report, return, warnings_as_errors, force_recompile],
  1336. errors = [error_info([no_out_dir])]
  1337. },
  1338. #test{
  1339. title = "illegal character",
  1340. source = <<"{{{">>,
  1341. errors = [error_info([{{1,3},erlydtl_scanner,{illegal_char, ${}}])]
  1342. },
  1343. #test{
  1344. title = "unexpected end of file - in code",
  1345. source = <<"{{">>,
  1346. errors = [error_info([{{1,3},erlydtl_scanner,{eof, in_code}}])]
  1347. },
  1348. #test{
  1349. title = "unexpected end of file - in comment",
  1350. source = <<"{#">>,
  1351. errors = [error_info([{{1,3},erlydtl_scanner,{eof, in_comment}}])]
  1352. },
  1353. {"unknown library",
  1354. <<"{% load foo %}">>, [], [], [],
  1355. <<>>,
  1356. [error_info(
  1357. [{{1,9},erlydtl_compiler_utils,{load_library,foo,foo,nofile}}
  1358. ])]
  1359. },
  1360. {"not a library",
  1361. <<"{% load foo %}">>, [], [],
  1362. [{libraries, [{foo, ?MODULE}]}],
  1363. <<>>,
  1364. [error_info(
  1365. [{{1,9},erlydtl_compiler_utils,{load_library,foo,?MODULE,behaviour}}
  1366. ])]
  1367. },
  1368. {"library version",
  1369. <<"{% load foo %}">>, [], [],
  1370. [{libraries, [{foo, erlydtl_lib_testversion}]}],
  1371. <<>>,
  1372. [error_info(
  1373. [{{1,9},erlydtl_compiler_utils,{load_library,foo,erlydtl_lib_testversion,{version,invalid}}}
  1374. ])]
  1375. },
  1376. {"not in library",
  1377. <<"{% load foo bar from test1 %}\n{{ \"w00t\"|reverse }}">>, [], [],
  1378. [{libraries, [{test1, erlydtl_lib_test1}]}],
  1379. <<"\n">>,
  1380. [error_info(
  1381. [{{2,11},erlydtl_beam_compiler,{unknown_filter,reverse,1}},
  1382. {{1,22},erlydtl_compiler_utils,{load_from,test1,erlydtl_lib_test1,foo}},
  1383. {{1,22},erlydtl_compiler_utils,{load_from,test1,erlydtl_lib_test1,bar}}
  1384. ])]
  1385. },
  1386. {"pre load unknown library",
  1387. <<"{{ '123'|reverse }}">>, [], [],
  1388. [{default_libraries, [test1]}],
  1389. <<"">>,
  1390. [error_info(
  1391. [{{1,10},erlydtl_beam_compiler,{unknown_filter,reverse,1}},
  1392. {none,erlydtl_compiler_utils,{load_library,test1,test1,nofile}}
  1393. ])]
  1394. },
  1395. {"pre load unknown legacy library",
  1396. <<"{% foo %}">>, [], [],
  1397. [{custom_tags_modules, [foo]}],
  1398. <<"">>,
  1399. [error_info([{none,erlydtl_compiler,{load_library,'(custom-legacy)',foo,nofile}}])]
  1400. },
  1401. {"unknown filter",
  1402. <<"{{ '123'|foo }}">>, [], [], [],
  1403. <<"">>,
  1404. [error_info([{{1,10},erlydtl_beam_compiler,{unknown_filter,foo,1}}])]
  1405. },
  1406. {"ssi file not found",
  1407. <<"{% ssi 'foo' %}">>, [],
  1408. {error, {read_file, <<"./foo">>, enoent}}
  1409. }
  1410. ]},
  1411. {"load",
  1412. [{"filter",
  1413. <<"{% load test1 %}{{ \"1234\"|reverse }}">>, [], [],
  1414. [{libraries, [{test1, erlydtl_lib_test1}]}],
  1415. <<"4321">>
  1416. },
  1417. {"named",
  1418. <<"{% load reverse from test1 %}{{ \"abcd\"|reverse }}">>, [], [],
  1419. [{libraries, [{test1, erlydtl_lib_test1}]}],
  1420. <<"dcba">>
  1421. },
  1422. {"pre loaded",
  1423. <<"{{ QWER|reverse }}">>, [{'QWER', "Qwerty"}], [],
  1424. [{default_libraries, [test1]},
  1425. {libraries, [{test1, erlydtl_lib_test1}]}],
  1426. <<"ytrewQ">>
  1427. }
  1428. ]},
  1429. {"functional",
  1430. [functional_test(F)
  1431. %% order is important.
  1432. || F <- ["autoescape", "comment", "extends", "filters", "for", "for_list",
  1433. "for_tuple", "for_list_preset", "for_preset", "for_records",
  1434. "for_records_preset", "include", "if", "if_preset", "ifequal",
  1435. "ifequal_preset", "ifnotequal", "ifnotequal_preset", "now",
  1436. "var", "var_preset", "cycle", "custom_tag", "custom_tag1",
  1437. "custom_tag2", "custom_tag3", "custom_tag4", "custom_call",
  1438. "include_template", "include_path", "ssi", "extends_path",
  1439. "extends_path2", "trans", "extends2", "extends3",
  1440. "recursive_block", "extend_recursive_block", "missing",
  1441. "block_super"]
  1442. ]},
  1443. {"compile_dir",
  1444. [setup_compile(T)
  1445. || T <- [#test{
  1446. title = "non-existing dir",
  1447. source = {dir, "non-existing-made-up-dir"},
  1448. renderer = fun(#test{ source={dir, Dir} }) -> Dir end,
  1449. output = "non-existing-made-up-dir"
  1450. },
  1451. #test{
  1452. title = "path1",
  1453. source = {dir, template_file(input, "path1")},
  1454. renderer = fun(#test{ module=M, render_vars=V, render_opts=O }) ->
  1455. M:render(base1, V, O)
  1456. end
  1457. }]
  1458. ]}
  1459. ].
  1460. %% {Name, DTL, Vars, Output}
  1461. %% {Name, DTL, Vars, RenderOpts, Output}
  1462. %% {Name, DTL, Vars, RenderOpts, CompilerOpts, Output}
  1463. %% {Name, DTL, Vars, RenderOpts, CompilerOpts, Output, Warnings}
  1464. def_to_test(Group, #test{ title=Name }=T) ->
  1465. T#test{ title = lists:concat([Group, ": ", Name]) };
  1466. def_to_test(Group, {Name, DTL, Vars, Output}) ->
  1467. def_to_test(Group, {Name, DTL, Vars, [], [], Output, default_warnings()});
  1468. def_to_test(Group, {Name, DTL, Vars, RenderOpts, Output}) ->
  1469. def_to_test(Group, {Name, DTL, Vars, RenderOpts, [], Output, default_warnings()});
  1470. def_to_test(Group, {Name, DTL, Vars, RenderOpts, CompilerOpts, Output}) ->
  1471. def_to_test(Group, {Name, DTL, Vars, RenderOpts, CompilerOpts, Output, default_warnings()});
  1472. def_to_test(Group, {Name, DTL, Vars, RenderOpts, CompilerOpts, Output, Warnings}) ->
  1473. #test{
  1474. title = lists:concat([Group, ": ", Name]),
  1475. source = {template, DTL},
  1476. render_vars = Vars,
  1477. render_opts = RenderOpts,
  1478. compile_opts = CompilerOpts ++ (#test{})#test.compile_opts,
  1479. output = Output,
  1480. warnings = Warnings
  1481. }.
  1482. generate_test_date() ->
  1483. {{Y,M,D}, _} = erlang:localtime(),
  1484. MonthName = [
  1485. "January", "February", "March", "April",
  1486. "May", "June", "July", "August", "September",
  1487. "October", "November", "December"
  1488. ],
  1489. OrdinalSuffix = [
  1490. "st","nd","rd","th","th","th","th","th","th","th", % 1-10
  1491. "th","th","th","th","th","th","th","th","th","th", % 10-20
  1492. "st","nd","rd","th","th","th","th","th","th","th", % 20-30
  1493. "st"
  1494. ],
  1495. list_to_binary([
  1496. "It is the ",
  1497. integer_to_list(D),
  1498. lists:nth(D, OrdinalSuffix),
  1499. " of ", lists:nth(M, MonthName),
  1500. " ", integer_to_list(Y), "."
  1501. ]).
  1502. default_warnings() -> [].
  1503. error_info(File, Ws, Mod) ->
  1504. {File, [error_info(W, Mod) || W <- Ws]}.
  1505. error_info({Line, ErrorDesc}, Mod)
  1506. when is_integer(Line); Line =:= none ->
  1507. {Line, Mod, ErrorDesc};
  1508. error_info({Line, Module, _}=ErrorDesc, _Mod)
  1509. when is_integer(Line), is_atom(Module) ->
  1510. ErrorDesc;
  1511. error_info({none, Module, _}=ErrorDesc, _Mod)
  1512. when is_atom(Module) ->
  1513. ErrorDesc;
  1514. error_info({{Line, Col}, Module, _}=ErrorDesc, _Mod)
  1515. when is_integer(Line), is_integer(Col), is_atom(Module) ->
  1516. ErrorDesc;
  1517. error_info(Ws, Mod) when is_list(Ws) ->
  1518. error_info("erly_test", Ws, Mod);
  1519. error_info(ErrorDesc, Mod) ->
  1520. {none, Mod, ErrorDesc}.
  1521. error_info(Ei) ->
  1522. error_info(Ei, erlydtl_beam_compiler).
  1523. template_file(Dir, Name) -> filename:join(["../test/files", Dir, Name]).
  1524. functional_test(F) ->
  1525. setup_compile(#test{
  1526. title = F,
  1527. module = list_to_atom("functional_test_" ++ F),
  1528. source = {file, template_file(input, F)}
  1529. }).
  1530. setup_compile(#test{ title=F, compile_opts=Opts }=T) ->
  1531. CompileOpts = [{doc_root, "../test/files/input"}|Opts],
  1532. case setup_compile(F) of
  1533. {ok, [CV|CO]} ->
  1534. setup(T#test{
  1535. compile_vars = CV,
  1536. compile_opts = CO ++ CompileOpts
  1537. });
  1538. {error, Es, Ws} ->
  1539. T#test{
  1540. errors = Es,
  1541. warnings = Ws,
  1542. compile_opts = CompileOpts
  1543. }
  1544. end;
  1545. setup_compile("for_list_preset") ->
  1546. CompileVars = [{fruit_list, [["apple", "apples"], ["banana", "bananas"], ["coconut", "coconuts"]]}],
  1547. {ok, [CompileVars]};
  1548. setup_compile("for_preset") ->
  1549. CompileVars = [{fruit_list, ["preset-apple", "preset-banana", "preset-coconut"]}],
  1550. {ok, [CompileVars]};
  1551. setup_compile("for_records_preset") ->
  1552. Link1a = [{name, "Amazon (preset)"}, {url, "http://amazon.com"}],
  1553. Link2a = [{name, "Google (preset)"}, {url, "http://google.com"}],
  1554. Link3a = [{name, "Microsoft (preset)"}, {url, "http://microsoft.com"}],
  1555. CompileVars = [{software_links, [Link1a, Link2a, Link3a]}],
  1556. {ok, [CompileVars]};
  1557. setup_compile("if_preset") ->
  1558. CompileVars = [{var1, "something"}],
  1559. {ok, [CompileVars]};
  1560. setup_compile("ifequal_preset") ->
  1561. CompileVars = [{var1, "foo"}, {var2, "foo"}],
  1562. {ok, [CompileVars]};
  1563. setup_compile("ifnotequal_preset") ->
  1564. CompileVars = [{var1, "foo"}, {var2, "foo"}],
  1565. {ok, [CompileVars]};
  1566. setup_compile("var_preset") ->
  1567. CompileVars = [{preset_var1, "preset-var1"}, {preset_var2, "preset-var2"}],
  1568. {ok, [CompileVars]};
  1569. setup_compile("extends2") ->
  1570. File = template_file(input, "extends2"),
  1571. Error = {none, erlydtl_beam_compiler, unexpected_extends_tag},
  1572. {error, [{File, [Error]}], []};
  1573. setup_compile("extends3") ->
  1574. File = template_file(input, "extends3"),
  1575. Include = template_file(input, "imaginary"),
  1576. Error = {none, erlydtl_beam_compiler, {read_file, Include, enoent}},
  1577. {error, [{File, [Error]}], []};
  1578. setup_compile("missing") ->
  1579. File = template_file(input, "missing"),
  1580. Error = {none, erlydtl_compiler, {read_file, File, enoent}},
  1581. {error, [{File, [Error]}], []};
  1582. setup_compile("custom_tag") ->
  1583. {ok, [[]|[{custom_tags_modules, [erlydtl_custom_tags]}]]};
  1584. setup_compile("custom_tag1") -> setup_compile("custom_tag");
  1585. setup_compile("custom_tag2") -> setup_compile("custom_tag");
  1586. setup_compile("custom_tag3") -> setup_compile("custom_tag");
  1587. setup_compile("custom_tag4") -> setup_compile("custom_tag");
  1588. setup_compile(_) ->
  1589. {ok, [[]]}.
  1590. expected(File) ->
  1591. Filename = template_file(expect, File),
  1592. case file:read_file(Filename) of
  1593. {ok, Data} -> Data;
  1594. _ -> fun (Data) ->
  1595. ok = file:write_file(Filename, Data),
  1596. io:format(
  1597. user,
  1598. "## Saved expected output for test ~p to ~p.~n"
  1599. " Verify the contents, as it is used to pass the test on subsequent test runs.~n"
  1600. "~n",
  1601. [File, Filename]),
  1602. throw({verify_new_expected_output, Filename})
  1603. end
  1604. end.
  1605. setup(#test{ title = F, output=undefined }=T) ->
  1606. {Vars, Opts, Result} =
  1607. case setup(F) of
  1608. {ok, V} -> {V, [], expected(F)};
  1609. {ok, V, O} -> {V, O, expected(F)};
  1610. {ok, V, O, skip_check} -> {V, O, fun (_) -> ok end};
  1611. {ok, V, O, R} -> {V, O, R}
  1612. end,
  1613. T#test{
  1614. render_vars = Vars,
  1615. render_opts = Opts,
  1616. output = Result
  1617. };
  1618. setup(#test{}=T) -> T;
  1619. setup("autoescape") ->
  1620. RenderVars = [{var1, "<b>bold</b>"}],
  1621. {ok, RenderVars};
  1622. setup("extends") ->
  1623. RenderVars = [{base_var, "base-barstring"}, {test_var, "test-barstring"}],
  1624. {ok, RenderVars};
  1625. setup("include_template") -> setup("extends");
  1626. setup("include_path") -> setup("extends");
  1627. setup("extends_path") -> setup("extends");
  1628. setup("extends_path2") -> setup("extends");
  1629. setup("block_super") -> setup("extends");
  1630. setup("filters") ->
  1631. RenderVars = [
  1632. {date_var1, {1975,7,24}},
  1633. {datetime_var1, {{1975,7,24}, {7,13,1}}},
  1634. {'list', ["eins", "zwei", "drei"]}
  1635. ],
  1636. {ok, RenderVars};
  1637. setup("for") ->
  1638. RenderVars = [{fruit_list, ["apple", "banana", "coconut"]}],
  1639. {ok, RenderVars};
  1640. setup("for_list") ->
  1641. RenderVars = [{fruit_list, [["apple", "apples", "$1"], ["banana", "bananas", "$2"], ["coconut", "coconuts", "$500"]]}],
  1642. {ok, RenderVars};
  1643. setup("for_tuple") ->
  1644. RenderVars = [{fruit_list, [{"apple", "apples"}, {"banana", "bananas"}, {"coconut", "coconuts"}]}],
  1645. {ok, RenderVars};
  1646. setup("for_records") ->
  1647. Link1 = [{name, "Amazon"}, {url, "http://amazon.com"}],
  1648. Link2 = [{name, "Google"}, {url, "http://google.com"}],
  1649. Link3 = [{name, "Microsoft"}, {url, "http://microsoft.com"}],
  1650. RenderVars = [{link_list, [Link1, Link2, Link3]}],
  1651. {ok, RenderVars};
  1652. setup("for_records_preset") ->
  1653. Link1b = [{name, "Canon"}, {url, "http://canon.com"}],
  1654. Link2b = [{name, "Leica"}, {url, "http://leica.com"}],
  1655. Link3b = [{name, "Nikon"}, {url, "http://nikon.com"}],
  1656. RenderVars = [{photo_links, [Link1b, Link2b, Link3b]}],
  1657. {ok, RenderVars};
  1658. setup("include") ->
  1659. RenderVars = [{var1, "foostring1"}, {var2, "foostring2"}],
  1660. {ok, RenderVars};
  1661. setup("if") ->
  1662. RenderVars = [{var1, "something"}],
  1663. {ok, RenderVars};
  1664. setup("ifequal") ->
  1665. RenderVars = [{var1, "foo"}, {var2, "foo"}, {var3, "bar"}],
  1666. {ok, RenderVars};
  1667. setup("ifequal_preset") ->
  1668. RenderVars = [{var3, "bar"}],
  1669. {ok, RenderVars};
  1670. setup("ifnotequal") ->
  1671. RenderVars = [{var1, "foo"}, {var2, "foo"}, {var3, "bar"}],
  1672. {ok, RenderVars};
  1673. setup("now") ->
  1674. {ok, [], [], skip_check};
  1675. setup("var") ->
  1676. RenderVars = [{var1, "foostring1"}, {var2, "foostring2"}, {var_not_used, "foostring3"}],
  1677. {ok, RenderVars};
  1678. setup("var_preset") ->
  1679. RenderVars = [{var1, "foostring1"}, {var2, "foostring2"}],
  1680. {ok, RenderVars};
  1681. setup("cycle") ->
  1682. RenderVars = [{test, [integer_to_list(X) || X <- lists:seq(1, 20)]},
  1683. {a, "Apple"}, {b, "Banana"}, {c, "Cherry"}],
  1684. {ok, RenderVars};
  1685. setup("trans") ->
  1686. RenderVars = [{locale, "reverse"}],
  1687. {ok, RenderVars};
  1688. setup("locale") ->
  1689. {ok, _RenderVars = [{locale, "ru"}]};
  1690. setup("custom_tag1") ->
  1691. {ok, [{a, <<"a1">>}], [{locale, ru}], <<"b1\n">>};
  1692. setup("custom_tag2") ->
  1693. {ok, [{a, <<"a1">>}], [{locale, ru}, {foo, bar}], <<"b2\n">>};
  1694. setup("custom_tag3") ->
  1695. {ok, [{a, <<"a1">>}], [{locale, ru}], <<"b3\n">>};
  1696. setup("custom_tag4") ->
  1697. {ok, [], [], <<"a\n">>};
  1698. setup("ssi") ->
  1699. RenderVars = [{path, "ssi_include.html"}],
  1700. {ok, RenderVars};
  1701. %%--------------------------------------------------------------------
  1702. %% Custom tags
  1703. %%--------------------------------------------------------------------
  1704. setup("custom_call") ->
  1705. RenderVars = [{var1, "something"}],
  1706. {ok, RenderVars};
  1707. setup(_) ->
  1708. {ok, []}.