erlydtl_test_defs.erl 88 KB

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