erlydtl_test_defs.erl 80 KB

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