sources_parser_unittests.erl 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. -module(sources_parser_unittests).
  2. -export([run_tests/0]).
  3. tests() ->
  4. [
  5. {"trans", [
  6. {"block with no trans", <<"<html>{% block main %} {% endblock %}</html>">>, []},
  7. {"block with trans", <<"<html>{% block main %} {% trans \"Hello\" %} {% endblock %}</html>">>, [{"Hello",{"dummy_path",1,33}}]},
  8. {"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}}]},
  9. {"if with trans", <<"<html>{% block content %}{% if thing %} {% trans \"Hello inside an if\" %} {% endif %} {% endblock %}</html>">>, [{"Hello inside an if",{"dummy_path",1,50}}]},
  10. {"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}}]},
  11. {"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}}]}
  12. ]}
  13. ].
  14. run_tests() ->
  15. io:format("Running unit tests...~n"),
  16. Failures = lists:foldl(
  17. fun({Group, Assertions}, GroupAcc) ->
  18. io:format(" Test group ~p...~n", [Group]),
  19. lists:foldl(fun({Name, Content, Output}, Acc) ->
  20. process_unit_test(Content, Output, Acc, Group, Name)
  21. end, GroupAcc, Assertions)
  22. end, [], tests()),
  23. io:format("Unit test failures: ~p~n", [Failures]).
  24. process_unit_test(Content, Output,Acc, Group, Name) ->
  25. Tokens = sources_parser:process_content("dummy_path", Content),
  26. %%io:format("Tokens are: ~p~n", [Tokens]),
  27. case Tokens of
  28. Output -> Acc;
  29. _ -> [{Group, Name, 'binary', {expected, Output}, {found, Tokens} } | Acc]
  30. end.