erlydtl_unittests.erl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. -module(erlydtl_unittests).
  2. -export([run_tests/0]).
  3. tests() ->
  4. [
  5. {"comment", [
  6. {"Comment block is excised",
  7. <<"Bob {% comment %}(moron){% endcomment %} Loblaw">>,
  8. [], <<"Bob Loblaw">>},
  9. {"Inline comment is excised",
  10. <<"You're {# not #} a very nice person">>,
  11. [], <<"You're a very nice person">>}
  12. ]},
  13. {"autoescape", [
  14. {"Autoescape works",
  15. <<"{% autoescape on %}{{ var1 }}{% endautoescape %}">>,
  16. [{var1, "<b>bold</b>"}], <<"&lt;b&gt;bold&lt;/b&gt;">>},
  17. {"Nested autoescape",
  18. <<"{% autoescape on %}{{ var1 }}{% autoescape off %}{{ var1 }}{% endautoescape %}{% endautoescape %}">>,
  19. [{var1, "<b>"}], <<"&lt;b&gt;<b>">>}
  20. ]},
  21. {"string literal", [
  22. {"Render literal",
  23. <<"{{ \"foo\" }} is my name">>, [], <<"foo is my name">>},
  24. {"Newlines are escaped",
  25. <<"{{ \"foo\\n\" }}">>, [], <<"foo\n">>}
  26. ]},
  27. {"cycle", [
  28. {"Cycling through quoted strings",
  29. <<"{% for i in test %}{% cycle 'a' 'b' %}{{ i }},{% endfor %}">>,
  30. [{test, ["0", "1", "2", "3", "4"]}], <<"a0,b1,a2,b3,a4,">>},
  31. {"Cycling through normal variables",
  32. <<"{% for i in test %}{% cycle aye bee %}{{ i }},{% endfor %}">>,
  33. [{test, ["0", "1", "2", "3", "4"]}, {aye, "a"}, {bee, "b"}],
  34. <<"a0,b1,a2,b3,a4,">>}
  35. ]},
  36. {"number literal", [
  37. {"Render integer",
  38. <<"{{ 5 }}">>, [], <<"5">>}
  39. ]},
  40. {"variable", [
  41. {"Render variable",
  42. <<"{{ var1 }} is my game">>, [{var1, "bar"}], <<"bar is my game">>},
  43. {"Render variable with attribute",
  44. <<"I enjoy {{ var1.game }}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
  45. {"Render variable in dict",
  46. <<"{{ var1 }}">>, dict:store(var1, "bar", dict:new()), <<"bar">>},
  47. {"Render variable in gb_tree",
  48. <<"{{ var1 }}">>, gb_trees:insert(var1, "bar", gb_trees:empty()), <<"bar">>},
  49. {"Render variable with attribute in dict",
  50. <<"{{ var1.attr }}">>, [{var1, dict:store(attr, "Othello", dict:new())}], <<"Othello">>},
  51. {"Render variable with attribute in gb_tree",
  52. <<"{{ var1.attr }}">>, [{var1, gb_trees:insert(attr, "Othello", gb_trees:empty())}], <<"Othello">>},
  53. {"Render variable in parameterized module",
  54. <<"{{ var1.some_var }}">>, [{var1, erlydtl_example_variable_storage:new("foo")}], <<"foo">>},
  55. {"Nested attributes",
  56. <<"{{ person.city.state.country }}">>, [{person, [{city, [{state, [{country, "Italy"}]}]}]}],
  57. <<"Italy">>}
  58. ]},
  59. {"now", [
  60. {"now functional",
  61. <<"It is the {% now \"jS o\\f F Y\" %}.">>, [{var1, ""}], generate_test_date()}
  62. ]},
  63. {"if", [
  64. {"If/else",
  65. <<"{% if var1 %}boo{% else %}yay{% endif %}">>, [{var1, ""}], <<"yay">>},
  66. {"If",
  67. <<"{% if var1 %}boo{% endif %}">>, [{var1, ""}], <<>>},
  68. {"If not",
  69. <<"{% if not var1 %}yay{% endif %}">>, [{var1, ""}], <<"yay">>},
  70. {"If \"0\"",
  71. <<"{% if var1 %}boo{% endif %}">>, [{var1, "0"}], <<>>},
  72. {"If false",
  73. <<"{% if var1 %}boo{% endif %}">>, [{var1, false}], <<>>},
  74. {"If undefined",
  75. <<"{% if var1 %}boo{% endif %}">>, [{var1, undefined}], <<>>},
  76. {"If other atom",
  77. <<"{% if var1 %}yay{% endif %}">>, [{var1, foobar}], <<"yay">>},
  78. {"If non-empty string",
  79. <<"{% if var1 %}yay{% endif %}">>, [{var1, "hello"}], <<"yay">>},
  80. {"If proplist",
  81. <<"{% if var1 %}yay{% endif %}">>, [{var1, [{foo, "bar"}]}], <<"yay">>}
  82. ]},
  83. {"for", [
  84. {"Simple loop",
  85. <<"{% for x in list %}{{ x }}{% endfor %}">>, [{'list', ["1", "2", "3"]}],
  86. <<"123">>},
  87. {"Expand list",
  88. <<"{% for x, y in list %}{{ x }},{{ y }}\n{% endfor %}">>, [{'list', [["X", "1"], ["X", "2"]]}],
  89. <<"X,1\nX,2\n">>},
  90. {"Expand tuple",
  91. <<"{% for x, y in list %}{{ x }},{{ y }}\n{% endfor %}">>, [{'list', [{"X", "1"}, {"X", "2"}]}],
  92. <<"X,1\nX,2\n">>},
  93. {"Resolve variable attribute",
  94. <<"{% for number in person.numbers %}{{ number }}\n{% endfor %}">>, [{person, [{numbers, ["411", "911"]}]}],
  95. <<"411\n911\n">>},
  96. {"Resolve nested variable attribute",
  97. <<"{% for number in person.home.numbers %}{{ number }}\n{% endfor %}">>, [{person, [{home, [{numbers, ["411", "911"]}]}]}],
  98. <<"411\n911\n">>},
  99. {"Counter0",
  100. <<"{% for number in numbers %}{{ forloop.counter0 }}. {{ number }}\n{% endfor %}">>,
  101. [{numbers, ["Zero", "One", "Two"]}], <<"0. Zero\n1. One\n2. Two\n">>},
  102. {"Counter",
  103. <<"{% for number in numbers %}{{ forloop.counter }}. {{ number }}\n{% endfor %}">>,
  104. [{numbers, ["One", "Two", "Three"]}], <<"1. One\n2. Two\n3. Three\n">>},
  105. {"Reverse Counter0",
  106. <<"{% for number in numbers %}{{ forloop.revcounter0 }}. {{ number }}\n{% endfor %}">>,
  107. [{numbers, ["Two", "One", "Zero"]}], <<"2. Two\n1. One\n0. Zero\n">>},
  108. {"Reverse Counter",
  109. <<"{% for number in numbers %}{{ forloop.revcounter }}. {{ number }}\n{% endfor %}">>,
  110. [{numbers, ["Three", "Two", "One"]}], <<"3. Three\n2. Two\n1. One\n">>},
  111. {"Counter \"first\"",
  112. <<"{% for number in numbers %}{% if forloop.first %}{{ number }}{% endif %}{% endfor %}">>,
  113. [{numbers, ["One", "Two", "Three"]}], <<"One">>},
  114. {"Counter \"last\"",
  115. <<"{% for number in numbers %}{% if forloop.last %}{{ number }}{% endif %}{% endfor %}">>,
  116. [{numbers, ["One", "Two", "Three"]}], <<"Three">>},
  117. {"Nested for loop",
  118. <<"{% for outer in list %}{% for inner in outer %}{{ inner }}\n{% endfor %}{% endfor %}">>,
  119. [{'list', [["Al", "Albert"], ["Jo", "Joseph"]]}],
  120. <<"Al\nAlbert\nJo\nJoseph\n">>},
  121. {"Access parent loop counters",
  122. <<"{% for outer in list %}{% for inner in outer %}({{ forloop.parentloop.counter0 }}, {{ forloop.counter0 }})\n{% endfor %}{% endfor %}">>,
  123. [{'list', [["One", "two"], ["One", "two"]]}], <<"(0, 0)\n(0, 1)\n(1, 0)\n(1, 1)\n">>}
  124. ]},
  125. {"ifequal", [
  126. {"Compare variable to literal",
  127. <<"{% ifequal var1 \"foo\" %}yay{% endifequal %}">>,
  128. [{var1, "foo"}], <<"yay">>},
  129. {"Compare variable to unequal literal",
  130. <<"{% ifequal var1 \"foo\" %}boo{% endifequal %}">>,
  131. [{var1, "bar"}], <<>>},
  132. {"Compare literal to variable",
  133. <<"{% ifequal \"foo\" var1 %}yay{% endifequal %}">>,
  134. [{var1, "foo"}], <<"yay">>},
  135. {"Compare literal to unequal variable",
  136. <<"{% ifequal \"foo\" var1 %}boo{% endifequal %}">>,
  137. [{var1, "bar"}], <<>>}
  138. ]},
  139. {"ifequal/else", [
  140. {"Compare variable to literal",
  141. <<"{% ifequal var1 \"foo\" %}yay{% else %}boo{% endifequal %}">>,
  142. [{var1, "foo"}], <<"yay">>},
  143. {"Compare variable to unequal literal",
  144. <<"{% ifequal var1 \"foo\" %}boo{% else %}yay{% endifequal %}">>,
  145. [{var1, "bar"}], <<"yay">>},
  146. {"Compare literal to variable",
  147. <<"{% ifequal \"foo\" var1 %}yay{% else %}boo{% endifequal %}">>,
  148. [{var1, "foo"}], <<"yay">>},
  149. {"Compare literal to unequal variable",
  150. <<"{% ifequal \"foo\" var1 %}boo{% else %}yay{% endifequal %}">>,
  151. [{var1, "bar"}], <<"yay">>}
  152. ]},
  153. {"ifnotequal", [
  154. {"Compare variable to literal",
  155. <<"{% ifnotequal var1 \"foo\" %}boo{% endifnotequal %}">>,
  156. [{var1, "foo"}], <<>>},
  157. {"Compare variable to unequal literal",
  158. <<"{% ifnotequal var1 \"foo\" %}yay{% endifnotequal %}">>,
  159. [{var1, "bar"}], <<"yay">>},
  160. {"Compare literal to variable",
  161. <<"{% ifnotequal \"foo\" var1 %}boo{% endifnotequal %}">>,
  162. [{var1, "foo"}], <<>>},
  163. {"Compare literal to unequal variable",
  164. <<"{% ifnotequal \"foo\" var1 %}yay{% endifnotequal %}">>,
  165. [{var1, "bar"}], <<"yay">>}
  166. ]},
  167. {"ifnotequal/else", [
  168. {"Compare variable to literal",
  169. <<"{% ifnotequal var1 \"foo\" %}boo{% else %}yay{% endifnotequal %}">>,
  170. [{var1, "foo"}], <<"yay">>},
  171. {"Compare variable to unequal literal",
  172. <<"{% ifnotequal var1 \"foo\" %}yay{% else %}boo{% endifnotequal %}">>,
  173. [{var1, "bar"}], <<"yay">>},
  174. {"Compare literal to variable",
  175. <<"{% ifnotequal \"foo\" var1 %}boo{% else %}yay{% endifnotequal %}">>,
  176. [{var1, "foo"}], <<"yay">>},
  177. {"Compare literal to unequal variable",
  178. <<"{% ifnotequal \"foo\" var1 %}yay{% else %}boo{% endifnotequal %}">>,
  179. [{var1, "bar"}], <<"yay">>}
  180. ]},
  181. {"filters", [
  182. {"Filter a literal",
  183. <<"{{ \"pop\"|capfirst }}">>, [],
  184. <<"Pop">>},
  185. {"Filters applied in order",
  186. <<"{{ var1|force_escape|length }}">>, [{var1, <<"&">>}],
  187. <<"5">>},
  188. {"Escape is applied last",
  189. <<"{{ var1|escape|linebreaksbr }}">>, [{var1, <<"\n">>}],
  190. <<"&lt;br /&gt;">>},
  191. {"|add:4",
  192. <<"{{ one|add:4 }}">>, [{one, "1"}],
  193. <<"5">>},
  194. {"|capfirst",
  195. <<"{{ var1|capfirst }}">>, [{var1, "dana boyd"}],
  196. <<"Dana boyd">>},
  197. {"|center:10",
  198. <<"{{ var1|center:10 }}">>, [{var1, "MB"}],
  199. <<" MB ">>},
  200. {"|center:1",
  201. <<"{{ var1|center:1 }}">>, [{var1, "KBR"}],
  202. <<"B">>},
  203. {"|date 1",
  204. <<"{{ var1|date:\"r\" }}">>,
  205. [{var1, {1975,7,24}}],
  206. <<"Thu, 24 Jul 1975 00:00:00 +0000">>},
  207. {"|date 2",
  208. <<"{{ var1|date:\"r\" }}">>,
  209. [{var1, {{1975,7,24}, {7,13,1}}}],
  210. <<"Thu, 24 Jul 1975 07:13:01 +0000">>},
  211. {"|escapejs",
  212. <<"{{ var1|escapejs }}">>, [{var1, "Skip's \"Old-Timey\" Diner"}],
  213. <<"Skip\\'s \\\"Old-Timey\\\" Diner">>},
  214. {"|first",
  215. <<"{{ var1|first }}">>, [{var1, "James"}],
  216. <<"J">>},
  217. {"|fix_ampersands",
  218. <<"{{ var1|fix_ampersands }}">>, [{var1, "Ben & Jerry's"}],
  219. <<"Ben &amp; Jerry's">>},
  220. {"|force_escape",
  221. <<"{{ var1|force_escape }}">>, [{var1, "Ben & Jerry's <=> \"The World's Best Ice Cream\""}],
  222. <<"Ben &amp; Jerry&#039;s &lt;=&gt; &quot;The World&#039;s Best Ice Cream&quot;">>},
  223. {"|format_integer",
  224. <<"{{ var1|format_integer }}">>, [{var1, 28}], <<"28">>},
  225. {"|join:\", \"",
  226. <<"{{ var1|join:\", \" }}">>, [{var1, ["Liberte", "Egalite", "Fraternite"]}],
  227. <<"Liberte, Egalite, Fraternite">>},
  228. {"|last",
  229. <<"{{ var1|last }}">>, [{var1, "XYZ"}],
  230. <<"Z">>},
  231. {"|length",
  232. <<"{{ var1|length }}">>, [{var1, "antidisestablishmentarianism"}],
  233. <<"28">>},
  234. {"|linebreaksbr",
  235. <<"{{ var1|linebreaksbr }}">>, [{var1, "One\nTwo\n\nThree\n\n\n"}],
  236. <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
  237. {"|linebreaksbr",
  238. <<"{{ \"One\\nTwo\\n\\nThree\\n\\n\\n\"|linebreaksbr }}">>, [],
  239. <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
  240. {"|ljust:10",
  241. <<"{{ var1|ljust:10 }}">>, [{var1, "Gore"}],
  242. <<"Gore ">>},
  243. {"|lower",
  244. <<"{{ var1|lower }}">>, [{var1, "E. E. Cummings"}],
  245. <<"e. e. cummings">>},
  246. {"|rjust:10",
  247. <<"{{ var1|rjust:10 }}">>, [{var1, "Bush"}],
  248. <<" Bush">>},
  249. {"|upper",
  250. <<"{{ message|upper }}">>, [{message, "That man has a gun."}],
  251. <<"THAT MAN HAS A GUN.">>},
  252. {"|urlencode",
  253. <<"{{ url|urlencode }}">>, [{url, "You #$*@!!"}],
  254. <<"You+%23%24%2A%40%21%21">>}
  255. ]}
  256. ].
  257. run_tests() ->
  258. Failures = lists:foldl(
  259. fun({Group, Assertions}, GroupAcc) ->
  260. io:format("Running test group ~p...~n", [Group]),
  261. lists:foldl(fun({Name, DTL, Vars, Output}, Acc) ->
  262. case erlydtl_compiler:compile(DTL, erlydtl_running_test, []) of
  263. {ok, _} ->
  264. {ok, IOList} = erlydtl_running_test:render(Vars),
  265. {ok, IOListBin} = erlydtl_running_test:render(vars_to_binary(Vars)),
  266. case {iolist_to_binary(IOList), iolist_to_binary(IOListBin)} of
  267. {Output, Output} ->
  268. Acc;
  269. {Output, Unexpected} ->
  270. [{Group, Name, 'binary', Unexpected, Output} | Acc];
  271. {Unexpected, Output} ->
  272. [{Group, Name, 'list', Unexpected, Output} | Acc];
  273. {Unexpected1, Unexpected2} ->
  274. [{Group, Name, 'list', Unexpected1, Output},
  275. {Group, Name, 'binary', Unexpected2, Output} | Acc]
  276. end;
  277. Err ->
  278. [{Group, Name, Err} | Acc]
  279. end
  280. end, GroupAcc, Assertions)
  281. end, [], tests()),
  282. io:format("Failures: ~p~n", [Failures]),
  283. erlang:halt().
  284. vars_to_binary(Vars) when is_list(Vars) ->
  285. lists:map(fun
  286. ({Key, [H|_] = Value}) when is_tuple(H) ->
  287. {Key, vars_to_binary(Value)};
  288. ({Key, [H|_] = Value}) when is_integer(H) ->
  289. {Key, list_to_binary(Value)};
  290. ({Key, Value}) ->
  291. {Key, Value}
  292. end, Vars);
  293. vars_to_binary(Vars) ->
  294. Vars.
  295. generate_test_date() ->
  296. {{Y,M,D}, _} = erlang:localtime(),
  297. MonthName = [
  298. "January", "February", "March", "April",
  299. "May", "June", "July", "August", "September",
  300. "October", "November", "December"
  301. ],
  302. OrdinalSuffix = [
  303. "st","nd","rd","th","th","th","th","th","th","th", % 1-10
  304. "th","th","th","th","th","th","th","th","th","th", % 10-20
  305. "st","nd","rd","th","th","th","th","th","th","th", % 20-30
  306. "st"
  307. ],
  308. list_to_binary([
  309. "It is the ",
  310. integer_to_list(D),
  311. lists:nth(D, OrdinalSuffix),
  312. " of ", lists:nth(M, MonthName),
  313. " ", integer_to_list(Y), "."
  314. ]).