erlydtl_test_defs.erl 91 KB

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