erlydtl_test_defs.erl 88 KB

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