erlydtl_test_defs.erl 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210
  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. {"|join:\", \" (numbers)",
  728. <<"{{ var1|join:\", \" }}">>, [{var1, [1, 2, 3]}],
  729. <<"1, 2, 3">>},
  730. {"|last",
  731. <<"{{ var1|last }}">>, [{var1, "XYZ"}],
  732. <<"Z">>},
  733. {"|length",
  734. <<"{{ var1|length }}">>, [{var1, "antidisestablishmentarianism"}],
  735. <<"28">>},
  736. {"|linebreaks",
  737. <<"{{ var1|linebreaks }}">>, [{var1, "Joel\nis a slug"}],
  738. <<"<p>Joel<br />is a slug</p>">>},
  739. {"|linebreaks",
  740. <<"{{ var1|linebreaks }}">>, [{var1, "Joel\n\n\n\nis a slug"}],
  741. <<"<p>Joel</p><p>is a slug</p>">>},
  742. {"|linebreaks",
  743. <<"{{ var1|linebreaks }}">>, [{var1, "Joel\n\nis a \nslug"}],
  744. <<"<p>Joel</p><p>is a <br />slug</p>">>},
  745. {"|linebreaksbr",
  746. <<"{{ var1|linebreaksbr }}">>, [{var1, "One\nTwo\n\nThree\n\n\n"}],
  747. <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
  748. {"|linebreaksbr",
  749. <<"{{ \"One\\nTwo\\n\\nThree\\n\\n\\n\"|linebreaksbr }}">>, [],
  750. <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
  751. {"|linenumbers",
  752. <<"{{ var1|linenumbers }}">>, [{var1, "a\nb\nc"}],
  753. <<"1. a\n2. b\n3. c">>},
  754. {"|linenumbers",
  755. <<"{{ var1|linenumbers }}">>, [{var1, "a"}],
  756. <<"1. a">>},
  757. {"|linenumbers",
  758. <<"{{ var1|linenumbers }}">>, [{var1, "a\n"}],
  759. <<"1. a\n2. ">>},
  760. {"|ljust:10",
  761. <<"{{ var1|ljust:10 }}">>, [{var1, "Gore"}],
  762. <<"Gore ">>},
  763. {"|lower",
  764. <<"{{ var1|lower }}">>, [{var1, "E. E. Cummings"}],
  765. <<"e. e. cummings">>},
  766. {"|makelist",
  767. <<"{{ list|make_list }}">>, [{list, "Joel"}],
  768. <<"J","o","e","l">>},
  769. {"|pluralize",
  770. <<"{{ num|pluralize }}">>, [{num, 1}],
  771. <<"">>},
  772. {"|pluralize",
  773. <<"{{ num|pluralize }}">>, [{num, 2}],
  774. <<"s">>},
  775. {"|pluralize:\"s\"",
  776. <<"{{ num|pluralize }}">>, [{num, 1}],
  777. <<"">>},
  778. {"|pluralize:\"s\"",
  779. <<"{{ num|pluralize }}">>, [{num, 2}],
  780. <<"s">>},
  781. {"|pluralize:\"y,es\" (list)",
  782. <<"{{ num|pluralize:\"y,es\" }}">>, [{num, 1}],
  783. <<"y">>},
  784. {"|pluralize:\"y,es\" (list)",
  785. <<"{{ num|pluralize:\"y,es\" }}">>, [{num, 2}],
  786. <<"es">>},
  787. {"|length|pluralize",
  788. <<"{{ list|length|pluralize:\"plural\" }}">>, [{list, [foo, bar]}],
  789. <<"plural">>},
  790. {"|length|pluralize",
  791. <<"{{ list|length|pluralize:\"plural\" }}">>, [{list, [foo]}],
  792. <<"">>},
  793. {"|random",
  794. <<"{{ var1|random }}">>, [{var1, ["foo", "foo", "foo"]}],
  795. <<"foo">>},
  796. {"|removetags:\"b span\"",
  797. <<"{{ var1|removetags:\"b span\" }}">>, [{var1, "<B>Joel</B> <button>is</button> a <span>slug</span>"}],
  798. <<"<B>Joel</B> <button>is</button> a slug">>},
  799. {"|rjust:10",
  800. <<"{{ var1|rjust:10 }}">>, [{var1, "Bush"}],
  801. <<" Bush">>},
  802. {"|safe",
  803. <<"{% autoescape on %}{{ var1|safe|escape }}{% endautoescape %}">>, [{var1, "&"}],
  804. <<"&">>},
  805. {"|safe is local",
  806. <<"{{ var1 }}{{ var1|safe }}{{ var1 }}">>, [{var1, "&"}], [], [auto_escape],
  807. <<"&amp;&&amp;">>},
  808. %%python/django slice is zero based, erlang lists are 1 based
  809. %%first number included, second number not
  810. %%negative numbers are allowed
  811. %%regex to convert from erlydtl_filters_tests:
  812. % for slice: \?assert.*\( \[(.*)\], erlydtl_filters:(.*)\((.*),"(.*)"\)\),
  813. % {"|slice:\"$4\"", <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],<<$1>>},
  814. % \t\t{"|slice:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>},
  815. %
  816. % for stringformat:
  817. % \?assert.*\( (.*), erlydtl_filters:(.*)\((.*), "(.*)"\) \)
  818. % \t\t{"|stringformat:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>}
  819. {"|slice:\":\"",
  820. <<"{{ var|slice:\":\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  821. <<1,2,3,4,5,6,7,8,9>>},
  822. {"|slice:\"1\"",
  823. <<"{{ var|slice:\"1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  824. <<"2">>},
  825. {"|slice:\"100\"",
  826. <<"{{ var|slice:\"100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  827. <<"indexError">>},
  828. {"|slice:\"-1\"",
  829. <<"{{ var|slice:\"-1\" }}">>, [{var, ["a","b","c","d","e","f","g","h","i"]}],
  830. <<"i">>},
  831. {"|slice:\"-1\"",
  832. <<"{{ var|slice:\"-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  833. <<"9">>},
  834. {"|slice:\"-100\"",
  835. <<"{{ var|slice:\"-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  836. <<"indexError">>},
  837. {"|slice:\"1:\"",
  838. <<"{{ var|slice:\"1:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  839. <<2,3,4,5,6,7,8,9>>},
  840. {"|slice:\"100:\"",
  841. <<"{{ var|slice:\"100:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  842. <<>>},
  843. {"|slice:\"-1:\"",
  844. <<"{{ var|slice:\"-1:\" }}">>, [{var, ["a","b","c","d","e","f","h","i","j"]}],
  845. <<"j">>},
  846. {"|slice:\"-1:\"",
  847. <<"{{ var|slice:\"-1:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  848. <<9>>},
  849. {"|slice:\"-100:\"",
  850. <<"{{ var|slice:\"-100:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  851. <<1,2,3,4,5,6,7,8,9>>},
  852. {"|slice:\":1\"",
  853. <<"{{ var|slice:\":1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  854. <<1>>},
  855. {"|slice:\":100\"",
  856. <<"{{ var|slice:\":100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  857. <<1,2,3,4,5,6,7,8,9>>},
  858. {"|slice:\":-1\"",
  859. <<"{{ var|slice:\":-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  860. <<1,2,3,4,5,6,7,8>>},
  861. {"|slice:\":-100\"",
  862. <<"{{ var|slice:\":-100\" }}">>, [{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. <<>>},
  870. {"|slice:\"1:-1\"",
  871. <<"{{ var|slice:\"1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  872. <<2,3,4,5,6,7,8>>},
  873. {"|slice:\"-1:1\"",
  874. <<"{{ var|slice:\"-1:1\" }}">>, [{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. <<>>},
  885. {"|slice:\"-100:100\"",
  886. <<"{{ var|slice:\"-100:100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  887. <<1,2,3,4,5,6,7,8,9>>},
  888. {"|slice:\"1:3\"",
  889. <<"{{ var|slice:\"1:3\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  890. <<2,3>>},
  891. {"|slice:\"::\"",
  892. <<"{{ var|slice:\"::\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  893. <<1,2,3,4,5,6,7,8,9>>},
  894. {"|slice:\"1:9:1\"",
  895. <<"{{ var|slice:\"1:9:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  896. <<2,3,4,5,6,7,8,9>>},
  897. {"|slice:\"10:1:-1\"",
  898. <<"{{ var|slice:\"10:1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  899. <<9,8,7,6,5,4,3>>},
  900. {"|slice:\"-111:-1:1\"",
  901. <<"{{ var|slice:\"-111:-1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  902. <<1,2,3,4,5,6,7,8>>},
  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. <<>>},
  909. {"|slice:\"-111:111:1\"",
  910. <<"{{ var|slice:\"-111:111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  911. <<1,2,3,4,5,6,7,8,9>>},
  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. <<>>},
  924. {"|slice:\"111:-111:-1\"",
  925. <<"{{ var|slice:\"111:-111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  926. <<9,8,7,6,5,4,3,2,1>>}, {"|phone2numeric",
  927. <<"{{ var1|phone2numeric }}">>, [{var1, "1-800-COLLECT"}],
  928. <<"1-800-2655328">>},
  929. {"|slugify",
  930. <<"{{ var1|slugify }}">>, [{var1, "What The $#_! Was He Thinking?"}],
  931. <<"what-the-_-was-he-thinking">>},
  932. {"|slice:\"s\"",
  933. <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
  934. <<"test">>},
  935. {"|stringformat:\"s\"",
  936. <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
  937. <<"test">>},
  938. {"|stringformat:\"s\"",
  939. <<"{{ var|stringformat:\"s\" }}">>, [{var, "1"}],
  940. <<"1">>},
  941. {"|stringformat:\"s\"",
  942. <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
  943. <<"test">>},
  944. {"|stringformat:\"10s\"",
  945. <<"{{ var|stringformat:\"10s\" }}">>, [{var, "test"}],
  946. <<" test">>},
  947. {"|stringformat:\"-10s\"",
  948. <<"{{ var|stringformat:\"-10s\" }}">>, [{var, "test"}],
  949. <<"test ">>},
  950. {"|stringformat:\"d\"",
  951. <<"{{ var|stringformat:\"d\" }}">>, [{var, "90"}],
  952. <<"90">>},
  953. {"|stringformat:\"10d\"",
  954. <<"{{ var|stringformat:\"10d\" }}">>, [{var, "90"}],
  955. <<" 90">>},
  956. {"|stringformat:\"-10d\"",
  957. <<"{{ var|stringformat:\"-10d\" }}">>, [{var, "90"}],
  958. <<"90 ">>},
  959. {"|stringformat:\"i\"",
  960. <<"{{ var|stringformat:\"i\" }}">>, [{var, "90"}],
  961. <<"90">>},
  962. {"|stringformat:\"10i\"",
  963. <<"{{ var|stringformat:\"10i\" }}">>, [{var, "90"}],
  964. <<" 90">>},
  965. {"|stringformat:\"-10i\"",
  966. <<"{{ var|stringformat:\"-10i\" }}">>, [{var, "90"}],
  967. <<"90 ">>},
  968. {"|stringformat:\"0.2d\"",
  969. <<"{{ var|stringformat:\"0.2d\" }}">>, [{var, "9"}],
  970. <<"09">>},
  971. {"|stringformat:\"10.4d\"",
  972. <<"{{ var|stringformat:\"10.4d\" }}">>, [{var, "9"}],
  973. <<" 0009">>},
  974. {"|stringformat:\"-10.4d\"",
  975. <<"{{ var|stringformat:\"-10.4d\" }}">>, [{var, "9"}],
  976. <<"0009 ">>},
  977. {"|stringformat:\"f\"",
  978. <<"{{ var|stringformat:\"f\" }}">>, [{var, "1"}],
  979. <<"1.000000">>},
  980. {"|stringformat:\".2f\"",
  981. <<"{{ var|stringformat:\".2f\" }}">>, [{var, "1"}],
  982. <<"1.00">>},
  983. {"|stringformat:\"0.2f\"",
  984. <<"{{ var|stringformat:\"0.2f\" }}">>, [{var, "1"}],
  985. <<"1.00">>},
  986. {"|stringformat:\"-0.2f\"",
  987. <<"{{ var|stringformat:\"-0.2f\" }}">>, [{var, "1"}],
  988. <<"1.00">>},
  989. {"|stringformat:\"10.2f\"",
  990. <<"{{ var|stringformat:\"10.2f\" }}">>, [{var, "1"}],
  991. <<" 1.00">>},
  992. {"|stringformat:\"-10.2f\"",
  993. <<"{{ var|stringformat:\"-10.2f\" }}">>, [{var, "1"}],
  994. <<"1.00 ">>},
  995. {"|stringformat:\".2f\"",
  996. <<"{{ var|stringformat:\".2f\" }}">>, [{var, "1"}],
  997. <<"1.00">>},
  998. {"|stringformat:\"x\"",
  999. <<"{{ var|stringformat:\"x\" }}">>, [{var, "90"}],
  1000. <<"5a">>},
  1001. {"|stringformat:\"X\"",
  1002. <<"{{ var|stringformat:\"X\" }}">>, [{var, "90"}],
  1003. <<"5A">>},
  1004. {"|stringformat:\"o\"",
  1005. <<"{{ var|stringformat:\"o\" }}">>, [{var, "90"}],
  1006. <<"132">>},
  1007. {"|stringformat:\"e\"",
  1008. <<"{{ var|stringformat:\"e\" }}">>, [{var, "90"}],
  1009. <<"9.000000e+01">>},
  1010. {"|stringformat:\"e\"",
  1011. <<"{{ var|stringformat:\"e\" }}">>, [{var, "90000000000"}],
  1012. <<"9.000000e+10">>},
  1013. {"|stringformat:\"E\"",
  1014. <<"{{ var|stringformat:\"E\" }}">>, [{var, "90"}],
  1015. <<"9.000000E+01">>},
  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, "<B>Joel</B> <button>is</button> a <span>slug</Span>"}],
  1021. <<"Joel is a slug">>},
  1022. {"|striptags",
  1023. <<"{{ var|striptags }}">>, [{var, "Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>"}],
  1024. <<"Check out http://www.djangoproject.com">>},
  1025. {"|time:\"H:i\"",
  1026. <<"{{ var|time:\"H:i\" }}">>, [{var, {{2010,12,1}, {10,11,12}} }],
  1027. <<"10:11">>},
  1028. {"|time",
  1029. <<"{{ var|time }}">>, [{var, {{2010,12,1}, {10,11,12}} }],
  1030. <<"10:11 a.m.">>},
  1031. {"|timesince:from_date",
  1032. <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2006,6,1},{8,0,0}} }, {from_date, {{2006,6,1},{0,0,0}} }],
  1033. <<"8 hours">>},
  1034. {"|timesince:from_date",
  1035. <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2010,6,1},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  1036. <<"4 years, 1 day">>}, % leap year
  1037. {"|timesince:from_date",
  1038. <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2006,7,15},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  1039. <<"1 month, 2 weeks">>},
  1040. {"|timeuntil:from_date",
  1041. <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2006,6,1},{8,0,0}} }, {from_date, {{2006,6,1},{0,0,0}} }],
  1042. <<"8 hours">>},
  1043. {"|timeuntil:from_date",
  1044. <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2010,6,1},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  1045. <<"4 years, 1 day">>},
  1046. {"|timeuntil:from_date",
  1047. <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2006,7,15},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  1048. <<"1 month, 2 weeks">>},
  1049. {"|title",
  1050. <<"{{ \"my title case\"|title }}">>, [],
  1051. <<"My Title Case">>},
  1052. {"|title (pre-formatted)",
  1053. <<"{{ \"My Title Case\"|title }}">>, [],
  1054. <<"My Title Case">>},
  1055. {"|title (wacky separators)",
  1056. <<"{{ \"my-title!case\"|title }}">>, [],
  1057. <<"My-Title!Case">>},
  1058. {"|title (numbers)",
  1059. <<"{{ \"my-title123CaSe\"|title }}">>, [],
  1060. <<"My-Title123case">>},
  1061. {"|title (Irish names)",
  1062. <<"{{ \"who's o'malley?\"|title }}">>, [],
  1063. <<"Who's O'Malley?">>},
  1064. {"|truncatechars:0",
  1065. <<"{{ var1|truncatechars:0 }}">>, [{var1, "Empty Me"}],
  1066. <<"...">>},
  1067. {"|truncatechars:14",
  1068. <<"{{ var1|truncatechars:14 }}">>, [{var1, "Truncate Me Please"}],
  1069. <<"Truncate Me...">>},
  1070. {"|truncatechars:17",
  1071. <<"{{ var1|truncatechars:17 }}">>, [{var1, "Don't Truncate Me"}],
  1072. <<"Don't Truncate Me">>},
  1073. {"|truncatechars:4 (UTF-8)",
  1074. <<"{{ var1|truncatechars:4 }}">>, [{var1, "\x{E2}\x{82}\x{AC}1.99"}],
  1075. <<"\x{E2}\x{82}\x{AC}...">>},
  1076. {"|truncatechars:5 (UTF-8)",
  1077. <<"{{ var1|truncatechars:5 }}">>, [{var1, "\x{E2}\x{82}\x{AC} 1.99"}],
  1078. <<"\x{E2}\x{82}\x{AC} ...">>},
  1079. {"|truncatewords:0",
  1080. <<"{{ var1|truncatewords:0 }}">>, [{var1, "Empty Me"}],
  1081. <<" ...">>},
  1082. {"|truncatewords:2",
  1083. <<"{{ var1|truncatewords:2 }}">>, [{var1, "Truncate Me Please"}],
  1084. <<"Truncate Me ...">>},
  1085. {"|truncatewords:3",
  1086. <<"{{ var1|truncatewords:3 }}">>, [{var1, "Don't Truncate Me"}],
  1087. <<"Don't Truncate Me">>},
  1088. {"|truncatewords_html:4",
  1089. <<"{{ var1|truncatewords_html:4 }}">>, [{var1, "<p>The <strong>Long and <em>Winding</em> Road</strong> is too long</p>"}],
  1090. <<"<p>The <strong>Long and <em>Winding</em>...</strong></p>">>},
  1091. {"|truncatewords_html:50",
  1092. <<"{{ var1|truncatewords_html:50 }}">>, [{var1, "<p>The <strong>Long and <em>Winding</em> Road</strong> is too long</p>"}],
  1093. <<"<p>The <strong>Long and <em>Winding</em> Road</strong> is too long</p>">>},
  1094. {"|unordered_list",
  1095. <<"{{ var1|unordered_list }}">>, [{var1, ["States", ["Kansas", ["Lawrence", "Topeka"], "Illinois"]]}],
  1096. <<"<li>States<ul><li>Kansas<ul><li>Lawrence</li><li>Topeka</li></ul></li><li>Illinois</li></ul></li>">>},
  1097. {"|upper",
  1098. <<"{{ message|upper }}">>, [{message, "That man has a gun."}],
  1099. <<"THAT MAN HAS A GUN.">>},
  1100. {"|urlencode",
  1101. <<"{{ url|urlencode }}">>, [{url, "You #$*@!!"}],
  1102. <<"You%20%23%24%2A%40%21%21">>},
  1103. {"|urlencode",
  1104. <<"{{ url|urlencode }}">>, [{url, "http://www.example.org/foo?a=b&c=d"}],
  1105. <<"http%3A//www.example.org/foo%3Fa%3Db%26c%3Dd">>},
  1106. {"|urlencode",
  1107. <<"{{ url|urlencode:\"\" }}">>, [{url, "http://www.example.org/foo?a=b&c=d"}],
  1108. <<"http%3A%2F%2Fwww.example.org%2Ffoo%3Fa%3Db%26c%3Dd">>},
  1109. {"|urlencode",
  1110. <<"{{ url|urlencode:\":/?=&\" }}">>, [{url, "http://www.example.org/foo?a=b&c=d"}],
  1111. <<"http://www.example.org/foo?a=b&c=d">>},
  1112. {"|urlize",
  1113. <<"{{ var|urlize }}">>, [{var, "Check out www.djangoproject.com"}],
  1114. <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">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. {"|urlize",
  1119. <<"{{ var|urlize }}">>, [{var, "Check out \"http://www.djangoproject.com\""}],
  1120. <<"Check out \"<a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>\"">>},
  1121. {"|urlizetrunc:15",
  1122. <<"{{ var|urlizetrunc:15 }}">>, [{var, "Check out www.djangoproject.com"}],
  1123. <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">www.djangopr...</a>">>},
  1124. {"|wordcount",
  1125. <<"{{ words|wordcount }}">>, [{words, "Why Hello There!"}],
  1126. <<"3">>},
  1127. {"|wordwrap:2",
  1128. <<"{{ words|wordwrap:2 }}">>, [{words, "this is"}],
  1129. <<"this \nis">>},
  1130. {"|wordwrap:100",
  1131. <<"{{ words|wordwrap:100 }}">>, [{words, "testing testing"}],
  1132. <<"testing testing">>},
  1133. {"|wordwrap:10",
  1134. <<"{{ words|wordwrap:10 }}">>, [{words, ""}],
  1135. <<"">>},
  1136. {"|wordwrap:1",
  1137. <<"{{ words|wordwrap:1 }}">>, [{words, "two"}],
  1138. <<"two">>},
  1139. %% yesno match: \?assert.*\( (.*), erlydtl_filters:(.*)\((.*), "(.*)"\)\)
  1140. %% yesno replace: \t\t{"|$2:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>}
  1141. {"|yesno:\"yeah,no,maybe\"",
  1142. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, true}],
  1143. <<"yeah">>},
  1144. {"|yesno:\"yeah,no,maybe\"",
  1145. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, false}],
  1146. <<"no">>},
  1147. {"|yesno:\"yeah,no\"",
  1148. <<"{{ var|yesno:\"yeah,no\" }}">>, [{var, undefined}],
  1149. <<"no">>},
  1150. {"|yesno:\"yeah,no,maybe\"",
  1151. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, undefined}],
  1152. <<"maybe">>},
  1153. {"string |yesno:\"yeah,no,maybe\"",
  1154. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, "non-empty string"}],
  1155. <<"yeah">>},
  1156. {"binary |yesno:\"yeah,no,maybe\"",
  1157. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, <<"non-empty binary">>}],
  1158. <<"yeah">>},
  1159. {"empty string |yesno:\"yeah,no,maybe\"",
  1160. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, ""}],
  1161. <<"no">>},
  1162. {"empty binary |yesno:\"yeah,no\"",
  1163. <<"{{ var|yesno:\",no\" }}">>, [{var, <<"">>}],
  1164. <<"no">>},
  1165. {"term |yesno:\"yeah,,maybe\"",
  1166. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, {my, [term, "test"]}}],
  1167. <<"yeah">>},
  1168. {"|yesno:\"yeah,\"",
  1169. <<"{{ var|yesno:\"yeah,\" }}">>, [{var, false}],
  1170. <<"">>},
  1171. {"|yesno:\"yeah,,maybe\"",
  1172. <<"{{ var|yesno:\"yeah,,maybe\" }}">>, [{var, false}],
  1173. <<"">>},
  1174. #test{
  1175. title = "|yesno:\"missing_false_choice\"",
  1176. source = <<"{{ var|yesno:\"missing_false_choice\" }}">>,
  1177. render_vars = [{var, true}],
  1178. output = {error, {yesno, choices}}
  1179. },
  1180. {"escape only once (#150) - no auto escape",
  1181. %% note that auto_escape is off by default in the test suite
  1182. %% due to how the tests have been written (and it's too much
  1183. %% work for me to rewrite them)
  1184. <<"{{ foo }}{{ foo|add:'bar' }}">>,
  1185. [{foo, "foo&"}],
  1186. <<"foo&foo&bar">>},
  1187. {"escape only once (#150) - auto escape block",
  1188. <<"{% autoescape on %}{{ foo }}{{ foo|add:'bar' }}{% endautoescape %}">>,
  1189. [{foo, "foo&"}],
  1190. <<"foo&amp;foo&amp;bar">>},
  1191. {"escape only once (#150) - auto escape",
  1192. <<"{{ foo }}{{ foo|add:'bar' }}">>,
  1193. [{foo, "foo&"}], [], [auto_escape],
  1194. <<"foo&amp;foo&amp;bar">>},
  1195. {"escape only once (#150) - auto escape, safe",
  1196. <<"{{ foo|safe }}{{ foo|add:'bar'|safe }}&{{ foo|safe|add:'bar' }}">>,
  1197. [{foo, "foo&"}], [], [auto_escape],
  1198. <<"foo&foo&bar&foo&bar">>},
  1199. {"escape only once (#150) - escape filter",
  1200. <<"{{ foo|escape }}{{ foo|add:'bar'|escape }}&{{ foo|escape|add:'bar' }}">>,
  1201. [{foo, "foo&"}],
  1202. <<"foo&amp;foo&amp;bar&foo&amp;bar">>},
  1203. {"escape only once (#150) - auto escape + escape filter",
  1204. <<"{{ foo|escape }}{{ foo|add:'bar'|escape }}&{{ foo|escape|add:'bar' }}">>,
  1205. [{foo, "foo&"}], [], [auto_escape],
  1206. <<"foo&amp;foo&amp;bar&foo&amp;bar">>}
  1207. ]},
  1208. {"filters_if",
  1209. [{"Filter if 1.1",
  1210. <<"{% if var1|length_is:0 %}Y{% else %}N{% endif %}">>,
  1211. [{var1, []}],
  1212. <<"Y">>},
  1213. {"Filter if 1.2",
  1214. <<"{% if var1|length_is:1 %}Y{% else %}N{% endif %}">>,
  1215. [{var1, []}],
  1216. <<"N">>},
  1217. {"Filter if 1.3",
  1218. <<"{% if var1|length_is:7 %}Y{% else %}N{% endif %}">>,
  1219. [{var1, []}],
  1220. <<"N">>},
  1221. {"Filter if 2.1",
  1222. <<"{% if var1|length_is:0 %}Y{% else %}N{% endif %}">>,
  1223. [{var1, ["foo"]}],
  1224. <<"N">>},
  1225. {"Filter if 2.2",
  1226. <<"{% if var1|length_is:1 %}Y{% else %}N{% endif %}">>,
  1227. [{var1, ["foo"]}],
  1228. <<"Y">>},
  1229. {"Filter if 2.3",
  1230. <<"{% if var1|length_is:7 %}Y{% else %}N{% endif %}">>,
  1231. [{var1, ["foo"]}],
  1232. <<"N">>},
  1233. {"Filter if 3.1",
  1234. <<"{% ifequal var1|length 0 %}Y{% else %}N{% endifequal %}">>,
  1235. [{var1, []}],
  1236. <<"Y">>},
  1237. {"Filter if 3.2",
  1238. <<"{% ifequal var1|length 1 %}Y{% else %}N{% endifequal %}">>,
  1239. [{var1, []}],
  1240. <<"N">>},
  1241. {"Filter if 4.1",
  1242. <<"{% ifequal var1|length 3 %}Y{% else %}N{% endifequal %}">>,
  1243. [{var1, ["foo", "bar", "baz"]}],
  1244. <<"Y">>},
  1245. {"Filter if 4.2",
  1246. <<"{% ifequal var1|length 0 %}Y{% else %}N{% endifequal %}">>,
  1247. [{var1, ["foo", "bar", "baz"]}],
  1248. <<"N">>},
  1249. {"Filter if 4.3",
  1250. <<"{% ifequal var1|length 1 %}Y{% else %}N{% endifequal %}">>,
  1251. [{var1, ["foo", "bar", "baz"]}],
  1252. <<"N">>}
  1253. ]},
  1254. {"firstof",
  1255. [{"Firstof first",
  1256. <<"{% firstof foo bar baz %}">>,
  1257. [{foo, "1"},{bar, "2"}],
  1258. <<"1">>},
  1259. {"Firstof second",
  1260. <<"{% firstof foo bar baz %}">>,
  1261. [{bar, "2"}],
  1262. <<"2">>},
  1263. {"Firstof none",
  1264. <<"{% firstof foo bar baz %}">>,
  1265. [],
  1266. <<"">>},
  1267. {"Firstof complex",
  1268. <<"{% firstof foo.bar.baz bar %}">>,
  1269. [{foo, [{bar, [{baz, "quux"}]}]}],
  1270. <<"quux">>},
  1271. {"Firstof undefined complex",
  1272. <<"{% firstof foo.bar.baz bar %}">>,
  1273. [{bar, "bar"}],
  1274. <<"bar">>},
  1275. {"Firstof literal",
  1276. <<"{% firstof foo bar \"baz\" %}">>,
  1277. [],
  1278. <<"baz">>}
  1279. ]},
  1280. {"regroup .. endregroup",
  1281. [{"Ordered",
  1282. <<"{% 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 %}">>,
  1283. [{people, [[{first_name, "George"}, {gender, "Male"}], [{first_name, "Bill"}, {gender, "Male"}],
  1284. [{first_name, "Margaret"}, {gender, "Female"}], [{first_name, "Condi"}, {gender, "Female"}]]}],
  1285. <<"Male\nGeorge\nBill\nFemale\nMargaret\nCondi\n">>},
  1286. {"Unordered",
  1287. <<"{% 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 %}">>,
  1288. [{people, [[{first_name, "George"}, {gender, "Male"}],
  1289. [{first_name, "Margaret"}, {gender, "Female"}],
  1290. [{first_name, "Condi"}, {gender, "Female"}],
  1291. [{first_name, "Bill"}, {gender, "Male"}]
  1292. ]}],
  1293. <<"Male\nGeorge\nFemale\nMargaret\nCondi\nMale\nBill\n">>},
  1294. {"NestedOrdered",
  1295. <<"{% 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 %}">>,
  1296. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1297. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1298. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1299. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1300. <<"Costanza\nGeorge\nMargaret\nBuffalo\nBill\nCondi\n">>},
  1301. {"NestedUnordered",
  1302. <<"{% 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 %}">>,
  1303. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1304. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1305. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1306. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1307. <<"Costanza\nGeorge\nBuffalo\nBill\nCostanza\nMargaret\nBuffalo\nCondi\n">>},
  1308. {"Filter",
  1309. <<"{% 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 %}">>,
  1310. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1311. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1312. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1313. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1314. <<"Buffalo\nBill\nCondi\nCostanza\nGeorge\nMargaret\n">>}
  1315. ]},
  1316. {"regroup",
  1317. [{"Ordered",
  1318. <<"{% 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 %}">>,
  1319. [{people, [[{first_name, "George"}, {gender, "Male"}], [{first_name, "Bill"}, {gender, "Male"}],
  1320. [{first_name, "Margaret"}, {gender, "Female"}], [{first_name, "Condi"}, {gender, "Female"}]]}],
  1321. <<"Male\nGeorge\nBill\nFemale\nMargaret\nCondi\n">>},
  1322. {"Unordered",
  1323. <<"{% 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 %}">>,
  1324. [{people, [[{first_name, "George"}, {gender, "Male"}],
  1325. [{first_name, "Margaret"}, {gender, "Female"}],
  1326. [{first_name, "Condi"}, {gender, "Female"}],
  1327. [{first_name, "Bill"}, {gender, "Male"}]
  1328. ]}],
  1329. <<"Male\nGeorge\nFemale\nMargaret\nCondi\nMale\nBill\n">>},
  1330. {"NestedOrdered",
  1331. <<"{% 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 %}">>,
  1332. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1333. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1334. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1335. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1336. <<"Costanza\nGeorge\nMargaret\nBuffalo\nBill\nCondi\n">>},
  1337. {"NestedUnordered",
  1338. <<"{% 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 %}">>,
  1339. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1340. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1341. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1342. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1343. <<"Costanza\nGeorge\nBuffalo\nBill\nCostanza\nMargaret\nBuffalo\nCondi\n">>},
  1344. {"Filter",
  1345. <<"{% 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 %}">>,
  1346. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1347. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1348. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1349. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1350. <<"Buffalo\nBill\nCondi\nCostanza\nGeorge\nMargaret\n">>},
  1351. {"With surrounding context",
  1352. <<"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.">>,
  1353. [{people, [[{first_name, "George"}, {gender, "Male"}], [{first_name, "Bill"}, {gender, "Male"}],
  1354. [{first_name, "Margaret"}, {gender, "Female"}], [{first_name, "Condi"}, {gender, "Female"}]]}],
  1355. <<"People: Male\nGeorge\nBill\nFemale\nMargaret\nCondi\nDone.">>},
  1356. #test{
  1357. title = "regroup record",
  1358. 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 %}">>,
  1359. compile_opts = [{record_info, [{person, record_info(fields, person)}]} | (#test{})#test.compile_opts],
  1360. render_vars = [{people, [#person{ first_name = "George", gender = "Male" },
  1361. #person{ first_name = "Bill", gender = "Male" },
  1362. #person{ first_name = "Margaret", gender = "Female" },
  1363. #person{ first_name = "Condi", gender = "Female" }
  1364. ]}
  1365. ],
  1366. output = <<"Male:\n - George\n - Bill\nFemale:\n - Margaret\n - Condi\n">>
  1367. }
  1368. ]},
  1369. {"spaceless",
  1370. [{"Beginning", <<"{% spaceless %} <b>foo</b>{% endspaceless %}">>, [], <<"<b>foo</b>">>},
  1371. {"Middle", <<"{% spaceless %}<b>foo</b> <b>bar</b>{% endspaceless %}">>, [], <<"<b>foo</b><b>bar</b>">>},
  1372. {"End", <<"{% spaceless %}<b>foo</b> {% endspaceless %}">>, [], <<"<b>foo</b>">>},
  1373. {"NewLine", <<"{% spaceless %}\n<div> \n <b>foo</b> \n </div>\n {% endspaceless %}">>, [], <<"<div><b>foo</b></div>">>}
  1374. ]},
  1375. {"templatetag",
  1376. [{"openblock", <<"{% templatetag openblock %}">>, [], <<"{%">>},
  1377. {"closeblock", <<"{% templatetag closeblock %}">>, [], <<"%}">>},
  1378. {"openvariable", <<"{% templatetag openvariable %}">>, [], <<"{{">>},
  1379. {"closevariable", <<"{% templatetag closevariable %}">>, [], <<"}}">>},
  1380. {"openbrace", <<"{% templatetag openbrace %}">>, [], <<"{">>},
  1381. {"closebrace", <<"{% templatetag closebrace %}">>, [], <<"}">>},
  1382. {"opencomment", <<"{% templatetag opencomment %}">>, [], <<"{#">>},
  1383. {"closecomment", <<"{% templatetag closecomment %}">>, [], <<"#}">>}
  1384. ]},
  1385. {"trans",
  1386. [{"trans functional default locale",
  1387. <<"Hello {% trans \"Hi\" %}">>, [], <<"Hello Hi">>
  1388. },
  1389. {"trans functional reverse locale",
  1390. <<"Hello {% trans \"Hi\" %}">>, [], [{locale, "reverse"}],
  1391. [{locales, ["reverse"]}, {translation_fun, fun("Hi"=Key, "reverse") -> list_to_binary(lists:reverse(Key)) end}],
  1392. <<"Hello iH">>
  1393. },
  1394. {"trans literal at run-time",
  1395. <<"Hello {% trans \"Hi\" %}">>, [], [{translation_fun, fun("Hi") -> "Konichiwa" end}], [],
  1396. <<"Hello Konichiwa">>},
  1397. {"trans variable at run-time",
  1398. <<"Hello {% trans var1 %}">>, [{var1, <<"Hi">>}], [{translation_fun, fun(<<"Hi">>) -> <<"Konichiwa">> end}], [],
  1399. <<"Hello Konichiwa">>},
  1400. {"trans literal at run-time: No-op",
  1401. <<"Hello {% trans \"Hi\" noop %}">>, [], [{translation_fun, fun("Hi") -> <<"Konichiwa">> end}], [],
  1402. <<"Hello Hi">>},
  1403. {"trans variable at run-time: No-op",
  1404. <<"Hello {% trans var1 noop %}">>, [{var1, <<"Hi">>}], [{translation_fun, fun(<<"Hi">>) -> <<"Konichiwa">> end}], [],
  1405. <<"Hello Hi">>},
  1406. {"trans as",
  1407. <<"{% trans 'Hans' as name %}Hello {{ name }}">>, [],
  1408. <<"Hello Hans">>},
  1409. {"trans value",
  1410. <<"{{ _('foo') }}">>, [], [], [{locale, default}, {translation_fun, fun ("foo") -> "bar" end}],
  1411. <<"bar">>}
  1412. ]},
  1413. {"blocktrans",
  1414. [{"blocktrans default locale",
  1415. <<"{% blocktrans %}Hello{% endblocktrans %}">>, [], <<"Hello">>},
  1416. {"blocktrans choose locale",
  1417. <<"{% blocktrans %}Hello, {{ name }}{% endblocktrans %}">>, [{name, "Mr. President"}], [{locale, "de"}],
  1418. [{locales, ["de"]}, {translation_fun, fun("Hello, {{ name }}", "de") -> <<"Guten tag, {{ name }}">> end}], <<"Guten tag, Mr. President">>},
  1419. {"blocktrans with args",
  1420. <<"{% blocktrans with var1=foo %}{{ var1 }}{% endblocktrans %}">>, [{foo, "Hello"}], <<"Hello">>},
  1421. #test{
  1422. title = "blocktrans blocks in content not allowed",
  1423. source = <<"{% blocktrans %}Hello{%if name%}, {{ name }}{%endif%}!{% endblocktrans %}">>,
  1424. errors = [error_info([{{1, 24}, erlydtl_parser, ["syntax error before: ",["\"if\""]]}])]
  1425. },
  1426. #test{
  1427. title = "blocktrans nested variables not allowed",
  1428. source = <<"{% blocktrans %}Hello, {{ user.name }}!{% endblocktrans %}">>,
  1429. errors = [error_info([{{1,31}, erlydtl_parser, ["syntax error before: ","'.'"]}])]
  1430. },
  1431. {"blocktrans runtime",
  1432. <<"{%blocktrans with v1=foo%}Hello, {{ name }}! See {{v1}}.{%endblocktrans%}">>,
  1433. [{name, "Mr. President"}, {foo, <<"rubber-duck">>}],
  1434. [{translation_fun, fun("Hello, {{ name }}! See {{ v1 }}.") -> <<"Guten tag, {{name}}! Sehen {{ v1 }}.">> end}],
  1435. [], <<"Guten tag, Mr. President! Sehen rubber-duck.">>},
  1436. {"trimmed",
  1437. <<"{% blocktrans trimmed %}\n foo \n bar here .\n \n \n baz{% endblocktrans %}">>,
  1438. [], [{translation_fun, fun ("foo bar here . baz") -> "ok" end}],
  1439. <<"ok">>}
  1440. ]},
  1441. {"extended translation features (#131)",
  1442. [{"trans default locale",
  1443. <<"test {% trans 'message' %}">>,
  1444. [], [{translation_fun, fun ("message", default) -> "ok" end}],
  1445. <<"test ok">>},
  1446. {"trans foo locale",
  1447. <<"test {% trans 'message' %}">>,
  1448. [], [{locale, "foo"}, {translation_fun, fun ("message", "foo") -> "ok" end}],
  1449. <<"test ok">>},
  1450. {"trans context (run-time)",
  1451. <<"test {% trans 'message' context 'foo' %}">>,
  1452. [], [{translation_fun, fun ("message", {default, "foo"}) -> "ok" end}],
  1453. <<"test ok">>},
  1454. {"trans context (compile-time)",
  1455. <<"test {% trans 'message' context 'foo' %}">>,
  1456. [], [{locale, "baz"}],
  1457. [{locales, ["bar", "baz"]},
  1458. {translation_fun, fun ("message", {L, "foo"}) ->
  1459. case L of
  1460. "bar" -> "rab";
  1461. "baz" -> "ok"
  1462. end
  1463. end}],
  1464. <<"test ok">>},
  1465. {"trans context noop",
  1466. <<"{% trans 'message' noop context 'foo' %}">>, [], [],
  1467. <<"message">>},
  1468. {"blocktrans context (run-time)",
  1469. <<"{% blocktrans context 'bar' %}translate this{% endblocktrans %}">>,
  1470. [], [{locale, "foo"}, {translation_fun,
  1471. fun ("translate this", {"foo", "bar"}) ->
  1472. "got it"
  1473. end}],
  1474. <<"got it">>},
  1475. {"blocktrans context (compile-time)",
  1476. <<"{% blocktrans context 'bar' %}translate this{% endblocktrans %}">>,
  1477. [], [{locale, "foo"}],
  1478. [{locale, "foo"}, {translation_fun,
  1479. fun ("translate this", {"foo", "bar"}) ->
  1480. "got it"
  1481. end}],
  1482. <<"got it">>},
  1483. {"blocktrans plural",
  1484. <<"{% blocktrans count foo=bar %}",
  1485. "There is just one foo..",
  1486. "{% plural %}",
  1487. "There are many foo's..",
  1488. "{% endblocktrans %}">>,
  1489. [{bar, 2}], [{locale, "baz"},
  1490. {translation_fun,
  1491. fun ({"There is just one foo..", {"There are many foo's..", 2}}, "baz") ->
  1492. "ok"
  1493. end}],
  1494. <<"ok">>},
  1495. {"blocktrans a lot of stuff",
  1496. <<"{% blocktrans with foo=a.b count c=a|length context 'quux' %}"
  1497. "foo={{ foo }};bar={{ bar }};c={{ c }}:"
  1498. "{% plural %}"
  1499. "FOO:{{ foo }},BAR:{{ bar }},C:{{ c }}."
  1500. "{% endblocktrans %}">>,
  1501. [{a, [{b, "B"}]}, {bar, "BAR"}],
  1502. [{locale, "rub"},
  1503. {translation_fun, fun ({Single, {Plural, "1"=_Count}}, {Locale, Context}) ->
  1504. [Single, Plural, Locale, Context]
  1505. end}],
  1506. <<"foo=B;bar=BAR;c=1:"
  1507. "FOO:B,BAR:BAR,C:1."
  1508. "rub" "quux">>},
  1509. {"new translation options",
  1510. <<"{% trans foo %}{% blocktrans %}abc{% endblocktrans %}">>,
  1511. [{foo, "1234"}], [{locale, "test"}, {translation_fun, fun (Msg) -> lists:reverse(Msg) end}],
  1512. [{locale, "foo"}, {locale, "test"}, {locales, ["bar", "baz"]},
  1513. {translation_fun, fun (Msg, _) -> [Msg, lists:reverse(Msg)] end}],
  1514. <<"4321" "abccba">>}
  1515. %% This does work, but always prints a warning to std err.. :/
  1516. %% Warning: template translation: variable not closed: "bar {{ 123"
  1517. %% {"variable error",
  1518. %% <<"{% blocktrans %}foo{{ bar }}{% endblocktrans %}">>,
  1519. %% [], [{translation_fun, fun (_) -> "bar {{ 123" end}],
  1520. %% <<"foo">>}
  1521. ]},
  1522. {"i18n",
  1523. [{"setup translation context, using fun, at render time",
  1524. <<"{% trans 'foo' %}">>, [],
  1525. [{translation_fun, fun () -> fun (Msg) -> string:to_upper(Msg) end end}],
  1526. <<"FOO">>},
  1527. {"setup translation context, using fun, at compile time",
  1528. <<"{% trans 'foo' %}">>, [], [],
  1529. [{locale, default}, {translation_fun, fun () -> fun lists:reverse/1 end}],
  1530. <<"oof">>}
  1531. ]},
  1532. {"language",
  1533. [{"override locale",
  1534. <<"{% trans 'foo' %}{% language 'other' %}{% trans 'foo' %}{% endlanguage %}">>,
  1535. [], [{locale, <<"default">>}, {translation_fun, fun ("foo", <<"default">>) -> "1"; ("foo", <<"other">>) -> "2"; (A, B) -> [A, B] end}],
  1536. <<"12">>}
  1537. ]},
  1538. {"verbatim",
  1539. [{"Plain verbatim",
  1540. <<"{% verbatim %}{{ oh no{% foobar %}{% endverbatim %}">>, [],
  1541. <<"{{ oh no{% foobar %}">>},
  1542. {"Named verbatim",
  1543. <<"{% verbatim foobar %}{% verbatim %}{% endverbatim foobar2 %}{% endverbatim foobar %}">>, [],
  1544. <<"{% verbatim %}{% endverbatim foobar2 %}">>}
  1545. ]},
  1546. {"widthratio",
  1547. [{"Literals", <<"{% widthratio 5 10 100 %}">>, [], <<"50">>},
  1548. {"Rounds up", <<"{% widthratio a b 100 %}">>, [{a, 175}, {b, 200}], <<"88">>}
  1549. ]},
  1550. {"with",
  1551. [{"Cache literal",
  1552. <<"{% with a=1 %}{{ a }}{% endwith %}">>, [], <<"1">>},
  1553. {"Cache variable",
  1554. <<"{% with a=b %}{{ a }}{% endwith %}">>, [{b, "foo"}], <<"foo">>},
  1555. {"Cache variable with attribute",
  1556. <<"I enjoy {% with a = var1 %}{{ a.game }}{% endwith %}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
  1557. {"Cache variable attribute",
  1558. <<"I enjoy {% with a = var1.game %}{{ a }}{% endwith %}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
  1559. {"Cache multiple",
  1560. <<"{% with alpha=1 beta=b %}{{ alpha }}/{{ beta }}{% endwith %}">>, [{b, 2}], <<"1/2">>}
  1561. ]},
  1562. {"unicode",
  1563. [{"(tm) somewhere",
  1564. <<"™">>, [], <<"™">>}
  1565. ]},
  1566. {"contrib_humanize",
  1567. [{"intcomma",
  1568. <<"{{ a|intcomma }} {{ b|intcomma }} {{ c|intcomma }} {{ d|intcomma }}">>,
  1569. [{a, 999}, {b, 123456789}, {c, 12345}, {d, 1234567890}], [],
  1570. [{custom_filters_modules, [erlydtl_contrib_humanize]}],
  1571. <<"999 123,456,789 12,345 1,234,567,890">>}
  1572. ]},
  1573. %% custom syntax stuff
  1574. {"extension_module",
  1575. [ %% the erlydtl_test_extension module replaces a foo identifier with bar when hitting a # following foo.
  1576. {"replace parsed token", <<"{{ foo # }}">>, [{bar, "ok"}], [],
  1577. [{extension_module, erlydtl_test_extension}], <<"ok">>},
  1578. #test{
  1579. title = "proper error message",
  1580. source = <<"{{ bar # }}">>,
  1581. render_vars = [{bar, "ok"}],
  1582. compile_opts = [{extension_module, erlydtl_test_extension},
  1583. report, return, force_recompile, {out_dir, false}],
  1584. errors = [error_info([{{1,8},erlydtl_scanner,{illegal_char, $#}}])]
  1585. },
  1586. %% accept identifiers as expressions (this is a dummy functionality to test the parser extensibility)
  1587. {"identifiers as expressions", <<"{{ foo.bar or baz }}">>, [{baz, "ok"}], [],
  1588. [{extension_module, erlydtl_test_extension}], <<"ok">>}
  1589. ]},
  1590. {"records",
  1591. [{"field access",
  1592. <<"{{ r.baz }}">>, [{r, #testrec{ foo="Foo", bar="Bar", baz="Baz" }}], [],
  1593. [{record_info, [{testrec, record_info(fields, testrec)}]}],
  1594. <<"Baz">>}
  1595. ]},
  1596. {"error reporting",
  1597. [#test{
  1598. title = "no out dir warning",
  1599. source = <<"foo bar">>,
  1600. compile_opts = [report, return, force_recompile],
  1601. output = <<"foo bar">>,
  1602. warnings = [error_info([no_out_dir])]
  1603. },
  1604. #test{
  1605. title = "warnings as errors",
  1606. source = <<"foo bar">>,
  1607. compile_opts = [report, return, warnings_as_errors, force_recompile],
  1608. errors = [error_info([no_out_dir])]
  1609. },
  1610. #test{
  1611. title = "illegal character",
  1612. source = <<"{{{">>,
  1613. errors = [error_info([{{1,3},erlydtl_scanner,{illegal_char, ${}}])]
  1614. },
  1615. #test{
  1616. title = "unexpected end of file - in code",
  1617. source = <<"{{">>,
  1618. errors = [error_info([{{1,3},erlydtl_scanner,{eof, in_code}}])]
  1619. },
  1620. #test{
  1621. title = "unexpected end of file - in comment",
  1622. source = <<"{#">>,
  1623. errors = [error_info([{{1,3},erlydtl_scanner,{eof, in_comment}}])]
  1624. },
  1625. {"unknown library",
  1626. <<"{% load foo %}">>, [], [], [],
  1627. <<>>,
  1628. [error_info(
  1629. [{{1,9},erlydtl_compiler_utils,{load_library,foo,foo,nofile}}
  1630. ])]
  1631. },
  1632. {"not a library",
  1633. <<"{% load foo %}">>, [], [],
  1634. [{libraries, [{foo, ?MODULE}]}],
  1635. <<>>,
  1636. [error_info(
  1637. [{{1,9},erlydtl_compiler_utils,{load_library,foo,?MODULE,behaviour}}
  1638. ])]
  1639. },
  1640. {"library version",
  1641. <<"{% load foo %}">>, [], [],
  1642. [{libraries, [{foo, erlydtl_lib_testversion}]}],
  1643. <<>>,
  1644. [error_info(
  1645. [{{1,9},erlydtl_compiler_utils,{load_library,foo,erlydtl_lib_testversion,{version,invalid}}}
  1646. ])]
  1647. },
  1648. {"not in library",
  1649. <<"{% load foo bar from test1 %}\n{{ \"w00t\"|reverse }}">>, [], [],
  1650. [{libraries, [{test1, erlydtl_lib_test1}]}],
  1651. <<"\n">>,
  1652. [error_info(
  1653. [{{2,11},erlydtl_beam_compiler,{unknown_filter,reverse,1}},
  1654. {{1,22},erlydtl_compiler_utils,{load_from,test1,erlydtl_lib_test1,foo}},
  1655. {{1,22},erlydtl_compiler_utils,{load_from,test1,erlydtl_lib_test1,bar}}
  1656. ])]
  1657. },
  1658. {"pre load unknown library",
  1659. <<"{{ '123'|reverse }}">>, [], [],
  1660. [{default_libraries, [test1]}],
  1661. <<"">>,
  1662. [error_info(
  1663. [{{1,10},erlydtl_beam_compiler,{unknown_filter,reverse,1}},
  1664. {none,erlydtl_compiler_utils,{load_library,test1,test1,nofile}}
  1665. ])]
  1666. },
  1667. {"pre load unknown legacy library",
  1668. <<"{% foo %}">>, [], [],
  1669. [{custom_tags_modules, [foo]}],
  1670. <<"">>,
  1671. [error_info(
  1672. [{none,erlydtl_beam_compiler,{unknown_tag, foo}},
  1673. {none,erlydtl_compiler,{load_library,'(custom-legacy)',foo,nofile}}
  1674. ])]
  1675. },
  1676. {"unknown filter",
  1677. <<"{{ '123'|foo }}">>, [], [], [],
  1678. <<"">>,
  1679. [error_info([{{1,10},erlydtl_beam_compiler,{unknown_filter,foo,1}}])]
  1680. },
  1681. {"unknown tag",
  1682. <<"a{% b %}c">>, [], [], [],
  1683. <<"ac">>,
  1684. [error_info([{none,erlydtl_beam_compiler,{unknown_tag, b}}])]
  1685. },
  1686. {"ssi file not found",
  1687. <<"{% ssi 'foo' %}">>, [],
  1688. {error, {read_file, <<"./foo">>, enoent}}
  1689. },
  1690. {"deprecated compile options",
  1691. <<"">>, [], [],
  1692. [{blocktrans_locales, []}, {blocktrans_fun, fun (_) -> [] end}],
  1693. <<"">>,
  1694. [error_info([{deprecated_option, O, N}
  1695. || {O, N} <- [{blocktrans_locales, locales},
  1696. {blocktrans_fun, translation_fun}]],
  1697. erlydtl_compiler)]
  1698. }
  1699. ]},
  1700. {"load",
  1701. [{"filter",
  1702. <<"{% load test1 %}{{ \"1234\"|reverse }}">>, [], [],
  1703. [{libraries, [{test1, erlydtl_lib_test1}]}],
  1704. <<"4321">>
  1705. },
  1706. {"named",
  1707. <<"{% load reverse from test1 %}{{ \"abcd\"|reverse }}">>, [], [],
  1708. [{libraries, [{test1, erlydtl_lib_test1}]}],
  1709. <<"dcba">>
  1710. },
  1711. {"pre loaded",
  1712. <<"{{ QWER|reverse }}">>, [{'QWER', "Qwerty"}], [],
  1713. [{default_libraries, [test1]},
  1714. {libraries, [{test1, erlydtl_lib_test1}]}],
  1715. <<"ytrewQ">>
  1716. },
  1717. {"lib with multiple behaviours",
  1718. <<"{{ QWER|reverse }}">>, [{'QWER', "Qwerty"}], [],
  1719. [{default_libraries, [test2]},
  1720. {libraries, [{test2, erlydtl_lib_test2}]}],
  1721. <<"ytrewQ">>
  1722. },
  1723. {"lib with multiple behaviors (alternative spelling)",
  1724. <<"{{ QWER|reverse }}">>, [{'QWER', "Qwerty"}], [],
  1725. [{default_libraries, [test2]},
  1726. {libraries, [{test2, erlydtl_lib_test2a}]}],
  1727. <<"ytrewQ">>
  1728. }
  1729. ]},
  1730. {"compile time default vars/constants",
  1731. begin
  1732. Tpl = <<"Test {{ var1 }}:{{ var2 }}.">>,
  1733. Txt = <<"Test 123:abc.">>,
  1734. Fun = fun (F) ->
  1735. fun (#test{ module=M }) ->
  1736. M:F()
  1737. end
  1738. end,
  1739. [{"default vars",
  1740. Tpl, [], [],
  1741. [{default_vars, [{var1, 123}, {var2, abc}]}], Txt},
  1742. {"default vars (using fun)",
  1743. Tpl, [], [],
  1744. [{default_vars, [{var1, 123}, {var2, fun () -> abc end}]}], Txt},
  1745. {"override default vars",
  1746. Tpl, [{var2, abc}], [],
  1747. [{default_vars, [{var1, 123}, {var2, 456}]}], Txt},
  1748. {"constants",
  1749. Tpl, [], [],
  1750. [{constants, [{var1, 123}, {var2, abc}]}], Txt},
  1751. {"constants (using fun)",
  1752. Tpl, [], [],
  1753. [{constants, [{var1, 123}, {var2, fun () -> abc end}]}], Txt},
  1754. {"constants non-overridable",
  1755. Tpl, [{var1, ohno}, {var2, noway}], [],
  1756. [{constants, [{var1, 123}, {var2, "abc"}]}], Txt}
  1757. |[#test{ title = T,
  1758. source = Tpl,
  1759. compile_vars = undefined,
  1760. compile_opts = CO ++ (#test{})#test.compile_opts,
  1761. renderer = Fun(F),
  1762. output = O
  1763. }
  1764. || {T, F, O, CO} <-
  1765. [{"variables/0",
  1766. variables, [var1, var2], []},
  1767. {"variables/0 w. defaults",
  1768. variables, [var1, var2], [{default_vars, [{var1, aaa}]}]},
  1769. {"variables/0 w. constants",
  1770. variables, [var2], [{constants, [{var1, bbb}]}]},
  1771. {"default_variables/0",
  1772. default_variables, [], []},
  1773. {"default_variables/0 w. defaults",
  1774. default_variables, [var1], [{default_vars, [{var1, aaa}]}]},
  1775. {"default_variables/0 w. constants",
  1776. default_variables, [], [{constants, [{var1, bbb}]}]},
  1777. {"constants/0",
  1778. constants, [], []},
  1779. {"constants/0 w. defaults",
  1780. constants, [], [{default_vars, [{var1, aaa}]}]},
  1781. {"constants/0 w. constants",
  1782. constants, [var1], [{constants, [{var1, bbb}]}]}
  1783. ]
  1784. ]]
  1785. end},
  1786. {"functional",
  1787. [functional_test(F)
  1788. %% order is important for a few of these tests, unfortunately.
  1789. || F <- ["autoescape", "comment", "extends", "filters", "for", "for_list", "for_tuple",
  1790. "for_list_preset", "for_preset", "for_records", "for_records_preset", "include",
  1791. "if", "if_preset", "ifequal", "ifequal_preset", "ifnotequal", "ifnotequal_preset",
  1792. "now", "var", "var_preset", "cycle", "custom_tag", "custom_tag1", "custom_tag2",
  1793. "custom_tag3", "custom_tag4", "custom_tag_var", "custom_tag_lib_var", "custom_call", "include_template", "include_path",
  1794. "ssi", "extends_path", "extends_path2", "trans", "extends_for", "extends2",
  1795. "extends3", "recursive_block", "extend_recursive_block", "missing", "block_super",
  1796. "wrapper", "extends4", "super_escaped", "extends_chain", "reader_options", "ssi_reader_options",
  1797. "extend_doubleblock"]
  1798. ]},
  1799. {"compile_dir",
  1800. [setup_compile(T)
  1801. || T <- [#test{
  1802. title = "non-existing dir",
  1803. source = {dir, "non-existing-made-up-dir"},
  1804. renderer = fun(#test{ source={dir, Dir} }) -> Dir end,
  1805. output = "non-existing-made-up-dir"
  1806. },
  1807. #test{
  1808. title = "path1",
  1809. source = {dir, template_file(input, "path1")},
  1810. renderer = fun(#test{ module=M, render_vars=V, render_opts=O }) ->
  1811. M:render(base1, V, O)
  1812. end
  1813. }
  1814. ]
  1815. ]}
  1816. ].
  1817. %% {Name, DTL, Vars, Output}
  1818. %% {Name, DTL, Vars, RenderOpts, Output}
  1819. %% {Name, DTL, Vars, RenderOpts, CompilerOpts, Output}
  1820. %% {Name, DTL, Vars, RenderOpts, CompilerOpts, Output, Warnings}
  1821. def_to_test(Group, #test{ title=Name }=T) ->
  1822. T#test{ title = lists:concat([Group, ": ", Name]) };
  1823. def_to_test(Group, {Name, DTL, Vars, Output}) ->
  1824. def_to_test(Group, {Name, DTL, Vars, [], [], Output, default_warnings()});
  1825. def_to_test(Group, {Name, DTL, Vars, RenderOpts, Output}) ->
  1826. def_to_test(Group, {Name, DTL, Vars, RenderOpts, [], Output, default_warnings()});
  1827. def_to_test(Group, {Name, DTL, Vars, RenderOpts, CompilerOpts, Output}) ->
  1828. def_to_test(Group, {Name, DTL, Vars, RenderOpts, CompilerOpts, Output, default_warnings()});
  1829. def_to_test(Group, {Name, DTL, Vars, RenderOpts, CompilerOpts, Output, Warnings}) ->
  1830. #test{
  1831. title = lists:concat([Group, ": ", Name]),
  1832. source = {template, DTL},
  1833. render_vars = Vars,
  1834. render_opts = RenderOpts,
  1835. compile_vars = undefined,
  1836. compile_opts = CompilerOpts ++ (#test{})#test.compile_opts,
  1837. output = Output,
  1838. warnings = Warnings
  1839. }.
  1840. date_translation(Val, LC) when is_list(Val) ->
  1841. io:format("Translating ~p~n", [Val]),
  1842. date_translation(list_to_binary(Val),LC);
  1843. % date a
  1844. date_translation(<<"p.m.">>, <<"ru">>) ->
  1845. <<"п.п."/utf8>>;
  1846. % date A
  1847. date_translation(<<"PM">>, <<"ru">>) ->
  1848. <<"ПП"/utf8>>;
  1849. % date b
  1850. date_translation(<<"jul">>, <<"ru">>) ->
  1851. <<"июл"/utf8>>;
  1852. % date D
  1853. date_translation(<<"Thu">>, <<"ru">>) ->
  1854. <<"Чтв"/utf8>>;
  1855. % date E
  1856. date_translation(<<"July">>, {<<"ru">>, <<"alt. month">>}) ->
  1857. <<"Июля"/utf8>>;
  1858. % date F
  1859. date_translation(<<"July">>, <<"ru">>) ->
  1860. <<"Июль"/utf8>>;
  1861. % date l
  1862. date_translation(<<"Thursday">>, <<"ru">>) ->
  1863. <<"Четверг"/utf8>>;
  1864. % date M
  1865. date_translation(<<"Sep">>, <<"ru">>) ->
  1866. <<"Сен"/utf8>>;
  1867. % date N
  1868. date_translation(<<"Sept.">>, {<<"ru">>, <<"abbrev. month">>}) ->
  1869. <<"Сен."/utf8>>;
  1870. % date P
  1871. date_translation(<<"noon">>, <<"ru">>) ->
  1872. <<"полдень"/utf8>>;
  1873. date_translation(Text, <<"ru">>) ->
  1874. proplists:get_value(Text,
  1875. lists:zip(
  1876. lists:map(fun list_to_binary/1, en_months()),
  1877. ru_months()),
  1878. Text);
  1879. date_translation(Text, _) ->
  1880. Text.
  1881. ru_months() -> [ <<"Январь"/utf8>>, <<"Февраль"/utf8>>, <<"Март"/utf8>>, <<"Апрель"/utf8>>,
  1882. <<"Май"/utf8>>, <<"Июнь"/utf8>>, <<"Июль"/utf8>>, <<"Август"/utf8>>, <<"Сентябрь"/utf8>>,
  1883. <<"Октябрь"/utf8>>, <<"Ноябрь"/utf8>>, <<"Декабрь"/utf8>>].
  1884. en_months() -> ["January", "February", "March", "April",
  1885. "May", "June", "July", "August", "September",
  1886. "October", "November", "December"].
  1887. generate_test_date() ->
  1888. generate_test_date(false).
  1889. generate_test_date(Translation) ->
  1890. {{Y,M,D}, _} = erlang:localtime(),
  1891. MonthName = case Translation of
  1892. russian -> ru_months();
  1893. _ -> en_months()
  1894. end,
  1895. OrdinalSuffix = [
  1896. "st","nd","rd","th","th","th","th","th","th","th", % 1-10
  1897. "th","th","th","th","th","th","th","th","th","th", % 10-20
  1898. "st","nd","rd","th","th","th","th","th","th","th", % 20-30
  1899. "st"
  1900. ],
  1901. list_to_binary([
  1902. "It is the ",
  1903. integer_to_list(D),
  1904. lists:nth(D, OrdinalSuffix),
  1905. " of ", lists:nth(M, MonthName),
  1906. " ", integer_to_list(Y), "."
  1907. ]).
  1908. default_warnings() -> [].
  1909. error_info(File, Ws, Mod) ->
  1910. {File, [error_info(W, Mod) || W <- Ws]}.
  1911. error_info({Line, ErrorDesc}, Mod)
  1912. when is_integer(Line); Line =:= none ->
  1913. {Line, Mod, ErrorDesc};
  1914. error_info({Line, Module, _}=ErrorDesc, _Mod)
  1915. when is_integer(Line), is_atom(Module) ->
  1916. ErrorDesc;
  1917. error_info({none, Module, _}=ErrorDesc, _Mod)
  1918. when is_atom(Module) ->
  1919. ErrorDesc;
  1920. error_info({{Line, Col}, Module, _}=ErrorDesc, _Mod)
  1921. when is_integer(Line), is_integer(Col), is_atom(Module) ->
  1922. ErrorDesc;
  1923. error_info(Ws, Mod) when is_list(Ws) ->
  1924. error_info("erly_test", Ws, Mod);
  1925. error_info(ErrorDesc, Mod) ->
  1926. {none, Mod, ErrorDesc}.
  1927. error_info(Ei) ->
  1928. error_info(Ei, erlydtl_beam_compiler).
  1929. template_file(Dir, Name) -> filename:join(["../test/files", Dir, Name]).
  1930. functional_test(F) ->
  1931. setup_compile(#test{
  1932. title = F,
  1933. module = list_to_atom("functional_test_" ++ F),
  1934. source = {file, template_file(input, F)}
  1935. }).
  1936. setup_compile(#test{ title=F, compile_opts=Opts }=T) ->
  1937. CompileOpts = [{doc_root, "../test/files/input"}|Opts],
  1938. case setup_compile(F) of
  1939. {ok, [CV|Other]} ->
  1940. CO = proplists:get_value(compile_opts, Other, []),
  1941. Ws = proplists:get_value(warnings, Other, []),
  1942. setup(T#test{
  1943. compile_vars = CV,
  1944. compile_opts = CO ++ CompileOpts,
  1945. warnings = Ws
  1946. });
  1947. {error, Es, Ws} ->
  1948. T#test{
  1949. errors = Es,
  1950. warnings = Ws,
  1951. compile_opts = CompileOpts
  1952. }
  1953. end;
  1954. setup_compile("for_list_preset") ->
  1955. CompileVars = [{fruit_list, [["apple", "apples"], ["banana", "bananas"], ["coconut", "coconuts"]]}],
  1956. {ok, [CompileVars]};
  1957. setup_compile("for_preset") ->
  1958. CompileVars = [{fruit_list, ["preset-apple", "preset-banana", "preset-coconut"]}],
  1959. {ok, [CompileVars]};
  1960. setup_compile("for_records_preset") ->
  1961. Link1a = [{name, "Amazon (preset)"}, {url, "http://amazon.com"}],
  1962. Link2a = [{name, "Google (preset)"}, {url, "http://google.com"}],
  1963. Link3a = [{name, "Microsoft (preset)"}, {url, "http://microsoft.com"}],
  1964. CompileVars = [{software_links, [Link1a, Link2a, Link3a]}],
  1965. {ok, [CompileVars]};
  1966. setup_compile("if_preset") ->
  1967. CompileVars = [{var1, "something"}],
  1968. {ok, [CompileVars]};
  1969. setup_compile("ifequal_preset") ->
  1970. CompileVars = [{var1, "foo"}, {var2, "foo"}],
  1971. {ok, [CompileVars]};
  1972. setup_compile("ifnotequal_preset") ->
  1973. CompileVars = [{var1, "foo"}, {var2, "foo"}],
  1974. {ok, [CompileVars]};
  1975. setup_compile("var_preset") ->
  1976. CompileVars = [{preset_var1, "preset-var1"}, {preset_var2, "preset-var2"}],
  1977. {ok, [CompileVars]};
  1978. setup_compile("extends_for") ->
  1979. CompileVars = [{veggie_list, ["broccoli", "beans", "peas", "carrots"]}],
  1980. {ok, [CompileVars]};
  1981. setup_compile("extends2") ->
  1982. File = template_file(input, "extends2"),
  1983. Error = {none, erlydtl_beam_compiler, unexpected_extends_tag},
  1984. {error, [{File, [Error]}], []};
  1985. setup_compile("extends3") ->
  1986. File = template_file(input, "extends3"),
  1987. Include = template_file(input, "imaginary"),
  1988. Error = {none, erlydtl_beam_compiler, {read_file, Include, enoent}},
  1989. {error, [{File, [Error]}], []};
  1990. setup_compile("extends4") ->
  1991. File = template_file(input, "extends4"),
  1992. Warning = {{1,21}, erlydtl_beam_compiler, non_block_tag},
  1993. {ok, [[]|[{warnings, [{File, [Warning]}]}]]};
  1994. setup_compile("missing") ->
  1995. File = template_file(input, "missing"),
  1996. Error = {none, erlydtl_compiler, {read_file, File, enoent}},
  1997. {error, [{File, [Error]}], []};
  1998. setup_compile("custom_tag") ->
  1999. {ok, [[]|[{compile_opts, [{custom_tags_modules, [erlydtl_custom_tags]}]}]]};
  2000. setup_compile("custom_tag1") -> setup_compile("custom_tag");
  2001. setup_compile("custom_tag2") -> setup_compile("custom_tag");
  2002. setup_compile("custom_tag3") -> setup_compile("custom_tag");
  2003. setup_compile("custom_tag4") -> setup_compile("custom_tag");
  2004. setup_compile("custom_tag_var") -> setup_compile("custom_tag");
  2005. setup_compile("custom_tag_lib_var") ->
  2006. {ok, [[]|[{compile_opts, [{libraries, [{custom_tag_lib,erlydtl_custom_tags_lib}]}, {default_libraries, [custom_tag_lib]}]}]]};
  2007. setup_compile("super_escaped") ->
  2008. {ok, [[]|[{compile_opts, [auto_escape]}]]};
  2009. setup_compile("reader_options") ->
  2010. {ok, [[]|[{compile_opts, [{reader, {?MODULE, extra_reader}}, {reader_options, [{user_id, <<"007">>}, {user_name, <<"Agent">>}]}]}]]};
  2011. setup_compile("ssi_reader_options") ->
  2012. {ok, [[]|[{compile_opts, [{reader, {?MODULE, extra_reader}}, {reader_options, [{user_id, <<"007">>}, {user_name, <<"Agent">>}]}]}]]};
  2013. %%setup_compile("path1") ->
  2014. %% {ok, [[]|[{compile_opts, [debug_compiler]}]]};
  2015. setup_compile(_) ->
  2016. {ok, [[]]}.
  2017. extra_reader(FileName, ReaderOptions) ->
  2018. UserID = proplists:get_value(user_id, ReaderOptions, <<"IDUnknown">>),
  2019. UserName = proplists:get_value(user_name, ReaderOptions, <<"NameUnknown">>),
  2020. case file:read_file(FileName) of
  2021. {ok, Data} when UserID == <<"007">>, UserName == <<"Agent">> ->
  2022. {ok, Data};
  2023. {ok, _Data} ->
  2024. {error, "Not Found"};
  2025. Err ->
  2026. Err
  2027. end.
  2028. expected(File) ->
  2029. Filename = template_file(expect, File),
  2030. case file:read_file(Filename) of
  2031. {ok, Data} -> Data;
  2032. _ -> fun (Data) ->
  2033. ok = file:write_file(Filename, Data),
  2034. io:format(
  2035. user,
  2036. "## Saved expected output for test ~p to ~p.~n"
  2037. " Verify the contents, as it is used to pass the test on subsequent test runs.~n"
  2038. "~n",
  2039. [File, Filename]),
  2040. throw({verify_new_expected_output, Filename})
  2041. end
  2042. end.
  2043. setup(#test{ title = F, output=undefined }=T) ->
  2044. {Vars, Opts, Result} =
  2045. case setup(F) of
  2046. {ok, V} -> {V, [], expected(F)};
  2047. {ok, V, O} -> {V, O, expected(F)};
  2048. {ok, V, O, skip_check} -> {V, O, fun (_) -> ok end};
  2049. {ok, V, O, R} -> {V, O, R}
  2050. end,
  2051. T#test{
  2052. render_vars = Vars,
  2053. render_opts = Opts,
  2054. output = Result
  2055. };
  2056. setup(#test{}=T) -> T;
  2057. setup("autoescape") ->
  2058. RenderVars = [{var1, "<b>bold</b>"}],
  2059. {ok, RenderVars};
  2060. setup("extends") ->
  2061. RenderVars = [{base_var, "base-barstring"}, {test_var, "test-barstring"}],
  2062. {ok, RenderVars};
  2063. setup("include_template") -> setup("extends");
  2064. setup("include_path") -> setup("extends");
  2065. setup("extends_path") -> setup("extends");
  2066. setup("extends_path2") -> setup("extends");
  2067. setup("block_super") -> setup("extends");
  2068. setup("filters") ->
  2069. RenderVars = [
  2070. {date_var1, {1975,7,24}},
  2071. {datetime_var1, {{1975,7,24}, {7,13,1}}},
  2072. {'list', ["eins", "zwei", "drei"]}
  2073. ],
  2074. {ok, RenderVars};
  2075. setup("for") ->
  2076. RenderVars = [{fruit_list, ["apple", "banana", "coconut"]}],
  2077. {ok, RenderVars};
  2078. setup("for_list") ->
  2079. RenderVars = [{fruit_list, [["apple", "apples", "$1"], ["banana", "bananas", "$2"], ["coconut", "coconuts", "$500"]]}],
  2080. {ok, RenderVars};
  2081. setup("for_tuple") ->
  2082. RenderVars = [{fruit_list, [{"apple", "apples"}, {"banana", "bananas"}, {"coconut", "coconuts"}]}],
  2083. {ok, RenderVars};
  2084. setup("for_records") ->
  2085. Link1 = [{name, "Amazon"}, {url, "http://amazon.com"}],
  2086. Link2 = [{name, "Google"}, {url, "http://google.com"}],
  2087. Link3 = [{name, "Microsoft"}, {url, "http://microsoft.com"}],
  2088. RenderVars = [{link_list, [Link1, Link2, Link3]}],
  2089. {ok, RenderVars};
  2090. setup("for_records_preset") ->
  2091. Link1b = [{name, "Canon"}, {url, "http://canon.com"}],
  2092. Link2b = [{name, "Leica"}, {url, "http://leica.com"}],
  2093. Link3b = [{name, "Nikon"}, {url, "http://nikon.com"}],
  2094. RenderVars = [{photo_links, [Link1b, Link2b, Link3b]}],
  2095. {ok, RenderVars};
  2096. setup("include") ->
  2097. RenderVars = [{var1, "foostring1"}, {var2, "foostring2"}],
  2098. {ok, RenderVars};
  2099. setup("if") ->
  2100. RenderVars = [{var1, "something"}],
  2101. {ok, RenderVars};
  2102. setup("ifequal") ->
  2103. RenderVars = [{var1, "foo"}, {var2, "foo"}, {var3, "bar"}],
  2104. {ok, RenderVars};
  2105. setup("ifequal_preset") ->
  2106. RenderVars = [{var3, "bar"}],
  2107. {ok, RenderVars};
  2108. setup("ifnotequal") ->
  2109. RenderVars = [{var1, "foo"}, {var2, "foo"}, {var3, "bar"}],
  2110. {ok, RenderVars};
  2111. setup("now") ->
  2112. {ok, [], [], skip_check};
  2113. setup("var") ->
  2114. RenderVars = [{var1, "foostring1"}, {var2, "foostring2"}, {var_not_used, "foostring3"}],
  2115. {ok, RenderVars};
  2116. setup("var_preset") ->
  2117. RenderVars = [{var1, "foostring1"}, {var2, "foostring2"}],
  2118. {ok, RenderVars};
  2119. setup("cycle") ->
  2120. RenderVars = [{test, [integer_to_list(X) || X <- lists:seq(1, 20)]},
  2121. {a, "Apple"}, {b, "Banana"}, {c, "Cherry"}],
  2122. {ok, RenderVars};
  2123. setup("trans") ->
  2124. RenderOpts = [{translation_fun, fun lists:reverse/1}],
  2125. {ok, [], RenderOpts};
  2126. setup("locale") ->
  2127. {ok, _RenderVars = [{locale, "ru"}]};
  2128. setup("custom_tag1") ->
  2129. {ok, [{a, <<"a1">>}], [{locale, ru}], <<"b1\n">>};
  2130. setup("custom_tag2") ->
  2131. {ok, [{a, <<"a1">>}], [{locale, ru}, {foo, bar}], <<"b2\n">>};
  2132. setup("custom_tag3") ->
  2133. {ok, [{a, <<"a1">>}], [{locale, ru}], <<"b3\n">>};
  2134. setup("custom_tag4") ->
  2135. {ok, [], [], <<"a\n">>};
  2136. setup("custom_tag_var") ->
  2137. {ok, [{a, <<"a1">>}], [{locale, ru}], <<"\nb1\n11\n">>};
  2138. setup("custom_tag_lib_var") ->
  2139. {ok, [{a, <<"a1">>}], [{locale, ru}], <<"\nb1\n11\n">>};
  2140. setup("ssi") ->
  2141. RenderVars = [{path, "ssi_include.html"}],
  2142. {ok, RenderVars};
  2143. setup("wrapper") ->
  2144. RenderVars = [{types, ["b", "a", "c"]}],
  2145. {ok, RenderVars};
  2146. setup("reader_options") ->
  2147. RenderVars = [{base_var, "base-barstring"}, {test_var, "test-barstring"}],
  2148. % Options = [],%[{compile_opts, [{reader, {?MODULE, extra_reader}}, {reader_options, [{user_id, <<"007">>}, {user_name, <<"Agent">>}]}]}],
  2149. {ok, RenderVars};
  2150. setup("ssi_reader_options") ->
  2151. RenderVars = [{path, "ssi_include.html"}],
  2152. {ok, RenderVars};
  2153. %%--------------------------------------------------------------------
  2154. %% Custom tags
  2155. %%--------------------------------------------------------------------
  2156. setup("custom_call") ->
  2157. RenderVars = [{var1, "something"}],
  2158. {ok, RenderVars};
  2159. setup(_) ->
  2160. {ok, []}.