erlydtl_test_defs.erl 88 KB

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