erlydtl_test_defs.erl 98 KB

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