erlydtl_unittests.erl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 substring in string",
  107. <<"{% if var1 in var2 %}yay{% endif %}">>, [{var1, "rook"}, {var2, "Crooks"}], <<"yay">>},
  108. {"If substring in string (false)",
  109. <<"{% if var1 in var2 %}boo{% endif %}">>, [{var1, "Cook"}, {var2, "Crooks"}], <<>>},
  110. {"If substring not in string",
  111. <<"{% if var1 not in var2 %}yay{% endif %}">>, [{var1, "Cook"}, {var2, "Crooks"}], <<"yay">>},
  112. {"If substring not in string (false)",
  113. <<"{% if var1 not in var2 %}boo{% endif %}">>, [{var1, "rook"}, {var2, "Crooks"}], <<>>},
  114. {"If literal substring in string",
  115. <<"{% if \"man\" in \"Ottoman\" %}yay{% endif %}">>, [], <<"yay">>},
  116. {"If literal substring in string (false)",
  117. <<"{% if \"woman\" in \"Ottoman\" %}boo{% endif %}">>, [], <<>>},
  118. {"If element in list",
  119. <<"{% if var1 in var2 %}yay{% endif %}">>, [{var1, "foo"}, {var2, ["bar", "foo", "baz"]}], <<"yay">>},
  120. {"If element in list (false)",
  121. <<"{% if var1 in var2 %}boo{% endif %}">>, [{var1, "FOO"}, {var2, ["bar", "foo", "baz"]}], <<>>},
  122. {"If complex",
  123. <<"{% if foo.bar.baz %}omgwtfbbq{% endif %}">>, [], <<"">>}
  124. ]},
  125. {"for", [
  126. {"Simple loop",
  127. <<"{% for x in list %}{{ x }}{% endfor %}">>, [{'list', ["1", "2", "3"]}],
  128. <<"123">>},
  129. {"Expand list",
  130. <<"{% for x, y in list %}{{ x }},{{ y }}\n{% endfor %}">>, [{'list', [["X", "1"], ["X", "2"]]}],
  131. <<"X,1\nX,2\n">>},
  132. {"Expand tuple",
  133. <<"{% for x, y in list %}{{ x }},{{ y }}\n{% endfor %}">>, [{'list', [{"X", "1"}, {"X", "2"}]}],
  134. <<"X,1\nX,2\n">>},
  135. {"Resolve variable attribute",
  136. <<"{% for number in person.numbers %}{{ number }}\n{% endfor %}">>, [{person, [{numbers, ["411", "911"]}]}],
  137. <<"411\n911\n">>},
  138. {"Resolve nested variable attribute",
  139. <<"{% for number in person.home.numbers %}{{ number }}\n{% endfor %}">>, [{person, [{home, [{numbers, ["411", "911"]}]}]}],
  140. <<"411\n911\n">>},
  141. {"Counter0",
  142. <<"{% for number in numbers %}{{ forloop.counter0 }}. {{ number }}\n{% endfor %}">>,
  143. [{numbers, ["Zero", "One", "Two"]}], <<"0. Zero\n1. One\n2. Two\n">>},
  144. {"Counter",
  145. <<"{% for number in numbers %}{{ forloop.counter }}. {{ number }}\n{% endfor %}">>,
  146. [{numbers, ["One", "Two", "Three"]}], <<"1. One\n2. Two\n3. Three\n">>},
  147. {"Reverse Counter0",
  148. <<"{% for number in numbers %}{{ forloop.revcounter0 }}. {{ number }}\n{% endfor %}">>,
  149. [{numbers, ["Two", "One", "Zero"]}], <<"2. Two\n1. One\n0. Zero\n">>},
  150. {"Reverse Counter",
  151. <<"{% for number in numbers %}{{ forloop.revcounter }}. {{ number }}\n{% endfor %}">>,
  152. [{numbers, ["Three", "Two", "One"]}], <<"3. Three\n2. Two\n1. One\n">>},
  153. {"Counter \"first\"",
  154. <<"{% for number in numbers %}{% if forloop.first %}{{ number }}{% endif %}{% endfor %}">>,
  155. [{numbers, ["One", "Two", "Three"]}], <<"One">>},
  156. {"Counter \"last\"",
  157. <<"{% for number in numbers %}{% if forloop.last %}{{ number }}{% endif %}{% endfor %}">>,
  158. [{numbers, ["One", "Two", "Three"]}], <<"Three">>},
  159. {"Nested for loop",
  160. <<"{% for outer in list %}{% for inner in outer %}{{ inner }}\n{% endfor %}{% endfor %}">>,
  161. [{'list', [["Al", "Albert"], ["Jo", "Joseph"]]}],
  162. <<"Al\nAlbert\nJo\nJoseph\n">>},
  163. {"Access parent loop counters",
  164. <<"{% for outer in list %}{% for inner in outer %}({{ forloop.parentloop.counter0 }}, {{ forloop.counter0 }})\n{% endfor %}{% endfor %}">>,
  165. [{'list', [["One", "two"], ["One", "two"]]}], <<"(0, 0)\n(0, 1)\n(1, 0)\n(1, 1)\n">>}
  166. ]},
  167. {"ifequal", [
  168. {"Compare variable to literal",
  169. <<"{% ifequal var1 \"foo\" %}yay{% endifequal %}">>,
  170. [{var1, "foo"}], <<"yay">>},
  171. {"Compare variable to unequal literal",
  172. <<"{% ifequal var1 \"foo\" %}boo{% endifequal %}">>,
  173. [{var1, "bar"}], <<>>},
  174. {"Compare literal to variable",
  175. <<"{% ifequal \"foo\" var1 %}yay{% endifequal %}">>,
  176. [{var1, "foo"}], <<"yay">>},
  177. {"Compare literal to unequal variable",
  178. <<"{% ifequal \"foo\" var1 %}boo{% endifequal %}">>,
  179. [{var1, "bar"}], <<>>},
  180. {"Compare variable to literal (int string)",
  181. <<"{% ifequal var1 \"2\" %}yay{% else %}boo{% endifequal %}">>,
  182. [{var1, "2"}], <<"yay">>},
  183. {"Compare variable to literal (int)",
  184. <<"{% ifequal var1 2 %}yay{% else %}boo{% endifequal %}">>,
  185. [{var1, 2}], <<"yay">>},
  186. {"Compare variable to unequal literal (int)",
  187. <<"{% ifequal var1 2 %}boo{% else %}yay{% endifequal %}">>,
  188. [{var1, 3}], <<"yay">>},
  189. {"Compare variable to equal literal (atom)",
  190. <<"{% ifequal var1 \"foo\"%}yay{% endifequal %}">>,
  191. [{var1, foo}], <<"yay">>},
  192. {"Compare variable to unequal literal (atom)",
  193. <<"{% ifequal var1 \"foo\"%}yay{% else %}boo{% endifequal %}">>,
  194. [{var1, bar}], <<"boo">>}
  195. ]},
  196. {"ifequal/else", [
  197. {"Compare variable to literal",
  198. <<"{% ifequal var1 \"foo\" %}yay{% else %}boo{% endifequal %}">>,
  199. [{var1, "foo"}], <<"yay">>},
  200. {"Compare variable to unequal literal",
  201. <<"{% ifequal var1 \"foo\" %}boo{% else %}yay{% endifequal %}">>,
  202. [{var1, "bar"}], <<"yay">>},
  203. {"Compare literal to variable",
  204. <<"{% ifequal \"foo\" var1 %}yay{% else %}boo{% endifequal %}">>,
  205. [{var1, "foo"}], <<"yay">>},
  206. {"Compare literal to unequal variable",
  207. <<"{% ifequal \"foo\" var1 %}boo{% else %}yay{% endifequal %}">>,
  208. [{var1, "bar"}], <<"yay">>}
  209. ]},
  210. {"ifnotequal", [
  211. {"Compare variable to literal",
  212. <<"{% ifnotequal var1 \"foo\" %}boo{% endifnotequal %}">>,
  213. [{var1, "foo"}], <<>>},
  214. {"Compare variable to unequal literal",
  215. <<"{% ifnotequal var1 \"foo\" %}yay{% endifnotequal %}">>,
  216. [{var1, "bar"}], <<"yay">>},
  217. {"Compare literal to variable",
  218. <<"{% ifnotequal \"foo\" var1 %}boo{% endifnotequal %}">>,
  219. [{var1, "foo"}], <<>>},
  220. {"Compare literal to unequal variable",
  221. <<"{% ifnotequal \"foo\" var1 %}yay{% endifnotequal %}">>,
  222. [{var1, "bar"}], <<"yay">>}
  223. ]},
  224. {"ifnotequal/else", [
  225. {"Compare variable to literal",
  226. <<"{% ifnotequal var1 \"foo\" %}boo{% else %}yay{% endifnotequal %}">>,
  227. [{var1, "foo"}], <<"yay">>},
  228. {"Compare variable to unequal literal",
  229. <<"{% ifnotequal var1 \"foo\" %}yay{% else %}boo{% endifnotequal %}">>,
  230. [{var1, "bar"}], <<"yay">>},
  231. {"Compare literal to variable",
  232. <<"{% ifnotequal \"foo\" var1 %}boo{% else %}yay{% endifnotequal %}">>,
  233. [{var1, "foo"}], <<"yay">>},
  234. {"Compare literal to unequal variable",
  235. <<"{% ifnotequal \"foo\" var1 %}yay{% else %}boo{% endifnotequal %}">>,
  236. [{var1, "bar"}], <<"yay">>}
  237. ]},
  238. {"filters", [
  239. {"Filter a literal",
  240. <<"{{ \"pop\"|capfirst }}">>, [],
  241. <<"Pop">>},
  242. {"Filters applied in order",
  243. <<"{{ var1|force_escape|length }}">>, [{var1, <<"&">>}],
  244. <<"5">>},
  245. {"Escape is applied last",
  246. <<"{{ var1|escape|linebreaksbr }}">>, [{var1, <<"\n">>}],
  247. <<"&lt;br /&gt;">>},
  248. {"|add:4",
  249. <<"{{ one|add:4 }}">>, [{one, "1"}],
  250. <<"5">>},
  251. {"|capfirst",
  252. <<"{{ var1|capfirst }}">>, [{var1, "dana boyd"}],
  253. <<"Dana boyd">>},
  254. {"|center:10",
  255. <<"{{ var1|center:10 }}">>, [{var1, "MB"}],
  256. <<" MB ">>},
  257. {"|center:1",
  258. <<"{{ var1|center:1 }}">>, [{var1, "KBR"}],
  259. <<"B">>},
  260. {"|date 1",
  261. <<"{{ var1|date:\"jS F Y H:i\" }}">>,
  262. [{var1, {1975,7,24}}],
  263. <<"24th July 1975 00:00">>},
  264. {"|date 2",
  265. <<"{{ var1|date:\"jS F Y H:i\" }}">>,
  266. [{var1, {{1975,7,24}, {7,13,1}}}],
  267. <<"24th July 1975 07:13">>},
  268. {"|default_if_none:\"foo\"",
  269. <<"{{ var1|default_if_none:\"foo\" }}">>, [], <<"foo">>},
  270. {"|default_if_none:\"foo\" 2",
  271. <<"{{ var1|default_if_none:\"foo\" }}">>, [{var1, "bar"}], <<"bar">>},
  272. {"|escapejs",
  273. <<"{{ var1|escapejs }}">>, [{var1, "Skip's \"Old-Timey\" Diner"}],
  274. <<"Skip\\'s \\\"Old-Timey\\\" Diner">>},
  275. {"|first",
  276. <<"{{ var1|first }}">>, [{var1, "James"}],
  277. <<"J">>},
  278. {"|fix_ampersands",
  279. <<"{{ var1|fix_ampersands }}">>, [{var1, "Ben & Jerry's"}],
  280. <<"Ben &amp; Jerry's">>},
  281. {"|force_escape",
  282. <<"{{ var1|force_escape }}">>, [{var1, "Ben & Jerry's <=> \"The World's Best Ice Cream\""}],
  283. <<"Ben &amp; Jerry&#039;s &lt;=&gt; &quot;The World&#039;s Best Ice Cream&quot;">>},
  284. {"|format_integer",
  285. <<"{{ var1|format_integer }}">>, [{var1, 28}], <<"28">>},
  286. {"|format_number 1",
  287. <<"{{ var1|format_number }}">>, [{var1, 28}], <<"28">>},
  288. {"|format_number 2",
  289. <<"{{ var1|format_number }}">>, [{var1, 23.77}], <<"23.77">>},
  290. {"|format_number 3",
  291. <<"{{ var1|format_number }}">>, [{var1, "28.77"}], <<"28.77">>},
  292. {"|format_number 4",
  293. <<"{{ var1|format_number }}">>, [{var1, "23.77"}], <<"23.77">>},
  294. {"|format_number 5",
  295. <<"{{ var1|format_number }}">>, [{var1, fun() -> 29 end}], <<"29">>},
  296. {"|format_number 6",
  297. <<"{{ var1|format_number }}">>, [{var1, fun() -> fun() -> 31 end end}], <<"31">>},
  298. {"|join:\", \" (list)",
  299. <<"{{ var1|join:\", \" }}">>, [{var1, ["Liberte", "Egalite", "Fraternite"]}],
  300. <<"Liberte, Egalite, Fraternite">>},
  301. {"|join:\", \" (binary)",
  302. <<"{{ var1|join:\", \" }}">>, [{var1, [<<"Liberte">>, "Egalite", <<"Fraternite">>]}],
  303. <<"Liberte, Egalite, Fraternite">>},
  304. {"|last",
  305. <<"{{ var1|last }}">>, [{var1, "XYZ"}],
  306. <<"Z">>},
  307. {"|length",
  308. <<"{{ var1|length }}">>, [{var1, "antidisestablishmentarianism"}],
  309. <<"28">>},
  310. {"|linebreaksbr",
  311. <<"{{ var1|linebreaksbr }}">>, [{var1, "One\nTwo\n\nThree\n\n\n"}],
  312. <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
  313. {"|linebreaksbr",
  314. <<"{{ \"One\\nTwo\\n\\nThree\\n\\n\\n\"|linebreaksbr }}">>, [],
  315. <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
  316. {"|ljust:10",
  317. <<"{{ var1|ljust:10 }}">>, [{var1, "Gore"}],
  318. <<"Gore ">>},
  319. {"|lower",
  320. <<"{{ var1|lower }}">>, [{var1, "E. E. Cummings"}],
  321. <<"e. e. cummings">>},
  322. {"|rjust:10",
  323. <<"{{ var1|rjust:10 }}">>, [{var1, "Bush"}],
  324. <<" Bush">>},
  325. {"|truncatewords:0",
  326. <<"{{ var1|truncatewords:0 }}">>, [{var1, "Empty Me"}],
  327. <<"">>},
  328. {"|truncatewords:2",
  329. <<"{{ var1|truncatewords:2 }}">>, [{var1, "Truncate Me Please"}],
  330. <<"Truncate Me...">>},
  331. {"|truncatewords:3",
  332. <<"{{ var1|truncatewords:3 }}">>, [{var1, "Don't Truncate Me"}],
  333. <<"Don't Truncate Me">>},
  334. {"|upper",
  335. <<"{{ message|upper }}">>, [{message, "That man has a gun."}],
  336. <<"THAT MAN HAS A GUN.">>},
  337. {"|urlencode",
  338. <<"{{ url|urlencode }}">>, [{url, "You #$*@!!"}],
  339. <<"You+%23%24%2A%40%21%21">>}
  340. ]},
  341. {"filters_if", [
  342. {"Filter if 1.1",
  343. <<"{% if var1|length_is:0 %}Y{% else %}N{% endif %}">>,
  344. [{var1, []}],
  345. <<"Y">>},
  346. {"Filter if 1.2",
  347. <<"{% if var1|length_is:1 %}Y{% else %}N{% endif %}">>,
  348. [{var1, []}],
  349. <<"N">>},
  350. {"Filter if 1.3",
  351. <<"{% if var1|length_is:7 %}Y{% else %}N{% endif %}">>,
  352. [{var1, []}],
  353. <<"N">>},
  354. {"Filter if 2.1",
  355. <<"{% if var1|length_is:0 %}Y{% else %}N{% endif %}">>,
  356. [{var1, ["foo"]}],
  357. <<"N">>},
  358. {"Filter if 2.2",
  359. <<"{% if var1|length_is:1 %}Y{% else %}N{% endif %}">>,
  360. [{var1, ["foo"]}],
  361. <<"Y">>},
  362. {"Filter if 2.3",
  363. <<"{% if var1|length_is:7 %}Y{% else %}N{% endif %}">>,
  364. [{var1, ["foo"]}],
  365. <<"N">>},
  366. {"Filter if 3.1",
  367. <<"{% ifequal var1|length 0 %}Y{% else %}N{% endifequal %}">>,
  368. [{var1, []}],
  369. <<"Y">>},
  370. {"Filter if 3.1",
  371. <<"{% ifequal var1|length 1 %}Y{% else %}N{% endifequal %}">>,
  372. [{var1, []}],
  373. <<"N">>},
  374. {"Filter if 4.1",
  375. <<"{% ifequal var1|length 3 %}Y{% else %}N{% endifequal %}">>,
  376. [{var1, ["foo", "bar", "baz"]}],
  377. <<"Y">>},
  378. {"Filter if 4.2",
  379. <<"{% ifequal var1|length 0 %}Y{% else %}N{% endifequal %}">>,
  380. [{var1, ["foo", "bar", "baz"]}],
  381. <<"N">>},
  382. {"Filter if 4.3",
  383. <<"{% ifequal var1|length 1 %}Y{% else %}N{% endifequal %}">>,
  384. [{var1, ["foo", "bar", "baz"]}],
  385. <<"N">>}
  386. ]},
  387. {"firstof", [
  388. {"Firstof first",
  389. <<"{% firstof foo bar baz %}">>,
  390. [{foo, "1"},{bar, "2"}],
  391. <<"1">>},
  392. {"Firstof second",
  393. <<"{% firstof foo bar baz %}">>,
  394. [{bar, "2"}],
  395. <<"2">>},
  396. {"Firstof none",
  397. <<"{% firstof foo bar baz %}">>,
  398. [],
  399. <<"">>},
  400. {"Firstof complex",
  401. <<"{% firstof foo.bar.baz bar %}">>,
  402. [{foo, [{bar, [{baz, "quux"}]}]}],
  403. <<"quux">>},
  404. {"Firstof undefined complex",
  405. <<"{% firstof foo.bar.baz bar %}">>,
  406. [{bar, "bar"}],
  407. <<"bar">>},
  408. {"Firstof literal",
  409. <<"{% firstof foo bar \"baz\" %}">>,
  410. [],
  411. <<"baz">>}
  412. ]}
  413. ].
  414. run_tests() ->
  415. io:format("Running unit tests...~n"),
  416. Failures = lists:foldl(
  417. fun({Group, Assertions}, GroupAcc) ->
  418. io:format(" Test group ~p...~n", [Group]),
  419. lists:foldl(fun({Name, DTL, Vars, Output}, Acc) ->
  420. case erlydtl:compile(DTL, erlydtl_running_test, []) of
  421. {ok, _} ->
  422. {ok, IOList} = erlydtl_running_test:render(Vars),
  423. {ok, IOListBin} = erlydtl_running_test:render(vars_to_binary(Vars)),
  424. case {iolist_to_binary(IOList), iolist_to_binary(IOListBin)} of
  425. {Output, Output} ->
  426. Acc;
  427. {Output, Unexpected} ->
  428. [{Group, Name, 'binary', Unexpected, Output} | Acc];
  429. {Unexpected, Output} ->
  430. [{Group, Name, 'list', Unexpected, Output} | Acc];
  431. {Unexpected1, Unexpected2} ->
  432. [{Group, Name, 'list', Unexpected1, Output},
  433. {Group, Name, 'binary', Unexpected2, Output} | Acc]
  434. end;
  435. Err ->
  436. [{Group, Name, Err} | Acc]
  437. end
  438. end, GroupAcc, Assertions)
  439. end, [], tests()),
  440. io:format("Unit test failures: ~p~n", [Failures]).
  441. vars_to_binary(Vars) when is_list(Vars) ->
  442. lists:map(fun
  443. ({Key, [H|_] = Value}) when is_tuple(H) ->
  444. {Key, vars_to_binary(Value)};
  445. ({Key, [H|_] = Value}) when is_integer(H) ->
  446. {Key, list_to_binary(Value)};
  447. ({Key, Value}) ->
  448. {Key, Value}
  449. end, Vars);
  450. vars_to_binary(Vars) ->
  451. Vars.
  452. generate_test_date() ->
  453. {{Y,M,D}, _} = erlang:localtime(),
  454. MonthName = [
  455. "January", "February", "March", "April",
  456. "May", "June", "July", "August", "September",
  457. "October", "November", "December"
  458. ],
  459. OrdinalSuffix = [
  460. "st","nd","rd","th","th","th","th","th","th","th", % 1-10
  461. "th","th","th","th","th","th","th","th","th","th", % 10-20
  462. "st","nd","rd","th","th","th","th","th","th","th", % 20-30
  463. "st"
  464. ],
  465. list_to_binary([
  466. "It is the ",
  467. integer_to_list(D),
  468. lists:nth(D, OrdinalSuffix),
  469. " of ", lists:nth(M, MonthName),
  470. " ", integer_to_list(Y), "."
  471. ]).