erlydtl_test_defs.erl 87 KB

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