erlydtl_test_defs.erl 75 KB

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