erlydtl_test_defs.erl 78 KB

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