erlydtl_test_defs.erl 89 KB

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