erlydtl_unittests.erl 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. -module(erlydtl_unittests).
  2. -export([run_tests/0]).
  3. tests() ->
  4. [
  5. {"vars", [
  6. {"string",
  7. <<"String value is: {{ var1 }}">>,
  8. [{var1, "foo"}], <<"String value is: foo">>},
  9. {"int",
  10. <<"The magic number is: {{ var1 }}">>,
  11. [{var1, 42}], <<"The magic number is: 42">>},
  12. {"float",
  13. <<"The price of milk is: {{ var1 }}">>,
  14. [{var1, 0.42}], <<"The price of milk is: 0.42">>},
  15. {"No spaces",
  16. <<"{{var1}}">>,
  17. [{var1, "foo"}], <<"foo">>}
  18. ]},
  19. {"comment", [
  20. {"comment block is excised",
  21. <<"bob {% comment %}(moron){% endcomment %} loblaw">>,
  22. [], <<"bob loblaw">>},
  23. {"inline comment is excised",
  24. <<"you're {# not #} a very nice person">>,
  25. [], <<"you're a very nice person">>}
  26. ]},
  27. {"autoescape", [
  28. {"Autoescape works",
  29. <<"{% autoescape on %}{{ var1 }}{% endautoescape %}">>,
  30. [{var1, "<b>bold</b>"}], <<"&lt;b&gt;bold&lt;/b&gt;">>},
  31. {"Nested autoescape",
  32. <<"{% autoescape on %}{{ var1 }}{% autoescape off %}{{ var1 }}{% endautoescape %}{% endautoescape %}">>,
  33. [{var1, "<b>"}], <<"&lt;b&gt;<b>">>}
  34. ]},
  35. {"string literal", [
  36. {"Render literal",
  37. <<"{{ \"foo\" }} is my name">>, [], <<"foo is my name">>},
  38. {"Newlines are escaped",
  39. <<"{{ \"foo\\n\" }}">>, [], <<"foo\n">>}
  40. ]},
  41. {"cycle", [
  42. {"Cycling through quoted strings",
  43. <<"{% for i in test %}{% cycle 'a' 'b' %}{{ i }},{% endfor %}">>,
  44. [{test, ["0", "1", "2", "3", "4"]}], <<"a0,b1,a2,b3,a4,">>},
  45. {"Cycling through normal variables",
  46. <<"{% for i in test %}{% cycle aye bee %}{{ i }},{% endfor %}">>,
  47. [{test, ["0", "1", "2", "3", "4"]}, {aye, "a"}, {bee, "b"}],
  48. <<"a0,b1,a2,b3,a4,">>}
  49. ]},
  50. {"number literal", [
  51. {"Render integer",
  52. <<"{{ 5 }}">>, [], <<"5">>}
  53. ]},
  54. {"variable", [
  55. {"Render variable",
  56. <<"{{ var1 }} is my game">>, [{var1, "bar"}], <<"bar is my game">>},
  57. {"Render variable with attribute",
  58. <<"I enjoy {{ var1.game }}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
  59. {"Render variable with string-key attribute",
  60. <<"I also enjoy {{ var1.game }}">>, [{var1, [{"game", "Parcheesi"}]}], <<"I also enjoy Parcheesi">>},
  61. {"Render variable with binary-key attribute",
  62. <<"I also enjoy {{ var1.game }}">>, [{var1, [{<<"game">>, "Parcheesi"}]}], <<"I also enjoy Parcheesi">>},
  63. {"Render variable in dict",
  64. <<"{{ var1 }}">>, dict:store(var1, "bar", dict:new()), <<"bar">>},
  65. {"Render variable in gb_tree",
  66. <<"{{ var1 }}">>, gb_trees:insert(var1, "bar", gb_trees:empty()), <<"bar">>},
  67. {"Render variable in arity-1 func",
  68. <<"I enjoy {{ var1 }}">>, fun (var1) -> "Othello" end, <<"I enjoy Othello">>},
  69. {"Render variable with attribute in dict",
  70. <<"{{ var1.attr }}">>, [{var1, dict:store(attr, "Othello", dict:new())}], <<"Othello">>},
  71. {"Render variable with attribute in gb_tree",
  72. <<"{{ var1.attr }}">>, [{var1, gb_trees:insert(attr, "Othello", gb_trees:empty())}], <<"Othello">>},
  73. {"Render variable with attribute in arity-1 func",
  74. <<"I enjoy {{ var1.game }}">>, [{var1, fun (game) -> "Othello" end}], <<"I enjoy Othello">>},
  75. {"Render variable in parameterized module",
  76. <<"{{ var1.some_var }}">>, [{var1, erlydtl_example_variable_storage:new("foo")}], <<"foo">>},
  77. {"Nested attributes",
  78. <<"{{ person.city.state.country }}">>, [{person, [{city, [{state, [{country, "Italy"}]}]}]}],
  79. <<"Italy">>}
  80. ]},
  81. {"now", [
  82. {"now functional",
  83. <<"It is the {% now \"jS o\\f F Y\" %}.">>, [{var1, ""}], generate_test_date()}
  84. ]},
  85. {"if", [
  86. {"If/else",
  87. <<"{% if var1 %}boo{% else %}yay{% endif %}">>, [{var1, ""}], <<"yay">>},
  88. {"If",
  89. <<"{% if var1 %}boo{% endif %}">>, [{var1, ""}], <<>>},
  90. {"If not",
  91. <<"{% if not var1 %}yay{% endif %}">>, [{var1, ""}], <<"yay">>},
  92. {"If \"0\"",
  93. <<"{% if var1 %}boo{% endif %}">>, [{var1, "0"}], <<>>},
  94. {"If false",
  95. <<"{% if var1 %}boo{% endif %}">>, [{var1, false}], <<>>},
  96. {"If false string",
  97. <<"{% if var1 %}boo{% endif %}">>, [{var1, "false"}], <<"boo">>},
  98. {"If undefined",
  99. <<"{% if var1 %}boo{% endif %}">>, [{var1, undefined}], <<>>},
  100. {"If other atom",
  101. <<"{% if var1 %}yay{% endif %}">>, [{var1, foobar}], <<"yay">>},
  102. {"If non-empty string",
  103. <<"{% if var1 %}yay{% endif %}">>, [{var1, "hello"}], <<"yay">>},
  104. {"If proplist",
  105. <<"{% if var1 %}yay{% endif %}">>, [{var1, [{foo, "bar"}]}], <<"yay">>},
  106. {"If complex",
  107. <<"{% if foo.bar.baz %}omgwtfbbq{% endif %}">>, [], <<"">>}
  108. ]},
  109. {"if .. in ..", [
  110. {"If substring in string",
  111. <<"{% if var1 in var2 %}yay{% endif %}">>, [{var1, "rook"}, {var2, "Crooks"}], <<"yay">>},
  112. {"If substring in string (false)",
  113. <<"{% if var1 in var2 %}boo{% endif %}">>, [{var1, "Cook"}, {var2, "Crooks"}], <<>>},
  114. {"If substring not in string",
  115. <<"{% if var1 not in var2 %}yay{% endif %}">>, [{var1, "Cook"}, {var2, "Crooks"}], <<"yay">>},
  116. {"If substring not in string (false)",
  117. <<"{% if var1 not in var2 %}boo{% endif %}">>, [{var1, "rook"}, {var2, "Crooks"}], <<>>},
  118. {"If literal substring in string",
  119. <<"{% if \"man\" in \"Ottoman\" %}yay{% endif %}">>, [], <<"yay">>},
  120. {"If literal substring in string (false)",
  121. <<"{% if \"woman\" in \"Ottoman\" %}boo{% endif %}">>, [], <<>>},
  122. {"If element in list",
  123. <<"{% if var1 in var2 %}yay{% endif %}">>, [{var1, "foo"}, {var2, ["bar", "foo", "baz"]}], <<"yay">>},
  124. {"If element in list (false)",
  125. <<"{% if var1 in var2 %}boo{% endif %}">>, [{var1, "FOO"}, {var2, ["bar", "foo", "baz"]}], <<>>}
  126. ]},
  127. {"if .. and ..", [
  128. {"If true and true",
  129. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, true}, {var2, true}], <<"yay">>},
  130. {"If true and false",
  131. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, true}, {var2, false}], <<"">>},
  132. {"If false and true",
  133. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, false}, {var2, true}], <<"">>},
  134. {"If false and false ",
  135. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, false}, {var2, false}], <<"">>}
  136. ]},
  137. {"if .. or ..", [
  138. {"If true or true",
  139. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, true}, {var2, true}], <<"yay">>},
  140. {"If true or false",
  141. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, true}, {var2, false}], <<"yay">>},
  142. {"If false or true",
  143. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, false}, {var2, true}], <<"yay">>},
  144. {"If false or false ",
  145. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, false}, {var2, false}], <<"">>}
  146. ]},
  147. {"if equality", [
  148. {"If int equals number literal",
  149. <<"{% if var1 == 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
  150. {"If int equals number literal (false)",
  151. <<"{% if var1 == 2 %}yay{% endif %}">>, [{var1, 3}], <<"">>},
  152. {"If string equals string literal",
  153. <<"{% if var1 == \"2\" %}yay{% endif %}">>, [{var1, "2"}], <<"yay">>},
  154. {"If string equals string literal (false)",
  155. <<"{% if var1 == \"2\" %}yay{% endif %}">>, [{var1, "3"}], <<"">>},
  156. {"If int not equals number literal",
  157. <<"{% if var1 != 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
  158. {"If string not equals string literal",
  159. <<"{% if var1 != \"2\" %}yay{% endif %}">>, [{var1, "3"}], <<"yay">>},
  160. {"If filter result equals number literal",
  161. <<"{% if var1|length == 2 %}yay{% endif %}">>, [{var1, ["fo", "bo"]}], <<"yay">>},
  162. {"If filter result equals string literal",
  163. <<"{% if var1|capfirst == \"Foo\" %}yay{% endif %}">>, [{var1, "foo"}], <<"yay">>}
  164. ]},
  165. {"if size comparison", [
  166. {"If int greater than number literal",
  167. <<"{% if var1 > 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
  168. {"If int greater than number literal (false)",
  169. <<"{% if var1 > 2 %}yay{% endif %}">>, [{var1, 2}], <<"">>},
  170. {"If int greater than or equal to number literal",
  171. <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
  172. {"If int greater than or equal to number literal (2)",
  173. <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
  174. {"If int greater than or equal to number literal (false)",
  175. <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 1}], <<"">>},
  176. {"If int less than number literal",
  177. <<"{% if var1 < 2 %}yay{% endif %}">>, [{var1, 1}], <<"yay">>},
  178. {"If int less than number literal (false)",
  179. <<"{% if var1 < 2 %}yay{% endif %}">>, [{var1, 2}], <<"">>},
  180. {"If int less than or equal to number literal",
  181. <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 1}], <<"yay">>},
  182. {"If int less than or equal to number literal",
  183. <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
  184. {"If int less than or equal to number literal (false)",
  185. <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 3}], <<"">>}
  186. ]},
  187. {"if complex bool", [
  188. {"If (true or false) and true",
  189. <<"{% if (var1 or var2) and var3 %}yay{% endif %}">>,
  190. [{var1, true}, {var2, false}, {var3, true}], <<"yay">>},
  191. {"If true or (false and true)",
  192. <<"{% if var1 or (var2 and var3) %}yay{% endif %}">>,
  193. [{var1, true}, {var2, false}, {var3, true}], <<"yay">>}
  194. ]},
  195. {"for", [
  196. {"Simple loop",
  197. <<"{% for x in list %}{{ x }}{% endfor %}">>, [{'list', ["1", "2", "3"]}],
  198. <<"123">>},
  199. {"Expand list",
  200. <<"{% for x, y in list %}{{ x }},{{ y }}\n{% endfor %}">>, [{'list', [["X", "1"], ["X", "2"]]}],
  201. <<"X,1\nX,2\n">>},
  202. {"Expand tuple",
  203. <<"{% for x, y in list %}{{ x }},{{ y }}\n{% endfor %}">>, [{'list', [{"X", "1"}, {"X", "2"}]}],
  204. <<"X,1\nX,2\n">>},
  205. {"Resolve variable attribute",
  206. <<"{% for number in person.numbers %}{{ number }}\n{% endfor %}">>, [{person, [{numbers, ["411", "911"]}]}],
  207. <<"411\n911\n">>},
  208. {"Resolve nested variable attribute",
  209. <<"{% for number in person.home.numbers %}{{ number }}\n{% endfor %}">>, [{person, [{home, [{numbers, ["411", "911"]}]}]}],
  210. <<"411\n911\n">>},
  211. {"Counter0",
  212. <<"{% for number in numbers %}{{ forloop.counter0 }}. {{ number }}\n{% endfor %}">>,
  213. [{numbers, ["Zero", "One", "Two"]}], <<"0. Zero\n1. One\n2. Two\n">>},
  214. {"Counter",
  215. <<"{% for number in numbers %}{{ forloop.counter }}. {{ number }}\n{% endfor %}">>,
  216. [{numbers, ["One", "Two", "Three"]}], <<"1. One\n2. Two\n3. Three\n">>},
  217. {"Reverse Counter0",
  218. <<"{% for number in numbers %}{{ forloop.revcounter0 }}. {{ number }}\n{% endfor %}">>,
  219. [{numbers, ["Two", "One", "Zero"]}], <<"2. Two\n1. One\n0. Zero\n">>},
  220. {"Reverse Counter",
  221. <<"{% for number in numbers %}{{ forloop.revcounter }}. {{ number }}\n{% endfor %}">>,
  222. [{numbers, ["Three", "Two", "One"]}], <<"3. Three\n2. Two\n1. One\n">>},
  223. {"Counter \"first\"",
  224. <<"{% for number in numbers %}{% if forloop.first %}{{ number }}{% endif %}{% endfor %}">>,
  225. [{numbers, ["One", "Two", "Three"]}], <<"One">>},
  226. {"Counter \"last\"",
  227. <<"{% for number in numbers %}{% if forloop.last %}{{ number }}{% endif %}{% endfor %}">>,
  228. [{numbers, ["One", "Two", "Three"]}], <<"Three">>},
  229. {"Nested for loop",
  230. <<"{% for outer in list %}{% for inner in outer %}{{ inner }}\n{% endfor %}{% endfor %}">>,
  231. [{'list', [["Al", "Albert"], ["Jo", "Joseph"]]}],
  232. <<"Al\nAlbert\nJo\nJoseph\n">>},
  233. {"Access parent loop counters",
  234. <<"{% for outer in list %}{% for inner in outer %}({{ forloop.parentloop.counter0 }}, {{ forloop.counter0 }})\n{% endfor %}{% endfor %}">>,
  235. [{'list', [["One", "two"], ["One", "two"]]}], <<"(0, 0)\n(0, 1)\n(1, 0)\n(1, 1)\n">>},
  236. {"If changed",
  237. <<"{% for x in list %}{% ifchanged %}{{ x }}\n{% endifchanged %}{% endfor %}">>,
  238. [{'list', ["one", "two", "two", "three", "three", "three"]}], <<"one\ntwo\nthree\n">>},
  239. {"If changed/2",
  240. <<"{% for x, y in list %}{% ifchanged %}{{ x|upper }}{% endifchanged %}{% ifchanged %}{{ y|lower }}{% endifchanged %}\n{% endfor %}">>,
  241. [{'list', [["one", "a"], ["two", "A"], ["two", "B"], ["three", "b"], ["three", "c"], ["Three", "b"]]}], <<"ONEa\nTWO\nb\nTHREE\nc\nb\n">>},
  242. {"If changed/else",
  243. <<"{% for x in list %}{% ifchanged %}{{ x }}\n{% else %}foo\n{% endifchanged %}{% endfor %}">>,
  244. [{'list', ["one", "two", "two", "three", "three", "three"]}], <<"one\ntwo\nfoo\nthree\nfoo\nfoo\n">>},
  245. {"If changed/param",
  246. <<"{% for date in list %}{% ifchanged date.month %} {{ date.month }}:{{ date.day }}{% else %},{{ date.day }}{% endifchanged %}{% endfor %}\n">>,
  247. [{'list', [[{month,"Jan"},{day,1}],[{month,"Jan"},{day,2}],[{month,"Apr"},{day,10}],
  248. [{month,"Apr"},{day,11}],[{month,"May"},{day,4}]]}],
  249. <<" Jan:1,2 Apr:10,11 May:4\n">>},
  250. {"If changed/param2",
  251. <<"{% for x, y in list %}{% ifchanged y|upper %}{{ x|upper }}{% endifchanged %}\n{% endfor %}">>,
  252. [{'list', [["one", "a"], ["two", "A"], ["two", "B"], ["three", "b"], ["three", "c"], ["Three", "b"]]}], <<"ONE\n\nTWO\n\nTHREE\nTHREE\n">>},
  253. {"If changed/param2 combined",
  254. <<"{% for x, y in list %}{% ifchanged x y|upper %}{{ x }}{% endifchanged %}\n{% endfor %}">>,
  255. [{'list', [["one", "a"], ["two", "A"], ["two", "B"], ["three", "b"], ["three", "B"], ["three", "c"]]}], <<"one\ntwo\ntwo\nthree\n\nthree\n">>},
  256. {"If changed/resolve",
  257. <<"{% for x in list %}{% ifchanged x.name|first %}{{ x.value }}{% endifchanged %}\n{% endfor %}">>,
  258. [{'list', [[{"name", ["nA","nB"]},{"value","1"}],[{"name", ["nA","nC"]},{"value","2"}],
  259. [{"name", ["nB","nC"]},{"value","3"}],[{"name", ["nB","nA"]},{"value","4"}]]}],
  260. <<"1\n\n3\n\n">>}
  261. ]},
  262. {"for/empty", [
  263. {"Simple loop",
  264. <<"{% for x in list %}{{ x }}{% empty %}shucks{% endfor %}">>, [{'list', ["1", "2", "3"]}],
  265. <<"123">>},
  266. {"Simple loop (empty)",
  267. <<"{% for x in list %}{{ x }}{% empty %}shucks{% endfor %}">>, [{'list', []}],
  268. <<"shucks">>}
  269. ]},
  270. {"ifequal", [
  271. {"Compare variable to literal",
  272. <<"{% ifequal var1 \"foo\" %}yay{% endifequal %}">>,
  273. [{var1, "foo"}], <<"yay">>},
  274. {"Compare variable to unequal literal",
  275. <<"{% ifequal var1 \"foo\" %}boo{% endifequal %}">>,
  276. [{var1, "bar"}], <<>>},
  277. {"Compare literal to variable",
  278. <<"{% ifequal \"foo\" var1 %}yay{% endifequal %}">>,
  279. [{var1, "foo"}], <<"yay">>},
  280. {"Compare literal to unequal variable",
  281. <<"{% ifequal \"foo\" var1 %}boo{% endifequal %}">>,
  282. [{var1, "bar"}], <<>>},
  283. {"Compare variable to literal (int string)",
  284. <<"{% ifequal var1 \"2\" %}yay{% else %}boo{% endifequal %}">>,
  285. [{var1, "2"}], <<"yay">>},
  286. {"Compare variable to literal (int)",
  287. <<"{% ifequal var1 2 %}yay{% else %}boo{% endifequal %}">>,
  288. [{var1, 2}], <<"yay">>},
  289. {"Compare variable to unequal literal (int)",
  290. <<"{% ifequal var1 2 %}boo{% else %}yay{% endifequal %}">>,
  291. [{var1, 3}], <<"yay">>},
  292. {"Compare variable to equal literal (atom)",
  293. <<"{% ifequal var1 \"foo\"%}yay{% endifequal %}">>,
  294. [{var1, foo}], <<"yay">>},
  295. {"Compare variable to unequal literal (atom)",
  296. <<"{% ifequal var1 \"foo\"%}yay{% else %}boo{% endifequal %}">>,
  297. [{var1, bar}], <<"boo">>}
  298. ]},
  299. {"ifequal/else", [
  300. {"Compare variable to literal",
  301. <<"{% ifequal var1 \"foo\" %}yay{% else %}boo{% endifequal %}">>,
  302. [{var1, "foo"}], <<"yay">>},
  303. {"Compare variable to unequal literal",
  304. <<"{% ifequal var1 \"foo\" %}boo{% else %}yay{% endifequal %}">>,
  305. [{var1, "bar"}], <<"yay">>},
  306. {"Compare literal to variable",
  307. <<"{% ifequal \"foo\" var1 %}yay{% else %}boo{% endifequal %}">>,
  308. [{var1, "foo"}], <<"yay">>},
  309. {"Compare literal to unequal variable",
  310. <<"{% ifequal \"foo\" var1 %}boo{% else %}yay{% endifequal %}">>,
  311. [{var1, "bar"}], <<"yay">>}
  312. ]},
  313. {"ifnotequal", [
  314. {"Compare variable to literal",
  315. <<"{% ifnotequal var1 \"foo\" %}boo{% endifnotequal %}">>,
  316. [{var1, "foo"}], <<>>},
  317. {"Compare variable to unequal literal",
  318. <<"{% ifnotequal var1 \"foo\" %}yay{% endifnotequal %}">>,
  319. [{var1, "bar"}], <<"yay">>},
  320. {"Compare literal to variable",
  321. <<"{% ifnotequal \"foo\" var1 %}boo{% endifnotequal %}">>,
  322. [{var1, "foo"}], <<>>},
  323. {"Compare literal to unequal variable",
  324. <<"{% ifnotequal \"foo\" var1 %}yay{% endifnotequal %}">>,
  325. [{var1, "bar"}], <<"yay">>}
  326. ]},
  327. {"ifnotequal/else", [
  328. {"Compare variable to literal",
  329. <<"{% ifnotequal var1 \"foo\" %}boo{% else %}yay{% endifnotequal %}">>,
  330. [{var1, "foo"}], <<"yay">>},
  331. {"Compare variable to unequal literal",
  332. <<"{% ifnotequal var1 \"foo\" %}yay{% else %}boo{% endifnotequal %}">>,
  333. [{var1, "bar"}], <<"yay">>},
  334. {"Compare literal to variable",
  335. <<"{% ifnotequal \"foo\" var1 %}boo{% else %}yay{% endifnotequal %}">>,
  336. [{var1, "foo"}], <<"yay">>},
  337. {"Compare literal to unequal variable",
  338. <<"{% ifnotequal \"foo\" var1 %}yay{% else %}boo{% endifnotequal %}">>,
  339. [{var1, "bar"}], <<"yay">>}
  340. ]},
  341. {"filter tag", [
  342. {"Apply a filter",
  343. <<"{% filter escape %}&{% endfilter %}">>, [], <<"&amp;">>},
  344. {"Chained filters",
  345. <<"{% filter linebreaksbr|escape %}\n{% endfilter %}">>, [], <<"&lt;br /&gt;">>}
  346. ]},
  347. {"filters", [
  348. {"Filter a literal",
  349. <<"{{ \"pop\"|capfirst }}">>, [],
  350. <<"Pop">>},
  351. {"Filters applied in order",
  352. <<"{{ var1|force_escape|length }}">>, [{var1, <<"&">>}],
  353. <<"5">>},
  354. {"Escape is applied last",
  355. <<"{{ var1|escape|linebreaksbr }}">>, [{var1, <<"\n">>}],
  356. <<"&lt;br /&gt;">>},
  357. {"|add:4",
  358. <<"{{ one|add:4 }}">>, [{one, "1"}],
  359. <<"5">>},
  360. {"|addslashes",
  361. <<"{{ var1|addslashes }}">>, [{var1, "Jimmy's \"great\" meats'n'things"}],
  362. <<"Jimmy\\'s \\\"great\\\" meats\\'n\\'things">>},
  363. {"|capfirst",
  364. <<"{{ var1|capfirst }}">>, [{var1, "dana boyd"}],
  365. <<"Dana boyd">>},
  366. {"|center:10",
  367. <<"{{ var1|center:10 }}">>, [{var1, "MB"}],
  368. <<" MB ">>},
  369. {"|center:1",
  370. <<"{{ var1|center:1 }}">>, [{var1, "KBR"}],
  371. <<"B">>},
  372. {"|cut:\" \"",
  373. <<"{{ var1|cut:\" \" }}">>, [{var1, "String with spaces"}],
  374. <<"Stringwithspaces">>},
  375. {"|date 1",
  376. <<"{{ var1|date:\"jS F Y H:i\" }}">>,
  377. [{var1, {1975,7,24}}],
  378. <<"24th July 1975 00:00">>},
  379. {"|date 2",
  380. <<"{{ var1|date:\"jS F Y H:i\" }}">>,
  381. [{var1, {{1975,7,24}, {7,13,1}}}],
  382. <<"24th July 1975 07:13">>},
  383. {"|date 3",
  384. <<"{{ var1|date }}">>,
  385. [{var1, {{1975,7,24}, {7,13,1}}}],
  386. <<"July 24, 1975">>},
  387. {"|default:\"foo\" 1",
  388. <<"{{ var1|default:\"foo\" }}">>, [], <<"foo">>},
  389. {"|default:\"foo\" 2",
  390. <<"{{ var1|default:\"foo\" }}">>, [{var1, "bar"}], <<"bar">>},
  391. {"|default:\"foo\" 3",
  392. <<"{{ var1|default:\"foo\" }}">>, [{var1, "0"}], <<"foo">>},
  393. {"|default_if_none:\"foo\"",
  394. <<"{{ var1|default_if_none:\"foo\" }}">>, [], <<"foo">>},
  395. {"|default_if_none:\"foo\" 2",
  396. <<"{{ var1|default_if_none:\"foo\" }}">>, [{var1, "bar"}], <<"bar">>},
  397. {"|divisibleby:\"3\"",
  398. <<"{% if var1|divisibleby:\"3\" %}yay{% endif %}">>, [{var1, 21}], <<"yay">>},
  399. {"|divisibleby:\"3\"",
  400. <<"{% if var1|divisibleby:\"3\" %}yay{% endif %}">>, [{var1, 22}], <<"">>},
  401. {"|escape",
  402. <<"{% autoescape on %}{{ var1|escape|escape|escape }}{% endautoescape %}">>, [{var1, ">&1"}], <<"&gt;&amp;1">>},
  403. {"|escapejs",
  404. <<"{{ var1|escapejs }}">>, [{var1, "testing\r\njavascript 'string\" <b>escaping</b>"}],
  405. <<"testing\\u000D\\u000Ajavascript \\u0027string\\u0022 \\u003Cb\\u003Eescaping\\u003C/b\\u003E">>},
  406. {"|filesizeformat (bytes)",
  407. <<"{{ var1|filesizeformat }}">>, [{var1, 1023}], <<"1023 bytes">>},
  408. {"|filesizeformat (KB)",
  409. <<"{{ var1|filesizeformat }}">>, [{var1, 3487}], <<"3.4 KB">>},
  410. {"|filesizeformat (MB)",
  411. <<"{{ var1|filesizeformat }}">>, [{var1, 6277098}], <<"6.0 MB">>},
  412. {"|filesizeformat (GB)",
  413. <<"{{ var1|filesizeformat }}">>, [{var1, 1024 * 1024 * 1024}], <<"1.0 GB">>},
  414. {"|first",
  415. <<"{{ var1|first }}">>, [{var1, "James"}],
  416. <<"J">>},
  417. {"|fix_ampersands",
  418. <<"{{ var1|fix_ampersands }}">>, [{var1, "Ben & Jerry's"}],
  419. <<"Ben &amp; Jerry's">>},
  420. {"|floatformat:\"-1\"",
  421. <<"{{ var1|floatformat:\"-1\" }}">>, [{var1, 34.23234}],
  422. <<"34.2">>},
  423. %% ?assertEqual( "", erlydtl_filters:floatformat(,)),
  424. %% ?assertEqual( "34", erlydtl_filters:floatformat(34.00000,-1)),
  425. %% ?assertEqual( "34.3", erlydtl_filters:floatformat(34.26000,-1)),
  426. %% ?assertEqual( "34.232", erlydtl_filters:floatformat(34.23234,3)),
  427. %% ?assertEqual( "34.000", erlydtl_filters:floatformat(34.00000,3)),
  428. %% ?assertEqual( "34.260", erlydtl_filters:floatformat(34.26000,3)),
  429. %% ?assertEqual( "34.232", erlydtl_filters:floatformat(34.23234,-3)),
  430. %% ?assertEqual( "34", erlydtl_filters:floatformat(34.00000,-3)),
  431. %% ?assertEqual( "34.260", erlydtl_filters:floatformat(34.26000,-3)).
  432. {"|force_escape",
  433. <<"{{ var1|force_escape }}">>, [{var1, "Ben & Jerry's <=> \"The World's Best Ice Cream\""}],
  434. <<"Ben &amp; Jerry&#039;s &lt;=&gt; &quot;The World&#039;s Best Ice Cream&quot;">>},
  435. {"|format_integer",
  436. <<"{{ var1|format_integer }}">>, [{var1, 28}], <<"28">>},
  437. {"|format_number 1",
  438. <<"{{ var1|format_number }}">>, [{var1, 28}], <<"28">>},
  439. {"|format_number 2",
  440. <<"{{ var1|format_number }}">>, [{var1, 23.77}], <<"23.77">>},
  441. {"|format_number 3",
  442. <<"{{ var1|format_number }}">>, [{var1, "28.77"}], <<"28.77">>},
  443. {"|format_number 4",
  444. <<"{{ var1|format_number }}">>, [{var1, "23.77"}], <<"23.77">>},
  445. {"|format_number 5",
  446. <<"{{ var1|format_number }}">>, [{var1, fun() -> 29 end}], <<"29">>},
  447. {"|format_number 6",
  448. <<"{{ var1|format_number }}">>, [{var1, fun() -> fun() -> 31 end end}], <<"31">>},
  449. {"|get_digit:\"2\"",
  450. <<"{{ var1|get_digit:\"2\" }}">>, [{var1, 42}], <<"4">>},
  451. {"|iriencode",
  452. <<"{{ url|iriencode }}">>, [{url, "You #$*@!!"}], <<"You+#$*@!!">>},
  453. {"|join:\", \" (list)",
  454. <<"{{ var1|join:\", \" }}">>, [{var1, ["Liberte", "Egalite", "Fraternite"]}],
  455. <<"Liberte, Egalite, Fraternite">>},
  456. {"|join:\", \" (binary)",
  457. <<"{{ var1|join:\", \" }}">>, [{var1, [<<"Liberte">>, "Egalite", <<"Fraternite">>]}],
  458. <<"Liberte, Egalite, Fraternite">>},
  459. {"|last",
  460. <<"{{ var1|last }}">>, [{var1, "XYZ"}],
  461. <<"Z">>},
  462. {"|length",
  463. <<"{{ var1|length }}">>, [{var1, "antidisestablishmentarianism"}],
  464. <<"28">>},
  465. {"|linebreaks",
  466. <<"{{ var1|linebreaks }}">>, [{var1, "Joel\nis a slug"}],
  467. <<"<p>Joel<br />is a slug</p>">>},
  468. {"|linebreaks",
  469. <<"{{ var1|linebreaks }}">>, [{var1, "Joel\n\n\n\nis a slug"}],
  470. <<"<p>Joel</p><p>is a slug</p>">>},
  471. {"|linebreaks",
  472. <<"{{ var1|linebreaks }}">>, [{var1, "Joel\n\nis a \nslug"}],
  473. <<"<p>Joel</p><p>is a <br />slug</p>">>},
  474. {"|linebreaksbr",
  475. <<"{{ var1|linebreaksbr }}">>, [{var1, "One\nTwo\n\nThree\n\n\n"}],
  476. <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
  477. {"|linebreaksbr",
  478. <<"{{ \"One\\nTwo\\n\\nThree\\n\\n\\n\"|linebreaksbr }}">>, [],
  479. <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
  480. {"|linenumbers",
  481. <<"{{ var1|linenumbers }}">>, [{var1, "a\nb\nc"}],
  482. <<"1. a\n2. b\n3. c">>},
  483. {"|linenumbers",
  484. <<"{{ var1|linenumbers }}">>, [{var1, "a"}],
  485. <<"1. a">>},
  486. {"|linenumbers",
  487. <<"{{ var1|linenumbers }}">>, [{var1, "a\n"}],
  488. <<"1. a\n2. ">>},
  489. {"|ljust:10",
  490. <<"{{ var1|ljust:10 }}">>, [{var1, "Gore"}],
  491. <<"Gore ">>},
  492. {"|lower",
  493. <<"{{ var1|lower }}">>, [{var1, "E. E. Cummings"}],
  494. <<"e. e. cummings">>},
  495. {"|makelist",
  496. <<"{{ list|make_list }}">>, [{list, "Joel"}],
  497. <<"J","o","e","l">>},
  498. {"|pluralize",
  499. <<"{{ num|pluralize }}">>, [{num, 1}],
  500. <<"">>},
  501. {"|pluralize",
  502. <<"{{ num|pluralize }}">>, [{num, 2}],
  503. <<"s">>},
  504. {"|pluralize:\"s\"",
  505. <<"{{ num|pluralize }}">>, [{num, 1}],
  506. <<"">>},
  507. {"|pluralize:\"s\"",
  508. <<"{{ num|pluralize }}">>, [{num, 2}],
  509. <<"s">>},
  510. {"|pluralize:\"y,es\" (list)",
  511. <<"{{ num|pluralize:\"y,es\" }}">>, [{num, 1}],
  512. <<"y">>},
  513. {"|pluralize:\"y,es\" (list)",
  514. <<"{{ num|pluralize:\"y,es\" }}">>, [{num, 2}],
  515. <<"es">>},
  516. {"|random",
  517. <<"{{ var1|random }}">>, [{var1, ["foo", "foo", "foo"]}],
  518. <<"foo">>},
  519. {"|removetags:\"b span\"",
  520. <<"{{ var1|removetags:\"b span\" }}">>, [{var1, "<B>Joel</B> <button>is</button> a <span>slug</span>"}],
  521. <<"<B>Joel</B> <button>is</button> a slug">>},
  522. {"|rjust:10",
  523. <<"{{ var1|rjust:10 }}">>, [{var1, "Bush"}],
  524. <<" Bush">>},
  525. {"|safe",
  526. <<"{% autoescape on %}{{ var1|safe|escape }}{% endautoescape %}">>, [{var1, "&"}],
  527. <<"&">>},
  528. %%python/django slice is zero based, erlang lists are 1 based
  529. %%first number included, second number not
  530. %%negative numbers are allowed
  531. %%regex to convert from erlydtl_filters_tests:
  532. % for slice: \?assert.*\( \[(.*)\], erlydtl_filters:(.*)\((.*),"(.*)"\)\),
  533. % {"|slice:\"$4\"", <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],<<$1>>},
  534. % \t\t{"|slice:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>},
  535. %
  536. % for stringformat:
  537. % \?assert.*\( (.*), erlydtl_filters:(.*)\((.*), "(.*)"\) \)
  538. % \t\t{"|stringformat:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>}
  539. {"|slice:\":\"",
  540. <<"{{ var|slice:\":\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  541. <<1,2,3,4,5,6,7,8,9>>},
  542. {"|slice:\"1\"",
  543. <<"{{ var|slice:\"1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  544. <<"2">>},
  545. {"|slice:\"100\"",
  546. <<"{{ var|slice:\"100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  547. <<"indexError">>},
  548. {"|slice:\"-1\"",
  549. <<"{{ var|slice:\"-1\" }}">>, [{var, ["a","b","c","d","e","f","g","h","i"]}],
  550. <<"i">>},
  551. {"|slice:\"-1\"",
  552. <<"{{ var|slice:\"-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  553. <<"9">>},
  554. {"|slice:\"-100\"",
  555. <<"{{ var|slice:\"-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  556. <<"indexError">>},
  557. {"|slice:\"1:\"",
  558. <<"{{ var|slice:\"1:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  559. <<2,3,4,5,6,7,8,9>>},
  560. {"|slice:\"100:\"",
  561. <<"{{ var|slice:\"100:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  562. <<>>},
  563. {"|slice:\"-1:\"",
  564. <<"{{ var|slice:\"-1:\" }}">>, [{var, ["a","b","c","d","e","f","h","i","j"]}],
  565. <<"j">>},
  566. {"|slice:\"-1:\"",
  567. <<"{{ var|slice:\"-1:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  568. <<9>>},
  569. {"|slice:\"-100:\"",
  570. <<"{{ var|slice:\"-100:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  571. <<1,2,3,4,5,6,7,8,9>>},
  572. {"|slice:\":1\"",
  573. <<"{{ var|slice:\":1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  574. <<1>>},
  575. {"|slice:\":100\"",
  576. <<"{{ var|slice:\":100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  577. <<1,2,3,4,5,6,7,8,9>>},
  578. {"|slice:\":-1\"",
  579. <<"{{ var|slice:\":-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  580. <<1,2,3,4,5,6,7,8>>},
  581. {"|slice:\":-100\"",
  582. <<"{{ var|slice:\":-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  583. <<>>},
  584. {"|slice:\"-1:-1\"",
  585. <<"{{ var|slice:\"-1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  586. <<>>},
  587. {"|slice:\"1:1\"",
  588. <<"{{ var|slice:\"1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  589. <<>>},
  590. {"|slice:\"1:-1\"",
  591. <<"{{ var|slice:\"1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  592. <<2,3,4,5,6,7,8>>},
  593. {"|slice:\"-1:1\"",
  594. <<"{{ var|slice:\"-1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  595. <<>>},
  596. {"|slice:\"-100:-100\"",
  597. <<"{{ var|slice:\"-100:-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  598. <<>>},
  599. {"|slice:\"100:100\"",
  600. <<"{{ var|slice:\"100:100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  601. <<>>},
  602. {"|slice:\"100:-100\"",
  603. <<"{{ var|slice:\"100:-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  604. <<>>},
  605. {"|slice:\"-100:100\"",
  606. <<"{{ var|slice:\"-100:100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  607. <<1,2,3,4,5,6,7,8,9>>},
  608. {"|slice:\"1:3\"",
  609. <<"{{ var|slice:\"1:3\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  610. <<2,3>>},
  611. {"|slice:\"::\"",
  612. <<"{{ var|slice:\"::\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  613. <<1,2,3,4,5,6,7,8,9>>},
  614. {"|slice:\"1:9:1\"",
  615. <<"{{ var|slice:\"1:9:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  616. <<2,3,4,5,6,7,8,9>>},
  617. {"|slice:\"10:1:-1\"",
  618. <<"{{ var|slice:\"10:1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  619. <<9,8,7,6,5,4,3>>},
  620. {"|slice:\"-111:-1:1\"",
  621. <<"{{ var|slice:\"-111:-1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  622. <<1,2,3,4,5,6,7,8>>},
  623. {"|slice:\"-111:-111:1\"",
  624. <<"{{ var|slice:\"-111:-111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  625. <<>>},
  626. {"|slice:\"111:111:1\"",
  627. <<"{{ var|slice:\"111:111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  628. <<>>},
  629. {"|slice:\"-111:111:1\"",
  630. <<"{{ var|slice:\"-111:111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  631. <<1,2,3,4,5,6,7,8,9>>},
  632. {"|slice:\"111:-111:1\"",
  633. <<"{{ var|slice:\"111:-111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  634. <<>>},
  635. {"|slice:\"-111:-111:-1\"",
  636. <<"{{ var|slice:\"-111:-111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  637. <<>>},
  638. {"|slice:\"111:111:-1\"",
  639. <<"{{ var|slice:\"111:111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  640. <<>>},
  641. {"|slice:\"-111:111:-1\"",
  642. <<"{{ var|slice:\"-111:111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  643. <<>>},
  644. {"|slice:\"111:-111:-1\"",
  645. <<"{{ var|slice:\"111:-111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  646. <<9,8,7,6,5,4,3,2,1>>}, {"|phone2numeric",
  647. <<"{{ var1|phone2numeric }}">>, [{var1, "1-800-COLLECT"}],
  648. <<"1-800-2655328">>},
  649. {"|slugify",
  650. <<"{{ var1|slugify }}">>, [{var1, "What The $#_! Was He Thinking?"}],
  651. <<"what-the-_-was-he-thinking">>},
  652. {"|slice:\"s\"",
  653. <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
  654. <<"test">>},
  655. {"|stringformat:\"s\"",
  656. <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
  657. <<"test">>},
  658. {"|stringformat:\"s\"",
  659. <<"{{ var|stringformat:\"s\" }}">>, [{var, "1"}],
  660. <<"1">>},
  661. {"|stringformat:\"s\"",
  662. <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
  663. <<"test">>},
  664. {"|stringformat:\"10s\"",
  665. <<"{{ var|stringformat:\"10s\" }}">>, [{var, "test"}],
  666. <<" test">>},
  667. {"|stringformat:\"-10s\"",
  668. <<"{{ var|stringformat:\"-10s\" }}">>, [{var, "test"}],
  669. <<"test ">>},
  670. {"|stringformat:\"d\"",
  671. <<"{{ var|stringformat:\"d\" }}">>, [{var, "90"}],
  672. <<"90">>},
  673. {"|stringformat:\"10d\"",
  674. <<"{{ var|stringformat:\"10d\" }}">>, [{var, "90"}],
  675. <<" 90">>},
  676. {"|stringformat:\"-10d\"",
  677. <<"{{ var|stringformat:\"-10d\" }}">>, [{var, "90"}],
  678. <<"90 ">>},
  679. {"|stringformat:\"i\"",
  680. <<"{{ var|stringformat:\"i\" }}">>, [{var, "90"}],
  681. <<"90">>},
  682. {"|stringformat:\"10i\"",
  683. <<"{{ var|stringformat:\"10i\" }}">>, [{var, "90"}],
  684. <<" 90">>},
  685. {"|stringformat:\"-10i\"",
  686. <<"{{ var|stringformat:\"-10i\" }}">>, [{var, "90"}],
  687. <<"90 ">>},
  688. {"|stringformat:\"0.2d\"",
  689. <<"{{ var|stringformat:\"0.2d\" }}">>, [{var, "9"}],
  690. <<"09">>},
  691. {"|stringformat:\"10.4d\"",
  692. <<"{{ var|stringformat:\"10.4d\" }}">>, [{var, "9"}],
  693. <<" 0009">>},
  694. {"|stringformat:\"-10.4d\"",
  695. <<"{{ var|stringformat:\"-10.4d\" }}">>, [{var, "9"}],
  696. <<"0009 ">>},
  697. {"|stringformat:\"f\"",
  698. <<"{{ var|stringformat:\"f\" }}">>, [{var, "1"}],
  699. <<"1.000000">>},
  700. {"|stringformat:\".2f\"",
  701. <<"{{ var|stringformat:\".2f\" }}">>, [{var, "1"}],
  702. <<"1.00">>},
  703. {"|stringformat:\"0.2f\"",
  704. <<"{{ var|stringformat:\"0.2f\" }}">>, [{var, "1"}],
  705. <<"1.00">>},
  706. {"|stringformat:\"-0.2f\"",
  707. <<"{{ var|stringformat:\"-0.2f\" }}">>, [{var, "1"}],
  708. <<"1.00">>},
  709. {"|stringformat:\"10.2f\"",
  710. <<"{{ var|stringformat:\"10.2f\" }}">>, [{var, "1"}],
  711. <<" 1.00">>},
  712. {"|stringformat:\"-10.2f\"",
  713. <<"{{ var|stringformat:\"-10.2f\" }}">>, [{var, "1"}],
  714. <<"1.00 ">>},
  715. {"|stringformat:\".2f\"",
  716. <<"{{ var|stringformat:\".2f\" }}">>, [{var, "1"}],
  717. <<"1.00">>},
  718. {"|stringformat:\"x\"",
  719. <<"{{ var|stringformat:\"x\" }}">>, [{var, "90"}],
  720. <<"5a">>},
  721. {"|stringformat:\"X\"",
  722. <<"{{ var|stringformat:\"X\" }}">>, [{var, "90"}],
  723. <<"5A">>},
  724. {"|stringformat:\"o\"",
  725. <<"{{ var|stringformat:\"o\" }}">>, [{var, "90"}],
  726. <<"132">>},
  727. {"|stringformat:\"e\"",
  728. <<"{{ var|stringformat:\"e\" }}">>, [{var, "90"}],
  729. <<"9.000000e+01">>},
  730. {"|stringformat:\"e\"",
  731. <<"{{ var|stringformat:\"e\" }}">>, [{var, "90000000000"}],
  732. <<"9.000000e+10">>},
  733. {"|stringformat:\"E\"",
  734. <<"{{ var|stringformat:\"E\" }}">>, [{var, "90"}],
  735. <<"9.000000E+01">>},
  736. {"|striptags",
  737. <<"{{ var|striptags }}">>, [{var, "<b>Joel</b> <button>is</button> a <span>slug</span>"}],
  738. <<"Joel is a slug">>},
  739. {"|striptags",
  740. <<"{{ var|striptags }}">>, [{var, "<B>Joel</B> <button>is</button> a <span>slug</Span>"}],
  741. <<"Joel is a slug">>},
  742. {"|striptags",
  743. <<"{{ var|striptags }}">>, [{var, "Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>"}],
  744. <<"Check out http://www.djangoproject.com">>},
  745. {"|time:\"H:i\"",
  746. <<"{{ var|time:\"H:i\" }}">>, [{var, {{2010,12,1}, {10,11,12}} }],
  747. <<"10:11">>},
  748. {"|time",
  749. <<"{{ var|time }}">>, [{var, {{2010,12,1}, {10,11,12}} }],
  750. <<"10:11 a.m.">>},
  751. {"|timesince:from_date",
  752. <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2006,6,1},{8,0,0}} }, {from_date, {{2006,6,1},{0,0,0}} }],
  753. <<"8 hours">>},
  754. {"|timesince:from_date",
  755. <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2010,6,1},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  756. <<"4 years, 1 day">>}, % leap year
  757. {"|timesince:from_date",
  758. <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2006,7,15},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  759. <<"1 month, 2 weeks">>},
  760. {"|timeuntil:from_date",
  761. <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2006,6,1},{8,0,0}} }, {from_date, {{2006,6,1},{0,0,0}} }],
  762. <<"8 hours">>},
  763. {"|timeuntil:from_date",
  764. <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2010,6,1},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  765. <<"4 years, 1 day">>},
  766. {"|timeuntil:from_date",
  767. <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2006,7,15},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  768. <<"1 month, 2 weeks">>},
  769. {"|title",
  770. <<"{{ \"my title case\"|title }}">>, [],
  771. <<"My Title Case">>},
  772. {"|title (pre-formatted)",
  773. <<"{{ \"My Title Case\"|title }}">>, [],
  774. <<"My Title Case">>},
  775. {"|truncatechars:0",
  776. <<"{{ var1|truncatechars:0 }}">>, [{var1, "Empty Me"}],
  777. <<"">>},
  778. {"|truncatechars:11",
  779. <<"{{ var1|truncatechars:11 }}">>, [{var1, "Truncate Me Please"}],
  780. <<"Truncate Me...">>},
  781. {"|truncatechars:17",
  782. <<"{{ var1|truncatechars:17 }}">>, [{var1, "Don't Truncate Me"}],
  783. <<"Don't Truncate Me">>},
  784. {"|truncatewords:0",
  785. <<"{{ var1|truncatewords:0 }}">>, [{var1, "Empty Me"}],
  786. <<"">>},
  787. {"|truncatewords:2",
  788. <<"{{ var1|truncatewords:2 }}">>, [{var1, "Truncate Me Please"}],
  789. <<"Truncate Me...">>},
  790. {"|truncatewords:3",
  791. <<"{{ var1|truncatewords:3 }}">>, [{var1, "Don't Truncate Me"}],
  792. <<"Don't Truncate Me">>},
  793. {"|truncatewords_html:4",
  794. <<"{{ var1|truncatewords_html:4 }}">>, [{var1, "<p>The <strong>Long and <em>Winding</em> Road</strong> is too long</p>"}],
  795. <<"<p>The <strong>Long and <em>Winding</em>...</strong></p>">>},
  796. {"|unordered_list",
  797. <<"{{ var1|unordered_list }}">>, [{var1, ["States", ["Kansas", ["Lawrence", "Topeka"], "Illinois"]]}],
  798. <<"<li>States<ul><li>Kansas<ul><li>Lawrence</li><li>Topeka</li></ul></li><li>Illinois</li></ul></li>">>},
  799. {"|upper",
  800. <<"{{ message|upper }}">>, [{message, "That man has a gun."}],
  801. <<"THAT MAN HAS A GUN.">>},
  802. {"|urlencode",
  803. <<"{{ url|urlencode }}">>, [{url, "You #$*@!!"}],
  804. <<"You+%23%24%2A%40%21%21">>},
  805. {"|urlize",
  806. <<"{{ var|urlize }}">>, [{var, "Check out www.djangoproject.com"}],
  807. <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">www.djangoproject.com</a>">>},
  808. {"|urlize",
  809. <<"{{ var|urlize }}">>, [{var, "Check out http://www.djangoproject.com"}],
  810. <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>">>},
  811. {"|urlize",
  812. <<"{{ var|urlize }}">>, [{var, "Check out \"http://www.djangoproject.com\""}],
  813. <<"Check out \"<a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>\"">>},
  814. {"|urlizetrunc:15",
  815. <<"{{ var|urlizetrunc:15 }}">>, [{var, "Check out www.djangoproject.com"}],
  816. <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">www.djangopr...</a>">>},
  817. {"|wordcount",
  818. <<"{{ words|wordcount }}">>, [{words, "Why Hello There!"}],
  819. <<"3">>},
  820. {"|wordwrap:2",
  821. <<"{{ words|wordwrap:2 }}">>, [{words, "this is"}],
  822. <<"this \nis">>},
  823. {"|wordwrap:100",
  824. <<"{{ words|wordwrap:100 }}">>, [{words, "testing testing"}],
  825. <<"testing testing">>},
  826. {"|wordwrap:10",
  827. <<"{{ words|wordwrap:10 }}">>, [{words, ""}],
  828. <<"">>},
  829. {"|wordwrap:1",
  830. <<"{{ words|wordwrap:1 }}">>, [{words, "two"}],
  831. <<"two">>},
  832. % yesno match: \?assert.*\( (.*), erlydtl_filters:(.*)\((.*), "(.*)"\)\)
  833. % yesno replace: \t\t{"|$2:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>}
  834. {"|yesno:\"yeah,no,maybe\"",
  835. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, true}],
  836. <<"yeah">>},
  837. {"|yesno:\"yeah,no,maybe\"",
  838. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, false}],
  839. <<"no">>},
  840. {"|yesno:\"yeah,no\"",
  841. <<"{{ var|yesno:\"yeah,no\" }}">>, [{var, undefined}],
  842. <<"no">>},
  843. {"|yesno:\"yeah,no,maybe\"",
  844. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, undefined}],
  845. <<"maybe">>}
  846. ]},
  847. {"filters_if", [
  848. {"Filter if 1.1",
  849. <<"{% if var1|length_is:0 %}Y{% else %}N{% endif %}">>,
  850. [{var1, []}],
  851. <<"Y">>},
  852. {"Filter if 1.2",
  853. <<"{% if var1|length_is:1 %}Y{% else %}N{% endif %}">>,
  854. [{var1, []}],
  855. <<"N">>},
  856. {"Filter if 1.3",
  857. <<"{% if var1|length_is:7 %}Y{% else %}N{% endif %}">>,
  858. [{var1, []}],
  859. <<"N">>},
  860. {"Filter if 2.1",
  861. <<"{% if var1|length_is:0 %}Y{% else %}N{% endif %}">>,
  862. [{var1, ["foo"]}],
  863. <<"N">>},
  864. {"Filter if 2.2",
  865. <<"{% if var1|length_is:1 %}Y{% else %}N{% endif %}">>,
  866. [{var1, ["foo"]}],
  867. <<"Y">>},
  868. {"Filter if 2.3",
  869. <<"{% if var1|length_is:7 %}Y{% else %}N{% endif %}">>,
  870. [{var1, ["foo"]}],
  871. <<"N">>},
  872. {"Filter if 3.1",
  873. <<"{% ifequal var1|length 0 %}Y{% else %}N{% endifequal %}">>,
  874. [{var1, []}],
  875. <<"Y">>},
  876. {"Filter if 3.2",
  877. <<"{% ifequal var1|length 1 %}Y{% else %}N{% endifequal %}">>,
  878. [{var1, []}],
  879. <<"N">>},
  880. {"Filter if 4.1",
  881. <<"{% ifequal var1|length 3 %}Y{% else %}N{% endifequal %}">>,
  882. [{var1, ["foo", "bar", "baz"]}],
  883. <<"Y">>},
  884. {"Filter if 4.2",
  885. <<"{% ifequal var1|length 0 %}Y{% else %}N{% endifequal %}">>,
  886. [{var1, ["foo", "bar", "baz"]}],
  887. <<"N">>},
  888. {"Filter if 4.3",
  889. <<"{% ifequal var1|length 1 %}Y{% else %}N{% endifequal %}">>,
  890. [{var1, ["foo", "bar", "baz"]}],
  891. <<"N">>}
  892. ]},
  893. {"firstof", [
  894. {"Firstof first",
  895. <<"{% firstof foo bar baz %}">>,
  896. [{foo, "1"},{bar, "2"}],
  897. <<"1">>},
  898. {"Firstof second",
  899. <<"{% firstof foo bar baz %}">>,
  900. [{bar, "2"}],
  901. <<"2">>},
  902. {"Firstof none",
  903. <<"{% firstof foo bar baz %}">>,
  904. [],
  905. <<"">>},
  906. {"Firstof complex",
  907. <<"{% firstof foo.bar.baz bar %}">>,
  908. [{foo, [{bar, [{baz, "quux"}]}]}],
  909. <<"quux">>},
  910. {"Firstof undefined complex",
  911. <<"{% firstof foo.bar.baz bar %}">>,
  912. [{bar, "bar"}],
  913. <<"bar">>},
  914. {"Firstof literal",
  915. <<"{% firstof foo bar \"baz\" %}">>,
  916. [],
  917. <<"baz">>}
  918. ]},
  919. {"regroup", [
  920. {"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 %}">>,
  921. [{people, [[{first_name, "George"}, {gender, "Male"}], [{first_name, "Bill"}, {gender, "Male"}],
  922. [{first_name, "Margaret"}, {gender, "Female"}], [{first_name, "Condi"}, {gender, "Female"}]]}],
  923. <<"Male\nGeorge\nBill\nFemale\nMargaret\nCondi\n">>},
  924. {"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 %}">>,
  925. [{people, [[{first_name, "George"}, {gender, "Male"}],
  926. [{first_name, "Margaret"}, {gender, "Female"}],
  927. [{first_name, "Condi"}, {gender, "Female"}],
  928. [{first_name, "Bill"}, {gender, "Male"}]
  929. ]}],
  930. <<"Male\nGeorge\nFemale\nMargaret\nCondi\nMale\nBill\n">>}
  931. ]},
  932. {"spaceless", [
  933. {"Beginning", <<"{% spaceless %} <b>foo</b>{% endspaceless %}">>, [], <<"<b>foo</b>">>},
  934. {"Middle", <<"{% spaceless %}<b>foo</b> <b>bar</b>{% endspaceless %}">>, [], <<"<b>foo</b><b>bar</b>">>},
  935. {"End", <<"{% spaceless %}<b>foo</b> {% endspaceless %}">>, [], <<"<b>foo</b>">>}
  936. ]},
  937. {"templatetag", [
  938. {"openblock", <<"{% templatetag openblock %}">>, [], <<"{%">>},
  939. {"closeblock", <<"{% templatetag closeblock %}">>, [], <<"%}">>},
  940. {"openvariable", <<"{% templatetag openvariable %}">>, [], <<"{{">>},
  941. {"closevariable", <<"{% templatetag closevariable %}">>, [], <<"}}">>},
  942. {"openbrace", <<"{% templatetag openbrace %}">>, [], <<"{">>},
  943. {"closebrace", <<"{% templatetag closebrace %}">>, [], <<"}">>},
  944. {"opencomment", <<"{% templatetag opencomment %}">>, [], <<"{#">>},
  945. {"closecomment", <<"{% templatetag closecomment %}">>, [], <<"#}">>}
  946. ]},
  947. {"trans",
  948. [
  949. {"trans functional default locale",
  950. <<"Hello {% trans \"Hi\" %}">>, [], <<"Hello Hi">>
  951. },
  952. {"trans functional reverse locale",
  953. <<"Hello {% trans \"Hi\" %}">>, [], [], [{locale, "reverse"}], <<"Hello iH">>
  954. },
  955. {"trans literal at run-time",
  956. <<"Hello {% trans \"Hi\" %}">>, [], [{translation_fun, fun("Hi") -> "Konichiwa" end}], [],
  957. <<"Hello Konichiwa">>},
  958. {"trans variable at run-time",
  959. <<"Hello {% trans var1 %}">>, [{var1, <<"Hi">>}], [{translation_fun, fun(<<"Hi">>) -> <<"Konichiwa">> end}], [],
  960. <<"Hello Konichiwa">>},
  961. {"trans literal at run-time: No-op",
  962. <<"Hello {% trans \"Hi\" noop %}">>, [], [{translation_fun, fun("Hi") -> <<"Konichiwa">> end}], [],
  963. <<"Hello Hi">>},
  964. {"trans variable at run-time: No-op",
  965. <<"Hello {% trans var1 noop %}">>, [{var1, <<"Hi">>}], [{translation_fun, fun(<<"Hi">>) -> <<"Konichiwa">> end}], [],
  966. <<"Hello Hi">>}
  967. ]},
  968. {"blocktrans",
  969. [
  970. {"blocktrans default locale",
  971. <<"{% blocktrans %}Hello{% endblocktrans %}">>, [], <<"Hello">>},
  972. {"blocktrans choose locale",
  973. <<"{% blocktrans %}Hello, {{ name }}{% endblocktrans %}">>, [{name, "Mr. President"}], [{locale, "de"}],
  974. [{blocktrans_locales, ["de"]}, {blocktrans_fun, fun("Hello, {{ name }}", "de") -> <<"Guten tag, {{ name }}">> end}], <<"Guten tag, Mr. President">>},
  975. {"blocktrans with args",
  976. <<"{% blocktrans with var1=foo %}{{ var1 }}{% endblocktrans %}">>, [{foo, "Hello"}], <<"Hello">>}
  977. ]},
  978. {"widthratio", [
  979. {"Literals", <<"{% widthratio 5 10 100 %}">>, [], <<"50">>},
  980. {"Rounds up", <<"{% widthratio a b 100 %}">>, [{a, 175}, {b, 200}], <<"88">>}
  981. ]},
  982. {"with", [
  983. {"Cache literal",
  984. <<"{% with a=1 %}{{ a }}{% endwith %}">>, [], <<"1">>},
  985. {"Cache variable",
  986. <<"{% with a=b %}{{ a }}{% endwith %}">>, [{b, "foo"}], <<"foo">>},
  987. {"Cache multiple",
  988. <<"{% with alpha=1 beta=b %}{{ alpha }}/{{ beta }}{% endwith %}">>, [{b, 2}], <<"1/2">>}
  989. ]},
  990. {"unicode", [
  991. {"(tm) somewhere",
  992. <<"™">>, [], <<"™">>}
  993. ]}
  994. ].
  995. run_tests() ->
  996. io:format("Running unit tests...~n"),
  997. DefaultOptions = [],
  998. Failures = lists:foldl(
  999. fun({Group, Assertions}, GroupAcc) ->
  1000. io:format(" Test group ~p...~n", [Group]),
  1001. lists:foldl(fun
  1002. ({Name, DTL, Vars, Output}, Acc) ->
  1003. process_unit_test(erlydtl:compile(DTL, erlydtl_running_test, DefaultOptions),
  1004. Vars, [], Output, Acc, Group, Name);
  1005. ({Name, DTL, Vars, RenderOpts, Output}, Acc) ->
  1006. process_unit_test(erlydtl:compile(DTL, erlydtl_running_test, DefaultOptions),
  1007. Vars, RenderOpts, Output, Acc, Group, Name);
  1008. ({Name, DTL, Vars, RenderOpts, CompilerOpts, Output}, Acc) ->
  1009. process_unit_test(erlydtl:compile(DTL, erlydtl_running_test, CompilerOpts ++ DefaultOptions),
  1010. Vars, RenderOpts, Output, Acc, Group, Name)
  1011. end, GroupAcc, Assertions)
  1012. end, [], tests()),
  1013. io:format("Unit test failures: ~p~n", [lists:reverse(Failures)]).
  1014. process_unit_test(CompiledTemplate, Vars, RenderOpts, Output,Acc, Group, Name) ->
  1015. case CompiledTemplate of
  1016. {ok, _} ->
  1017. {ok, IOList} = erlydtl_running_test:render(Vars, RenderOpts),
  1018. {ok, IOListBin} = erlydtl_running_test:render(vars_to_binary(Vars), RenderOpts),
  1019. case {iolist_to_binary(IOList), iolist_to_binary(IOListBin)} of
  1020. {Output, Output} ->
  1021. Acc;
  1022. {Output, Unexpected} ->
  1023. [{Group, Name, 'binary', Unexpected, Output} | Acc];
  1024. {Unexpected, Output} ->
  1025. [{Group, Name, 'list', Unexpected, Output} | Acc];
  1026. {Unexpected1, Unexpected2} ->
  1027. [{Group, Name, 'list', Unexpected1, Output},
  1028. {Group, Name, 'binary', Unexpected2, Output} | Acc]
  1029. end;
  1030. Err ->
  1031. [{Group, Name, Err} | Acc]
  1032. end.
  1033. vars_to_binary(Vars) when is_list(Vars) ->
  1034. lists:map(fun
  1035. ({Key, [H|_] = Value}) when is_tuple(H) ->
  1036. {Key, vars_to_binary(Value)};
  1037. ({Key, [H|_] = Value}) when is_integer(H) ->
  1038. {Key, list_to_binary(Value)};
  1039. ({Key, Value}) ->
  1040. {Key, Value}
  1041. end, Vars);
  1042. vars_to_binary(Vars) ->
  1043. Vars.
  1044. generate_test_date() ->
  1045. {{Y,M,D}, _} = erlang:localtime(),
  1046. MonthName = [
  1047. "January", "February", "March", "April",
  1048. "May", "June", "July", "August", "September",
  1049. "October", "November", "December"
  1050. ],
  1051. OrdinalSuffix = [
  1052. "st","nd","rd","th","th","th","th","th","th","th", % 1-10
  1053. "th","th","th","th","th","th","th","th","th","th", % 10-20
  1054. "st","nd","rd","th","th","th","th","th","th","th", % 20-30
  1055. "st"
  1056. ],
  1057. list_to_binary([
  1058. "It is the ",
  1059. integer_to_list(D),
  1060. lists:nth(D, OrdinalSuffix),
  1061. " of ", lists:nth(M, MonthName),
  1062. " ", integer_to_list(Y), "."
  1063. ]).