erlydtl_unittests.erl 52 KB

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