erlydtl_test_defs.erl 79 KB

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