erlydtl_test_defs.erl 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053
  1. -module(erlydtl_test_defs).
  2. -export([tests/0]).
  3. -include("testrunner.hrl").
  4. -record(testrec, {foo, bar, baz}).
  5. -record(person, {first_name, gender}).
  6. %% {Name, DTL, Vars, Output}
  7. %% {Name, DTL, Vars, RenderOpts, Output}
  8. %% {Name, DTL, Vars, RenderOpts, CompilerOpts, Output}
  9. %% {Name, DTL, Vars, RenderOpts, CompilerOpts, Output, Warnings}
  10. tests() ->
  11. [def_to_test(G, D) || {G, Ds} <- all_test_defs(), D <- Ds].
  12. all_test_defs() ->
  13. [{"vars",
  14. [{"string",
  15. <<"String value is: {{ var1 }}">>,
  16. [{var1, "foo"}], <<"String value is: foo">>},
  17. {"int",
  18. <<"The magic number is: {{ var1 }}">>,
  19. [{var1, 42}], <<"The magic number is: 42">>},
  20. {"float",
  21. <<"The price of milk is: {{ var1 }}">>,
  22. [{var1, 0.42}], <<"The price of milk is: 0.42">>},
  23. {"No spaces",
  24. <<"{{var1}}">>,
  25. [{var1, "foo"}], <<"foo">>},
  26. {"Variable name is a tag name",
  27. <<"{{ comment }}">>,
  28. [{comment, "Nice work!"}], <<"Nice work!">>},
  29. #test{
  30. title = "reserved name ok as variable name",
  31. source = <<"{{ from }}">>,
  32. render_vars = [{from, "test"}],
  33. output = <<"test">>
  34. }
  35. ]},
  36. {"maps",
  37. case erlang:is_builtin(erlang, is_map, 1) of
  38. false -> [];
  39. true ->
  40. [#test{
  41. title = "simple test",
  42. source = <<"{{ msg.hello }}">>,
  43. render_vars = [{msg, maps:put(hello, "world", maps:new())}],
  44. output = <<"world">>
  45. },
  46. #test{
  47. title = "various key types",
  48. source = <<"{{ msg.key1 }},{{ msg.key2 }},{{ msg.key3 }},{{ msg.4 }}">>,
  49. render_vars = [{msg, maps:from_list([{key1, 1}, {"key2", 2}, {<<"key3">>, 3}, {4, "value4"}])}],
  50. output = <<"1,2,3,value4">>
  51. }
  52. ]
  53. end},
  54. {"comment",
  55. [{"comment block is excised",
  56. <<"bob {% comment %}(moron){% endcomment %} loblaw">>,
  57. [], <<"bob loblaw">>},
  58. {"inline comment is excised",
  59. <<"you're {# not #} a very nice person">>,
  60. [], <<"you're a very nice person">>}
  61. ]},
  62. {"autoescape",
  63. [{"Autoescape works",
  64. <<"{% autoescape on %}{{ var1 }}{% endautoescape %}">>,
  65. [{var1, "<b>bold</b>"}], <<"&lt;b&gt;bold&lt;/b&gt;">>},
  66. {"Nested autoescape",
  67. <<"{% autoescape on %}{{ var1 }}{% autoescape off %}{{ var1 }}{% endautoescape %}{% endautoescape %}">>,
  68. [{var1, "<b>"}], <<"&lt;b&gt;<b>">>},
  69. {"default auto escape",
  70. <<"{{ var1 }}">>, [{var1, "&"}], [], [auto_escape],
  71. <<"&amp;">>},
  72. {"intermixed autoescape",
  73. <<"{% autoescape on %}1:{{ var1 }}{% endautoescape %} 2:{{ var1 }}{% autoescape on %} 3:{{ var1 }}{% endautoescape %}">>,
  74. [{var1, "&"}],
  75. <<"1:&amp; 2:& 3:&amp;">>}
  76. ]},
  77. {"string literal",
  78. [{"Render literal",
  79. <<"{{ \"foo\" }} is my name">>, [], <<"foo is my name">>},
  80. {"Newlines are escaped",
  81. <<"{{ \"foo\\n\" }}">>, [], <<"foo\n">>},
  82. {"strip quotes",
  83. <<"{{ \"foo\"|add:\"\\\"\" }}">>, [], <<"foo\"">>}
  84. ]},
  85. {"cycle",
  86. [#test{
  87. title = "deprecated cycle syntax",
  88. source = <<"{% for i in test %}{% cycle a,b %}{{ i }},{% endfor %}">>,
  89. render_vars = [{test, [0,1,2,3,4]}],
  90. output = <<"a0,b1,a2,b3,a4,">>
  91. },
  92. {"Cycling through quoted strings",
  93. <<"{% for i in test %}{% cycle 'a' 'b' %}{{ i }},{% endfor %}">>,
  94. [{test, ["0", "1", "2", "3", "4"]}], <<"a0,b1,a2,b3,a4,">>},
  95. {"Cycling through normal variables",
  96. <<"{% for i in test %}{% cycle aye bee %}{{ i }},{% endfor %}">>,
  97. [{test, ["0", "1", "2", "3", "4"]}, {aye, "a"}, {bee, "b"}],
  98. <<"a0,b1,a2,b3,a4,">>},
  99. #test{
  100. title = "mix strings and variables",
  101. source = <<"{% for i in test %}{% cycle 'a' b 'c' %}{{ i }},{% endfor %}">>,
  102. render_vars = [{test, [0,1,2,3,4]}, {b, 'B'}],
  103. output = <<"a0,B1,c2,a3,B4,">>
  104. },
  105. #test{
  106. title = "keep current value in local variable",
  107. source = <<"{% for i in test %}{% cycle 'a' 'b' as c %}{{ i }}{{ c }},{% endfor %}">>,
  108. render_vars = [{test, [0,1,2,3,4]}],
  109. output = <<"a0a,b1b,a2a,b3b,a4a,">>
  110. },
  111. #test{
  112. title = "keep current value silently in local variable",
  113. source = <<"{% for i in test %}{% cycle 'a' 'b' as c silent %}{{ i }}{{ c }},{% endfor %}">>,
  114. render_vars = [{test, [0,1,2,3,4]}],
  115. output = <<"0a,1b,2a,3b,4a,">>
  116. }
  117. ]},
  118. {"number literal",
  119. [{"Render integer",
  120. <<"{{ 5 }}">>, [], <<"5">>}
  121. ]},
  122. {"variable",
  123. [{"Render variable",
  124. <<"{{ var1 }} is my game">>, [{var1, "bar"}], <<"bar is my game">>},
  125. {"Render variable with attribute",
  126. <<"I enjoy {{ var1.game }}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
  127. {"Render variable with string-key attribute",
  128. <<"I also enjoy {{ var1.game }}">>, [{var1, [{"game", "Parcheesi"}]}], <<"I also enjoy Parcheesi">>},
  129. {"Render variable with binary-key attribute",
  130. <<"I also enjoy {{ var1.game }}">>, [{var1, [{<<"game">>, "Parcheesi"}]}], <<"I also enjoy Parcheesi">>},
  131. {"Render variable in dict",
  132. <<"{{ var1 }}">>, dict:store(var1, "bar", dict:new()), <<"bar">>},
  133. {"Render variable with missing attribute in dict",
  134. <<"{{ var1.foo }}">>, [{var1, dict:store(bar, "Othello", dict:new())}], <<"">>},
  135. {"Render variable in gb_tree",
  136. <<"{{ var1 }}">>, gb_trees:insert(var1, "bar", gb_trees:empty()), <<"bar">>},
  137. {"Render variable in arity-1 func",
  138. <<"I enjoy {{ var1 }}">>, fun (var1) -> "Othello" end, <<"I enjoy Othello">>},
  139. {"Render variable with attribute in dict",
  140. <<"{{ var1.attr }}">>, [{var1, dict:store(attr, "Othello", dict:new())}], <<"Othello">>},
  141. {"Render variable with attribute in gb_tree",
  142. <<"{{ var1.attr }}">>, [{var1, gb_trees:insert(attr, "Othello", gb_trees:empty())}], <<"Othello">>},
  143. {"Render variable with attribute in arity-1 func",
  144. <<"I enjoy {{ var1.game }}">>, [{var1, fun (game) -> "Othello" end}], <<"I enjoy Othello">>},
  145. %% {"Render variable in parameterized module",
  146. %% <<"{{ var1.some_var }}">>, [{var1, erlydtl_example_variable_storage:new("foo")}], <<"foo">>},
  147. {"Nested attributes",
  148. <<"{{ person.city.state.country }}">>, [{person, [{city, [{state, [{country, "Italy"}]}]}]}],
  149. <<"Italy">>},
  150. {"Index list variable",
  151. <<"{{ var1.2 }}">>, [{var1, [a, b, c]}],
  152. <<"b">>},
  153. {"Index tuple variable",
  154. <<"{{ var1.2 }}">>, [{var1, {a, b, c}}],
  155. <<"b">>},
  156. {"Index all elements of list (default, 1-based)",
  157. <<"{{ var1.1 }},{{ var1.2 }},{{ var1.3 }}.">>,
  158. [{var1, [a, b, c]}],
  159. <<"a,b,c.">>},
  160. {"Index all list elements 0-based (selected at compile time)",
  161. <<"{{ var1.0 }},{{ var1.1 }},{{ var1.2 }}.">>,
  162. [{var1, [a, b, c]}], [], [lists_0_based],
  163. <<"a,b,c.">>},
  164. {"Index all list elements 0-based (selected at render time)",
  165. <<"{{ var1.0 }},{{ var1.1 }},{{ var1.2 }}.">>,
  166. [{var1, [a, b, c]}], [lists_0_based], [{lists_0_based, defer}],
  167. <<"a,b,c.">>},
  168. {"Index all list elements 1-based (selected at render time)",
  169. <<"{{ var1.1 }},{{ var1.2 }},{{ var1.3 }}.">>,
  170. [{var1, [a, b, c]}], [], [{lists_0_based, defer}],
  171. <<"a,b,c.">>},
  172. {"Index all elements of tuple (default, 1-based)",
  173. <<"{{ var1.1 }},{{ var1.2 }},{{ var1.3 }}.">>,
  174. [{var1, {a, b, c}}],
  175. <<"a,b,c.">>},
  176. {"Index all tuple elements 0-based (selected at compile time)",
  177. <<"{{ var1.0 }},{{ var1.1 }},{{ var1.2 }}.">>,
  178. [{var1, {a, b, c}}], [], [tuples_0_based],
  179. <<"a,b,c.">>},
  180. {"Index all tuple elements 0-based (selected at render time)",
  181. <<"{{ var1.0 }},{{ var1.1 }},{{ var1.2 }}.">>,
  182. [{var1, {a, b, c}}], [tuples_0_based], [{tuples_0_based, defer}],
  183. <<"a,b,c.">>},
  184. {"Index all tuple elements 1-based (selected at render time)",
  185. <<"{{ var1.1 }},{{ var1.2 }},{{ var1.3 }}.">>,
  186. [{var1, {a, b, c}}], [], [{tuples_0_based, defer}],
  187. <<"a,b,c.">>},
  188. {"Index tuple using a \"reserved\" keyword",
  189. <<"{{ list.count }}">>,
  190. [{list, [{count, 123}]}],
  191. <<"123">>},
  192. {"Index list value",
  193. <<"{{ content.description }}">>,
  194. [{content, "test"}], <<"">>},
  195. {"Index binary value",
  196. <<"{{ content.description }}">>,
  197. [{content, <<"test">>}], <<"">>}
  198. ]},
  199. {"now",
  200. [{"now functional",
  201. <<"It is the {% now \"jS \\o\\f F Y\" %}.">>, [{var1, ""}], generate_test_date()}
  202. ]},
  203. {"if",
  204. [{"If/else",
  205. <<"{% if var1 %}boo{% else %}yay{% endif %}">>, [{var1, ""}], <<"yay">>},
  206. {"If elif",
  207. <<"{% if var1 %}boo{% elif var2 %}yay{% endif %}">>, [{var1, ""}, {var2, "happy"}], <<"yay">>},
  208. {"If elif/else",
  209. <<"{% if var1 %}boo{% elif var2 %}sad{% else %}yay{% endif %}">>, [{var1, ""}, {var2, ""}], <<"yay">>},
  210. {"If elif/elif/else",
  211. <<"{% if var1 %}boo{% elif var2 %}yay{% elif var3 %}sad{% else %}noo{% endif %}">>,
  212. [{var1, ""}, {var2, "happy"}, {var3, "not_taken"}],
  213. <<"yay">>},
  214. {"If",
  215. <<"{% if var1 %}boo{% endif %}">>, [{var1, ""}], <<>>},
  216. {"If not",
  217. <<"{% if not var1 %}yay{% endif %}">>, [{var1, ""}], <<"yay">>},
  218. {"If \"0\"",
  219. <<"{% if var1 %}boo{% endif %}">>, [{var1, "0"}], <<>>},
  220. {"If 0",
  221. <<"{% if var1 %}boo{% endif %}">>, [{var1, 0}], <<>>},
  222. {"If false",
  223. <<"{% if var1 %}boo{% endif %}">>, [{var1, false}], <<>>},
  224. {"If false string",
  225. <<"{% if var1 %}boo{% endif %}">>, [{var1, "false"}], <<"boo">>},
  226. {"If undefined",
  227. <<"{% if var1 %}boo{% endif %}">>, [{var1, undefined}], <<>>},
  228. {"If other atom",
  229. <<"{% if var1 %}yay{% endif %}">>, [{var1, foobar}], <<"yay">>},
  230. {"If non-empty string",
  231. <<"{% if var1 %}yay{% endif %}">>, [{var1, "hello"}], <<"yay">>},
  232. {"If proplist",
  233. <<"{% if var1 %}yay{% endif %}">>, [{var1, [{foo, "bar"}]}], <<"yay">>},
  234. {"If complex",
  235. <<"{% if foo.bar.baz %}omgwtfbbq{% endif %}">>, [], <<"">>}
  236. ]},
  237. {"if .. in ..",
  238. [{"If substring in string",
  239. <<"{% if var1 in var2 %}yay{% endif %}">>, [{var1, "rook"}, {var2, "Crooks"}], <<"yay">>},
  240. {"If substring in string (false)",
  241. <<"{% if var1 in var2 %}boo{% endif %}">>, [{var1, "Cook"}, {var2, "Crooks"}], <<>>},
  242. {"If substring not in string",
  243. <<"{% if var1 not in var2 %}yay{% endif %}">>, [{var1, "Cook"}, {var2, "Crooks"}], <<"yay">>},
  244. {"If substring not in string (false)",
  245. <<"{% if var1 not in var2 %}boo{% endif %}">>, [{var1, "rook"}, {var2, "Crooks"}], <<>>},
  246. {"If literal substring in string",
  247. <<"{% if \"man\" in \"Ottoman\" %}yay{% endif %}">>, [], <<"yay">>},
  248. {"If literal substring in string (false)",
  249. <<"{% if \"woman\" in \"Ottoman\" %}boo{% endif %}">>, [], <<>>},
  250. {"If element in list",
  251. <<"{% if var1 in var2 %}yay{% endif %}">>, [{var1, "foo"}, {var2, ["bar", "foo", "baz"]}], <<"yay">>},
  252. {"If element in list (false)",
  253. <<"{% if var1 in var2 %}boo{% endif %}">>, [{var1, "FOO"}, {var2, ["bar", "foo", "baz"]}], <<>>}
  254. ]},
  255. {"if .. and ..",
  256. [{"If true and true",
  257. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, true}, {var2, true}], <<"yay">>},
  258. {"If true and false",
  259. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, true}, {var2, false}], <<"">>},
  260. {"If false and true",
  261. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, false}, {var2, true}], <<"">>},
  262. {"If false and false ",
  263. <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, false}, {var2, false}], <<"">>}
  264. ]},
  265. {"if .. or ..",
  266. [{"If true or true",
  267. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, true}, {var2, true}], <<"yay">>},
  268. {"If true or false",
  269. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, true}, {var2, false}], <<"yay">>},
  270. {"If false or true",
  271. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, false}, {var2, true}], <<"yay">>},
  272. {"If false or false ",
  273. <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, false}, {var2, false}], <<"">>}
  274. ]},
  275. {"if equality",
  276. [{"If int equals number literal",
  277. <<"{% if var1 == 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
  278. {"If int equals number literal (false)",
  279. <<"{% if var1 == 2 %}yay{% endif %}">>, [{var1, 3}], <<"">>},
  280. {"If string equals string literal",
  281. <<"{% if var1 == \"2\" %}yay{% endif %}">>, [{var1, "2"}], <<"yay">>},
  282. {"If string equals string literal (false)",
  283. <<"{% if var1 == \"2\" %}yay{% endif %}">>, [{var1, "3"}], <<"">>},
  284. {"If int not equals number literal",
  285. <<"{% if var1 != 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
  286. {"If string not equals string literal",
  287. <<"{% if var1 != \"2\" %}yay{% endif %}">>, [{var1, "3"}], <<"yay">>},
  288. {"If filter result equals number literal",
  289. <<"{% if var1|length == 2 %}yay{% endif %}">>, [{var1, ["fo", "bo"]}], <<"yay">>},
  290. {"If filter result equals string literal",
  291. <<"{% if var1|capfirst == \"Foo\" %}yay{% endif %}">>, [{var1, "foo"}], <<"yay">>}
  292. ]},
  293. {"if size comparison",
  294. [{"If int greater than number literal",
  295. <<"{% if var1 > 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
  296. {"If int greater than negative number literal",
  297. <<"{% if var1 > -2 %}yay{% endif %}">>, [{var1, -1}], <<"yay">>},
  298. {"If int greater than number literal (false)",
  299. <<"{% if var1 > 2 %}yay{% endif %}">>, [{var1, 2}], <<"">>},
  300. {"If int greater than or equal to number literal",
  301. <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
  302. {"If int greater than or equal to number literal (2)",
  303. <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
  304. {"If int greater than or equal to number literal (false)",
  305. <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 1}], <<"">>},
  306. {"If int less than number literal",
  307. <<"{% if var1 < 2 %}yay{% endif %}">>, [{var1, 1}], <<"yay">>},
  308. {"If int less than number literal (false)",
  309. <<"{% if var1 < 2 %}yay{% endif %}">>, [{var1, 2}], <<"">>},
  310. {"If int less than or equal to number literal",
  311. <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 1}], <<"yay">>},
  312. {"If int less than or equal to number literal",
  313. <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
  314. {"If int less than or equal to number literal (false)",
  315. <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 3}], <<"">>}
  316. ]},
  317. {"if complex bool",
  318. [{"If (true or false) and true",
  319. <<"{% if (var1 or var2) and var3 %}yay{% endif %}">>,
  320. [{var1, true}, {var2, false}, {var3, true}], <<"yay">>},
  321. {"If true or (false and true)",
  322. <<"{% if var1 or (var2 and var3) %}yay{% endif %}">>,
  323. [{var1, true}, {var2, false}, {var3, true}], <<"yay">>}
  324. ]},
  325. {"for",
  326. [{"Simple loop",
  327. <<"{% for x in list %}{{ x }}{% endfor %}">>, [{'list', ["1", "2", "3"]}],
  328. <<"123">>},
  329. {"Reversed loop",
  330. <<"{% for x in list reversed %}{{ x }}{% endfor %}">>, [{'list', ["1", "2", "3"]}],
  331. <<"321">>},
  332. {"Expand list",
  333. <<"{% for x, y in list %}{{ x }},{{ y }}\n{% endfor %}">>, [{'list', [["X", "1"], ["X", "2"]]}],
  334. <<"X,1\nX,2\n">>},
  335. {"Expand tuple",
  336. <<"{% for x, y in list %}{{ x }},{{ y }}\n{% endfor %}">>, [{'list', [{"X", "1"}, {"X", "2"}]}],
  337. <<"X,1\nX,2\n">>},
  338. {"Resolve variable attribute",
  339. <<"{% for number in person.numbers %}{{ number }}\n{% endfor %}">>, [{person, [{numbers, ["411", "911"]}]}],
  340. <<"411\n911\n">>},
  341. {"Resolve nested variable attribute",
  342. <<"{% for number in person.home.numbers %}{{ number }}\n{% endfor %}">>, [{person, [{home, [{numbers, ["411", "911"]}]}]}],
  343. <<"411\n911\n">>},
  344. {"Counter0",
  345. <<"{% for number in numbers %}{{ forloop.counter0 }}. {{ number }}\n{% endfor %}">>,
  346. [{numbers, ["Zero", "One", "Two"]}], <<"0. Zero\n1. One\n2. Two\n">>},
  347. {"Counter",
  348. <<"{% for number in numbers %}{{ forloop.counter }}. {{ number }}\n{% endfor %}">>,
  349. [{numbers, ["One", "Two", "Three"]}], <<"1. One\n2. Two\n3. Three\n">>},
  350. {"Reverse Counter0",
  351. <<"{% for number in numbers %}{{ forloop.revcounter0 }}. {{ number }}\n{% endfor %}">>,
  352. [{numbers, ["Two", "One", "Zero"]}], <<"2. Two\n1. One\n0. Zero\n">>},
  353. {"Reverse Counter",
  354. <<"{% for number in numbers %}{{ forloop.revcounter }}. {{ number }}\n{% endfor %}">>,
  355. [{numbers, ["Three", "Two", "One"]}], <<"3. Three\n2. Two\n1. One\n">>},
  356. {"Counter \"first\"",
  357. <<"{% for number in numbers %}{% if forloop.first %}{{ number }}{% endif %}{% endfor %}">>,
  358. [{numbers, ["One", "Two", "Three"]}], <<"One">>},
  359. {"Counter \"last\"",
  360. <<"{% for number in numbers %}{% if forloop.last %}{{ number }}{% endif %}{% endfor %}">>,
  361. [{numbers, ["One", "Two", "Three"]}], <<"Three">>},
  362. {"Nested for loop",
  363. <<"{% for outer in list %}{% for inner in outer %}{{ inner }}\n{% endfor %}{% endfor %}">>,
  364. [{'list', [["Al", "Albert"], ["Jo", "Joseph"]]}],
  365. <<"Al\nAlbert\nJo\nJoseph\n">>},
  366. {"Unused variable in foreach proplist",
  367. <<"{% for k,v in plist %}{{v}}{% endfor %}">>,
  368. [{'plist',[{1,"one"},{2,"two"}]}], [], [], <<"onetwo">>,
  369. [error_info([{0, erl_lint, {unused_var, 'Var_k/1_1:8'}}])]},
  370. {"Unused variable in foreach proplist, prefixed with underscore",
  371. <<"{% for _k,v in plist %}{{v}}{% endfor %}">>,
  372. [{'plist',[{1,"one"},{2,"two"}]}], [], [], <<"onetwo">>},
  373. {"Access parent loop counters",
  374. <<"{% for outer in list %}{% for inner in outer %}({{ forloop.parentloop.counter0 }}, {{ forloop.counter0 }})\n{% endfor %}{% endfor %}">>,
  375. [{'list', [["One", "two"], ["One", "two"]]}], [], [], <<"(0, 0)\n(0, 1)\n(1, 0)\n(1, 1)\n">>,
  376. %% the warnings we get from the erlang compiler still needs some care..
  377. [error_info([{0, erl_lint, {unused_var, 'Var_inner/3_1:31'}}])]},
  378. {"If changed",
  379. <<"{% for x in list %}{% ifchanged %}{{ x }}\n{% endifchanged %}{% endfor %}">>,
  380. [{'list', ["one", "two", "two", "three", "three", "three"]}], <<"one\ntwo\nthree\n">>},
  381. {"If changed/2",
  382. <<"{% for x, y in list %}{% ifchanged %}{{ x|upper }}{% endifchanged %}{% ifchanged %}{{ y|lower }}{% endifchanged %}\n{% endfor %}">>,
  383. [{'list', [["one", "a"], ["two", "A"], ["two", "B"], ["three", "b"], ["three", "c"], ["Three", "b"]]}], <<"ONEa\nTWO\nb\nTHREE\nc\nb\n">>},
  384. {"If changed/else",
  385. <<"{% for x in list %}{% ifchanged %}{{ x }}\n{% else %}foo\n{% endifchanged %}{% endfor %}">>,
  386. [{'list', ["one", "two", "two", "three", "three", "three"]}], <<"one\ntwo\nfoo\nthree\nfoo\nfoo\n">>},
  387. {"If changed/param",
  388. <<"{% for date in list %}{% ifchanged date.month %} {{ date.month }}:{{ date.day }}{% else %},{{ date.day }}{% endifchanged %}{% endfor %}\n">>,
  389. [{'list', [[{month,"Jan"},{day,1}],[{month,"Jan"},{day,2}],[{month,"Apr"},{day,10}],
  390. [{month,"Apr"},{day,11}],[{month,"May"},{day,4}]]}],
  391. <<" Jan:1,2 Apr:10,11 May:4\n">>},
  392. {"If changed/param2",
  393. <<"{% for x, y in list %}{% ifchanged y|upper %}{{ x|upper }}{% endifchanged %}\n{% endfor %}">>,
  394. [{'list', [["one", "a"], ["two", "A"], ["two", "B"], ["three", "b"], ["three", "c"], ["Three", "b"]]}], <<"ONE\n\nTWO\n\nTHREE\nTHREE\n">>},
  395. {"If changed/param2 combined",
  396. <<"{% for x, y in list %}{% ifchanged x y|upper %}{{ x }}{% endifchanged %}\n{% endfor %}">>,
  397. [{'list', [["one", "a"], ["two", "A"], ["two", "B"], ["three", "b"], ["three", "B"], ["three", "c"]]}], <<"one\ntwo\ntwo\nthree\n\nthree\n">>},
  398. {"If changed/resolve",
  399. <<"{% for x in list %}{% ifchanged x.name|first %}{{ x.value }}{% endifchanged %}\n{% endfor %}">>,
  400. [{'list', [[{"name", ["nA","nB"]},{"value","1"}],[{"name", ["nA","nC"]},{"value","2"}],
  401. [{"name", ["nB","nC"]},{"value","3"}],[{"name", ["nB","nA"]},{"value","4"}]]}],
  402. <<"1\n\n3\n\n">>},
  403. {"Loop undefined var",
  404. <<"{% for i in undef %}i = {{ i }}.\n{% endfor %}">>,
  405. [],
  406. <<"">>},
  407. {"Loop filtered value rather than variable",
  408. <<"{% for x in 123|make_list %}{% if not forloop.first %}, {% endif %}{{ x }}{% endfor %}">>,
  409. [],
  410. <<"1, 2, 3">>}
  411. ]},
  412. {"for/empty",
  413. [{"Simple loop",
  414. <<"{% for x in list %}{{ x }}{% empty %}shucks{% endfor %}">>, [{'list', ["1", "2", "3"]}],
  415. <<"123">>},
  416. {"Simple loop (empty)",
  417. <<"{% for x in list %}{{ x }}{% empty %}shucks{% endfor %}">>, [{'list', []}],
  418. <<"shucks">>}
  419. ]},
  420. {"ifequal",
  421. [{"Compare variable to literal",
  422. <<"{% ifequal var1 \"foo\" %}yay{% endifequal %}">>,
  423. [{var1, "foo"}], <<"yay">>},
  424. {"Compare variable to unequal literal",
  425. <<"{% ifequal var1 \"foo\" %}boo{% endifequal %}">>,
  426. [{var1, "bar"}], <<>>},
  427. {"Compare literal to variable",
  428. <<"{% ifequal \"foo\" var1 %}yay{% endifequal %}">>,
  429. [{var1, "foo"}], <<"yay">>},
  430. {"Compare literal to unequal variable",
  431. <<"{% ifequal \"foo\" var1 %}boo{% endifequal %}">>,
  432. [{var1, "bar"}], <<>>},
  433. {"Compare variable to literal (int string)",
  434. <<"{% ifequal var1 \"2\" %}yay{% else %}boo{% endifequal %}">>,
  435. [{var1, "2"}], <<"yay">>},
  436. {"Compare variable to literal (int)",
  437. <<"{% ifequal var1 2 %}yay{% else %}boo{% endifequal %}">>,
  438. [{var1, 2}], <<"yay">>},
  439. {"Compare variable to unequal literal (int)",
  440. <<"{% ifequal var1 2 %}boo{% else %}yay{% endifequal %}">>,
  441. [{var1, 3}], <<"yay">>},
  442. {"Compare variable to equal literal (atom)",
  443. <<"{% ifequal var1 \"foo\"%}yay{% endifequal %}">>,
  444. [{var1, foo}], <<"yay">>},
  445. {"Compare variable to unequal literal (atom)",
  446. <<"{% ifequal var1 \"foo\"%}yay{% else %}boo{% endifequal %}">>,
  447. [{var1, bar}], <<"boo">>}
  448. ]},
  449. {"ifequal/else",
  450. [{"Compare variable to literal",
  451. <<"{% ifequal var1 \"foo\" %}yay{% else %}boo{% endifequal %}">>,
  452. [{var1, "foo"}], <<"yay">>},
  453. {"Compare variable to unequal literal",
  454. <<"{% ifequal var1 \"foo\" %}boo{% else %}yay{% endifequal %}">>,
  455. [{var1, "bar"}], <<"yay">>},
  456. {"Compare literal to variable",
  457. <<"{% ifequal \"foo\" var1 %}yay{% else %}boo{% endifequal %}">>,
  458. [{var1, "foo"}], <<"yay">>},
  459. {"Compare literal to unequal variable",
  460. <<"{% ifequal \"foo\" var1 %}boo{% else %}yay{% endifequal %}">>,
  461. [{var1, "bar"}], <<"yay">>}
  462. ]},
  463. {"ifnotequal",
  464. [{"Compare variable to literal",
  465. <<"{% ifnotequal var1 \"foo\" %}boo{% endifnotequal %}">>,
  466. [{var1, "foo"}], <<>>},
  467. {"Compare variable to unequal literal",
  468. <<"{% ifnotequal var1 \"foo\" %}yay{% endifnotequal %}">>,
  469. [{var1, "bar"}], <<"yay">>},
  470. {"Compare literal to variable",
  471. <<"{% ifnotequal \"foo\" var1 %}boo{% endifnotequal %}">>,
  472. [{var1, "foo"}], <<>>},
  473. {"Compare literal to unequal variable",
  474. <<"{% ifnotequal \"foo\" var1 %}yay{% endifnotequal %}">>,
  475. [{var1, "bar"}], <<"yay">>}
  476. ]},
  477. {"ifnotequal/else",
  478. [{"Compare variable to literal",
  479. <<"{% ifnotequal var1 \"foo\" %}boo{% else %}yay{% endifnotequal %}">>,
  480. [{var1, "foo"}], <<"yay">>},
  481. {"Compare variable to unequal literal",
  482. <<"{% ifnotequal var1 \"foo\" %}yay{% else %}boo{% endifnotequal %}">>,
  483. [{var1, "bar"}], <<"yay">>},
  484. {"Compare literal to variable",
  485. <<"{% ifnotequal \"foo\" var1 %}boo{% else %}yay{% endifnotequal %}">>,
  486. [{var1, "foo"}], <<"yay">>},
  487. {"Compare literal to unequal variable",
  488. <<"{% ifnotequal \"foo\" var1 %}yay{% else %}boo{% endifnotequal %}">>,
  489. [{var1, "bar"}], <<"yay">>}
  490. ]},
  491. {"filter tag",
  492. [{"Apply a filter",
  493. <<"{% filter escape %}&{% endfilter %}">>, [], <<"&amp;">>},
  494. {"Chained filters",
  495. <<"{% filter linebreaksbr|escape %}\n{% endfilter %}">>, [], <<"&lt;br /&gt;">>}
  496. ]},
  497. {"filters",
  498. [{"Filter a literal",
  499. <<"{{ \"pop\"|capfirst }}">>, [],
  500. <<"Pop">>},
  501. {"Filters applied in order",
  502. <<"{{ var1|force_escape|length }}">>, [{var1, <<"&">>}],
  503. <<"5">>},
  504. {"Escape is applied last",
  505. <<"{{ var1|escape|linebreaksbr }}">>, [{var1, <<"\n">>}],
  506. <<"&lt;br /&gt;">>},
  507. {"add; lhs number, rhs number",
  508. <<"{{ one|add:4}}">>, [{one, 1}],
  509. <<"5">>},
  510. {"add; lhs numeric string, rhs number",
  511. <<"{{ one|add:4}}">>, [{one, "1"}],
  512. <<"5">>},
  513. {"add; lhs number, rhs numeric string",
  514. <<"{{ one|add:'4'}}">>, [{one, 1}],
  515. <<"5">>},
  516. {"add; lhs non-numeric string, rhs number",
  517. <<"{{ one|add:4}}">>, [{one, "foo"}],
  518. <<"foo4">>},
  519. {"add; lhs number, rhs non-numeric string",
  520. <<"{{ one|add:'foo'}}">>, [{one, 1}],
  521. <<"1foo">>},
  522. {"add; lhs non-numeric string, rhs non-numeric string",
  523. <<"{{ one|add:'bar'}}">>, [{one, "foo"}],
  524. <<"foobar">>},
  525. {"add; lhs numeric string, rhs numeric string",
  526. <<"{{ one|add:'4'}}">>, [{one, "1"}],
  527. <<"5">>},
  528. {"|addslashes",
  529. <<"{{ var1|addslashes }}">>, [{var1, "Jimmy's \"great\" meats'n'things"}],
  530. <<"Jimmy\\'s \\\"great\\\" meats\\'n\\'things">>},
  531. {"|capfirst",
  532. <<"{{ var1|capfirst }}">>, [{var1, "dana boyd"}],
  533. <<"Dana boyd">>},
  534. {"|center:10",
  535. <<"{{ var1|center:10 }}">>, [{var1, "MB"}],
  536. <<" MB ">>},
  537. {"|center:1",
  538. <<"{{ var1|center:1 }}">>, [{var1, "KBR"}],
  539. <<"B">>},
  540. {"|cut:\" \"",
  541. <<"{{ var1|cut:\" \" }}">>, [{var1, "String with spaces"}],
  542. <<"Stringwithspaces">>},
  543. {"|date 1",
  544. <<"{{ var1|date:\"jS F Y H:i\" }}">>,
  545. [{var1, {1975,7,24}}],
  546. <<"24th July 1975 00:00">>},
  547. {"|date 2",
  548. <<"{{ var1|date:\"jS F Y H:i\" }}">>,
  549. [{var1, {{1975,7,24}, {7,13,1}}}],
  550. <<"24th July 1975 07:13">>},
  551. {"|date 3",
  552. <<"{{ var1|date }}">>,
  553. [{var1, {{1975,7,24}, {7,13,1}}}],
  554. <<"July 24, 1975">>},
  555. {"|default:\"foo\" 1",
  556. <<"{{ var1|default:\"foo\" }}">>, [], <<"foo">>},
  557. {"|default:\"foo\" 2",
  558. <<"{{ var1|default:\"foo\" }}">>, [{var1, "bar"}], <<"bar">>},
  559. {"|default:\"foo\" 3",
  560. <<"{{ var1|default:\"foo\" }}">>, [{var1, "0"}], <<"foo">>},
  561. {"|default_if_none:\"foo\"",
  562. <<"{{ var1|default_if_none:\"foo\" }}">>, [], <<"foo">>},
  563. {"|default_if_none:\"foo\" 2",
  564. <<"{{ var1|default_if_none:\"foo\" }}">>, [{var1, "bar"}], <<"bar">>},
  565. {"|dictsort 1",
  566. <<"{{ var1|dictsort:\"foo\" }}">>,
  567. [{var1,[[{foo,2}],[{foo,1}]]}], <<"{foo,1}{foo,2}">>},
  568. {"|dictsort 2",
  569. <<"{{ var1|dictsort:\"foo.bar\" }}">>,
  570. [{var1,[[{foo,[{bar,2}]}],[{foo,[{bar,1}]}]]}],
  571. <<"{foo,[{bar,1}]}{foo,[{bar,2}]}">>},
  572. {"|divisibleby:\"3\"",
  573. <<"{% if var1|divisibleby:\"3\" %}yay{% endif %}">>, [{var1, 21}], <<"yay">>},
  574. {"|divisibleby:\"3\"",
  575. <<"{% if var1|divisibleby:\"3\" %}yay{% endif %}">>, [{var1, 22}], <<"">>},
  576. {"|escape",
  577. <<"{% autoescape on %}{{ var1|escape|escape|escape }}{% endautoescape %}">>, [{var1, ">&1"}], <<"&gt;&amp;1">>},
  578. {"|escapejs",
  579. <<"{{ var1|escapejs }}">>, [{var1, "testing\r\njavascript 'string\" <b>escaping</b>"}],
  580. <<"testing\\u000D\\u000Ajavascript \\u0027string\\u0022 \\u003Cb\\u003Eescaping\\u003C/b\\u003E">>},
  581. {"|filesizeformat (bytes)",
  582. <<"{{ var1|filesizeformat }}">>, [{var1, 1023}], <<"1023 bytes">>},
  583. {"|filesizeformat (KB)",
  584. <<"{{ var1|filesizeformat }}">>, [{var1, 3487}], <<"3.4 KB">>},
  585. {"|filesizeformat (MB)",
  586. <<"{{ var1|filesizeformat }}">>, [{var1, 6277098}], <<"6.0 MB">>},
  587. {"|filesizeformat (GB)",
  588. <<"{{ var1|filesizeformat }}">>, [{var1, 1024 * 1024 * 1024}], <<"1.0 GB">>},
  589. {"|first",
  590. <<"{{ var1|first }}">>, [{var1, "James"}],
  591. <<"J">>},
  592. {"|fix_ampersands",
  593. <<"{{ var1|fix_ampersands }}">>, [{var1, "Ben & Jerry's"}],
  594. <<"Ben &amp; Jerry's">>},
  595. {"|floatformat:\"-1\"",
  596. <<"{{ var1|floatformat:\"-1\" }}">>, [{var1, 34.23234}],
  597. <<"34.2">>},
  598. {"int |floatformat",
  599. <<"{{ var1|floatformat:\"-1\" }}">>, [{var1, 123}],
  600. <<"123">>},
  601. {"string |floatformat",
  602. <<"{{ var1|floatformat:\"-1\" }}">>, [{var1, "123.321"}],
  603. <<"123.3">>},
  604. {"binary |floatformat",
  605. <<"{{ var1|floatformat:\"-1\" }}">>, [{var1, <<"123.321">>}],
  606. <<"123.3">>},
  607. %% from: https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#floatformat
  608. {"1.a) |floatformat",
  609. <<"{{ var1|floatformat }}">>, [{var1, 34.23234}],
  610. <<"34.2">>},
  611. {"1.b) |floatformat",
  612. <<"{{ var1|floatformat }}">>, [{var1, 34.00000}],
  613. <<"34">>},
  614. {"1.c) |floatformat",
  615. <<"{{ var1|floatformat }}">>, [{var1, 34.26000}],
  616. <<"34.3">>},
  617. {"2.a) |floatformat:\"3\"",
  618. <<"{{ var1|floatformat:\"3\" }}">>, [{var1, 34.23234}],
  619. <<"34.232">>},
  620. {"2.b) |floatformat:\"3\"",
  621. <<"{{ var1|floatformat:\"3\" }}">>, [{var1, 34.00000}],
  622. <<"34.000">>},
  623. {"2.c) |floatformat:\"3\"",
  624. <<"{{ var1|floatformat:\"3\" }}">>, [{var1, 34.26000}],
  625. <<"34.260">>},
  626. {"3.a) |floatformat:\"0\"",
  627. <<"{{ var1|floatformat:\"0\" }}">>, [{var1, 34.23234}],
  628. <<"34">>},
  629. {"3.b) |floatformat:\"0\"",
  630. <<"{{ var1|floatformat:\"0\" }}">>, [{var1, 34.00000}],
  631. <<"34">>},
  632. {"3.c) |floatformat:\"0\"",
  633. <<"{{ var1|floatformat:\"0\" }}">>, [{var1, 39.56000}],
  634. <<"40">>},
  635. {"4.a) |floatformat:\"-3\"",
  636. <<"{{ var1|floatformat:\"-3\" }}">>, [{var1, 34.23234}],
  637. <<"34.232">>},
  638. {"4.b) |floatformat:\"-3\"",
  639. <<"{{ var1|floatformat:\"-3\" }}">>, [{var1, 34.00000}],
  640. <<"34">>},
  641. {"4.c) |floatformat:\"-3\"",
  642. <<"{{ var1|floatformat:\"-3\" }}">>, [{var1, 34.26000}],
  643. <<"34.260">>},
  644. {"|force_escape",
  645. <<"{{ var1|force_escape }}">>, [{var1, "Ben & Jerry's <=> \"The World's Best Ice Cream\""}],
  646. <<"Ben &amp; Jerry&#039;s &lt;=&gt; &quot;The World&#039;s Best Ice Cream&quot;">>},
  647. {"iolist |force_escape",
  648. <<"{{ var1|force_escape }}">>, [{var1, ["'a'"]}],
  649. <<"&#039;a&#039;">>},
  650. {"nested iolist |force_escape",
  651. <<"{{ var1|force_escape }}">>, [{var1, ["a'", <<"b">>, [<<"<c">>, "d", ["e>"]]]}],
  652. <<"a&#039;b&lt;cde&gt;">>},
  653. {"|format_integer",
  654. <<"{{ var1|format_integer }}">>, [{var1, 28}], <<"28">>},
  655. {"|format_number 1",
  656. <<"{{ var1|format_number }}">>, [{var1, 28}], <<"28">>},
  657. {"|format_number 2",
  658. <<"{{ var1|format_number }}">>, [{var1, 23.77}], <<"23.77">>},
  659. {"|format_number 3",
  660. <<"{{ var1|format_number }}">>, [{var1, "28.77"}], <<"28.77">>},
  661. {"|format_number 4",
  662. <<"{{ var1|format_number }}">>, [{var1, "23.77"}], <<"23.77">>},
  663. {"|format_number 5",
  664. <<"{{ var1|format_number }}">>, [{var1, fun() -> 29 end}], <<"29">>},
  665. {"|format_number 6",
  666. <<"{{ var1|format_number }}">>, [{var1, fun() -> fun() -> 31 end end}], <<"31">>},
  667. {"|get_digit:\"2\"",
  668. <<"{{ var1|get_digit:\"2\" }}">>, [{var1, 42}], <<"4">>},
  669. {"|iriencode",
  670. <<"{{ url|iriencode }}">>, [{url, "You #$*@!!"}], <<"You+#$*@!!">>},
  671. {"|join:\", \" (list)",
  672. <<"{{ var1|join:\", \" }}">>, [{var1, ["Liberte", "Egalite", "Fraternite"]}],
  673. <<"Liberte, Egalite, Fraternite">>},
  674. {"|join:\", \" (binary)",
  675. <<"{{ var1|join:\", \" }}">>, [{var1, [<<"Liberte">>, "Egalite", <<"Fraternite">>]}],
  676. <<"Liberte, Egalite, Fraternite">>},
  677. {"|last",
  678. <<"{{ var1|last }}">>, [{var1, "XYZ"}],
  679. <<"Z">>},
  680. {"|length",
  681. <<"{{ var1|length }}">>, [{var1, "antidisestablishmentarianism"}],
  682. <<"28">>},
  683. {"|linebreaks",
  684. <<"{{ var1|linebreaks }}">>, [{var1, "Joel\nis a slug"}],
  685. <<"<p>Joel<br />is a slug</p>">>},
  686. {"|linebreaks",
  687. <<"{{ var1|linebreaks }}">>, [{var1, "Joel\n\n\n\nis a slug"}],
  688. <<"<p>Joel</p><p>is a slug</p>">>},
  689. {"|linebreaks",
  690. <<"{{ var1|linebreaks }}">>, [{var1, "Joel\n\nis a \nslug"}],
  691. <<"<p>Joel</p><p>is a <br />slug</p>">>},
  692. {"|linebreaksbr",
  693. <<"{{ var1|linebreaksbr }}">>, [{var1, "One\nTwo\n\nThree\n\n\n"}],
  694. <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
  695. {"|linebreaksbr",
  696. <<"{{ \"One\\nTwo\\n\\nThree\\n\\n\\n\"|linebreaksbr }}">>, [],
  697. <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
  698. {"|linenumbers",
  699. <<"{{ var1|linenumbers }}">>, [{var1, "a\nb\nc"}],
  700. <<"1. a\n2. b\n3. c">>},
  701. {"|linenumbers",
  702. <<"{{ var1|linenumbers }}">>, [{var1, "a"}],
  703. <<"1. a">>},
  704. {"|linenumbers",
  705. <<"{{ var1|linenumbers }}">>, [{var1, "a\n"}],
  706. <<"1. a\n2. ">>},
  707. {"|ljust:10",
  708. <<"{{ var1|ljust:10 }}">>, [{var1, "Gore"}],
  709. <<"Gore ">>},
  710. {"|lower",
  711. <<"{{ var1|lower }}">>, [{var1, "E. E. Cummings"}],
  712. <<"e. e. cummings">>},
  713. {"|makelist",
  714. <<"{{ list|make_list }}">>, [{list, "Joel"}],
  715. <<"J","o","e","l">>},
  716. {"|pluralize",
  717. <<"{{ num|pluralize }}">>, [{num, 1}],
  718. <<"">>},
  719. {"|pluralize",
  720. <<"{{ num|pluralize }}">>, [{num, 2}],
  721. <<"s">>},
  722. {"|pluralize:\"s\"",
  723. <<"{{ num|pluralize }}">>, [{num, 1}],
  724. <<"">>},
  725. {"|pluralize:\"s\"",
  726. <<"{{ num|pluralize }}">>, [{num, 2}],
  727. <<"s">>},
  728. {"|pluralize:\"y,es\" (list)",
  729. <<"{{ num|pluralize:\"y,es\" }}">>, [{num, 1}],
  730. <<"y">>},
  731. {"|pluralize:\"y,es\" (list)",
  732. <<"{{ num|pluralize:\"y,es\" }}">>, [{num, 2}],
  733. <<"es">>},
  734. {"|length|pluralize",
  735. <<"{{ list|length|pluralize:\"plural\" }}">>, [{list, [foo, bar]}],
  736. <<"plural">>},
  737. {"|length|pluralize",
  738. <<"{{ list|length|pluralize:\"plural\" }}">>, [{list, [foo]}],
  739. <<"">>},
  740. {"|random",
  741. <<"{{ var1|random }}">>, [{var1, ["foo", "foo", "foo"]}],
  742. <<"foo">>},
  743. {"|removetags:\"b span\"",
  744. <<"{{ var1|removetags:\"b span\" }}">>, [{var1, "<B>Joel</B> <button>is</button> a <span>slug</span>"}],
  745. <<"<B>Joel</B> <button>is</button> a slug">>},
  746. {"|rjust:10",
  747. <<"{{ var1|rjust:10 }}">>, [{var1, "Bush"}],
  748. <<" Bush">>},
  749. {"|safe",
  750. <<"{% autoescape on %}{{ var1|safe|escape }}{% endautoescape %}">>, [{var1, "&"}],
  751. <<"&">>},
  752. {"|safe is local",
  753. <<"{{ var1 }}{{ var1|safe }}{{ var1 }}">>, [{var1, "&"}], [], [auto_escape],
  754. <<"&amp;&&amp;">>},
  755. %%python/django slice is zero based, erlang lists are 1 based
  756. %%first number included, second number not
  757. %%negative numbers are allowed
  758. %%regex to convert from erlydtl_filters_tests:
  759. % for slice: \?assert.*\( \[(.*)\], erlydtl_filters:(.*)\((.*),"(.*)"\)\),
  760. % {"|slice:\"$4\"", <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],<<$1>>},
  761. % \t\t{"|slice:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>},
  762. %
  763. % for stringformat:
  764. % \?assert.*\( (.*), erlydtl_filters:(.*)\((.*), "(.*)"\) \)
  765. % \t\t{"|stringformat:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>}
  766. {"|slice:\":\"",
  767. <<"{{ var|slice:\":\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  768. <<1,2,3,4,5,6,7,8,9>>},
  769. {"|slice:\"1\"",
  770. <<"{{ var|slice:\"1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  771. <<"2">>},
  772. {"|slice:\"100\"",
  773. <<"{{ var|slice:\"100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  774. <<"indexError">>},
  775. {"|slice:\"-1\"",
  776. <<"{{ var|slice:\"-1\" }}">>, [{var, ["a","b","c","d","e","f","g","h","i"]}],
  777. <<"i">>},
  778. {"|slice:\"-1\"",
  779. <<"{{ var|slice:\"-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  780. <<"9">>},
  781. {"|slice:\"-100\"",
  782. <<"{{ var|slice:\"-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  783. <<"indexError">>},
  784. {"|slice:\"1:\"",
  785. <<"{{ var|slice:\"1:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  786. <<2,3,4,5,6,7,8,9>>},
  787. {"|slice:\"100:\"",
  788. <<"{{ var|slice:\"100:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  789. <<>>},
  790. {"|slice:\"-1:\"",
  791. <<"{{ var|slice:\"-1:\" }}">>, [{var, ["a","b","c","d","e","f","h","i","j"]}],
  792. <<"j">>},
  793. {"|slice:\"-1:\"",
  794. <<"{{ var|slice:\"-1:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  795. <<9>>},
  796. {"|slice:\"-100:\"",
  797. <<"{{ var|slice:\"-100:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  798. <<1,2,3,4,5,6,7,8,9>>},
  799. {"|slice:\":1\"",
  800. <<"{{ var|slice:\":1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  801. <<1>>},
  802. {"|slice:\":100\"",
  803. <<"{{ var|slice:\":100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  804. <<1,2,3,4,5,6,7,8,9>>},
  805. {"|slice:\":-1\"",
  806. <<"{{ var|slice:\":-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  807. <<1,2,3,4,5,6,7,8>>},
  808. {"|slice:\":-100\"",
  809. <<"{{ var|slice:\":-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  810. <<>>},
  811. {"|slice:\"-1:-1\"",
  812. <<"{{ var|slice:\"-1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  813. <<>>},
  814. {"|slice:\"1:1\"",
  815. <<"{{ var|slice:\"1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  816. <<>>},
  817. {"|slice:\"1:-1\"",
  818. <<"{{ var|slice:\"1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  819. <<2,3,4,5,6,7,8>>},
  820. {"|slice:\"-1:1\"",
  821. <<"{{ var|slice:\"-1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  822. <<>>},
  823. {"|slice:\"-100:-100\"",
  824. <<"{{ var|slice:\"-100:-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  825. <<>>},
  826. {"|slice:\"100:100\"",
  827. <<"{{ var|slice:\"100:100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  828. <<>>},
  829. {"|slice:\"100:-100\"",
  830. <<"{{ var|slice:\"100:-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  831. <<>>},
  832. {"|slice:\"-100:100\"",
  833. <<"{{ var|slice:\"-100:100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  834. <<1,2,3,4,5,6,7,8,9>>},
  835. {"|slice:\"1:3\"",
  836. <<"{{ var|slice:\"1:3\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  837. <<2,3>>},
  838. {"|slice:\"::\"",
  839. <<"{{ var|slice:\"::\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  840. <<1,2,3,4,5,6,7,8,9>>},
  841. {"|slice:\"1:9:1\"",
  842. <<"{{ var|slice:\"1:9:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  843. <<2,3,4,5,6,7,8,9>>},
  844. {"|slice:\"10:1:-1\"",
  845. <<"{{ var|slice:\"10:1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  846. <<9,8,7,6,5,4,3>>},
  847. {"|slice:\"-111:-1:1\"",
  848. <<"{{ var|slice:\"-111:-1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  849. <<1,2,3,4,5,6,7,8>>},
  850. {"|slice:\"-111:-111:1\"",
  851. <<"{{ var|slice:\"-111:-111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  852. <<>>},
  853. {"|slice:\"111:111:1\"",
  854. <<"{{ var|slice:\"111:111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  855. <<>>},
  856. {"|slice:\"-111:111:1\"",
  857. <<"{{ var|slice:\"-111:111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  858. <<1,2,3,4,5,6,7,8,9>>},
  859. {"|slice:\"111:-111:1\"",
  860. <<"{{ var|slice:\"111:-111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  861. <<>>},
  862. {"|slice:\"-111:-111:-1\"",
  863. <<"{{ var|slice:\"-111:-111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  864. <<>>},
  865. {"|slice:\"111:111:-1\"",
  866. <<"{{ var|slice:\"111:111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  867. <<>>},
  868. {"|slice:\"-111:111:-1\"",
  869. <<"{{ var|slice:\"-111:111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  870. <<>>},
  871. {"|slice:\"111:-111:-1\"",
  872. <<"{{ var|slice:\"111:-111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
  873. <<9,8,7,6,5,4,3,2,1>>}, {"|phone2numeric",
  874. <<"{{ var1|phone2numeric }}">>, [{var1, "1-800-COLLECT"}],
  875. <<"1-800-2655328">>},
  876. {"|slugify",
  877. <<"{{ var1|slugify }}">>, [{var1, "What The $#_! Was He Thinking?"}],
  878. <<"what-the-_-was-he-thinking">>},
  879. {"|slice:\"s\"",
  880. <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
  881. <<"test">>},
  882. {"|stringformat:\"s\"",
  883. <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
  884. <<"test">>},
  885. {"|stringformat:\"s\"",
  886. <<"{{ var|stringformat:\"s\" }}">>, [{var, "1"}],
  887. <<"1">>},
  888. {"|stringformat:\"s\"",
  889. <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
  890. <<"test">>},
  891. {"|stringformat:\"10s\"",
  892. <<"{{ var|stringformat:\"10s\" }}">>, [{var, "test"}],
  893. <<" test">>},
  894. {"|stringformat:\"-10s\"",
  895. <<"{{ var|stringformat:\"-10s\" }}">>, [{var, "test"}],
  896. <<"test ">>},
  897. {"|stringformat:\"d\"",
  898. <<"{{ var|stringformat:\"d\" }}">>, [{var, "90"}],
  899. <<"90">>},
  900. {"|stringformat:\"10d\"",
  901. <<"{{ var|stringformat:\"10d\" }}">>, [{var, "90"}],
  902. <<" 90">>},
  903. {"|stringformat:\"-10d\"",
  904. <<"{{ var|stringformat:\"-10d\" }}">>, [{var, "90"}],
  905. <<"90 ">>},
  906. {"|stringformat:\"i\"",
  907. <<"{{ var|stringformat:\"i\" }}">>, [{var, "90"}],
  908. <<"90">>},
  909. {"|stringformat:\"10i\"",
  910. <<"{{ var|stringformat:\"10i\" }}">>, [{var, "90"}],
  911. <<" 90">>},
  912. {"|stringformat:\"-10i\"",
  913. <<"{{ var|stringformat:\"-10i\" }}">>, [{var, "90"}],
  914. <<"90 ">>},
  915. {"|stringformat:\"0.2d\"",
  916. <<"{{ var|stringformat:\"0.2d\" }}">>, [{var, "9"}],
  917. <<"09">>},
  918. {"|stringformat:\"10.4d\"",
  919. <<"{{ var|stringformat:\"10.4d\" }}">>, [{var, "9"}],
  920. <<" 0009">>},
  921. {"|stringformat:\"-10.4d\"",
  922. <<"{{ var|stringformat:\"-10.4d\" }}">>, [{var, "9"}],
  923. <<"0009 ">>},
  924. {"|stringformat:\"f\"",
  925. <<"{{ var|stringformat:\"f\" }}">>, [{var, "1"}],
  926. <<"1.000000">>},
  927. {"|stringformat:\".2f\"",
  928. <<"{{ var|stringformat:\".2f\" }}">>, [{var, "1"}],
  929. <<"1.00">>},
  930. {"|stringformat:\"0.2f\"",
  931. <<"{{ var|stringformat:\"0.2f\" }}">>, [{var, "1"}],
  932. <<"1.00">>},
  933. {"|stringformat:\"-0.2f\"",
  934. <<"{{ var|stringformat:\"-0.2f\" }}">>, [{var, "1"}],
  935. <<"1.00">>},
  936. {"|stringformat:\"10.2f\"",
  937. <<"{{ var|stringformat:\"10.2f\" }}">>, [{var, "1"}],
  938. <<" 1.00">>},
  939. {"|stringformat:\"-10.2f\"",
  940. <<"{{ var|stringformat:\"-10.2f\" }}">>, [{var, "1"}],
  941. <<"1.00 ">>},
  942. {"|stringformat:\".2f\"",
  943. <<"{{ var|stringformat:\".2f\" }}">>, [{var, "1"}],
  944. <<"1.00">>},
  945. {"|stringformat:\"x\"",
  946. <<"{{ var|stringformat:\"x\" }}">>, [{var, "90"}],
  947. <<"5a">>},
  948. {"|stringformat:\"X\"",
  949. <<"{{ var|stringformat:\"X\" }}">>, [{var, "90"}],
  950. <<"5A">>},
  951. {"|stringformat:\"o\"",
  952. <<"{{ var|stringformat:\"o\" }}">>, [{var, "90"}],
  953. <<"132">>},
  954. {"|stringformat:\"e\"",
  955. <<"{{ var|stringformat:\"e\" }}">>, [{var, "90"}],
  956. <<"9.000000e+01">>},
  957. {"|stringformat:\"e\"",
  958. <<"{{ var|stringformat:\"e\" }}">>, [{var, "90000000000"}],
  959. <<"9.000000e+10">>},
  960. {"|stringformat:\"E\"",
  961. <<"{{ var|stringformat:\"E\" }}">>, [{var, "90"}],
  962. <<"9.000000E+01">>},
  963. {"|striptags",
  964. <<"{{ var|striptags }}">>, [{var, "<b>Joel</b> <button>is</button> a <span>slug</span>"}],
  965. <<"Joel is a slug">>},
  966. {"|striptags",
  967. <<"{{ var|striptags }}">>, [{var, "<B>Joel</B> <button>is</button> a <span>slug</Span>"}],
  968. <<"Joel is a slug">>},
  969. {"|striptags",
  970. <<"{{ var|striptags }}">>, [{var, "Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>"}],
  971. <<"Check out http://www.djangoproject.com">>},
  972. {"|time:\"H:i\"",
  973. <<"{{ var|time:\"H:i\" }}">>, [{var, {{2010,12,1}, {10,11,12}} }],
  974. <<"10:11">>},
  975. {"|time",
  976. <<"{{ var|time }}">>, [{var, {{2010,12,1}, {10,11,12}} }],
  977. <<"10:11 a.m.">>},
  978. {"|timesince:from_date",
  979. <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2006,6,1},{8,0,0}} }, {from_date, {{2006,6,1},{0,0,0}} }],
  980. <<"8 hours">>},
  981. {"|timesince:from_date",
  982. <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2010,6,1},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  983. <<"4 years, 1 day">>}, % leap year
  984. {"|timesince:from_date",
  985. <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2006,7,15},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  986. <<"1 month, 2 weeks">>},
  987. {"|timeuntil:from_date",
  988. <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2006,6,1},{8,0,0}} }, {from_date, {{2006,6,1},{0,0,0}} }],
  989. <<"8 hours">>},
  990. {"|timeuntil:from_date",
  991. <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2010,6,1},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  992. <<"4 years, 1 day">>},
  993. {"|timeuntil:from_date",
  994. <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2006,7,15},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
  995. <<"1 month, 2 weeks">>},
  996. {"|title",
  997. <<"{{ \"my title case\"|title }}">>, [],
  998. <<"My Title Case">>},
  999. {"|title (pre-formatted)",
  1000. <<"{{ \"My Title Case\"|title }}">>, [],
  1001. <<"My Title Case">>},
  1002. {"|title (wacky separators)",
  1003. <<"{{ \"my-title!case\"|title }}">>, [],
  1004. <<"My-Title!Case">>},
  1005. {"|title (numbers)",
  1006. <<"{{ \"my-title123CaSe\"|title }}">>, [],
  1007. <<"My-Title123case">>},
  1008. {"|title (Irish names)",
  1009. <<"{{ \"who's o'malley?\"|title }}">>, [],
  1010. <<"Who's O'Malley?">>},
  1011. {"|truncatechars:0",
  1012. <<"{{ var1|truncatechars:0 }}">>, [{var1, "Empty Me"}],
  1013. <<"...">>},
  1014. {"|truncatechars:14",
  1015. <<"{{ var1|truncatechars:14 }}">>, [{var1, "Truncate Me Please"}],
  1016. <<"Truncate Me...">>},
  1017. {"|truncatechars:17",
  1018. <<"{{ var1|truncatechars:17 }}">>, [{var1, "Don't Truncate Me"}],
  1019. <<"Don't Truncate Me">>},
  1020. {"|truncatechars:4 (UTF-8)",
  1021. <<"{{ var1|truncatechars:4 }}">>, [{var1, "\x{E2}\x{82}\x{AC}1.99"}],
  1022. <<"\x{E2}\x{82}\x{AC}...">>},
  1023. {"|truncatechars:5 (UTF-8)",
  1024. <<"{{ var1|truncatechars:5 }}">>, [{var1, "\x{E2}\x{82}\x{AC} 1.99"}],
  1025. <<"\x{E2}\x{82}\x{AC} ...">>},
  1026. {"|truncatewords:0",
  1027. <<"{{ var1|truncatewords:0 }}">>, [{var1, "Empty Me"}],
  1028. <<" ...">>},
  1029. {"|truncatewords:2",
  1030. <<"{{ var1|truncatewords:2 }}">>, [{var1, "Truncate Me Please"}],
  1031. <<"Truncate Me ...">>},
  1032. {"|truncatewords:3",
  1033. <<"{{ var1|truncatewords:3 }}">>, [{var1, "Don't Truncate Me"}],
  1034. <<"Don't Truncate Me">>},
  1035. {"|truncatewords_html:4",
  1036. <<"{{ var1|truncatewords_html:4 }}">>, [{var1, "<p>The <strong>Long and <em>Winding</em> Road</strong> is too long</p>"}],
  1037. <<"<p>The <strong>Long and <em>Winding</em>...</strong></p>">>},
  1038. {"|unordered_list",
  1039. <<"{{ var1|unordered_list }}">>, [{var1, ["States", ["Kansas", ["Lawrence", "Topeka"], "Illinois"]]}],
  1040. <<"<li>States<ul><li>Kansas<ul><li>Lawrence</li><li>Topeka</li></ul></li><li>Illinois</li></ul></li>">>},
  1041. {"|upper",
  1042. <<"{{ message|upper }}">>, [{message, "That man has a gun."}],
  1043. <<"THAT MAN HAS A GUN.">>},
  1044. {"|urlencode",
  1045. <<"{{ url|urlencode }}">>, [{url, "You #$*@!!"}],
  1046. <<"You%20%23%24%2A%40%21%21">>},
  1047. {"|urlencode",
  1048. <<"{{ url|urlencode }}">>, [{url, "http://www.example.org/foo?a=b&c=d"}],
  1049. <<"http%3A//www.example.org/foo%3Fa%3Db%26c%3Dd">>},
  1050. {"|urlencode",
  1051. <<"{{ url|urlencode:\"\" }}">>, [{url, "http://www.example.org/foo?a=b&c=d"}],
  1052. <<"http%3A%2F%2Fwww.example.org%2Ffoo%3Fa%3Db%26c%3Dd">>},
  1053. {"|urlencode",
  1054. <<"{{ url|urlencode:\":/?=&\" }}">>, [{url, "http://www.example.org/foo?a=b&c=d"}],
  1055. <<"http://www.example.org/foo?a=b&c=d">>},
  1056. {"|urlize",
  1057. <<"{{ var|urlize }}">>, [{var, "Check out www.djangoproject.com"}],
  1058. <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">www.djangoproject.com</a>">>},
  1059. {"|urlize",
  1060. <<"{{ var|urlize }}">>, [{var, "Check out http://www.djangoproject.com"}],
  1061. <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>">>},
  1062. {"|urlize",
  1063. <<"{{ var|urlize }}">>, [{var, "Check out \"http://www.djangoproject.com\""}],
  1064. <<"Check out \"<a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>\"">>},
  1065. {"|urlizetrunc:15",
  1066. <<"{{ var|urlizetrunc:15 }}">>, [{var, "Check out www.djangoproject.com"}],
  1067. <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">www.djangopr...</a>">>},
  1068. {"|wordcount",
  1069. <<"{{ words|wordcount }}">>, [{words, "Why Hello There!"}],
  1070. <<"3">>},
  1071. {"|wordwrap:2",
  1072. <<"{{ words|wordwrap:2 }}">>, [{words, "this is"}],
  1073. <<"this \nis">>},
  1074. {"|wordwrap:100",
  1075. <<"{{ words|wordwrap:100 }}">>, [{words, "testing testing"}],
  1076. <<"testing testing">>},
  1077. {"|wordwrap:10",
  1078. <<"{{ words|wordwrap:10 }}">>, [{words, ""}],
  1079. <<"">>},
  1080. {"|wordwrap:1",
  1081. <<"{{ words|wordwrap:1 }}">>, [{words, "two"}],
  1082. <<"two">>},
  1083. %% yesno match: \?assert.*\( (.*), erlydtl_filters:(.*)\((.*), "(.*)"\)\)
  1084. %% yesno replace: \t\t{"|$2:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>}
  1085. {"|yesno:\"yeah,no,maybe\"",
  1086. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, true}],
  1087. <<"yeah">>},
  1088. {"|yesno:\"yeah,no,maybe\"",
  1089. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, false}],
  1090. <<"no">>},
  1091. {"|yesno:\"yeah,no\"",
  1092. <<"{{ var|yesno:\"yeah,no\" }}">>, [{var, undefined}],
  1093. <<"no">>},
  1094. {"|yesno:\"yeah,no,maybe\"",
  1095. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, undefined}],
  1096. <<"maybe">>},
  1097. {"string |yesno:\"yeah,no,maybe\"",
  1098. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, "non-empty string"}],
  1099. <<"yeah">>},
  1100. {"binary |yesno:\"yeah,no,maybe\"",
  1101. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, <<"non-empty binary">>}],
  1102. <<"yeah">>},
  1103. {"empty string |yesno:\"yeah,no,maybe\"",
  1104. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, ""}],
  1105. <<"no">>},
  1106. {"empty binary |yesno:\"yeah,no\"",
  1107. <<"{{ var|yesno:\",no\" }}">>, [{var, <<"">>}],
  1108. <<"no">>},
  1109. {"term |yesno:\"yeah,,maybe\"",
  1110. <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, {my, [term, "test"]}}],
  1111. <<"yeah">>},
  1112. {"|yesno:\"yeah,\"",
  1113. <<"{{ var|yesno:\"yeah,\" }}">>, [{var, false}],
  1114. <<"">>},
  1115. {"|yesno:\"yeah,,maybe\"",
  1116. <<"{{ var|yesno:\"yeah,,maybe\" }}">>, [{var, false}],
  1117. <<"">>},
  1118. #test{
  1119. title = "|yesno:\"missing_false_choice\"",
  1120. source = <<"{{ var|yesno:\"missing_false_choice\" }}">>,
  1121. render_vars = [{var, true}],
  1122. output = {error, {yesno, choices}}
  1123. },
  1124. {"escape only once (#150) - no auto escape",
  1125. %% note that auto_escape is off by default in the test suite
  1126. %% due to how the tests have been written (and it's too much
  1127. %% work for me to rewrite them)
  1128. <<"{{ foo }}{{ foo|add:'bar' }}">>,
  1129. [{foo, "foo&"}],
  1130. <<"foo&foo&bar">>},
  1131. {"escape only once (#150) - auto escape block",
  1132. <<"{% autoescape on %}{{ foo }}{{ foo|add:'bar' }}{% endautoescape %}">>,
  1133. [{foo, "foo&"}],
  1134. <<"foo&amp;foo&amp;bar">>},
  1135. {"escape only once (#150) - auto escape",
  1136. <<"{{ foo }}{{ foo|add:'bar' }}">>,
  1137. [{foo, "foo&"}], [], [auto_escape],
  1138. <<"foo&amp;foo&amp;bar">>},
  1139. {"escape only once (#150) - auto escape, safe",
  1140. <<"{{ foo|safe }}{{ foo|add:'bar'|safe }}&{{ foo|safe|add:'bar' }}">>,
  1141. [{foo, "foo&"}], [], [auto_escape],
  1142. <<"foo&foo&bar&foo&bar">>},
  1143. {"escape only once (#150) - escape filter",
  1144. <<"{{ foo|escape }}{{ foo|add:'bar'|escape }}&{{ foo|escape|add:'bar' }}">>,
  1145. [{foo, "foo&"}],
  1146. <<"foo&amp;foo&amp;bar&foo&amp;bar">>},
  1147. {"escape only once (#150) - auto escape + escape filter",
  1148. <<"{{ foo|escape }}{{ foo|add:'bar'|escape }}&{{ foo|escape|add:'bar' }}">>,
  1149. [{foo, "foo&"}], [], [auto_escape],
  1150. <<"foo&amp;foo&amp;bar&foo&amp;bar">>}
  1151. ]},
  1152. {"filters_if",
  1153. [{"Filter if 1.1",
  1154. <<"{% if var1|length_is:0 %}Y{% else %}N{% endif %}">>,
  1155. [{var1, []}],
  1156. <<"Y">>},
  1157. {"Filter if 1.2",
  1158. <<"{% if var1|length_is:1 %}Y{% else %}N{% endif %}">>,
  1159. [{var1, []}],
  1160. <<"N">>},
  1161. {"Filter if 1.3",
  1162. <<"{% if var1|length_is:7 %}Y{% else %}N{% endif %}">>,
  1163. [{var1, []}],
  1164. <<"N">>},
  1165. {"Filter if 2.1",
  1166. <<"{% if var1|length_is:0 %}Y{% else %}N{% endif %}">>,
  1167. [{var1, ["foo"]}],
  1168. <<"N">>},
  1169. {"Filter if 2.2",
  1170. <<"{% if var1|length_is:1 %}Y{% else %}N{% endif %}">>,
  1171. [{var1, ["foo"]}],
  1172. <<"Y">>},
  1173. {"Filter if 2.3",
  1174. <<"{% if var1|length_is:7 %}Y{% else %}N{% endif %}">>,
  1175. [{var1, ["foo"]}],
  1176. <<"N">>},
  1177. {"Filter if 3.1",
  1178. <<"{% ifequal var1|length 0 %}Y{% else %}N{% endifequal %}">>,
  1179. [{var1, []}],
  1180. <<"Y">>},
  1181. {"Filter if 3.2",
  1182. <<"{% ifequal var1|length 1 %}Y{% else %}N{% endifequal %}">>,
  1183. [{var1, []}],
  1184. <<"N">>},
  1185. {"Filter if 4.1",
  1186. <<"{% ifequal var1|length 3 %}Y{% else %}N{% endifequal %}">>,
  1187. [{var1, ["foo", "bar", "baz"]}],
  1188. <<"Y">>},
  1189. {"Filter if 4.2",
  1190. <<"{% ifequal var1|length 0 %}Y{% else %}N{% endifequal %}">>,
  1191. [{var1, ["foo", "bar", "baz"]}],
  1192. <<"N">>},
  1193. {"Filter if 4.3",
  1194. <<"{% ifequal var1|length 1 %}Y{% else %}N{% endifequal %}">>,
  1195. [{var1, ["foo", "bar", "baz"]}],
  1196. <<"N">>}
  1197. ]},
  1198. {"firstof",
  1199. [{"Firstof first",
  1200. <<"{% firstof foo bar baz %}">>,
  1201. [{foo, "1"},{bar, "2"}],
  1202. <<"1">>},
  1203. {"Firstof second",
  1204. <<"{% firstof foo bar baz %}">>,
  1205. [{bar, "2"}],
  1206. <<"2">>},
  1207. {"Firstof none",
  1208. <<"{% firstof foo bar baz %}">>,
  1209. [],
  1210. <<"">>},
  1211. {"Firstof complex",
  1212. <<"{% firstof foo.bar.baz bar %}">>,
  1213. [{foo, [{bar, [{baz, "quux"}]}]}],
  1214. <<"quux">>},
  1215. {"Firstof undefined complex",
  1216. <<"{% firstof foo.bar.baz bar %}">>,
  1217. [{bar, "bar"}],
  1218. <<"bar">>},
  1219. {"Firstof literal",
  1220. <<"{% firstof foo bar \"baz\" %}">>,
  1221. [],
  1222. <<"baz">>}
  1223. ]},
  1224. {"regroup .. endregroup",
  1225. [{"Ordered",
  1226. <<"{% 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 %}">>,
  1227. [{people, [[{first_name, "George"}, {gender, "Male"}], [{first_name, "Bill"}, {gender, "Male"}],
  1228. [{first_name, "Margaret"}, {gender, "Female"}], [{first_name, "Condi"}, {gender, "Female"}]]}],
  1229. <<"Male\nGeorge\nBill\nFemale\nMargaret\nCondi\n">>},
  1230. {"Unordered",
  1231. <<"{% 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 %}">>,
  1232. [{people, [[{first_name, "George"}, {gender, "Male"}],
  1233. [{first_name, "Margaret"}, {gender, "Female"}],
  1234. [{first_name, "Condi"}, {gender, "Female"}],
  1235. [{first_name, "Bill"}, {gender, "Male"}]
  1236. ]}],
  1237. <<"Male\nGeorge\nFemale\nMargaret\nCondi\nMale\nBill\n">>},
  1238. {"NestedOrdered",
  1239. <<"{% 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 %}">>,
  1240. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1241. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1242. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1243. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1244. <<"Costanza\nGeorge\nMargaret\nBuffalo\nBill\nCondi\n">>},
  1245. {"NestedUnordered",
  1246. <<"{% 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 %}">>,
  1247. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1248. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1249. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1250. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1251. <<"Costanza\nGeorge\nBuffalo\nBill\nCostanza\nMargaret\nBuffalo\nCondi\n">>},
  1252. {"Filter",
  1253. <<"{% 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 %}">>,
  1254. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1255. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1256. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1257. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1258. <<"Buffalo\nBill\nCondi\nCostanza\nGeorge\nMargaret\n">>}
  1259. ]},
  1260. {"regroup",
  1261. [{"Ordered",
  1262. <<"{% 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 %}">>,
  1263. [{people, [[{first_name, "George"}, {gender, "Male"}], [{first_name, "Bill"}, {gender, "Male"}],
  1264. [{first_name, "Margaret"}, {gender, "Female"}], [{first_name, "Condi"}, {gender, "Female"}]]}],
  1265. <<"Male\nGeorge\nBill\nFemale\nMargaret\nCondi\n">>},
  1266. {"Unordered",
  1267. <<"{% 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 %}">>,
  1268. [{people, [[{first_name, "George"}, {gender, "Male"}],
  1269. [{first_name, "Margaret"}, {gender, "Female"}],
  1270. [{first_name, "Condi"}, {gender, "Female"}],
  1271. [{first_name, "Bill"}, {gender, "Male"}]
  1272. ]}],
  1273. <<"Male\nGeorge\nFemale\nMargaret\nCondi\nMale\nBill\n">>},
  1274. {"NestedOrdered",
  1275. <<"{% 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 %}">>,
  1276. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1277. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1278. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1279. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1280. <<"Costanza\nGeorge\nMargaret\nBuffalo\nBill\nCondi\n">>},
  1281. {"NestedUnordered",
  1282. <<"{% 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 %}">>,
  1283. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1284. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1285. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1286. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1287. <<"Costanza\nGeorge\nBuffalo\nBill\nCostanza\nMargaret\nBuffalo\nCondi\n">>},
  1288. {"Filter",
  1289. <<"{% 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 %}">>,
  1290. [{people, [[{name, [{first,"George"},{last,"Costanza"}]}],
  1291. [{name, [{first,"Bill"},{last,"Buffalo"}]}],
  1292. [{name, [{first,"Margaret"},{last,"Costanza"}]}],
  1293. [{name, [{first,"Condi"},{last,"Buffalo"}]}]]}],
  1294. <<"Buffalo\nBill\nCondi\nCostanza\nGeorge\nMargaret\n">>},
  1295. {"With surrounding context",
  1296. <<"People: {% 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 %}Done.">>,
  1297. [{people, [[{first_name, "George"}, {gender, "Male"}], [{first_name, "Bill"}, {gender, "Male"}],
  1298. [{first_name, "Margaret"}, {gender, "Female"}], [{first_name, "Condi"}, {gender, "Female"}]]}],
  1299. <<"People: Male\nGeorge\nBill\nFemale\nMargaret\nCondi\nDone.">>},
  1300. #test{
  1301. title = "regroup record",
  1302. source = <<"{% regroup people by gender as gender_list %}{% for gender in gender_list %}{{ gender.grouper }}:\n{% for person in gender.list %} - {{ person.first_name }}\n{% endfor %}{% endfor %}">>,
  1303. compile_opts = [{record_info, [{person, record_info(fields, person)}]} | (#test{})#test.compile_opts],
  1304. render_vars = [{people, [#person{ first_name = "George", gender = "Male" },
  1305. #person{ first_name = "Bill", gender = "Male" },
  1306. #person{ first_name = "Margaret", gender = "Female" },
  1307. #person{ first_name = "Condi", gender = "Female" }
  1308. ]}
  1309. ],
  1310. output = <<"Male:\n - George\n - Bill\nFemale:\n - Margaret\n - Condi\n">>
  1311. }
  1312. ]},
  1313. {"spaceless",
  1314. [{"Beginning", <<"{% spaceless %} <b>foo</b>{% endspaceless %}">>, [], <<"<b>foo</b>">>},
  1315. {"Middle", <<"{% spaceless %}<b>foo</b> <b>bar</b>{% endspaceless %}">>, [], <<"<b>foo</b><b>bar</b>">>},
  1316. {"End", <<"{% spaceless %}<b>foo</b> {% endspaceless %}">>, [], <<"<b>foo</b>">>},
  1317. {"NewLine", <<"{% spaceless %}\n<div> \n <b>foo</b> \n </div>\n {% endspaceless %}">>, [], <<"<div><b>foo</b></div>">>}
  1318. ]},
  1319. {"templatetag",
  1320. [{"openblock", <<"{% templatetag openblock %}">>, [], <<"{%">>},
  1321. {"closeblock", <<"{% templatetag closeblock %}">>, [], <<"%}">>},
  1322. {"openvariable", <<"{% templatetag openvariable %}">>, [], <<"{{">>},
  1323. {"closevariable", <<"{% templatetag closevariable %}">>, [], <<"}}">>},
  1324. {"openbrace", <<"{% templatetag openbrace %}">>, [], <<"{">>},
  1325. {"closebrace", <<"{% templatetag closebrace %}">>, [], <<"}">>},
  1326. {"opencomment", <<"{% templatetag opencomment %}">>, [], <<"{#">>},
  1327. {"closecomment", <<"{% templatetag closecomment %}">>, [], <<"#}">>}
  1328. ]},
  1329. {"trans",
  1330. [{"trans functional default locale",
  1331. <<"Hello {% trans \"Hi\" %}">>, [], <<"Hello Hi">>
  1332. },
  1333. {"trans functional reverse locale",
  1334. <<"Hello {% trans \"Hi\" %}">>, [], [{locale, "reverse"}],
  1335. [{locales, ["reverse"]}, {translation_fun, fun("Hi"=Key, "reverse") -> list_to_binary(lists:reverse(Key)) end}],
  1336. <<"Hello iH">>
  1337. },
  1338. {"trans literal at run-time",
  1339. <<"Hello {% trans \"Hi\" %}">>, [], [{translation_fun, fun("Hi") -> "Konichiwa" end}], [],
  1340. <<"Hello Konichiwa">>},
  1341. {"trans variable at run-time",
  1342. <<"Hello {% trans var1 %}">>, [{var1, <<"Hi">>}], [{translation_fun, fun(<<"Hi">>) -> <<"Konichiwa">> end}], [],
  1343. <<"Hello Konichiwa">>},
  1344. {"trans literal at run-time: No-op",
  1345. <<"Hello {% trans \"Hi\" noop %}">>, [], [{translation_fun, fun("Hi") -> <<"Konichiwa">> end}], [],
  1346. <<"Hello Hi">>},
  1347. {"trans variable at run-time: No-op",
  1348. <<"Hello {% trans var1 noop %}">>, [{var1, <<"Hi">>}], [{translation_fun, fun(<<"Hi">>) -> <<"Konichiwa">> end}], [],
  1349. <<"Hello Hi">>},
  1350. {"trans as",
  1351. <<"{% trans 'Hans' as name %}Hello {{ name }}">>, [],
  1352. <<"Hello Hans">>},
  1353. {"trans value",
  1354. <<"{{ _('foo') }}">>, [], [], [{locale, default}, {translation_fun, fun ("foo") -> "bar" end}],
  1355. <<"bar">>}
  1356. ]},
  1357. {"blocktrans",
  1358. [{"blocktrans default locale",
  1359. <<"{% blocktrans %}Hello{% endblocktrans %}">>, [], <<"Hello">>},
  1360. {"blocktrans choose locale",
  1361. <<"{% blocktrans %}Hello, {{ name }}{% endblocktrans %}">>, [{name, "Mr. President"}], [{locale, "de"}],
  1362. [{locales, ["de"]}, {translation_fun, fun("Hello, {{ name }}", "de") -> <<"Guten tag, {{ name }}">> end}], <<"Guten tag, Mr. President">>},
  1363. {"blocktrans with args",
  1364. <<"{% blocktrans with var1=foo %}{{ var1 }}{% endblocktrans %}">>, [{foo, "Hello"}], <<"Hello">>},
  1365. #test{
  1366. title = "blocktrans blocks in content not allowed",
  1367. source = <<"{% blocktrans %}Hello{%if name%}, {{ name }}{%endif%}!{% endblocktrans %}">>,
  1368. errors = [error_info([{{1, 24}, erlydtl_parser, ["syntax error before: ",["\"if\""]]}])]
  1369. },
  1370. #test{
  1371. title = "blocktrans nested variables not allowed",
  1372. source = <<"{% blocktrans %}Hello, {{ user.name }}!{% endblocktrans %}">>,
  1373. errors = [error_info([{{1,31}, erlydtl_parser, ["syntax error before: ","'.'"]}])]
  1374. },
  1375. {"blocktrans runtime",
  1376. <<"{% blocktrans with v1=foo%}Hello, {{ name }}! See {{v1}}.{%endblocktrans%}">>,
  1377. [{name, "Mr. President"}, {foo, <<"rubber-duck">>}],
  1378. [{translation_fun, fun("Hello, {{ name }}! See {{ v1 }}.") -> <<"Guten tag, {{name}}! Sehen {{ v1 }}.">> end}],
  1379. [], <<"Guten tag, Mr. President! Sehen rubber-duck.">>}
  1380. ]},
  1381. {"extended translation features (#131)",
  1382. [{"trans default locale",
  1383. <<"test {% trans 'message' %}">>,
  1384. [], [{translation_fun, fun ("message", default) -> "ok" end}],
  1385. <<"test ok">>},
  1386. {"trans foo locale",
  1387. <<"test {% trans 'message' %}">>,
  1388. [], [{locale, "foo"}, {translation_fun, fun ("message", "foo") -> "ok" end}],
  1389. <<"test ok">>},
  1390. {"trans context (run-time)",
  1391. <<"test {% trans 'message' context 'foo' %}">>,
  1392. [], [{translation_fun, fun ("message", {default, "foo"}) -> "ok" end}],
  1393. <<"test ok">>},
  1394. {"trans context (compile-time)",
  1395. <<"test {% trans 'message' context 'foo' %}">>,
  1396. [], [{locale, "baz"}],
  1397. [{locales, ["bar", "baz"]},
  1398. {translation_fun, fun ("message", {L, "foo"}) ->
  1399. case L of
  1400. "bar" -> "rab";
  1401. "baz" -> "ok"
  1402. end
  1403. end}],
  1404. <<"test ok">>},
  1405. {"trans context noop",
  1406. <<"{% trans 'message' noop context 'foo' %}">>, [], [],
  1407. <<"message">>},
  1408. {"blocktrans context (run-time)",
  1409. <<"{% blocktrans context 'bar' %}translate this{% endblocktrans %}">>,
  1410. [], [{locale, "foo"}, {translation_fun,
  1411. fun ("translate this", {"foo", "bar"}) ->
  1412. "got it"
  1413. end}],
  1414. <<"got it">>},
  1415. {"blocktrans context (compile-time)",
  1416. <<"{% blocktrans context 'bar' %}translate this{% endblocktrans %}">>,
  1417. [], [{locale, "foo"}],
  1418. [{locale, "foo"}, {translation_fun,
  1419. fun ("translate this", {"foo", "bar"}) ->
  1420. "got it"
  1421. end}],
  1422. <<"got it">>},
  1423. {"blocktrans plural",
  1424. <<"{% blocktrans count foo=bar %}",
  1425. "There is just one foo..",
  1426. "{% plural %}",
  1427. "There are many foo's..",
  1428. "{% endblocktrans %}">>,
  1429. [{bar, 2}], [{locale, "baz"},
  1430. {translation_fun,
  1431. fun ({"There is just one foo..", {"There are many foo's..", 2}}, "baz") ->
  1432. "ok"
  1433. end}],
  1434. <<"ok">>},
  1435. {"blocktrans a lot of stuff",
  1436. <<"{% blocktrans with foo=a.b count c=a|length context 'quux' %}"
  1437. "foo={{ foo }};bar={{ bar }};c={{ c }}:"
  1438. "{% plural %}"
  1439. "FOO:{{ foo }},BAR:{{ bar }},C:{{ c }}."
  1440. "{% endblocktrans %}">>,
  1441. [{a, [{b, "B"}]}, {bar, "BAR"}],
  1442. [{locale, "rub"},
  1443. {translation_fun, fun ({Single, {Plural, "1"=_Count}}, {Locale, Context}) ->
  1444. [Single, Plural, Locale, Context]
  1445. end}],
  1446. <<"foo=B;bar=BAR;c=1:"
  1447. "FOO:B,BAR:BAR,C:1."
  1448. "rub" "quux">>},
  1449. {"new translation options",
  1450. <<"{% trans foo %}{% blocktrans %}abc{% endblocktrans %}">>,
  1451. [{foo, "1234"}], [{locale, "test"}, {translation_fun, fun (Msg) -> lists:reverse(Msg) end}],
  1452. [{locale, "foo"}, {locale, "test"}, {locales, ["bar", "baz"]},
  1453. {translation_fun, fun (Msg, _) -> [Msg, lists:reverse(Msg)] end}],
  1454. <<"4321" "abccba">>}
  1455. %% This does work, but always prints a warning to std err.. :/
  1456. %% Warning: template translation: variable not closed: "bar {{ 123"
  1457. %% {"variable error",
  1458. %% <<"{% blocktrans %}foo{{ bar }}{% endblocktrans %}">>,
  1459. %% [], [{translation_fun, fun (_) -> "bar {{ 123" end}],
  1460. %% <<"foo">>}
  1461. ]},
  1462. {"i18n",
  1463. [{"setup translation context, using fun, at render time",
  1464. <<"{% trans 'foo' %}">>, [],
  1465. [{translation_fun, fun () -> fun (Msg) -> string:to_upper(Msg) end end}],
  1466. <<"FOO">>},
  1467. {"setup translation context, using fun, at compile time",
  1468. <<"{% trans 'foo' %}">>, [], [],
  1469. [{locale, default}, {translation_fun, fun () -> fun lists:reverse/1 end}],
  1470. <<"oof">>}
  1471. ]},
  1472. {"verbatim",
  1473. [{"Plain verbatim",
  1474. <<"{% verbatim %}{{ oh no{% foobar %}{% endverbatim %}">>, [],
  1475. <<"{{ oh no{% foobar %}">>},
  1476. {"Named verbatim",
  1477. <<"{% verbatim foobar %}{% verbatim %}{% endverbatim foobar2 %}{% endverbatim foobar %}">>, [],
  1478. <<"{% verbatim %}{% endverbatim foobar2 %}">>}
  1479. ]},
  1480. {"widthratio",
  1481. [{"Literals", <<"{% widthratio 5 10 100 %}">>, [], <<"50">>},
  1482. {"Rounds up", <<"{% widthratio a b 100 %}">>, [{a, 175}, {b, 200}], <<"88">>}
  1483. ]},
  1484. {"with",
  1485. [{"Cache literal",
  1486. <<"{% with a=1 %}{{ a }}{% endwith %}">>, [], <<"1">>},
  1487. {"Cache variable",
  1488. <<"{% with a=b %}{{ a }}{% endwith %}">>, [{b, "foo"}], <<"foo">>},
  1489. {"Cache variable with attribute",
  1490. <<"I enjoy {% with a = var1 %}{{ a.game }}{% endwith %}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
  1491. {"Cache variable attribute",
  1492. <<"I enjoy {% with a = var1.game %}{{ a }}{% endwith %}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
  1493. {"Cache multiple",
  1494. <<"{% with alpha=1 beta=b %}{{ alpha }}/{{ beta }}{% endwith %}">>, [{b, 2}], <<"1/2">>}
  1495. ]},
  1496. {"unicode",
  1497. [{"(tm) somewhere",
  1498. <<"™">>, [], <<"™">>}
  1499. ]},
  1500. {"contrib_humanize",
  1501. [{"intcomma",
  1502. <<"{{ a|intcomma }} {{ b|intcomma }} {{ c|intcomma }} {{ d|intcomma }}">>,
  1503. [{a, 999}, {b, 123456789}, {c, 12345}, {d, 1234567890}], [],
  1504. [{custom_filters_modules, [erlydtl_contrib_humanize]}],
  1505. <<"999 123,456,789 12,345 1,234,567,890">>}
  1506. ]},
  1507. %% custom syntax stuff
  1508. {"extension_module",
  1509. [ %% the erlydtl_test_extension module replaces a foo identifier with bar when hitting a # following foo.
  1510. {"replace parsed token", <<"{{ foo # }}">>, [{bar, "ok"}], [],
  1511. [{extension_module, erlydtl_test_extension}], <<"ok">>},
  1512. #test{
  1513. title = "proper error message",
  1514. source = <<"{{ bar # }}">>,
  1515. render_vars = [{bar, "ok"}],
  1516. compile_opts = [{extension_module, erlydtl_test_extension},
  1517. report, return, force_recompile, {out_dir, false}],
  1518. errors = [error_info([{{1,8},erlydtl_scanner,{illegal_char, $#}}])]
  1519. },
  1520. %% accept identifiers as expressions (this is a dummy functionality to test the parser extensibility)
  1521. {"identifiers as expressions", <<"{{ foo.bar or baz }}">>, [{baz, "ok"}], [],
  1522. [{extension_module, erlydtl_test_extension}], <<"ok">>}
  1523. ]},
  1524. {"records",
  1525. [{"field access",
  1526. <<"{{ r.baz }}">>, [{r, #testrec{ foo="Foo", bar="Bar", baz="Baz" }}], [],
  1527. [{record_info, [{testrec, record_info(fields, testrec)}]}],
  1528. <<"Baz">>}
  1529. ]},
  1530. {"error reporting",
  1531. [#test{
  1532. title = "no out dir warning",
  1533. source = <<"foo bar">>,
  1534. compile_opts = [report, return, force_recompile],
  1535. output = <<"foo bar">>,
  1536. warnings = [error_info([no_out_dir])]
  1537. },
  1538. #test{
  1539. title = "warnings as errors",
  1540. source = <<"foo bar">>,
  1541. compile_opts = [report, return, warnings_as_errors, force_recompile],
  1542. errors = [error_info([no_out_dir])]
  1543. },
  1544. #test{
  1545. title = "illegal character",
  1546. source = <<"{{{">>,
  1547. errors = [error_info([{{1,3},erlydtl_scanner,{illegal_char, ${}}])]
  1548. },
  1549. #test{
  1550. title = "unexpected end of file - in code",
  1551. source = <<"{{">>,
  1552. errors = [error_info([{{1,3},erlydtl_scanner,{eof, in_code}}])]
  1553. },
  1554. #test{
  1555. title = "unexpected end of file - in comment",
  1556. source = <<"{#">>,
  1557. errors = [error_info([{{1,3},erlydtl_scanner,{eof, in_comment}}])]
  1558. },
  1559. {"unknown library",
  1560. <<"{% load foo %}">>, [], [], [],
  1561. <<>>,
  1562. [error_info(
  1563. [{{1,9},erlydtl_compiler_utils,{load_library,foo,foo,nofile}}
  1564. ])]
  1565. },
  1566. {"not a library",
  1567. <<"{% load foo %}">>, [], [],
  1568. [{libraries, [{foo, ?MODULE}]}],
  1569. <<>>,
  1570. [error_info(
  1571. [{{1,9},erlydtl_compiler_utils,{load_library,foo,?MODULE,behaviour}}
  1572. ])]
  1573. },
  1574. {"library version",
  1575. <<"{% load foo %}">>, [], [],
  1576. [{libraries, [{foo, erlydtl_lib_testversion}]}],
  1577. <<>>,
  1578. [error_info(
  1579. [{{1,9},erlydtl_compiler_utils,{load_library,foo,erlydtl_lib_testversion,{version,invalid}}}
  1580. ])]
  1581. },
  1582. {"not in library",
  1583. <<"{% load foo bar from test1 %}\n{{ \"w00t\"|reverse }}">>, [], [],
  1584. [{libraries, [{test1, erlydtl_lib_test1}]}],
  1585. <<"\n">>,
  1586. [error_info(
  1587. [{{2,11},erlydtl_beam_compiler,{unknown_filter,reverse,1}},
  1588. {{1,22},erlydtl_compiler_utils,{load_from,test1,erlydtl_lib_test1,foo}},
  1589. {{1,22},erlydtl_compiler_utils,{load_from,test1,erlydtl_lib_test1,bar}}
  1590. ])]
  1591. },
  1592. {"pre load unknown library",
  1593. <<"{{ '123'|reverse }}">>, [], [],
  1594. [{default_libraries, [test1]}],
  1595. <<"">>,
  1596. [error_info(
  1597. [{{1,10},erlydtl_beam_compiler,{unknown_filter,reverse,1}},
  1598. {none,erlydtl_compiler_utils,{load_library,test1,test1,nofile}}
  1599. ])]
  1600. },
  1601. {"pre load unknown legacy library",
  1602. <<"{% foo %}">>, [], [],
  1603. [{custom_tags_modules, [foo]}],
  1604. <<"">>,
  1605. [error_info([{none,erlydtl_compiler,{load_library,'(custom-legacy)',foo,nofile}}])]
  1606. },
  1607. {"unknown filter",
  1608. <<"{{ '123'|foo }}">>, [], [], [],
  1609. <<"">>,
  1610. [error_info([{{1,10},erlydtl_beam_compiler,{unknown_filter,foo,1}}])]
  1611. },
  1612. {"ssi file not found",
  1613. <<"{% ssi 'foo' %}">>, [],
  1614. {error, {read_file, <<"./foo">>, enoent}}
  1615. },
  1616. {"deprecated compile options",
  1617. <<"">>, [], [],
  1618. [{blocktrans_locales, []}, {blocktrans_fun, fun (_) -> [] end}],
  1619. <<"">>,
  1620. [error_info([{deprecated_option, O, N}
  1621. || {O, N} <- [{blocktrans_locales, locales},
  1622. {blocktrans_fun, translation_fun}]],
  1623. erlydtl_compiler)]
  1624. }
  1625. ]},
  1626. {"load",
  1627. [{"filter",
  1628. <<"{% load test1 %}{{ \"1234\"|reverse }}">>, [], [],
  1629. [{libraries, [{test1, erlydtl_lib_test1}]}],
  1630. <<"4321">>
  1631. },
  1632. {"named",
  1633. <<"{% load reverse from test1 %}{{ \"abcd\"|reverse }}">>, [], [],
  1634. [{libraries, [{test1, erlydtl_lib_test1}]}],
  1635. <<"dcba">>
  1636. },
  1637. {"pre loaded",
  1638. <<"{{ QWER|reverse }}">>, [{'QWER', "Qwerty"}], [],
  1639. [{default_libraries, [test1]},
  1640. {libraries, [{test1, erlydtl_lib_test1}]}],
  1641. <<"ytrewQ">>
  1642. },
  1643. {"lib with multiple behaviours",
  1644. <<"{{ QWER|reverse }}">>, [{'QWER', "Qwerty"}], [],
  1645. [{default_libraries, [test2]},
  1646. {libraries, [{test2, erlydtl_lib_test2}]}],
  1647. <<"ytrewQ">>
  1648. }
  1649. ]},
  1650. {"compile time default vars/constants",
  1651. begin
  1652. Tpl = <<"Test {{ var1 }}:{{ var2 }}.">>,
  1653. Txt = <<"Test 123:abc.">>,
  1654. Fun = fun (F) ->
  1655. fun (#test{ module=M }) ->
  1656. M:F()
  1657. end
  1658. end,
  1659. [{"default vars",
  1660. Tpl, [], [],
  1661. [{default_vars, [{var1, 123}, {var2, abc}]}], Txt},
  1662. {"default vars (using fun)",
  1663. Tpl, [], [],
  1664. [{default_vars, [{var1, 123}, {var2, fun () -> abc end}]}], Txt},
  1665. {"override default vars",
  1666. Tpl, [{var2, abc}], [],
  1667. [{default_vars, [{var1, 123}, {var2, 456}]}], Txt},
  1668. {"constants",
  1669. Tpl, [], [],
  1670. [{constants, [{var1, 123}, {var2, abc}]}], Txt},
  1671. {"constants (using fun)",
  1672. Tpl, [], [],
  1673. [{constants, [{var1, 123}, {var2, fun () -> abc end}]}], Txt},
  1674. {"constants non-overridable",
  1675. Tpl, [{var1, ohno}, {var2, noway}], [],
  1676. [{constants, [{var1, 123}, {var2, "abc"}]}], Txt}
  1677. |[#test{ title = T,
  1678. source = Tpl,
  1679. compile_vars = undefined,
  1680. compile_opts = CO ++ (#test{})#test.compile_opts,
  1681. renderer = Fun(F),
  1682. output = O
  1683. }
  1684. || {T, F, O, CO} <-
  1685. [{"variables/0",
  1686. variables, [var1, var2], []},
  1687. {"variables/0 w. defaults",
  1688. variables, [var1, var2], [{default_vars, [{var1, aaa}]}]},
  1689. {"variables/0 w. constants",
  1690. variables, [var2], [{constants, [{var1, bbb}]}]},
  1691. {"default_variables/0",
  1692. default_variables, [], []},
  1693. {"default_variables/0 w. defaults",
  1694. default_variables, [var1], [{default_vars, [{var1, aaa}]}]},
  1695. {"default_variables/0 w. constants",
  1696. default_variables, [], [{constants, [{var1, bbb}]}]},
  1697. {"constants/0",
  1698. constants, [], []},
  1699. {"constants/0 w. defaults",
  1700. constants, [], [{default_vars, [{var1, aaa}]}]},
  1701. {"constants/0 w. constants",
  1702. constants, [var1], [{constants, [{var1, bbb}]}]}
  1703. ]
  1704. ]]
  1705. end},
  1706. {"functional",
  1707. [functional_test(F)
  1708. %% order is important for a few of these tests, unfortunately.
  1709. || F <- ["autoescape", "comment", "extends", "filters", "for", "for_list", "for_tuple",
  1710. "for_list_preset", "for_preset", "for_records", "for_records_preset", "include",
  1711. "if", "if_preset", "ifequal", "ifequal_preset", "ifnotequal", "ifnotequal_preset",
  1712. "now", "var", "var_preset", "cycle", "custom_tag", "custom_tag1", "custom_tag2",
  1713. "custom_tag3", "custom_tag4", "custom_tag_var", "custom_tag_lib_var", "custom_call", "include_template", "include_path",
  1714. "ssi", "extends_path", "extends_path2", "trans", "extends_for", "extends2",
  1715. "extends3", "recursive_block", "extend_recursive_block", "missing", "block_super",
  1716. "wrapper", "extends4", "super_escaped", "extends_chain"]
  1717. ]},
  1718. {"compile_dir",
  1719. [setup_compile(T)
  1720. || T <- [#test{
  1721. title = "non-existing dir",
  1722. source = {dir, "non-existing-made-up-dir"},
  1723. renderer = fun(#test{ source={dir, Dir} }) -> Dir end,
  1724. output = "non-existing-made-up-dir"
  1725. },
  1726. #test{
  1727. title = "path1",
  1728. source = {dir, template_file(input, "path1")},
  1729. renderer = fun(#test{ module=M, render_vars=V, render_opts=O }) ->
  1730. M:render(base1, V, O)
  1731. end
  1732. }
  1733. ]
  1734. ]}
  1735. ].
  1736. %% {Name, DTL, Vars, Output}
  1737. %% {Name, DTL, Vars, RenderOpts, Output}
  1738. %% {Name, DTL, Vars, RenderOpts, CompilerOpts, Output}
  1739. %% {Name, DTL, Vars, RenderOpts, CompilerOpts, Output, Warnings}
  1740. def_to_test(Group, #test{ title=Name }=T) ->
  1741. T#test{ title = lists:concat([Group, ": ", Name]) };
  1742. def_to_test(Group, {Name, DTL, Vars, Output}) ->
  1743. def_to_test(Group, {Name, DTL, Vars, [], [], Output, default_warnings()});
  1744. def_to_test(Group, {Name, DTL, Vars, RenderOpts, Output}) ->
  1745. def_to_test(Group, {Name, DTL, Vars, RenderOpts, [], Output, default_warnings()});
  1746. def_to_test(Group, {Name, DTL, Vars, RenderOpts, CompilerOpts, Output}) ->
  1747. def_to_test(Group, {Name, DTL, Vars, RenderOpts, CompilerOpts, Output, default_warnings()});
  1748. def_to_test(Group, {Name, DTL, Vars, RenderOpts, CompilerOpts, Output, Warnings}) ->
  1749. #test{
  1750. title = lists:concat([Group, ": ", Name]),
  1751. source = {template, DTL},
  1752. render_vars = Vars,
  1753. render_opts = RenderOpts,
  1754. compile_vars = undefined,
  1755. compile_opts = CompilerOpts ++ (#test{})#test.compile_opts,
  1756. output = Output,
  1757. warnings = Warnings
  1758. }.
  1759. generate_test_date() ->
  1760. {{Y,M,D}, _} = erlang:localtime(),
  1761. MonthName = [
  1762. "January", "February", "March", "April",
  1763. "May", "June", "July", "August", "September",
  1764. "October", "November", "December"
  1765. ],
  1766. OrdinalSuffix = [
  1767. "st","nd","rd","th","th","th","th","th","th","th", % 1-10
  1768. "th","th","th","th","th","th","th","th","th","th", % 10-20
  1769. "st","nd","rd","th","th","th","th","th","th","th", % 20-30
  1770. "st"
  1771. ],
  1772. list_to_binary([
  1773. "It is the ",
  1774. integer_to_list(D),
  1775. lists:nth(D, OrdinalSuffix),
  1776. " of ", lists:nth(M, MonthName),
  1777. " ", integer_to_list(Y), "."
  1778. ]).
  1779. default_warnings() -> [].
  1780. error_info(File, Ws, Mod) ->
  1781. {File, [error_info(W, Mod) || W <- Ws]}.
  1782. error_info({Line, ErrorDesc}, Mod)
  1783. when is_integer(Line); Line =:= none ->
  1784. {Line, Mod, ErrorDesc};
  1785. error_info({Line, Module, _}=ErrorDesc, _Mod)
  1786. when is_integer(Line), is_atom(Module) ->
  1787. ErrorDesc;
  1788. error_info({none, Module, _}=ErrorDesc, _Mod)
  1789. when is_atom(Module) ->
  1790. ErrorDesc;
  1791. error_info({{Line, Col}, Module, _}=ErrorDesc, _Mod)
  1792. when is_integer(Line), is_integer(Col), is_atom(Module) ->
  1793. ErrorDesc;
  1794. error_info(Ws, Mod) when is_list(Ws) ->
  1795. error_info("erly_test", Ws, Mod);
  1796. error_info(ErrorDesc, Mod) ->
  1797. {none, Mod, ErrorDesc}.
  1798. error_info(Ei) ->
  1799. error_info(Ei, erlydtl_beam_compiler).
  1800. template_file(Dir, Name) -> filename:join(["../test/files", Dir, Name]).
  1801. functional_test(F) ->
  1802. setup_compile(#test{
  1803. title = F,
  1804. module = list_to_atom("functional_test_" ++ F),
  1805. source = {file, template_file(input, F)}
  1806. }).
  1807. setup_compile(#test{ title=F, compile_opts=Opts }=T) ->
  1808. CompileOpts = [{doc_root, "../test/files/input"}|Opts],
  1809. case setup_compile(F) of
  1810. {ok, [CV|Other]} ->
  1811. CO = proplists:get_value(compile_opts, Other, []),
  1812. Ws = proplists:get_value(warnings, Other, []),
  1813. setup(T#test{
  1814. compile_vars = CV,
  1815. compile_opts = CO ++ CompileOpts,
  1816. warnings = Ws
  1817. });
  1818. {error, Es, Ws} ->
  1819. T#test{
  1820. errors = Es,
  1821. warnings = Ws,
  1822. compile_opts = CompileOpts
  1823. }
  1824. end;
  1825. setup_compile("for_list_preset") ->
  1826. CompileVars = [{fruit_list, [["apple", "apples"], ["banana", "bananas"], ["coconut", "coconuts"]]}],
  1827. {ok, [CompileVars]};
  1828. setup_compile("for_preset") ->
  1829. CompileVars = [{fruit_list, ["preset-apple", "preset-banana", "preset-coconut"]}],
  1830. {ok, [CompileVars]};
  1831. setup_compile("for_records_preset") ->
  1832. Link1a = [{name, "Amazon (preset)"}, {url, "http://amazon.com"}],
  1833. Link2a = [{name, "Google (preset)"}, {url, "http://google.com"}],
  1834. Link3a = [{name, "Microsoft (preset)"}, {url, "http://microsoft.com"}],
  1835. CompileVars = [{software_links, [Link1a, Link2a, Link3a]}],
  1836. {ok, [CompileVars]};
  1837. setup_compile("if_preset") ->
  1838. CompileVars = [{var1, "something"}],
  1839. {ok, [CompileVars]};
  1840. setup_compile("ifequal_preset") ->
  1841. CompileVars = [{var1, "foo"}, {var2, "foo"}],
  1842. {ok, [CompileVars]};
  1843. setup_compile("ifnotequal_preset") ->
  1844. CompileVars = [{var1, "foo"}, {var2, "foo"}],
  1845. {ok, [CompileVars]};
  1846. setup_compile("var_preset") ->
  1847. CompileVars = [{preset_var1, "preset-var1"}, {preset_var2, "preset-var2"}],
  1848. {ok, [CompileVars]};
  1849. setup_compile("extends_for") ->
  1850. CompileVars = [{veggie_list, ["broccoli", "beans", "peas", "carrots"]}],
  1851. {ok, [CompileVars]};
  1852. setup_compile("extends2") ->
  1853. File = template_file(input, "extends2"),
  1854. Error = {none, erlydtl_beam_compiler, unexpected_extends_tag},
  1855. {error, [{File, [Error]}], []};
  1856. setup_compile("extends3") ->
  1857. File = template_file(input, "extends3"),
  1858. Include = template_file(input, "imaginary"),
  1859. Error = {none, erlydtl_beam_compiler, {read_file, Include, enoent}},
  1860. {error, [{File, [Error]}], []};
  1861. setup_compile("extends4") ->
  1862. File = template_file(input, "extends4"),
  1863. Warning = {{1,21}, erlydtl_beam_compiler, non_block_tag},
  1864. {ok, [[]|[{warnings, [{File, [Warning]}]}]]};
  1865. setup_compile("missing") ->
  1866. File = template_file(input, "missing"),
  1867. Error = {none, erlydtl_compiler, {read_file, File, enoent}},
  1868. {error, [{File, [Error]}], []};
  1869. setup_compile("custom_tag") ->
  1870. {ok, [[]|[{compile_opts, [{custom_tags_modules, [erlydtl_custom_tags]}]}]]};
  1871. setup_compile("custom_tag1") -> setup_compile("custom_tag");
  1872. setup_compile("custom_tag2") -> setup_compile("custom_tag");
  1873. setup_compile("custom_tag3") -> setup_compile("custom_tag");
  1874. setup_compile("custom_tag4") -> setup_compile("custom_tag");
  1875. setup_compile("custom_tag_var") -> setup_compile("custom_tag");
  1876. setup_compile("custom_tag_lib_var") ->
  1877. {ok, [[]|[{compile_opts, [{libraries, [{custom_tag_lib,erlydtl_custom_tags_lib}]}, {default_libraries, [custom_tag_lib]}]}]]};
  1878. setup_compile("super_escaped") ->
  1879. {ok, [[]|[{compile_opts, [auto_escape]}]]};
  1880. setup_compile(_) ->
  1881. {ok, [[]]}.
  1882. expected(File) ->
  1883. Filename = template_file(expect, File),
  1884. case file:read_file(Filename) of
  1885. {ok, Data} -> Data;
  1886. _ -> fun (Data) ->
  1887. ok = file:write_file(Filename, Data),
  1888. io:format(
  1889. user,
  1890. "## Saved expected output for test ~p to ~p.~n"
  1891. " Verify the contents, as it is used to pass the test on subsequent test runs.~n"
  1892. "~n",
  1893. [File, Filename]),
  1894. throw({verify_new_expected_output, Filename})
  1895. end
  1896. end.
  1897. setup(#test{ title = F, output=undefined }=T) ->
  1898. {Vars, Opts, Result} =
  1899. case setup(F) of
  1900. {ok, V} -> {V, [], expected(F)};
  1901. {ok, V, O} -> {V, O, expected(F)};
  1902. {ok, V, O, skip_check} -> {V, O, fun (_) -> ok end};
  1903. {ok, V, O, R} -> {V, O, R}
  1904. end,
  1905. T#test{
  1906. render_vars = Vars,
  1907. render_opts = Opts,
  1908. output = Result
  1909. };
  1910. setup(#test{}=T) -> T;
  1911. setup("autoescape") ->
  1912. RenderVars = [{var1, "<b>bold</b>"}],
  1913. {ok, RenderVars};
  1914. setup("extends") ->
  1915. RenderVars = [{base_var, "base-barstring"}, {test_var, "test-barstring"}],
  1916. {ok, RenderVars};
  1917. setup("include_template") -> setup("extends");
  1918. setup("include_path") -> setup("extends");
  1919. setup("extends_path") -> setup("extends");
  1920. setup("extends_path2") -> setup("extends");
  1921. setup("block_super") -> setup("extends");
  1922. setup("filters") ->
  1923. RenderVars = [
  1924. {date_var1, {1975,7,24}},
  1925. {datetime_var1, {{1975,7,24}, {7,13,1}}},
  1926. {'list', ["eins", "zwei", "drei"]}
  1927. ],
  1928. {ok, RenderVars};
  1929. setup("for") ->
  1930. RenderVars = [{fruit_list, ["apple", "banana", "coconut"]}],
  1931. {ok, RenderVars};
  1932. setup("for_list") ->
  1933. RenderVars = [{fruit_list, [["apple", "apples", "$1"], ["banana", "bananas", "$2"], ["coconut", "coconuts", "$500"]]}],
  1934. {ok, RenderVars};
  1935. setup("for_tuple") ->
  1936. RenderVars = [{fruit_list, [{"apple", "apples"}, {"banana", "bananas"}, {"coconut", "coconuts"}]}],
  1937. {ok, RenderVars};
  1938. setup("for_records") ->
  1939. Link1 = [{name, "Amazon"}, {url, "http://amazon.com"}],
  1940. Link2 = [{name, "Google"}, {url, "http://google.com"}],
  1941. Link3 = [{name, "Microsoft"}, {url, "http://microsoft.com"}],
  1942. RenderVars = [{link_list, [Link1, Link2, Link3]}],
  1943. {ok, RenderVars};
  1944. setup("for_records_preset") ->
  1945. Link1b = [{name, "Canon"}, {url, "http://canon.com"}],
  1946. Link2b = [{name, "Leica"}, {url, "http://leica.com"}],
  1947. Link3b = [{name, "Nikon"}, {url, "http://nikon.com"}],
  1948. RenderVars = [{photo_links, [Link1b, Link2b, Link3b]}],
  1949. {ok, RenderVars};
  1950. setup("include") ->
  1951. RenderVars = [{var1, "foostring1"}, {var2, "foostring2"}],
  1952. {ok, RenderVars};
  1953. setup("if") ->
  1954. RenderVars = [{var1, "something"}],
  1955. {ok, RenderVars};
  1956. setup("ifequal") ->
  1957. RenderVars = [{var1, "foo"}, {var2, "foo"}, {var3, "bar"}],
  1958. {ok, RenderVars};
  1959. setup("ifequal_preset") ->
  1960. RenderVars = [{var3, "bar"}],
  1961. {ok, RenderVars};
  1962. setup("ifnotequal") ->
  1963. RenderVars = [{var1, "foo"}, {var2, "foo"}, {var3, "bar"}],
  1964. {ok, RenderVars};
  1965. setup("now") ->
  1966. {ok, [], [], skip_check};
  1967. setup("var") ->
  1968. RenderVars = [{var1, "foostring1"}, {var2, "foostring2"}, {var_not_used, "foostring3"}],
  1969. {ok, RenderVars};
  1970. setup("var_preset") ->
  1971. RenderVars = [{var1, "foostring1"}, {var2, "foostring2"}],
  1972. {ok, RenderVars};
  1973. setup("cycle") ->
  1974. RenderVars = [{test, [integer_to_list(X) || X <- lists:seq(1, 20)]},
  1975. {a, "Apple"}, {b, "Banana"}, {c, "Cherry"}],
  1976. {ok, RenderVars};
  1977. setup("trans") ->
  1978. RenderVars = [{locale, "reverse"}],
  1979. {ok, RenderVars};
  1980. setup("locale") ->
  1981. {ok, _RenderVars = [{locale, "ru"}]};
  1982. setup("custom_tag1") ->
  1983. {ok, [{a, <<"a1">>}], [{locale, ru}], <<"b1\n">>};
  1984. setup("custom_tag2") ->
  1985. {ok, [{a, <<"a1">>}], [{locale, ru}, {foo, bar}], <<"b2\n">>};
  1986. setup("custom_tag3") ->
  1987. {ok, [{a, <<"a1">>}], [{locale, ru}], <<"b3\n">>};
  1988. setup("custom_tag4") ->
  1989. {ok, [], [], <<"a\n">>};
  1990. setup("custom_tag_var") ->
  1991. {ok, [{a, <<"a1">>}], [{locale, ru}], <<"\nb1\n11\n">>};
  1992. setup("custom_tag_lib_var") ->
  1993. {ok, [{a, <<"a1">>}], [{locale, ru}], <<"\nb1\n11\n">>};
  1994. setup("ssi") ->
  1995. RenderVars = [{path, "ssi_include.html"}],
  1996. {ok, RenderVars};
  1997. setup("wrapper") ->
  1998. RenderVars = [{types, ["b", "a", "c"]}],
  1999. {ok, RenderVars};
  2000. %%--------------------------------------------------------------------
  2001. %% Custom tags
  2002. %%--------------------------------------------------------------------
  2003. setup("custom_call") ->
  2004. RenderVars = [{var1, "something"}],
  2005. {ok, RenderVars};
  2006. setup(_) ->
  2007. {ok, []}.