erlydtl_test_defs.erl 90 KB

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