erlydtl_test_defs.erl 82 KB

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