12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088 |
- -module(erlydtl_unittests).
-
- -export([run_tests/0]).
-
- tests() ->
- [
- {"vars", [
- {"string",
- <<"String value is: {{ var1 }}">>,
- [{var1, "foo"}], <<"String value is: foo">>},
- {"int",
- <<"The magic number is: {{ var1 }}">>,
- [{var1, 42}], <<"The magic number is: 42">>},
- {"float",
- <<"The price of milk is: {{ var1 }}">>,
- [{var1, 0.42}], <<"The price of milk is: 0.42">>},
- {"No spaces",
- <<"{{var1}}">>,
- [{var1, "foo"}], <<"foo">>}
- ]},
- {"comment", [
- {"comment block is excised",
- <<"bob {% comment %}(moron){% endcomment %} loblaw">>,
- [], <<"bob loblaw">>},
- {"inline comment is excised",
- <<"you're {# not #} a very nice person">>,
- [], <<"you're a very nice person">>}
- ]},
- {"autoescape", [
- {"Autoescape works",
- <<"{% autoescape on %}{{ var1 }}{% endautoescape %}">>,
- [{var1, "<b>bold</b>"}], <<"<b>bold</b>">>},
- {"Nested autoescape",
- <<"{% autoescape on %}{{ var1 }}{% autoescape off %}{{ var1 }}{% endautoescape %}{% endautoescape %}">>,
- [{var1, "<b>"}], <<"<b><b>">>}
- ]},
- {"string literal", [
- {"Render literal",
- <<"{{ \"foo\" }} is my name">>, [], <<"foo is my name">>},
- {"Newlines are escaped",
- <<"{{ \"foo\\n\" }}">>, [], <<"foo\n">>}
- ]},
- {"cycle", [
- {"Cycling through quoted strings",
- <<"{% for i in test %}{% cycle 'a' 'b' %}{{ i }},{% endfor %}">>,
- [{test, ["0", "1", "2", "3", "4"]}], <<"a0,b1,a2,b3,a4,">>},
- {"Cycling through normal variables",
- <<"{% for i in test %}{% cycle aye bee %}{{ i }},{% endfor %}">>,
- [{test, ["0", "1", "2", "3", "4"]}, {aye, "a"}, {bee, "b"}],
- <<"a0,b1,a2,b3,a4,">>}
- ]},
- {"number literal", [
- {"Render integer",
- <<"{{ 5 }}">>, [], <<"5">>}
- ]},
- {"variable", [
- {"Render variable",
- <<"{{ var1 }} is my game">>, [{var1, "bar"}], <<"bar is my game">>},
- {"Render variable with attribute",
- <<"I enjoy {{ var1.game }}">>, [{var1, [{game, "Othello"}]}], <<"I enjoy Othello">>},
- {"Render variable with string-key attribute",
- <<"I also enjoy {{ var1.game }}">>, [{var1, [{"game", "Parcheesi"}]}], <<"I also enjoy Parcheesi">>},
- {"Render variable with binary-key attribute",
- <<"I also enjoy {{ var1.game }}">>, [{var1, [{<<"game">>, "Parcheesi"}]}], <<"I also enjoy Parcheesi">>},
- {"Render variable in dict",
- <<"{{ var1 }}">>, dict:store(var1, "bar", dict:new()), <<"bar">>},
- {"Render variable in gb_tree",
- <<"{{ var1 }}">>, gb_trees:insert(var1, "bar", gb_trees:empty()), <<"bar">>},
- {"Render variable in arity-1 func",
- <<"I enjoy {{ var1 }}">>, fun (var1) -> "Othello" end, <<"I enjoy Othello">>},
- {"Render variable with attribute in dict",
- <<"{{ var1.attr }}">>, [{var1, dict:store(attr, "Othello", dict:new())}], <<"Othello">>},
- {"Render variable with attribute in gb_tree",
- <<"{{ var1.attr }}">>, [{var1, gb_trees:insert(attr, "Othello", gb_trees:empty())}], <<"Othello">>},
- {"Render variable with attribute in arity-1 func",
- <<"I enjoy {{ var1.game }}">>, [{var1, fun (game) -> "Othello" end}], <<"I enjoy Othello">>},
- {"Render variable in parameterized module",
- <<"{{ var1.some_var }}">>, [{var1, erlydtl_example_variable_storage:new("foo")}], <<"foo">>},
- {"Nested attributes",
- <<"{{ person.city.state.country }}">>, [{person, [{city, [{state, [{country, "Italy"}]}]}]}],
- <<"Italy">>}
- ]},
- {"now", [
- {"now functional",
- <<"It is the {% now \"jS o\\f F Y\" %}.">>, [{var1, ""}], generate_test_date()}
- ]},
- {"if", [
- {"If/else",
- <<"{% if var1 %}boo{% else %}yay{% endif %}">>, [{var1, ""}], <<"yay">>},
- {"If",
- <<"{% if var1 %}boo{% endif %}">>, [{var1, ""}], <<>>},
- {"If not",
- <<"{% if not var1 %}yay{% endif %}">>, [{var1, ""}], <<"yay">>},
- {"If \"0\"",
- <<"{% if var1 %}boo{% endif %}">>, [{var1, "0"}], <<>>},
- {"If false",
- <<"{% if var1 %}boo{% endif %}">>, [{var1, false}], <<>>},
- {"If false string",
- <<"{% if var1 %}boo{% endif %}">>, [{var1, "false"}], <<"boo">>},
- {"If undefined",
- <<"{% if var1 %}boo{% endif %}">>, [{var1, undefined}], <<>>},
- {"If other atom",
- <<"{% if var1 %}yay{% endif %}">>, [{var1, foobar}], <<"yay">>},
- {"If non-empty string",
- <<"{% if var1 %}yay{% endif %}">>, [{var1, "hello"}], <<"yay">>},
- {"If proplist",
- <<"{% if var1 %}yay{% endif %}">>, [{var1, [{foo, "bar"}]}], <<"yay">>},
- {"If complex",
- <<"{% if foo.bar.baz %}omgwtfbbq{% endif %}">>, [], <<"">>}
- ]},
- {"if .. in ..", [
- {"If substring in string",
- <<"{% if var1 in var2 %}yay{% endif %}">>, [{var1, "rook"}, {var2, "Crooks"}], <<"yay">>},
- {"If substring in string (false)",
- <<"{% if var1 in var2 %}boo{% endif %}">>, [{var1, "Cook"}, {var2, "Crooks"}], <<>>},
- {"If substring not in string",
- <<"{% if var1 not in var2 %}yay{% endif %}">>, [{var1, "Cook"}, {var2, "Crooks"}], <<"yay">>},
- {"If substring not in string (false)",
- <<"{% if var1 not in var2 %}boo{% endif %}">>, [{var1, "rook"}, {var2, "Crooks"}], <<>>},
- {"If literal substring in string",
- <<"{% if \"man\" in \"Ottoman\" %}yay{% endif %}">>, [], <<"yay">>},
- {"If literal substring in string (false)",
- <<"{% if \"woman\" in \"Ottoman\" %}boo{% endif %}">>, [], <<>>},
- {"If element in list",
- <<"{% if var1 in var2 %}yay{% endif %}">>, [{var1, "foo"}, {var2, ["bar", "foo", "baz"]}], <<"yay">>},
- {"If element in list (false)",
- <<"{% if var1 in var2 %}boo{% endif %}">>, [{var1, "FOO"}, {var2, ["bar", "foo", "baz"]}], <<>>}
- ]},
- {"if .. and ..", [
- {"If true and true",
- <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, true}, {var2, true}], <<"yay">>},
- {"If true and false",
- <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, true}, {var2, false}], <<"">>},
- {"If false and true",
- <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, false}, {var2, true}], <<"">>},
- {"If false and false ",
- <<"{% if var1 and var2 %}yay{% endif %}">>, [{var1, false}, {var2, false}], <<"">>}
- ]},
- {"if .. or ..", [
- {"If true or true",
- <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, true}, {var2, true}], <<"yay">>},
- {"If true or false",
- <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, true}, {var2, false}], <<"yay">>},
- {"If false or true",
- <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, false}, {var2, true}], <<"yay">>},
- {"If false or false ",
- <<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, false}, {var2, false}], <<"">>}
- ]},
- {"if equality", [
- {"If int equals number literal",
- <<"{% if var1 == 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
- {"If int equals number literal (false)",
- <<"{% if var1 == 2 %}yay{% endif %}">>, [{var1, 3}], <<"">>},
- {"If string equals string literal",
- <<"{% if var1 == \"2\" %}yay{% endif %}">>, [{var1, "2"}], <<"yay">>},
- {"If string equals string literal (false)",
- <<"{% if var1 == \"2\" %}yay{% endif %}">>, [{var1, "3"}], <<"">>},
- {"If int not equals number literal",
- <<"{% if var1 != 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
- {"If string not equals string literal",
- <<"{% if var1 != \"2\" %}yay{% endif %}">>, [{var1, "3"}], <<"yay">>},
- {"If filter result equals number literal",
- <<"{% if var1|length == 2 %}yay{% endif %}">>, [{var1, ["fo", "bo"]}], <<"yay">>},
- {"If filter result equals string literal",
- <<"{% if var1|capfirst == \"Foo\" %}yay{% endif %}">>, [{var1, "foo"}], <<"yay">>}
- ]},
- {"if size comparison", [
- {"If int greater than number literal",
- <<"{% if var1 > 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
- {"If int greater than number literal (false)",
- <<"{% if var1 > 2 %}yay{% endif %}">>, [{var1, 2}], <<"">>},
-
- {"If int greater than or equal to number literal",
- <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
- {"If int greater than or equal to number literal (2)",
- <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
- {"If int greater than or equal to number literal (false)",
- <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 1}], <<"">>},
-
- {"If int less than number literal",
- <<"{% if var1 < 2 %}yay{% endif %}">>, [{var1, 1}], <<"yay">>},
- {"If int less than number literal (false)",
- <<"{% if var1 < 2 %}yay{% endif %}">>, [{var1, 2}], <<"">>},
-
- {"If int less than or equal to number literal",
- <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 1}], <<"yay">>},
- {"If int less than or equal to number literal",
- <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
- {"If int less than or equal to number literal (false)",
- <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 3}], <<"">>}
- ]},
- {"if complex bool", [
- {"If (true or false) and true",
- <<"{% if (var1 or var2) and var3 %}yay{% endif %}">>,
- [{var1, true}, {var2, false}, {var3, true}], <<"yay">>},
- {"If true or (false and true)",
- <<"{% if var1 or (var2 and var3) %}yay{% endif %}">>,
- [{var1, true}, {var2, false}, {var3, true}], <<"yay">>}
- ]},
- {"for", [
- {"Simple loop",
- <<"{% for x in list %}{{ x }}{% endfor %}">>, [{'list', ["1", "2", "3"]}],
- <<"123">>},
- {"Expand list",
- <<"{% for x, y in list %}{{ x }},{{ y }}\n{% endfor %}">>, [{'list', [["X", "1"], ["X", "2"]]}],
- <<"X,1\nX,2\n">>},
- {"Expand tuple",
- <<"{% for x, y in list %}{{ x }},{{ y }}\n{% endfor %}">>, [{'list', [{"X", "1"}, {"X", "2"}]}],
- <<"X,1\nX,2\n">>},
- {"Resolve variable attribute",
- <<"{% for number in person.numbers %}{{ number }}\n{% endfor %}">>, [{person, [{numbers, ["411", "911"]}]}],
- <<"411\n911\n">>},
- {"Resolve nested variable attribute",
- <<"{% for number in person.home.numbers %}{{ number }}\n{% endfor %}">>, [{person, [{home, [{numbers, ["411", "911"]}]}]}],
- <<"411\n911\n">>},
- {"Counter0",
- <<"{% for number in numbers %}{{ forloop.counter0 }}. {{ number }}\n{% endfor %}">>,
- [{numbers, ["Zero", "One", "Two"]}], <<"0. Zero\n1. One\n2. Two\n">>},
- {"Counter",
- <<"{% for number in numbers %}{{ forloop.counter }}. {{ number }}\n{% endfor %}">>,
- [{numbers, ["One", "Two", "Three"]}], <<"1. One\n2. Two\n3. Three\n">>},
- {"Reverse Counter0",
- <<"{% for number in numbers %}{{ forloop.revcounter0 }}. {{ number }}\n{% endfor %}">>,
- [{numbers, ["Two", "One", "Zero"]}], <<"2. Two\n1. One\n0. Zero\n">>},
- {"Reverse Counter",
- <<"{% for number in numbers %}{{ forloop.revcounter }}. {{ number }}\n{% endfor %}">>,
- [{numbers, ["Three", "Two", "One"]}], <<"3. Three\n2. Two\n1. One\n">>},
- {"Counter \"first\"",
- <<"{% for number in numbers %}{% if forloop.first %}{{ number }}{% endif %}{% endfor %}">>,
- [{numbers, ["One", "Two", "Three"]}], <<"One">>},
- {"Counter \"last\"",
- <<"{% for number in numbers %}{% if forloop.last %}{{ number }}{% endif %}{% endfor %}">>,
- [{numbers, ["One", "Two", "Three"]}], <<"Three">>},
- {"Nested for loop",
- <<"{% for outer in list %}{% for inner in outer %}{{ inner }}\n{% endfor %}{% endfor %}">>,
- [{'list', [["Al", "Albert"], ["Jo", "Joseph"]]}],
- <<"Al\nAlbert\nJo\nJoseph\n">>},
- {"Access parent loop counters",
- <<"{% for outer in list %}{% for inner in outer %}({{ forloop.parentloop.counter0 }}, {{ forloop.counter0 }})\n{% endfor %}{% endfor %}">>,
- [{'list', [["One", "two"], ["One", "two"]]}], <<"(0, 0)\n(0, 1)\n(1, 0)\n(1, 1)\n">>},
- {"If changed",
- <<"{% for x in list %}{% ifchanged %}{{ x }}\n{% endifchanged %}{% endfor %}">>,
- [{'list', ["one", "two", "two", "three", "three", "three"]}], <<"one\ntwo\nthree\n">>},
- {"If changed/2",
- <<"{% for x, y in list %}{% ifchanged %}{{ x|upper }}{% endifchanged %}{% ifchanged %}{{ y|lower }}{% endifchanged %}\n{% endfor %}">>,
- [{'list', [["one", "a"], ["two", "A"], ["two", "B"], ["three", "b"], ["three", "c"], ["Three", "b"]]}], <<"ONEa\nTWO\nb\nTHREE\nc\nb\n">>},
- {"If changed/else",
- <<"{% for x in list %}{% ifchanged %}{{ x }}\n{% else %}foo\n{% endifchanged %}{% endfor %}">>,
- [{'list', ["one", "two", "two", "three", "three", "three"]}], <<"one\ntwo\nfoo\nthree\nfoo\nfoo\n">>},
- {"If changed/param",
- <<"{% for date in list %}{% ifchanged date.month %} {{ date.month }}:{{ date.day }}{% else %},{{ date.day }}{% endifchanged %}{% endfor %}\n">>,
- [{'list', [[{month,"Jan"},{day,1}],[{month,"Jan"},{day,2}],[{month,"Apr"},{day,10}],
- [{month,"Apr"},{day,11}],[{month,"May"},{day,4}]]}],
- <<" Jan:1,2 Apr:10,11 May:4\n">>},
- {"If changed/param2",
- <<"{% for x, y in list %}{% ifchanged y|upper %}{{ x|upper }}{% endifchanged %}\n{% endfor %}">>,
- [{'list', [["one", "a"], ["two", "A"], ["two", "B"], ["three", "b"], ["three", "c"], ["Three", "b"]]}], <<"ONE\n\nTWO\n\nTHREE\nTHREE\n">>},
- {"If changed/param2 combined",
- <<"{% for x, y in list %}{% ifchanged x y|upper %}{{ x }}{% endifchanged %}\n{% endfor %}">>,
- [{'list', [["one", "a"], ["two", "A"], ["two", "B"], ["three", "b"], ["three", "B"], ["three", "c"]]}], <<"one\ntwo\ntwo\nthree\n\nthree\n">>},
- {"If changed/resolve",
- <<"{% for x in list %}{% ifchanged x.name|first %}{{ x.value }}{% endifchanged %}\n{% endfor %}">>,
- [{'list', [[{"name", ["nA","nB"]},{"value","1"}],[{"name", ["nA","nC"]},{"value","2"}],
- [{"name", ["nB","nC"]},{"value","3"}],[{"name", ["nB","nA"]},{"value","4"}]]}],
- <<"1\n\n3\n\n">>}
- ]},
- {"for/empty", [
- {"Simple loop",
- <<"{% for x in list %}{{ x }}{% empty %}shucks{% endfor %}">>, [{'list', ["1", "2", "3"]}],
- <<"123">>},
- {"Simple loop (empty)",
- <<"{% for x in list %}{{ x }}{% empty %}shucks{% endfor %}">>, [{'list', []}],
- <<"shucks">>}
- ]},
- {"ifequal", [
- {"Compare variable to literal",
- <<"{% ifequal var1 \"foo\" %}yay{% endifequal %}">>,
- [{var1, "foo"}], <<"yay">>},
- {"Compare variable to unequal literal",
- <<"{% ifequal var1 \"foo\" %}boo{% endifequal %}">>,
- [{var1, "bar"}], <<>>},
- {"Compare literal to variable",
- <<"{% ifequal \"foo\" var1 %}yay{% endifequal %}">>,
- [{var1, "foo"}], <<"yay">>},
- {"Compare literal to unequal variable",
- <<"{% ifequal \"foo\" var1 %}boo{% endifequal %}">>,
- [{var1, "bar"}], <<>>},
- {"Compare variable to literal (int string)",
- <<"{% ifequal var1 \"2\" %}yay{% else %}boo{% endifequal %}">>,
- [{var1, "2"}], <<"yay">>},
- {"Compare variable to literal (int)",
- <<"{% ifequal var1 2 %}yay{% else %}boo{% endifequal %}">>,
- [{var1, 2}], <<"yay">>},
- {"Compare variable to unequal literal (int)",
- <<"{% ifequal var1 2 %}boo{% else %}yay{% endifequal %}">>,
- [{var1, 3}], <<"yay">>},
- {"Compare variable to equal literal (atom)",
- <<"{% ifequal var1 \"foo\"%}yay{% endifequal %}">>,
- [{var1, foo}], <<"yay">>},
- {"Compare variable to unequal literal (atom)",
- <<"{% ifequal var1 \"foo\"%}yay{% else %}boo{% endifequal %}">>,
- [{var1, bar}], <<"boo">>}
- ]},
- {"ifequal/else", [
- {"Compare variable to literal",
- <<"{% ifequal var1 \"foo\" %}yay{% else %}boo{% endifequal %}">>,
- [{var1, "foo"}], <<"yay">>},
- {"Compare variable to unequal literal",
- <<"{% ifequal var1 \"foo\" %}boo{% else %}yay{% endifequal %}">>,
- [{var1, "bar"}], <<"yay">>},
- {"Compare literal to variable",
- <<"{% ifequal \"foo\" var1 %}yay{% else %}boo{% endifequal %}">>,
- [{var1, "foo"}], <<"yay">>},
- {"Compare literal to unequal variable",
- <<"{% ifequal \"foo\" var1 %}boo{% else %}yay{% endifequal %}">>,
- [{var1, "bar"}], <<"yay">>}
- ]},
- {"ifnotequal", [
- {"Compare variable to literal",
- <<"{% ifnotequal var1 \"foo\" %}boo{% endifnotequal %}">>,
- [{var1, "foo"}], <<>>},
- {"Compare variable to unequal literal",
- <<"{% ifnotequal var1 \"foo\" %}yay{% endifnotequal %}">>,
- [{var1, "bar"}], <<"yay">>},
- {"Compare literal to variable",
- <<"{% ifnotequal \"foo\" var1 %}boo{% endifnotequal %}">>,
- [{var1, "foo"}], <<>>},
- {"Compare literal to unequal variable",
- <<"{% ifnotequal \"foo\" var1 %}yay{% endifnotequal %}">>,
- [{var1, "bar"}], <<"yay">>}
- ]},
- {"ifnotequal/else", [
- {"Compare variable to literal",
- <<"{% ifnotequal var1 \"foo\" %}boo{% else %}yay{% endifnotequal %}">>,
- [{var1, "foo"}], <<"yay">>},
- {"Compare variable to unequal literal",
- <<"{% ifnotequal var1 \"foo\" %}yay{% else %}boo{% endifnotequal %}">>,
- [{var1, "bar"}], <<"yay">>},
- {"Compare literal to variable",
- <<"{% ifnotequal \"foo\" var1 %}boo{% else %}yay{% endifnotequal %}">>,
- [{var1, "foo"}], <<"yay">>},
- {"Compare literal to unequal variable",
- <<"{% ifnotequal \"foo\" var1 %}yay{% else %}boo{% endifnotequal %}">>,
- [{var1, "bar"}], <<"yay">>}
- ]},
- {"filter tag", [
- {"Apply a filter",
- <<"{% filter escape %}&{% endfilter %}">>, [], <<"&">>},
- {"Chained filters",
- <<"{% filter linebreaksbr|escape %}\n{% endfilter %}">>, [], <<"<br />">>}
- ]},
- {"filters", [
- {"Filter a literal",
- <<"{{ \"pop\"|capfirst }}">>, [],
- <<"Pop">>},
- {"Filters applied in order",
- <<"{{ var1|force_escape|length }}">>, [{var1, <<"&">>}],
- <<"5">>},
- {"Escape is applied last",
- <<"{{ var1|escape|linebreaksbr }}">>, [{var1, <<"\n">>}],
- <<"<br />">>},
- {"|add:4",
- <<"{{ one|add:4 }}">>, [{one, "1"}],
- <<"5">>},
- {"|addslashes",
- <<"{{ var1|addslashes }}">>, [{var1, "Jimmy's \"great\" meats'n'things"}],
- <<"Jimmy\\'s \\\"great\\\" meats\\'n\\'things">>},
- {"|capfirst",
- <<"{{ var1|capfirst }}">>, [{var1, "dana boyd"}],
- <<"Dana boyd">>},
- {"|center:10",
- <<"{{ var1|center:10 }}">>, [{var1, "MB"}],
- <<" MB ">>},
- {"|center:1",
- <<"{{ var1|center:1 }}">>, [{var1, "KBR"}],
- <<"B">>},
- {"|cut:\" \"",
- <<"{{ var1|cut:\" \" }}">>, [{var1, "String with spaces"}],
- <<"Stringwithspaces">>},
- {"|date 1",
- <<"{{ var1|date:\"jS F Y H:i\" }}">>,
- [{var1, {1975,7,24}}],
- <<"24th July 1975 00:00">>},
- {"|date 2",
- <<"{{ var1|date:\"jS F Y H:i\" }}">>,
- [{var1, {{1975,7,24}, {7,13,1}}}],
- <<"24th July 1975 07:13">>},
- {"|date 3",
- <<"{{ var1|date }}">>,
- [{var1, {{1975,7,24}, {7,13,1}}}],
- <<"July 24, 1975">>},
- {"|default:\"foo\" 1",
- <<"{{ var1|default:\"foo\" }}">>, [], <<"foo">>},
- {"|default:\"foo\" 2",
- <<"{{ var1|default:\"foo\" }}">>, [{var1, "bar"}], <<"bar">>},
- {"|default:\"foo\" 3",
- <<"{{ var1|default:\"foo\" }}">>, [{var1, "0"}], <<"foo">>},
- {"|default_if_none:\"foo\"",
- <<"{{ var1|default_if_none:\"foo\" }}">>, [], <<"foo">>},
- {"|default_if_none:\"foo\" 2",
- <<"{{ var1|default_if_none:\"foo\" }}">>, [{var1, "bar"}], <<"bar">>},
- {"|divisibleby:\"3\"",
- <<"{% if var1|divisibleby:\"3\" %}yay{% endif %}">>, [{var1, 21}], <<"yay">>},
- {"|divisibleby:\"3\"",
- <<"{% if var1|divisibleby:\"3\" %}yay{% endif %}">>, [{var1, 22}], <<"">>},
- {"|escape",
- <<"{% autoescape on %}{{ var1|escape|escape|escape }}{% endautoescape %}">>, [{var1, ">&1"}], <<">&1">>},
- {"|escapejs",
- <<"{{ var1|escapejs }}">>, [{var1, "testing\r\njavascript 'string\" <b>escaping</b>"}],
- <<"testing\\u000D\\u000Ajavascript \\u0027string\\u0022 \\u003Cb\\u003Eescaping\\u003C/b\\u003E">>},
- {"|filesizeformat (bytes)",
- <<"{{ var1|filesizeformat }}">>, [{var1, 1023}], <<"1023 bytes">>},
- {"|filesizeformat (KB)",
- <<"{{ var1|filesizeformat }}">>, [{var1, 3487}], <<"3.4 KB">>},
- {"|filesizeformat (MB)",
- <<"{{ var1|filesizeformat }}">>, [{var1, 6277098}], <<"6.0 MB">>},
- {"|filesizeformat (GB)",
- <<"{{ var1|filesizeformat }}">>, [{var1, 1024 * 1024 * 1024}], <<"1.0 GB">>},
- {"|first",
- <<"{{ var1|first }}">>, [{var1, "James"}],
- <<"J">>},
- {"|fix_ampersands",
- <<"{{ var1|fix_ampersands }}">>, [{var1, "Ben & Jerry's"}],
- <<"Ben & Jerry's">>},
-
- {"|floatformat:\"-1\"",
- <<"{{ var1|floatformat:\"-1\" }}">>, [{var1, 34.23234}],
- <<"34.2">>},
- %% ?assertEqual( "", erlydtl_filters:floatformat(,)),
- %% ?assertEqual( "34", erlydtl_filters:floatformat(34.00000,-1)),
- %% ?assertEqual( "34.3", erlydtl_filters:floatformat(34.26000,-1)),
- %% ?assertEqual( "34.232", erlydtl_filters:floatformat(34.23234,3)),
- %% ?assertEqual( "34.000", erlydtl_filters:floatformat(34.00000,3)),
- %% ?assertEqual( "34.260", erlydtl_filters:floatformat(34.26000,3)),
- %% ?assertEqual( "34.232", erlydtl_filters:floatformat(34.23234,-3)),
- %% ?assertEqual( "34", erlydtl_filters:floatformat(34.00000,-3)),
- %% ?assertEqual( "34.260", erlydtl_filters:floatformat(34.26000,-3)).
- {"|force_escape",
- <<"{{ var1|force_escape }}">>, [{var1, "Ben & Jerry's <=> \"The World's Best Ice Cream\""}],
- <<"Ben & Jerry's <=> "The World's Best Ice Cream"">>},
- {"|format_integer",
- <<"{{ var1|format_integer }}">>, [{var1, 28}], <<"28">>},
- {"|format_number 1",
- <<"{{ var1|format_number }}">>, [{var1, 28}], <<"28">>},
- {"|format_number 2",
- <<"{{ var1|format_number }}">>, [{var1, 23.77}], <<"23.77">>},
- {"|format_number 3",
- <<"{{ var1|format_number }}">>, [{var1, "28.77"}], <<"28.77">>},
- {"|format_number 4",
- <<"{{ var1|format_number }}">>, [{var1, "23.77"}], <<"23.77">>},
- {"|format_number 5",
- <<"{{ var1|format_number }}">>, [{var1, fun() -> 29 end}], <<"29">>},
- {"|format_number 6",
- <<"{{ var1|format_number }}">>, [{var1, fun() -> fun() -> 31 end end}], <<"31">>},
- {"|get_digit:\"2\"",
- <<"{{ var1|get_digit:\"2\" }}">>, [{var1, 42}], <<"4">>},
- {"|iriencode",
- <<"{{ url|iriencode }}">>, [{url, "You #$*@!!"}], <<"You+#$*@!!">>},
- {"|join:\", \" (list)",
- <<"{{ var1|join:\", \" }}">>, [{var1, ["Liberte", "Egalite", "Fraternite"]}],
- <<"Liberte, Egalite, Fraternite">>},
- {"|join:\", \" (binary)",
- <<"{{ var1|join:\", \" }}">>, [{var1, [<<"Liberte">>, "Egalite", <<"Fraternite">>]}],
- <<"Liberte, Egalite, Fraternite">>},
- {"|last",
- <<"{{ var1|last }}">>, [{var1, "XYZ"}],
- <<"Z">>},
- {"|length",
- <<"{{ var1|length }}">>, [{var1, "antidisestablishmentarianism"}],
- <<"28">>},
- {"|linebreaks",
- <<"{{ var1|linebreaks }}">>, [{var1, "Joel\nis a slug"}],
- <<"<p>Joel<br />is a slug</p>">>},
- {"|linebreaks",
- <<"{{ var1|linebreaks }}">>, [{var1, "Joel\n\n\n\nis a slug"}],
- <<"<p>Joel</p><p>is a slug</p>">>},
- {"|linebreaks",
- <<"{{ var1|linebreaks }}">>, [{var1, "Joel\n\nis a \nslug"}],
- <<"<p>Joel</p><p>is a <br />slug</p>">>},
- {"|linebreaksbr",
- <<"{{ var1|linebreaksbr }}">>, [{var1, "One\nTwo\n\nThree\n\n\n"}],
- <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
- {"|linebreaksbr",
- <<"{{ \"One\\nTwo\\n\\nThree\\n\\n\\n\"|linebreaksbr }}">>, [],
- <<"One<br />Two<br /><br />Three<br /><br /><br />">>},
- {"|linenumbers",
- <<"{{ var1|linenumbers }}">>, [{var1, "a\nb\nc"}],
- <<"1. a\n2. b\n3. c">>},
- {"|linenumbers",
- <<"{{ var1|linenumbers }}">>, [{var1, "a"}],
- <<"1. a">>},
- {"|linenumbers",
- <<"{{ var1|linenumbers }}">>, [{var1, "a\n"}],
- <<"1. a\n2. ">>},
- {"|ljust:10",
- <<"{{ var1|ljust:10 }}">>, [{var1, "Gore"}],
- <<"Gore ">>},
- {"|lower",
- <<"{{ var1|lower }}">>, [{var1, "E. E. Cummings"}],
- <<"e. e. cummings">>},
- {"|makelist",
- <<"{{ list|make_list }}">>, [{list, "Joel"}],
- <<"J","o","e","l">>},
- {"|pluralize",
- <<"{{ num|pluralize }}">>, [{num, 1}],
- <<"">>},
- {"|pluralize",
- <<"{{ num|pluralize }}">>, [{num, 2}],
- <<"s">>},
- {"|pluralize:\"s\"",
- <<"{{ num|pluralize }}">>, [{num, 1}],
- <<"">>},
- {"|pluralize:\"s\"",
- <<"{{ num|pluralize }}">>, [{num, 2}],
- <<"s">>},
- {"|pluralize:\"y,es\" (list)",
- <<"{{ num|pluralize:\"y,es\" }}">>, [{num, 1}],
- <<"y">>},
- {"|pluralize:\"y,es\" (list)",
- <<"{{ num|pluralize:\"y,es\" }}">>, [{num, 2}],
- <<"es">>},
- {"|random",
- <<"{{ var1|random }}">>, [{var1, ["foo", "foo", "foo"]}],
- <<"foo">>},
- {"|removetags:\"b span\"",
- <<"{{ var1|removetags:\"b span\" }}">>, [{var1, "<B>Joel</B> <button>is</button> a <span>slug</span>"}],
- <<"<B>Joel</B> <button>is</button> a slug">>},
- {"|rjust:10",
- <<"{{ var1|rjust:10 }}">>, [{var1, "Bush"}],
- <<" Bush">>},
- {"|safe",
- <<"{% autoescape on %}{{ var1|safe|escape }}{% endautoescape %}">>, [{var1, "&"}],
- <<"&">>},
- %%python/django slice is zero based, erlang lists are 1 based
- %%first number included, second number not
- %%negative numbers are allowed
- %%regex to convert from erlydtl_filters_tests:
- % for slice: \?assert.*\( \[(.*)\], erlydtl_filters:(.*)\((.*),"(.*)"\)\),
- % {"|slice:\"$4\"", <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],<<$1>>},
- % \t\t{"|slice:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>},
- %
- % for stringformat:
- % \?assert.*\( (.*), erlydtl_filters:(.*)\((.*), "(.*)"\) \)
- % \t\t{"|stringformat:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>}
-
- {"|slice:\":\"",
- <<"{{ var|slice:\":\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<1,2,3,4,5,6,7,8,9>>},
- {"|slice:\"1\"",
- <<"{{ var|slice:\"1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<"2">>},
- {"|slice:\"100\"",
- <<"{{ var|slice:\"100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<"indexError">>},
- {"|slice:\"-1\"",
- <<"{{ var|slice:\"-1\" }}">>, [{var, ["a","b","c","d","e","f","g","h","i"]}],
- <<"i">>},
- {"|slice:\"-1\"",
- <<"{{ var|slice:\"-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<"9">>},
- {"|slice:\"-100\"",
- <<"{{ var|slice:\"-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<"indexError">>},
- {"|slice:\"1:\"",
- <<"{{ var|slice:\"1:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<2,3,4,5,6,7,8,9>>},
- {"|slice:\"100:\"",
- <<"{{ var|slice:\"100:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<>>},
- {"|slice:\"-1:\"",
- <<"{{ var|slice:\"-1:\" }}">>, [{var, ["a","b","c","d","e","f","h","i","j"]}],
- <<"j">>},
- {"|slice:\"-1:\"",
- <<"{{ var|slice:\"-1:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<9>>},
- {"|slice:\"-100:\"",
- <<"{{ var|slice:\"-100:\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<1,2,3,4,5,6,7,8,9>>},
-
- {"|slice:\":1\"",
- <<"{{ var|slice:\":1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<1>>},
- {"|slice:\":100\"",
- <<"{{ var|slice:\":100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<1,2,3,4,5,6,7,8,9>>},
- {"|slice:\":-1\"",
- <<"{{ var|slice:\":-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<1,2,3,4,5,6,7,8>>},
- {"|slice:\":-100\"",
- <<"{{ var|slice:\":-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<>>},
-
- {"|slice:\"-1:-1\"",
- <<"{{ var|slice:\"-1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<>>},
- {"|slice:\"1:1\"",
- <<"{{ var|slice:\"1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<>>},
- {"|slice:\"1:-1\"",
- <<"{{ var|slice:\"1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<2,3,4,5,6,7,8>>},
- {"|slice:\"-1:1\"",
- <<"{{ var|slice:\"-1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<>>},
-
- {"|slice:\"-100:-100\"",
- <<"{{ var|slice:\"-100:-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<>>},
- {"|slice:\"100:100\"",
- <<"{{ var|slice:\"100:100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<>>},
- {"|slice:\"100:-100\"",
- <<"{{ var|slice:\"100:-100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<>>},
- {"|slice:\"-100:100\"",
- <<"{{ var|slice:\"-100:100\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<1,2,3,4,5,6,7,8,9>>},
-
-
- {"|slice:\"1:3\"",
- <<"{{ var|slice:\"1:3\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<2,3>>},
-
- {"|slice:\"::\"",
- <<"{{ var|slice:\"::\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<1,2,3,4,5,6,7,8,9>>},
- {"|slice:\"1:9:1\"",
- <<"{{ var|slice:\"1:9:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<2,3,4,5,6,7,8,9>>},
- {"|slice:\"10:1:-1\"",
- <<"{{ var|slice:\"10:1:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<9,8,7,6,5,4,3>>},
- {"|slice:\"-111:-1:1\"",
- <<"{{ var|slice:\"-111:-1:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<1,2,3,4,5,6,7,8>>},
-
- {"|slice:\"-111:-111:1\"",
- <<"{{ var|slice:\"-111:-111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<>>},
- {"|slice:\"111:111:1\"",
- <<"{{ var|slice:\"111:111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<>>},
- {"|slice:\"-111:111:1\"",
- <<"{{ var|slice:\"-111:111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<1,2,3,4,5,6,7,8,9>>},
- {"|slice:\"111:-111:1\"",
- <<"{{ var|slice:\"111:-111:1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<>>},
-
- {"|slice:\"-111:-111:-1\"",
- <<"{{ var|slice:\"-111:-111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<>>},
- {"|slice:\"111:111:-1\"",
- <<"{{ var|slice:\"111:111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<>>},
- {"|slice:\"-111:111:-1\"",
- <<"{{ var|slice:\"-111:111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<>>},
- {"|slice:\"111:-111:-1\"",
- <<"{{ var|slice:\"111:-111:-1\" }}">>, [{var, [1,2,3,4,5,6,7,8,9]}],
- <<9,8,7,6,5,4,3,2,1>>}, {"|phone2numeric",
- <<"{{ var1|phone2numeric }}">>, [{var1, "1-800-COLLECT"}],
- <<"1-800-2655328">>},
- {"|slugify",
- <<"{{ var1|slugify }}">>, [{var1, "What The $#_! Was He Thinking?"}],
- <<"what-the-_-was-he-thinking">>},
- {"|slice:\"s\"",
- <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
- <<"test">>},
- {"|stringformat:\"s\"",
- <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
- <<"test">>},
- {"|stringformat:\"s\"",
- <<"{{ var|stringformat:\"s\" }}">>, [{var, "1"}],
- <<"1">>},
- {"|stringformat:\"s\"",
- <<"{{ var|stringformat:\"s\" }}">>, [{var, "test"}],
- <<"test">>},
- {"|stringformat:\"10s\"",
- <<"{{ var|stringformat:\"10s\" }}">>, [{var, "test"}],
- <<" test">>},
- {"|stringformat:\"-10s\"",
- <<"{{ var|stringformat:\"-10s\" }}">>, [{var, "test"}],
- <<"test ">>},
-
- {"|stringformat:\"d\"",
- <<"{{ var|stringformat:\"d\" }}">>, [{var, "90"}],
- <<"90">>},
- {"|stringformat:\"10d\"",
- <<"{{ var|stringformat:\"10d\" }}">>, [{var, "90"}],
- <<" 90">>},
- {"|stringformat:\"-10d\"",
- <<"{{ var|stringformat:\"-10d\" }}">>, [{var, "90"}],
- <<"90 ">>},
- {"|stringformat:\"i\"",
- <<"{{ var|stringformat:\"i\" }}">>, [{var, "90"}],
- <<"90">>},
- {"|stringformat:\"10i\"",
- <<"{{ var|stringformat:\"10i\" }}">>, [{var, "90"}],
- <<" 90">>},
- {"|stringformat:\"-10i\"",
- <<"{{ var|stringformat:\"-10i\" }}">>, [{var, "90"}],
- <<"90 ">>},
- {"|stringformat:\"0.2d\"",
- <<"{{ var|stringformat:\"0.2d\" }}">>, [{var, "9"}],
- <<"09">>},
- {"|stringformat:\"10.4d\"",
- <<"{{ var|stringformat:\"10.4d\" }}">>, [{var, "9"}],
- <<" 0009">>},
- {"|stringformat:\"-10.4d\"",
- <<"{{ var|stringformat:\"-10.4d\" }}">>, [{var, "9"}],
- <<"0009 ">>},
- {"|stringformat:\"f\"",
- <<"{{ var|stringformat:\"f\" }}">>, [{var, "1"}],
- <<"1.000000">>},
- {"|stringformat:\".2f\"",
- <<"{{ var|stringformat:\".2f\" }}">>, [{var, "1"}],
- <<"1.00">>},
- {"|stringformat:\"0.2f\"",
- <<"{{ var|stringformat:\"0.2f\" }}">>, [{var, "1"}],
- <<"1.00">>},
- {"|stringformat:\"-0.2f\"",
- <<"{{ var|stringformat:\"-0.2f\" }}">>, [{var, "1"}],
- <<"1.00">>},
- {"|stringformat:\"10.2f\"",
- <<"{{ var|stringformat:\"10.2f\" }}">>, [{var, "1"}],
- <<" 1.00">>},
- {"|stringformat:\"-10.2f\"",
- <<"{{ var|stringformat:\"-10.2f\" }}">>, [{var, "1"}],
- <<"1.00 ">>},
- {"|stringformat:\".2f\"",
- <<"{{ var|stringformat:\".2f\" }}">>, [{var, "1"}],
- <<"1.00">>},
- {"|stringformat:\"x\"",
- <<"{{ var|stringformat:\"x\" }}">>, [{var, "90"}],
- <<"5a">>},
- {"|stringformat:\"X\"",
- <<"{{ var|stringformat:\"X\" }}">>, [{var, "90"}],
- <<"5A">>},
-
- {"|stringformat:\"o\"",
- <<"{{ var|stringformat:\"o\" }}">>, [{var, "90"}],
- <<"132">>},
-
- {"|stringformat:\"e\"",
- <<"{{ var|stringformat:\"e\" }}">>, [{var, "90"}],
- <<"9.000000e+01">>},
- {"|stringformat:\"e\"",
- <<"{{ var|stringformat:\"e\" }}">>, [{var, "90000000000"}],
- <<"9.000000e+10">>},
- {"|stringformat:\"E\"",
- <<"{{ var|stringformat:\"E\" }}">>, [{var, "90"}],
- <<"9.000000E+01">>},
- {"|striptags",
- <<"{{ var|striptags }}">>, [{var, "<b>Joel</b> <button>is</button> a <span>slug</span>"}],
- <<"Joel is a slug">>},
- {"|striptags",
- <<"{{ var|striptags }}">>, [{var, "<B>Joel</B> <button>is</button> a <span>slug</Span>"}],
- <<"Joel is a slug">>},
- {"|striptags",
- <<"{{ var|striptags }}">>, [{var, "Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>"}],
- <<"Check out http://www.djangoproject.com">>},
- {"|time:\"H:i\"",
- <<"{{ var|time:\"H:i\" }}">>, [{var, {{2010,12,1}, {10,11,12}} }],
- <<"10:11">>},
- {"|time",
- <<"{{ var|time }}">>, [{var, {{2010,12,1}, {10,11,12}} }],
- <<"10:11 a.m.">>},
- {"|timesince:from_date",
- <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2006,6,1},{8,0,0}} }, {from_date, {{2006,6,1},{0,0,0}} }],
- <<"8 hours">>},
- {"|timesince:from_date",
- <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2010,6,1},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
- <<"4 years, 1 day">>}, % leap year
- {"|timesince:from_date",
- <<"{{ from_date|timesince:conference_date }}">>, [{conference_date, {{2006,7,15},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
- <<"1 month, 2 weeks">>},
- {"|timeuntil:from_date",
- <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2006,6,1},{8,0,0}} }, {from_date, {{2006,6,1},{0,0,0}} }],
- <<"8 hours">>},
- {"|timeuntil:from_date",
- <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2010,6,1},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
- <<"4 years, 1 day">>},
- {"|timeuntil:from_date",
- <<"{{ conference_date|timeuntil:from_date }}">>, [{conference_date, {{2006,7,15},{8,0,0}} },{from_date, {{2006,6,1},{0,0,0}} }],
- <<"1 month, 2 weeks">>},
- {"|title",
- <<"{{ \"my title case\"|title }}">>, [],
- <<"My Title Case">>},
- {"|title (pre-formatted)",
- <<"{{ \"My Title Case\"|title }}">>, [],
- <<"My Title Case">>},
- {"|truncatechars:0",
- <<"{{ var1|truncatechars:0 }}">>, [{var1, "Empty Me"}],
- <<"">>},
- {"|truncatechars:11",
- <<"{{ var1|truncatechars:11 }}">>, [{var1, "Truncate Me Please"}],
- <<"Truncate Me...">>},
- {"|truncatechars:17",
- <<"{{ var1|truncatechars:17 }}">>, [{var1, "Don't Truncate Me"}],
- <<"Don't Truncate Me">>},
- {"|truncatewords:0",
- <<"{{ var1|truncatewords:0 }}">>, [{var1, "Empty Me"}],
- <<"">>},
- {"|truncatewords:2",
- <<"{{ var1|truncatewords:2 }}">>, [{var1, "Truncate Me Please"}],
- <<"Truncate Me...">>},
- {"|truncatewords:3",
- <<"{{ var1|truncatewords:3 }}">>, [{var1, "Don't Truncate Me"}],
- <<"Don't Truncate Me">>},
- {"|truncatewords_html:4",
- <<"{{ var1|truncatewords_html:4 }}">>, [{var1, "<p>The <strong>Long and <em>Winding</em> Road</strong> is too long</p>"}],
- <<"<p>The <strong>Long and <em>Winding</em>...</strong></p>">>},
- {"|unordered_list",
- <<"{{ var1|unordered_list }}">>, [{var1, ["States", ["Kansas", ["Lawrence", "Topeka"], "Illinois"]]}],
- <<"<li>States<ul><li>Kansas<ul><li>Lawrence</li><li>Topeka</li></ul></li><li>Illinois</li></ul></li>">>},
- {"|upper",
- <<"{{ message|upper }}">>, [{message, "That man has a gun."}],
- <<"THAT MAN HAS A GUN.">>},
- {"|urlencode",
- <<"{{ url|urlencode }}">>, [{url, "You #$*@!!"}],
- <<"You+%23%24%2A%40%21%21">>},
- {"|urlize",
- <<"{{ var|urlize }}">>, [{var, "Check out www.djangoproject.com"}],
- <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">www.djangoproject.com</a>">>},
- {"|urlize",
- <<"{{ var|urlize }}">>, [{var, "Check out http://www.djangoproject.com"}],
- <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>">>},
- {"|urlize",
- <<"{{ var|urlize }}">>, [{var, "Check out \"http://www.djangoproject.com\""}],
- <<"Check out \"<a href=\"http://www.djangoproject.com\" rel=\"nofollow\">http://www.djangoproject.com</a>\"">>},
- {"|urlizetrunc:15",
- <<"{{ var|urlizetrunc:15 }}">>, [{var, "Check out www.djangoproject.com"}],
- <<"Check out <a href=\"http://www.djangoproject.com\" rel=\"nofollow\">www.djangopr...</a>">>},
- {"|wordcount",
- <<"{{ words|wordcount }}">>, [{words, "Why Hello There!"}],
- <<"3">>},
- {"|wordwrap:2",
- <<"{{ words|wordwrap:2 }}">>, [{words, "this is"}],
- <<"this \nis">>},
- {"|wordwrap:100",
- <<"{{ words|wordwrap:100 }}">>, [{words, "testing testing"}],
- <<"testing testing">>},
- {"|wordwrap:10",
- <<"{{ words|wordwrap:10 }}">>, [{words, ""}],
- <<"">>},
- {"|wordwrap:1",
- <<"{{ words|wordwrap:1 }}">>, [{words, "two"}],
- <<"two">>},
- % yesno match: \?assert.*\( (.*), erlydtl_filters:(.*)\((.*), "(.*)"\)\)
- % yesno replace: \t\t{"|$2:\"$4\"",\n\t\t\t\t\t <<"{{ var|$2:\"$4\" }}">>, [{var, $3}],\n\t\t\t\t\t<<$1>>}
- {"|yesno:\"yeah,no,maybe\"",
- <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, true}],
- <<"yeah">>},
- {"|yesno:\"yeah,no,maybe\"",
- <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, false}],
- <<"no">>},
- {"|yesno:\"yeah,no\"",
- <<"{{ var|yesno:\"yeah,no\" }}">>, [{var, undefined}],
- <<"no">>},
- {"|yesno:\"yeah,no,maybe\"",
- <<"{{ var|yesno:\"yeah,no,maybe\" }}">>, [{var, undefined}],
- <<"maybe">>}
- ]},
- {"filters_if", [
- {"Filter if 1.1",
- <<"{% if var1|length_is:0 %}Y{% else %}N{% endif %}">>,
- [{var1, []}],
- <<"Y">>},
- {"Filter if 1.2",
- <<"{% if var1|length_is:1 %}Y{% else %}N{% endif %}">>,
- [{var1, []}],
- <<"N">>},
- {"Filter if 1.3",
- <<"{% if var1|length_is:7 %}Y{% else %}N{% endif %}">>,
- [{var1, []}],
- <<"N">>},
- {"Filter if 2.1",
- <<"{% if var1|length_is:0 %}Y{% else %}N{% endif %}">>,
- [{var1, ["foo"]}],
- <<"N">>},
- {"Filter if 2.2",
- <<"{% if var1|length_is:1 %}Y{% else %}N{% endif %}">>,
- [{var1, ["foo"]}],
- <<"Y">>},
- {"Filter if 2.3",
- <<"{% if var1|length_is:7 %}Y{% else %}N{% endif %}">>,
- [{var1, ["foo"]}],
- <<"N">>},
- {"Filter if 3.1",
- <<"{% ifequal var1|length 0 %}Y{% else %}N{% endifequal %}">>,
- [{var1, []}],
- <<"Y">>},
- {"Filter if 3.2",
- <<"{% ifequal var1|length 1 %}Y{% else %}N{% endifequal %}">>,
- [{var1, []}],
- <<"N">>},
- {"Filter if 4.1",
- <<"{% ifequal var1|length 3 %}Y{% else %}N{% endifequal %}">>,
- [{var1, ["foo", "bar", "baz"]}],
- <<"Y">>},
- {"Filter if 4.2",
- <<"{% ifequal var1|length 0 %}Y{% else %}N{% endifequal %}">>,
- [{var1, ["foo", "bar", "baz"]}],
- <<"N">>},
- {"Filter if 4.3",
- <<"{% ifequal var1|length 1 %}Y{% else %}N{% endifequal %}">>,
- [{var1, ["foo", "bar", "baz"]}],
- <<"N">>}
- ]},
- {"firstof", [
- {"Firstof first",
- <<"{% firstof foo bar baz %}">>,
- [{foo, "1"},{bar, "2"}],
- <<"1">>},
- {"Firstof second",
- <<"{% firstof foo bar baz %}">>,
- [{bar, "2"}],
- <<"2">>},
- {"Firstof none",
- <<"{% firstof foo bar baz %}">>,
- [],
- <<"">>},
- {"Firstof complex",
- <<"{% firstof foo.bar.baz bar %}">>,
- [{foo, [{bar, [{baz, "quux"}]}]}],
- <<"quux">>},
- {"Firstof undefined complex",
- <<"{% firstof foo.bar.baz bar %}">>,
- [{bar, "bar"}],
- <<"bar">>},
- {"Firstof literal",
- <<"{% firstof foo bar \"baz\" %}">>,
- [],
- <<"baz">>}
- ]},
- {"regroup", [
- {"Ordered", <<"{% 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 %}">>,
- [{people, [[{first_name, "George"}, {gender, "Male"}], [{first_name, "Bill"}, {gender, "Male"}],
- [{first_name, "Margaret"}, {gender, "Female"}], [{first_name, "Condi"}, {gender, "Female"}]]}],
- <<"Male\nGeorge\nBill\nFemale\nMargaret\nCondi\n">>},
- {"Unordered", <<"{% 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 %}">>,
- [{people, [[{first_name, "George"}, {gender, "Male"}],
- [{first_name, "Margaret"}, {gender, "Female"}],
- [{first_name, "Condi"}, {gender, "Female"}],
- [{first_name, "Bill"}, {gender, "Male"}]
- ]}],
- <<"Male\nGeorge\nFemale\nMargaret\nCondi\nMale\nBill\n">>}
- ]},
- {"spaceless", [
- {"Beginning", <<"{% spaceless %} <b>foo</b>{% endspaceless %}">>, [], <<"<b>foo</b>">>},
- {"Middle", <<"{% spaceless %}<b>foo</b> <b>bar</b>{% endspaceless %}">>, [], <<"<b>foo</b><b>bar</b>">>},
- {"End", <<"{% spaceless %}<b>foo</b> {% endspaceless %}">>, [], <<"<b>foo</b>">>}
- ]},
- {"templatetag", [
- {"openblock", <<"{% templatetag openblock %}">>, [], <<"{%">>},
- {"closeblock", <<"{% templatetag closeblock %}">>, [], <<"%}">>},
- {"openvariable", <<"{% templatetag openvariable %}">>, [], <<"{{">>},
- {"closevariable", <<"{% templatetag closevariable %}">>, [], <<"}}">>},
- {"openbrace", <<"{% templatetag openbrace %}">>, [], <<"{">>},
- {"closebrace", <<"{% templatetag closebrace %}">>, [], <<"}">>},
- {"opencomment", <<"{% templatetag opencomment %}">>, [], <<"{#">>},
- {"closecomment", <<"{% templatetag closecomment %}">>, [], <<"#}">>}
- ]},
- {"trans",
- [
- {"trans functional default locale",
- <<"Hello {% trans \"Hi\" %}">>, [], <<"Hello Hi">>
- },
- {"trans functional reverse locale",
- <<"Hello {% trans \"Hi\" %}">>, [], [], [{locale, "reverse"}], <<"Hello iH">>
- },
- {"trans literal at run-time",
- <<"Hello {% trans \"Hi\" %}">>, [], [{translation_fun, fun("Hi") -> "Konichiwa" end}], [],
- <<"Hello Konichiwa">>},
- {"trans variable at run-time",
- <<"Hello {% trans var1 %}">>, [{var1, <<"Hi">>}], [{translation_fun, fun(<<"Hi">>) -> <<"Konichiwa">> end}], [],
- <<"Hello Konichiwa">>},
- {"trans literal at run-time: No-op",
- <<"Hello {% trans \"Hi\" noop %}">>, [], [{translation_fun, fun("Hi") -> <<"Konichiwa">> end}], [],
- <<"Hello Hi">>},
- {"trans variable at run-time: No-op",
- <<"Hello {% trans var1 noop %}">>, [{var1, <<"Hi">>}], [{translation_fun, fun(<<"Hi">>) -> <<"Konichiwa">> end}], [],
- <<"Hello Hi">>}
- ]},
- {"blocktrans",
- [
- {"blocktrans default locale",
- <<"{% blocktrans %}Hello{% endblocktrans %}">>, [], <<"Hello">>},
- {"blocktrans choose locale",
- <<"{% blocktrans %}Hello, {{ name }}{% endblocktrans %}">>, [{name, "Mr. President"}], [{locale, "de"}],
- [{blocktrans_locales, ["de"]}, {blocktrans_fun, fun("Hello, {{ name }}", "de") -> <<"Guten tag, {{ name }}">> end}], <<"Guten tag, Mr. President">>},
- {"blocktrans with args",
- <<"{% blocktrans with var1=foo %}{{ var1 }}{% endblocktrans %}">>, [{foo, "Hello"}], <<"Hello">>}
- ]},
- {"widthratio", [
- {"Literals", <<"{% widthratio 5 10 100 %}">>, [], <<"50">>},
- {"Rounds up", <<"{% widthratio a b 100 %}">>, [{a, 175}, {b, 200}], <<"88">>}
- ]},
- {"with", [
- {"Cache literal",
- <<"{% with a=1 %}{{ a }}{% endwith %}">>, [], <<"1">>},
- {"Cache variable",
- <<"{% with a=b %}{{ a }}{% endwith %}">>, [{b, "foo"}], <<"foo">>},
- {"Cache multiple",
- <<"{% with alpha=1 beta=b %}{{ alpha }}/{{ beta }}{% endwith %}">>, [{b, 2}], <<"1/2">>}
- ]},
- {"unicode", [
- {"(tm) somewhere",
- <<"™">>, [], <<"™">>}
- ]}
- ].
-
- run_tests() ->
- io:format("Running unit tests...~n"),
- DefaultOptions = [],
- Failures = lists:foldl(
- fun({Group, Assertions}, GroupAcc) ->
- io:format(" Test group ~p...~n", [Group]),
- lists:foldl(fun
- ({Name, DTL, Vars, Output}, Acc) ->
- process_unit_test(erlydtl:compile(DTL, erlydtl_running_test, DefaultOptions),
- Vars, [], Output, Acc, Group, Name);
- ({Name, DTL, Vars, RenderOpts, Output}, Acc) ->
- process_unit_test(erlydtl:compile(DTL, erlydtl_running_test, DefaultOptions),
- Vars, RenderOpts, Output, Acc, Group, Name);
- ({Name, DTL, Vars, RenderOpts, CompilerOpts, Output}, Acc) ->
- process_unit_test(erlydtl:compile(DTL, erlydtl_running_test, CompilerOpts ++ DefaultOptions),
- Vars, RenderOpts, Output, Acc, Group, Name)
- end, GroupAcc, Assertions)
- end, [], tests()),
-
- io:format("Unit test failures: ~p~n", [lists:reverse(Failures)]).
-
- process_unit_test(CompiledTemplate, Vars, RenderOpts, Output,Acc, Group, Name) ->
- case CompiledTemplate of
- {ok, _} ->
- {ok, IOList} = erlydtl_running_test:render(Vars, RenderOpts),
- {ok, IOListBin} = erlydtl_running_test:render(vars_to_binary(Vars), RenderOpts),
- case {iolist_to_binary(IOList), iolist_to_binary(IOListBin)} of
- {Output, Output} ->
- Acc;
- {Output, Unexpected} ->
- [{Group, Name, 'binary', Unexpected, Output} | Acc];
- {Unexpected, Output} ->
- [{Group, Name, 'list', Unexpected, Output} | Acc];
- {Unexpected1, Unexpected2} ->
- [{Group, Name, 'list', Unexpected1, Output},
- {Group, Name, 'binary', Unexpected2, Output} | Acc]
- end;
- Err ->
- [{Group, Name, Err} | Acc]
- end.
-
-
- vars_to_binary(Vars) when is_list(Vars) ->
- lists:map(fun
- ({Key, [H|_] = Value}) when is_tuple(H) ->
- {Key, vars_to_binary(Value)};
- ({Key, [H|_] = Value}) when is_integer(H) ->
- {Key, list_to_binary(Value)};
- ({Key, Value}) ->
- {Key, Value}
- end, Vars);
- vars_to_binary(Vars) ->
- Vars.
-
- generate_test_date() ->
- {{Y,M,D}, _} = erlang:localtime(),
- MonthName = [
- "January", "February", "March", "April",
- "May", "June", "July", "August", "September",
- "October", "November", "December"
- ],
- OrdinalSuffix = [
- "st","nd","rd","th","th","th","th","th","th","th", % 1-10
- "th","th","th","th","th","th","th","th","th","th", % 10-20
- "st","nd","rd","th","th","th","th","th","th","th", % 20-30
- "st"
- ],
- list_to_binary([
- "It is the ",
- integer_to_list(D),
- lists:nth(D, OrdinalSuffix),
- " of ", lists:nth(M, MonthName),
- " ", integer_to_list(Y), "."
- ]).
|