Skip to content

Commit

Permalink
Add tests for blocktrans trimmed (#212).
Browse files Browse the repository at this point in the history
  • Loading branch information
kaos committed Oct 18, 2015
1 parent 27cdc1c commit 8123a11
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/i18n/sources_parser.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@

%% New API
-export([parse_pattern/1, parse_file/1, parse_content/2, phrase_info/2]).

%% Deprecated API
-export([parse/0, parse/1, process_content/2]).
-deprecated([{parse, '_'}, {process_content, 2}]).

%% Type exports
-export_type([phrase/0, compat_phrase/0, field/0]).

%%
Expand Down Expand Up @@ -202,13 +205,18 @@ unescape(String) -> string:sub_string(String, 2, string:len(String) -1).
unparse(undefined) -> undefined;
unparse(Contents) -> erlydtl_unparser:unparse(Contents).

maybe_trim(undefined, _) -> undefined;
maybe_trim(Text, undefined) -> Text;
maybe_trim(Text, true) ->
tl(
lists:foldr(
fun (L, Ls) -> [" ", L|Ls] end, [],
lists:flatten(
re:replace(Text, <<"(^\s+)|(\s+$)|\n">>, <<"">>, [global, multiline])
binary_to_list(
iolist_to_binary(
tl(
lists:foldr(
fun (L, Ls) -> [" ", L|Ls] end, [],
lists:flatten(
re:replace(Text, <<"(^\\s+)|(\\s+$)|\n">>, <<"">>, [global, multiline])
)
)
)
)
).
Expand Down
10 changes: 8 additions & 2 deletions test/sources_parser_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ test_defs() ->
[{"Hello inside an if inside a for",{"dummy_path",1,73}}]},
{"if and else both with trans",
<<"<html>{% block content %}{% if thing %} {% trans \"Hello inside an if\" %} {% else %} {% trans \"Hello inside an else\" %} {% endif %} {% endblock %}</html>">>,
[{"Hello inside an else",{"dummy_path",1,94}}, {"Hello inside an if",{"dummy_path",1,50}}]}
[{"Hello inside an else",{"dummy_path",1,94}}, {"Hello inside an if",{"dummy_path",1,50}}]},
{"blocktrans with pretty format",
<<"<html>{% blocktrans %}\n This is a multiline\n message... \n{% endblocktrans %}">>,
[{"\n This is a multiline\n message... \n", {"dummy_path",1,10}}]},
{"blocktrans with pretty format, trimmed",
<<"<html>{% blocktrans trimmed %}\n This is a multiline\n message... \n{% endblocktrans %}">>,
[{"This is a multiline message...", {"dummy_path",1,18}}]}
]}
].

Expand Down Expand Up @@ -80,7 +86,7 @@ unparser_test_() ->
test_unparser_fun({Name, Tpl}) ->
{Name, fun() ->
%% take input Tpl value, parse it, "unparse" it, then parse it again.
%% the both parsed values should be equvialent, even if the source versions
%% both parsed values should be equvialent, even if the source versions
%% are not an exact match (there can be whitespace differences)
{ok, Dpt} = erlydtl_compiler:do_parse_template(
Tpl, #dtl_context{}),
Expand Down

0 comments on commit 8123a11

Please sign in to comment.