123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- -module(erlydtl_unittests).
- -export([run_tests/0]).
- tests() ->
- [
- {"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 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 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 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 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">>}
- ]},
- {"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">>}
- ]},
- {"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"}], <<>>}
- ]},
- {"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">>}
- ]},
- {"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">>},
- {"|capfirst",
- <<"{{ var1|capfirst }}">>, [{var1, "dana boyd"}],
- <<"Dana boyd">>},
- {"|center:10",
- <<"{{ var1|center:10 }}">>, [{var1, "MB"}],
- <<" MB ">>},
- {"|center:1",
- <<"{{ var1|center:1 }}">>, [{var1, "KBR"}],
- <<"B">>},
- {"|date 1",
- <<"{{ var1|date:\"r\" }}">>,
- [{var1, {1975,7,24}}],
- <<"Thu, 24 Jul 1975 00:00:00 +0000">>},
- {"|date 2",
- <<"{{ var1|date:\"r\" }}">>,
- [{var1, {{1975,7,24}, {7,13,1}}}],
- <<"Thu, 24 Jul 1975 07:13:01 +0000">>},
- {"|escapejs",
- <<"{{ var1|escapejs }}">>, [{var1, "Skip's \"Old-Timey\" Diner"}],
- <<"Skip\\'s \\\"Old-Timey\\\" Diner">>},
- {"|first",
- <<"{{ var1|first }}">>, [{var1, "James"}],
- <<"J">>},
- {"|fix_ampersands",
- <<"{{ var1|fix_ampersands }}">>, [{var1, "Ben & Jerry's"}],
- <<"Ben & Jerry's">>},
- {"|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">>},
- {"|join:\", \"",
- <<"{{ var1|join:\", \" }}">>, [{var1, ["Liberte", "Egalite", "Fraternite"]}],
- <<"Liberte, Egalite, Fraternite">>},
- {"|last",
- <<"{{ var1|last }}">>, [{var1, "XYZ"}],
- <<"Z">>},
- {"|length",
- <<"{{ var1|length }}">>, [{var1, "antidisestablishmentarianism"}],
- <<"28">>},
- {"|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 />">>},
- {"|ljust:10",
- <<"{{ var1|ljust:10 }}">>, [{var1, "Gore"}],
- <<"Gore ">>},
- {"|lower",
- <<"{{ var1|lower }}">>, [{var1, "E. E. Cummings"}],
- <<"e. e. cummings">>},
- {"|rjust:10",
- <<"{{ var1|rjust:10 }}">>, [{var1, "Bush"}],
- <<" Bush">>},
- {"|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">>}
- ]}
- ].
- run_tests() ->
- Failures = lists:foldl(
- fun({Group, Assertions}, GroupAcc) ->
- io:format("Running test group ~p...~n", [Group]),
- lists:foldl(fun({Name, DTL, Vars, Output}, Acc) ->
- case erlydtl_compiler:compile(DTL, erlydtl_running_test, []) of
- {ok, _} ->
- {ok, IOList} = erlydtl_running_test:render(Vars),
- {ok, IOListBin} = erlydtl_running_test:render(vars_to_binary(Vars)),
- 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
- end, GroupAcc, Assertions)
- end, [], tests()),
-
- io:format("Failures: ~p~n", [Failures]),
- erlang:halt().
- 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), "."
- ]).
|