erlydtl_test_defs.erl 98 KB

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