erlydtl_test_defs.erl 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  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 }}">>, [],
  1268. <<"Hello Hans">>}
  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. {"extended translation features (#131)",
  1295. [{"trans default locale",
  1296. <<"test {% trans 'message' %}">>,
  1297. [], [{translation_fun, fun ("message", none) -> "ok" end}],
  1298. <<"test ok">>},
  1299. {"trans foo locale",
  1300. <<"test {% trans 'message' %}">>,
  1301. [], [{locale, "foo"}, {translation_fun, fun ("message", "foo") -> "ok" end}],
  1302. <<"test ok">>},
  1303. {"trans context (run-time)",
  1304. <<"test {% trans 'message' context 'foo' %}">>,
  1305. [], [{translation_fun, fun ("message", {none, "foo"}) -> "ok" end}],
  1306. <<"test ok">>},
  1307. {"trans context (compile-time)",
  1308. <<"test {% trans 'message' context 'foo' %}">>,
  1309. [], [{locale, "baz"}],
  1310. [{blocktrans_locales, ["bar", "baz"]},
  1311. {blocktrans_fun, fun ("message", {L, "foo"}) ->
  1312. case L of
  1313. "bar" -> "rab";
  1314. "baz" -> "ok"
  1315. end
  1316. end}],
  1317. <<"test ok">>},
  1318. {"blocktrans context (run-time)",
  1319. <<"{% blocktrans context 'bar' %}translate this{% endblocktrans %}">>,
  1320. [], [{locale, "foo"}, {translation_fun,
  1321. fun ("translate this", {"foo", "bar"}) ->
  1322. "got it"
  1323. end}],
  1324. <<"got it">>},
  1325. {"blocktrans context (compile-time)",
  1326. <<"{% blocktrans context 'bar' %}translate this{% endblocktrans %}">>,
  1327. [], [{locale, "foo"}],
  1328. [{locale, "foo"}, {blocktrans_fun,
  1329. fun ("translate this", {"foo", "bar"}) ->
  1330. "got it"
  1331. end}],
  1332. <<"got it">>}
  1333. ]},
  1334. {"verbatim",
  1335. [{"Plain verbatim",
  1336. <<"{% verbatim %}{{ oh no{% foobar %}{% endverbatim %}">>, [],
  1337. <<"{{ oh no{% foobar %}">>},
  1338. {"Named verbatim",
  1339. <<"{% verbatim foobar %}{% verbatim %}{% endverbatim foobar2 %}{% endverbatim foobar %}">>, [],
  1340. <<"{% verbatim %}{% endverbatim foobar2 %}">>}
  1341. ]},
  1342. {"widthratio",
  1343. [{"Literals", <<"{% widthratio 5 10 100 %}">>, [], <<"50">>},
  1344. {"Rounds up", <<"{% widthratio a b 100 %}">>, [{a, 175}, {b, 200}], <<"88">>}
  1345. ]},
  1346. {"with",
  1347. [{"Cache literal",
  1348. <<"{% with a=1 %}{{ a }}{% endwith %}">>, [], <<"1">>},
  1349. {"Cache variable",
  1350. <<"{% with a=b %}{{ a }}{% endwith %}">>, [{b, "foo"}], <<"foo">>},
  1351. {"Cache variable with attribute",
  1352. <<"I enjoy {% with a = var1 %}{{ a.game }}{% endwith %}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
  1353. {"Cache variable attribute",
  1354. <<"I enjoy {% with a = var1.game %}{{ a }}{% endwith %}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
  1355. {"Cache multiple",
  1356. <<"{% with alpha=1 beta=b %}{{ alpha }}/{{ beta }}{% endwith %}">>, [{b, 2}], <<"1/2">>}
  1357. ]},
  1358. {"unicode",
  1359. [{"(tm) somewhere",
  1360. <<"™">>, [], <<"™">>}
  1361. ]},
  1362. {"contrib_humanize",
  1363. [{"intcomma",
  1364. <<"{{ a|intcomma }} {{ b|intcomma }} {{ c|intcomma }} {{ d|intcomma }}">>,
  1365. [{a, 999}, {b, 123456789}, {c, 12345}, {d, 1234567890}], [],
  1366. [{custom_filters_modules, [erlydtl_contrib_humanize]}],
  1367. <<"999 123,456,789 12,345 1,234,567,890">>}
  1368. ]},
  1369. %% custom syntax stuff
  1370. {"extension_module",
  1371. [ %% the erlydtl_test_extension module replaces a foo identifier with bar when hitting a # following foo.
  1372. {"replace parsed token", <<"{{ foo # }}">>, [{bar, "ok"}], [],
  1373. [{extension_module, erlydtl_test_extension}], <<"ok">>},
  1374. #test{
  1375. title = "proper error message",
  1376. source = <<"{{ bar # }}">>,
  1377. render_vars = [{bar, "ok"}],
  1378. compile_opts = [{extension_module, erlydtl_test_extension},
  1379. report, return, force_recompile, {out_dir, false}],
  1380. errors = [error_info([{{1,8},erlydtl_scanner,{illegal_char, $#}}])]
  1381. },
  1382. %% accept identifiers as expressions (this is a dummy functionality to test the parser extensibility)
  1383. {"identifiers as expressions", <<"{{ foo.bar or baz }}">>, [{baz, "ok"}], [],
  1384. [{extension_module, erlydtl_test_extension}], <<"ok">>}
  1385. ]},
  1386. {"records",
  1387. [{"field access",
  1388. <<"{{ r.baz }}">>, [{r, #testrec{ foo="Foo", bar="Bar", baz="Baz" }}], [],
  1389. [{record_info, [{testrec, record_info(fields, testrec)}]}],
  1390. <<"Baz">>}
  1391. ]},
  1392. {"error reporting",
  1393. [#test{
  1394. title = "no out dir warning",
  1395. source = <<"foo bar">>,
  1396. compile_opts = [report, return, force_recompile],
  1397. output = <<"foo bar">>,
  1398. warnings = [error_info([no_out_dir])]
  1399. },
  1400. #test{
  1401. title = "warnings as errors",
  1402. source = <<"foo bar">>,
  1403. compile_opts = [report, return, warnings_as_errors, force_recompile],
  1404. errors = [error_info([no_out_dir])]
  1405. },
  1406. #test{
  1407. title = "illegal character",
  1408. source = <<"{{{">>,
  1409. errors = [error_info([{{1,3},erlydtl_scanner,{illegal_char, ${}}])]
  1410. },
  1411. #test{
  1412. title = "unexpected end of file - in code",
  1413. source = <<"{{">>,
  1414. errors = [error_info([{{1,3},erlydtl_scanner,{eof, in_code}}])]
  1415. },
  1416. #test{
  1417. title = "unexpected end of file - in comment",
  1418. source = <<"{#">>,
  1419. errors = [error_info([{{1,3},erlydtl_scanner,{eof, in_comment}}])]
  1420. },
  1421. {"unknown library",
  1422. <<"{% load foo %}">>, [], [], [],
  1423. <<>>,
  1424. [error_info(
  1425. [{{1,9},erlydtl_compiler_utils,{load_library,foo,foo,nofile}}
  1426. ])]
  1427. },
  1428. {"not a library",
  1429. <<"{% load foo %}">>, [], [],
  1430. [{libraries, [{foo, ?MODULE}]}],
  1431. <<>>,
  1432. [error_info(
  1433. [{{1,9},erlydtl_compiler_utils,{load_library,foo,?MODULE,behaviour}}
  1434. ])]
  1435. },
  1436. {"library version",
  1437. <<"{% load foo %}">>, [], [],
  1438. [{libraries, [{foo, erlydtl_lib_testversion}]}],
  1439. <<>>,
  1440. [error_info(
  1441. [{{1,9},erlydtl_compiler_utils,{load_library,foo,erlydtl_lib_testversion,{version,invalid}}}
  1442. ])]
  1443. },
  1444. {"not in library",
  1445. <<"{% load foo bar from test1 %}\n{{ \"w00t\"|reverse }}">>, [], [],
  1446. [{libraries, [{test1, erlydtl_lib_test1}]}],
  1447. <<"\n">>,
  1448. [error_info(
  1449. [{{2,11},erlydtl_beam_compiler,{unknown_filter,reverse,1}},
  1450. {{1,22},erlydtl_compiler_utils,{load_from,test1,erlydtl_lib_test1,foo}},
  1451. {{1,22},erlydtl_compiler_utils,{load_from,test1,erlydtl_lib_test1,bar}}
  1452. ])]
  1453. },
  1454. {"pre load unknown library",
  1455. <<"{{ '123'|reverse }}">>, [], [],
  1456. [{default_libraries, [test1]}],
  1457. <<"">>,
  1458. [error_info(
  1459. [{{1,10},erlydtl_beam_compiler,{unknown_filter,reverse,1}},
  1460. {none,erlydtl_compiler_utils,{load_library,test1,test1,nofile}}
  1461. ])]
  1462. },
  1463. {"pre load unknown legacy library",
  1464. <<"{% foo %}">>, [], [],
  1465. [{custom_tags_modules, [foo]}],
  1466. <<"">>,
  1467. [error_info([{none,erlydtl_compiler,{load_library,'(custom-legacy)',foo,nofile}}])]
  1468. },
  1469. {"unknown filter",
  1470. <<"{{ '123'|foo }}">>, [], [], [],
  1471. <<"">>,
  1472. [error_info([{{1,10},erlydtl_beam_compiler,{unknown_filter,foo,1}}])]
  1473. },
  1474. {"ssi file not found",
  1475. <<"{% ssi 'foo' %}">>, [],
  1476. {error, {read_file, <<"./foo">>, enoent}}
  1477. }
  1478. ]},
  1479. {"load",
  1480. [{"filter",
  1481. <<"{% load test1 %}{{ \"1234\"|reverse }}">>, [], [],
  1482. [{libraries, [{test1, erlydtl_lib_test1}]}],
  1483. <<"4321">>
  1484. },
  1485. {"named",
  1486. <<"{% load reverse from test1 %}{{ \"abcd\"|reverse }}">>, [], [],
  1487. [{libraries, [{test1, erlydtl_lib_test1}]}],
  1488. <<"dcba">>
  1489. },
  1490. {"pre loaded",
  1491. <<"{{ QWER|reverse }}">>, [{'QWER', "Qwerty"}], [],
  1492. [{default_libraries, [test1]},
  1493. {libraries, [{test1, erlydtl_lib_test1}]}],
  1494. <<"ytrewQ">>
  1495. }
  1496. ]},
  1497. {"compile time default vars/constants",
  1498. begin
  1499. Tpl = <<"Test {{ var1 }}:{{ var2 }}.">>,
  1500. Txt = <<"Test 123:abc.">>,
  1501. Fun = fun (F) ->
  1502. fun (#test{ module=M }) ->
  1503. M:F()
  1504. end
  1505. end,
  1506. [{"default vars",
  1507. Tpl, [], [],
  1508. [{default_vars, [{var1, 123}, {var2, abc}]}], Txt},
  1509. {"default vars (using fun)",
  1510. Tpl, [], [],
  1511. [{default_vars, [{var1, 123}, {var2, fun () -> abc end}]}], Txt},
  1512. {"override default vars",
  1513. Tpl, [{var2, abc}], [],
  1514. [{default_vars, [{var1, 123}, {var2, 456}]}], Txt},
  1515. {"constants",
  1516. Tpl, [], [],
  1517. [{constants, [{var1, 123}, {var2, abc}]}], Txt},
  1518. {"constants (using fun)",
  1519. Tpl, [], [],
  1520. [{constants, [{var1, 123}, {var2, fun () -> abc end}]}], Txt},
  1521. {"constants non-overridable",
  1522. Tpl, [{var1, ohno}, {var2, noway}], [],
  1523. [{constants, [{var1, 123}, {var2, "abc"}]}], Txt}
  1524. |[#test{ title = T,
  1525. source = Tpl,
  1526. compile_vars = undefined,
  1527. compile_opts = CO ++ (#test{})#test.compile_opts,
  1528. renderer = Fun(F),
  1529. output = O
  1530. }
  1531. || {T, F, O, CO} <-
  1532. [{"variables/0",
  1533. variables, [var1, var2], []},
  1534. {"variables/0 w. defaults",
  1535. variables, [var1, var2], [{default_vars, [{var1, aaa}]}]},
  1536. {"variables/0 w. constants",
  1537. variables, [var2], [{constants, [{var1, bbb}]}]},
  1538. {"default_variables/0",
  1539. default_variables, [], []},
  1540. {"default_variables/0 w. defaults",
  1541. default_variables, [var1], [{default_vars, [{var1, aaa}]}]},
  1542. {"default_variables/0 w. constants",
  1543. default_variables, [], [{constants, [{var1, bbb}]}]},
  1544. {"constants/0",
  1545. constants, [], []},
  1546. {"constants/0 w. defaults",
  1547. constants, [], [{default_vars, [{var1, aaa}]}]},
  1548. {"constants/0 w. constants",
  1549. constants, [var1], [{constants, [{var1, bbb}]}]}
  1550. ]
  1551. ]]
  1552. end},
  1553. {"functional",
  1554. [functional_test(F)
  1555. %% order is important.
  1556. || F <- ["autoescape", "comment", "extends", "filters", "for", "for_list",
  1557. "for_tuple", "for_list_preset", "for_preset", "for_records",
  1558. "for_records_preset", "include", "if", "if_preset", "ifequal",
  1559. "ifequal_preset", "ifnotequal", "ifnotequal_preset", "now",
  1560. "var", "var_preset", "cycle", "custom_tag", "custom_tag1",
  1561. "custom_tag2", "custom_tag3", "custom_tag4", "custom_call",
  1562. "include_template", "include_path", "ssi", "extends_path",
  1563. "extends_path2", "trans", "extends2", "extends3",
  1564. "recursive_block", "extend_recursive_block", "missing",
  1565. "block_super"]
  1566. ]},
  1567. {"compile_dir",
  1568. [setup_compile(T)
  1569. || T <- [#test{
  1570. title = "non-existing dir",
  1571. source = {dir, "non-existing-made-up-dir"},
  1572. renderer = fun(#test{ source={dir, Dir} }) -> Dir end,
  1573. output = "non-existing-made-up-dir"
  1574. },
  1575. #test{
  1576. title = "path1",
  1577. source = {dir, template_file(input, "path1")},
  1578. renderer = fun(#test{ module=M, render_vars=V, render_opts=O }) ->
  1579. M:render(base1, V, O)
  1580. end
  1581. }
  1582. ]
  1583. ]}
  1584. ].
  1585. %% {Name, DTL, Vars, Output}
  1586. %% {Name, DTL, Vars, RenderOpts, Output}
  1587. %% {Name, DTL, Vars, RenderOpts, CompilerOpts, Output}
  1588. %% {Name, DTL, Vars, RenderOpts, CompilerOpts, Output, Warnings}
  1589. def_to_test(Group, #test{ title=Name }=T) ->
  1590. T#test{ title = lists:concat([Group, ": ", Name]) };
  1591. def_to_test(Group, {Name, DTL, Vars, Output}) ->
  1592. def_to_test(Group, {Name, DTL, Vars, [], [], Output, default_warnings()});
  1593. def_to_test(Group, {Name, DTL, Vars, RenderOpts, Output}) ->
  1594. def_to_test(Group, {Name, DTL, Vars, RenderOpts, [], Output, default_warnings()});
  1595. def_to_test(Group, {Name, DTL, Vars, RenderOpts, CompilerOpts, Output}) ->
  1596. def_to_test(Group, {Name, DTL, Vars, RenderOpts, CompilerOpts, Output, default_warnings()});
  1597. def_to_test(Group, {Name, DTL, Vars, RenderOpts, CompilerOpts, Output, Warnings}) ->
  1598. #test{
  1599. title = lists:concat([Group, ": ", Name]),
  1600. source = {template, DTL},
  1601. render_vars = Vars,
  1602. render_opts = RenderOpts,
  1603. compile_vars = undefined,
  1604. compile_opts = CompilerOpts ++ (#test{})#test.compile_opts,
  1605. output = Output,
  1606. warnings = Warnings
  1607. }.
  1608. generate_test_date() ->
  1609. {{Y,M,D}, _} = erlang:localtime(),
  1610. MonthName = [
  1611. "January", "February", "March", "April",
  1612. "May", "June", "July", "August", "September",
  1613. "October", "November", "December"
  1614. ],
  1615. OrdinalSuffix = [
  1616. "st","nd","rd","th","th","th","th","th","th","th", % 1-10
  1617. "th","th","th","th","th","th","th","th","th","th", % 10-20
  1618. "st","nd","rd","th","th","th","th","th","th","th", % 20-30
  1619. "st"
  1620. ],
  1621. list_to_binary([
  1622. "It is the ",
  1623. integer_to_list(D),
  1624. lists:nth(D, OrdinalSuffix),
  1625. " of ", lists:nth(M, MonthName),
  1626. " ", integer_to_list(Y), "."
  1627. ]).
  1628. default_warnings() -> [].
  1629. error_info(File, Ws, Mod) ->
  1630. {File, [error_info(W, Mod) || W <- Ws]}.
  1631. error_info({Line, ErrorDesc}, Mod)
  1632. when is_integer(Line); Line =:= none ->
  1633. {Line, Mod, ErrorDesc};
  1634. error_info({Line, Module, _}=ErrorDesc, _Mod)
  1635. when is_integer(Line), is_atom(Module) ->
  1636. ErrorDesc;
  1637. error_info({none, Module, _}=ErrorDesc, _Mod)
  1638. when is_atom(Module) ->
  1639. ErrorDesc;
  1640. error_info({{Line, Col}, Module, _}=ErrorDesc, _Mod)
  1641. when is_integer(Line), is_integer(Col), is_atom(Module) ->
  1642. ErrorDesc;
  1643. error_info(Ws, Mod) when is_list(Ws) ->
  1644. error_info("erly_test", Ws, Mod);
  1645. error_info(ErrorDesc, Mod) ->
  1646. {none, Mod, ErrorDesc}.
  1647. error_info(Ei) ->
  1648. error_info(Ei, erlydtl_beam_compiler).
  1649. template_file(Dir, Name) -> filename:join(["../test/files", Dir, Name]).
  1650. functional_test(F) ->
  1651. setup_compile(#test{
  1652. title = F,
  1653. module = list_to_atom("functional_test_" ++ F),
  1654. source = {file, template_file(input, F)}
  1655. }).
  1656. setup_compile(#test{ title=F, compile_opts=Opts }=T) ->
  1657. CompileOpts = [{doc_root, "../test/files/input"}|Opts],
  1658. case setup_compile(F) of
  1659. {ok, [CV|CO]} ->
  1660. setup(T#test{
  1661. compile_vars = CV,
  1662. compile_opts = CO ++ CompileOpts
  1663. });
  1664. {error, Es, Ws} ->
  1665. T#test{
  1666. errors = Es,
  1667. warnings = Ws,
  1668. compile_opts = CompileOpts
  1669. }
  1670. end;
  1671. setup_compile("for_list_preset") ->
  1672. CompileVars = [{fruit_list, [["apple", "apples"], ["banana", "bananas"], ["coconut", "coconuts"]]}],
  1673. {ok, [CompileVars]};
  1674. setup_compile("for_preset") ->
  1675. CompileVars = [{fruit_list, ["preset-apple", "preset-banana", "preset-coconut"]}],
  1676. {ok, [CompileVars]};
  1677. setup_compile("for_records_preset") ->
  1678. Link1a = [{name, "Amazon (preset)"}, {url, "http://amazon.com"}],
  1679. Link2a = [{name, "Google (preset)"}, {url, "http://google.com"}],
  1680. Link3a = [{name, "Microsoft (preset)"}, {url, "http://microsoft.com"}],
  1681. CompileVars = [{software_links, [Link1a, Link2a, Link3a]}],
  1682. {ok, [CompileVars]};
  1683. setup_compile("if_preset") ->
  1684. CompileVars = [{var1, "something"}],
  1685. {ok, [CompileVars]};
  1686. setup_compile("ifequal_preset") ->
  1687. CompileVars = [{var1, "foo"}, {var2, "foo"}],
  1688. {ok, [CompileVars]};
  1689. setup_compile("ifnotequal_preset") ->
  1690. CompileVars = [{var1, "foo"}, {var2, "foo"}],
  1691. {ok, [CompileVars]};
  1692. setup_compile("var_preset") ->
  1693. CompileVars = [{preset_var1, "preset-var1"}, {preset_var2, "preset-var2"}],
  1694. {ok, [CompileVars]};
  1695. setup_compile("extends2") ->
  1696. File = template_file(input, "extends2"),
  1697. Error = {none, erlydtl_beam_compiler, unexpected_extends_tag},
  1698. {error, [{File, [Error]}], []};
  1699. setup_compile("extends3") ->
  1700. File = template_file(input, "extends3"),
  1701. Include = template_file(input, "imaginary"),
  1702. Error = {none, erlydtl_beam_compiler, {read_file, Include, enoent}},
  1703. {error, [{File, [Error]}], []};
  1704. setup_compile("missing") ->
  1705. File = template_file(input, "missing"),
  1706. Error = {none, erlydtl_compiler, {read_file, File, enoent}},
  1707. {error, [{File, [Error]}], []};
  1708. setup_compile("custom_tag") ->
  1709. {ok, [[]|[{custom_tags_modules, [erlydtl_custom_tags]}]]};
  1710. setup_compile("custom_tag1") -> setup_compile("custom_tag");
  1711. setup_compile("custom_tag2") -> setup_compile("custom_tag");
  1712. setup_compile("custom_tag3") -> setup_compile("custom_tag");
  1713. setup_compile("custom_tag4") -> setup_compile("custom_tag");
  1714. setup_compile(_) ->
  1715. {ok, [[]]}.
  1716. expected(File) ->
  1717. Filename = template_file(expect, File),
  1718. case file:read_file(Filename) of
  1719. {ok, Data} -> Data;
  1720. _ -> fun (Data) ->
  1721. ok = file:write_file(Filename, Data),
  1722. io:format(
  1723. user,
  1724. "## Saved expected output for test ~p to ~p.~n"
  1725. " Verify the contents, as it is used to pass the test on subsequent test runs.~n"
  1726. "~n",
  1727. [File, Filename]),
  1728. throw({verify_new_expected_output, Filename})
  1729. end
  1730. end.
  1731. setup(#test{ title = F, output=undefined }=T) ->
  1732. {Vars, Opts, Result} =
  1733. case setup(F) of
  1734. {ok, V} -> {V, [], expected(F)};
  1735. {ok, V, O} -> {V, O, expected(F)};
  1736. {ok, V, O, skip_check} -> {V, O, fun (_) -> ok end};
  1737. {ok, V, O, R} -> {V, O, R}
  1738. end,
  1739. T#test{
  1740. render_vars = Vars,
  1741. render_opts = Opts,
  1742. output = Result
  1743. };
  1744. setup(#test{}=T) -> T;
  1745. setup("autoescape") ->
  1746. RenderVars = [{var1, "<b>bold</b>"}],
  1747. {ok, RenderVars};
  1748. setup("extends") ->
  1749. RenderVars = [{base_var, "base-barstring"}, {test_var, "test-barstring"}],
  1750. {ok, RenderVars};
  1751. setup("include_template") -> setup("extends");
  1752. setup("include_path") -> setup("extends");
  1753. setup("extends_path") -> setup("extends");
  1754. setup("extends_path2") -> setup("extends");
  1755. setup("block_super") -> setup("extends");
  1756. setup("filters") ->
  1757. RenderVars = [
  1758. {date_var1, {1975,7,24}},
  1759. {datetime_var1, {{1975,7,24}, {7,13,1}}},
  1760. {'list', ["eins", "zwei", "drei"]}
  1761. ],
  1762. {ok, RenderVars};
  1763. setup("for") ->
  1764. RenderVars = [{fruit_list, ["apple", "banana", "coconut"]}],
  1765. {ok, RenderVars};
  1766. setup("for_list") ->
  1767. RenderVars = [{fruit_list, [["apple", "apples", "$1"], ["banana", "bananas", "$2"], ["coconut", "coconuts", "$500"]]}],
  1768. {ok, RenderVars};
  1769. setup("for_tuple") ->
  1770. RenderVars = [{fruit_list, [{"apple", "apples"}, {"banana", "bananas"}, {"coconut", "coconuts"}]}],
  1771. {ok, RenderVars};
  1772. setup("for_records") ->
  1773. Link1 = [{name, "Amazon"}, {url, "http://amazon.com"}],
  1774. Link2 = [{name, "Google"}, {url, "http://google.com"}],
  1775. Link3 = [{name, "Microsoft"}, {url, "http://microsoft.com"}],
  1776. RenderVars = [{link_list, [Link1, Link2, Link3]}],
  1777. {ok, RenderVars};
  1778. setup("for_records_preset") ->
  1779. Link1b = [{name, "Canon"}, {url, "http://canon.com"}],
  1780. Link2b = [{name, "Leica"}, {url, "http://leica.com"}],
  1781. Link3b = [{name, "Nikon"}, {url, "http://nikon.com"}],
  1782. RenderVars = [{photo_links, [Link1b, Link2b, Link3b]}],
  1783. {ok, RenderVars};
  1784. setup("include") ->
  1785. RenderVars = [{var1, "foostring1"}, {var2, "foostring2"}],
  1786. {ok, RenderVars};
  1787. setup("if") ->
  1788. RenderVars = [{var1, "something"}],
  1789. {ok, RenderVars};
  1790. setup("ifequal") ->
  1791. RenderVars = [{var1, "foo"}, {var2, "foo"}, {var3, "bar"}],
  1792. {ok, RenderVars};
  1793. setup("ifequal_preset") ->
  1794. RenderVars = [{var3, "bar"}],
  1795. {ok, RenderVars};
  1796. setup("ifnotequal") ->
  1797. RenderVars = [{var1, "foo"}, {var2, "foo"}, {var3, "bar"}],
  1798. {ok, RenderVars};
  1799. setup("now") ->
  1800. {ok, [], [], skip_check};
  1801. setup("var") ->
  1802. RenderVars = [{var1, "foostring1"}, {var2, "foostring2"}, {var_not_used, "foostring3"}],
  1803. {ok, RenderVars};
  1804. setup("var_preset") ->
  1805. RenderVars = [{var1, "foostring1"}, {var2, "foostring2"}],
  1806. {ok, RenderVars};
  1807. setup("cycle") ->
  1808. RenderVars = [{test, [integer_to_list(X) || X <- lists:seq(1, 20)]},
  1809. {a, "Apple"}, {b, "Banana"}, {c, "Cherry"}],
  1810. {ok, RenderVars};
  1811. setup("trans") ->
  1812. RenderVars = [{locale, "reverse"}],
  1813. {ok, RenderVars};
  1814. setup("locale") ->
  1815. {ok, _RenderVars = [{locale, "ru"}]};
  1816. setup("custom_tag1") ->
  1817. {ok, [{a, <<"a1">>}], [{locale, ru}], <<"b1\n">>};
  1818. setup("custom_tag2") ->
  1819. {ok, [{a, <<"a1">>}], [{locale, ru}, {foo, bar}], <<"b2\n">>};
  1820. setup("custom_tag3") ->
  1821. {ok, [{a, <<"a1">>}], [{locale, ru}], <<"b3\n">>};
  1822. setup("custom_tag4") ->
  1823. {ok, [], [], <<"a\n">>};
  1824. setup("ssi") ->
  1825. RenderVars = [{path, "ssi_include.html"}],
  1826. {ok, RenderVars};
  1827. %%--------------------------------------------------------------------
  1828. %% Custom tags
  1829. %%--------------------------------------------------------------------
  1830. setup("custom_call") ->
  1831. RenderVars = [{var1, "something"}],
  1832. {ok, RenderVars};
  1833. setup(_) ->
  1834. {ok, []}.