erlydtl_unittests.erl 63 KB

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