erlydtl_unittests.erl 81 KB

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