erlydtl_test_defs.erl 99 KB

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