erlydtl_test_defs.erl 87 KB

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