erlydtl_test_defs.erl 68 KB

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