erlydtl_test_defs.erl 98 KB

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