erlydtl_test_defs.erl 76 KB

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