erlydtl_test_defs.erl 91 KB

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