erlydtl_test_defs.erl 99 KB

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