Permalink
Browse files

Recursively search for "trans" tags inside blocks.

Includes new tests for the i18n sources parser. Thanks to Dave García.
  • Loading branch information...
1 parent 25480ec commit 6865a605ac360d64979c7e0ce2c662b2a55a352a @evanmiller evanmiller committed Jun 22, 2010
Showing with 58 additions and 8 deletions.
  1. +1 −0 Emakefile
  2. +1 −0 Makefile
  3. +16 −8 src/erlydtl/i18n/sources_parser.erl
  4. +40 −0 src/tests/i18n/sources_parser_unittests.erl
View
@@ -1,4 +1,5 @@
{"src/erlydtl/*", [debug_info, {outdir, "ebin"}]}.
{"src/erlydtl/i18n/*", [debug_info, {outdir, "ebin"}]}.
{"src/tests/*", [debug_info, {outdir, "ebintest"}]}.
+{"src/tests/i18n/*", [debug_info, {outdir, "ebintest"}]}.
{"src/demo/*", [debug_info, {outdir, "ebintest"}]}.
View
@@ -19,6 +19,7 @@ test:
-s erlydtl_functional_tests run_tests \
-s erlydtl_dateformat_tests run_tests \
-s erlydtl_unittests run_tests \
+ -s sources_parser_unittests run_tests \
-s init stop
clean:
@@ -10,7 +10,7 @@
%%
%% Exported Functions
%%
--export([parse/0,parse/1]).
+-export([parse/0,parse/1, process_content/2]).
%%
%% API Functions
@@ -32,17 +32,21 @@ parse(Pattern) ->
parse_file(Path) ->
case file:read_file((Path)) of
{ok,Content} ->
- case erlydtl_compiler:parse(Content) of
- {ok,Data} ->
- {ok,Result} = process_ast(Path, Data),
- Result;
- _Error ->
- throw(io_lib:format("Template parsing failed for template ~s, cause ~p~n",[Path,_Error]))
- end;
+ process_content(Path,Content);
Error ->
throw(io_lib:format("Cannot read file ~s problem ~p~n", [Path,Error]))
end.
+process_content(Path,Content)->
+ case erlydtl_compiler:parse(Content) of
+ {ok,Data} ->
+ {ok,Result} = process_ast(Path, Data),
+ Result;
+ _Error ->
+ throw(io_lib:format("Template parsing failed for template ~s, cause ~p~n",[Path,_Error]))
+ end.
+
+
process_ast(Fname, Tokens) -> {ok, process_ast(Fname, Tokens ,[]) }.
process_ast(_Fname, [],Acc) -> Acc;
process_ast(Fname,[Head|Tail], Acc) ->
@@ -52,6 +56,10 @@ process_ast(Fname,[Head|Tail], Acc) ->
%%Block are recursivelly processed, trans are accumulated and other tags are ignored
process_token(Fname, {block,{identifier,{_Line,_Col},_Identifier},Children}, Acc ) -> process_ast(Fname, Children, Acc);
process_token(Fname, {trans,{string_literal,{Line,Col},String}}, Acc ) -> [{unescape(String), {Fname, Line, Col}} | Acc];
+process_token(Fname, {_Instr, _Cond, Children}, Acc) -> process_ast(Fname, Children, Acc);
+process_token(Fname, {_Instr, _Cond, Children, Children2}, Acc) ->
+ AccModified = process_ast(Fname, Children, Acc),
+ process_ast(Fname, Children2, AccModified);
process_token(_,_AST,Acc) -> Acc.
unescape(String) ->string:sub_string(String, 2, string:len(String) -1).
@@ -0,0 +1,40 @@
+-module(sources_parser_unittests).
+
+-export([run_tests/0]).
+
+tests() ->
+ [
+ {"trans", [
+ {"block with no trans", <<"<html>{% block main %} {% endblock %}</html>">>, []},
+
+ {"block with trans", <<"<html>{% block main %} {% trans \"Hello\" %} {% endblock %}</html>">>, [{"Hello",{"dummy_path",1,33}}]},
+
+ {"for with trans", <<"<html>{% block main %} {%for thing in things %}{% trans \"Hello inside a for\" %} {% endfor %} {% endblock %}</html>">>, [{"Hello inside a for",{"dummy_path",1,57}}]},
+
+ {"if with trans", <<"<html>{% block content %}{% if thing %} {% trans \"Hello inside an if\" %} {% endif %} {% endblock %}</html>">>, [{"Hello inside an if",{"dummy_path",1,50}}]},
+
+ {"if with trans inside a for", <<"<html>{% block content %}{%for thin in things %}{% if thing %} {% trans \"Hello inside an if inside a for\" %} {% endif %} {% endfor %}{% endblock %}</html>">>, [{"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}}]}
+ ]}
+ ].
+
+run_tests() ->
+ io:format("Running unit tests...~n"),
+ Failures = lists:foldl(
+ fun({Group, Assertions}, GroupAcc) ->
+ io:format(" Test group ~p...~n", [Group]),
+ lists:foldl(fun({Name, Content, Output}, Acc) ->
+ process_unit_test(Content, Output, Acc, Group, Name)
+ end, GroupAcc, Assertions)
+ end, [], tests()),
+
+ io:format("Unit test failures: ~p~n", [Failures]).
+
+process_unit_test(Content, Output,Acc, Group, Name) ->
+ Tokens = sources_parser:process_content("dummy_path", Content),
+ %%io:format("Tokens are: ~p~n", [Tokens]),
+ case Tokens of
+ Output -> Acc;
+ _ -> [{Group, Name, 'binary', {expected, Output}, {found, Tokens} } | Acc]
+ end.

0 comments on commit 6865a60

Please sign in to comment.