erlydtl_test_defs.erl 94 KB

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