erlydtl_unittests.erl 80 KB

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