sources_parser_unittests.erl 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. -module(sources_parser_unittests).
  2. -export([run_tests/0]).
  3. tests() ->
  4. [{"trans",
  5. [{"block with no trans",
  6. <<"<html>{% block main %} {% endblock %}</html>">>,
  7. []},
  8. {"block with trans",
  9. <<"<html>{% block main %} {% trans \"Hello\" %} {% endblock %}</html>">>,
  10. [{"Hello",{"dummy_path",1,33}}]},
  11. {"for with trans",
  12. <<"<html>{% block main %} {%for thing in things %}{% trans \"Hello inside a for\" %} {% endfor %} {% endblock %}</html>">>,
  13. [{"Hello inside a for",{"dummy_path",1,57}}]},
  14. {"if with trans",
  15. <<"<html>{% block content %}{% if thing %} {% trans \"Hello inside an if\" %} {% endif %} {% endblock %}</html>">>,
  16. [{"Hello inside an if",{"dummy_path",1,50}}]},
  17. {"if with trans inside a for",
  18. <<"<html>{% block content %}{%for thin in things %}{% if thing %} {% trans \"Hello inside an if inside a for\" %} {% endif %} {% endfor %}{% endblock %}</html>">>,
  19. [{"Hello inside an if inside a for",{"dummy_path",1,73}}]},
  20. {"if and else both with trans",
  21. <<"<html>{% block content %}{% if thing %} {% trans \"Hello inside an if\" %} {% else %} {% trans \"Hello inside an else\" %} {% endif %} {% endblock %}</html>">>,
  22. [ {"Hello inside an else",{"dummy_path",1,94}}, {"Hello inside an if",{"dummy_path",1,50}}]}
  23. ]}
  24. ].
  25. run_tests() ->
  26. io:format("Running source parser unit tests...~n"),
  27. Failures = lists:foldl(
  28. fun({Group, Assertions}, GroupAcc) ->
  29. io:format(" Test group ~p...~n", [Group]),
  30. lists:foldl(fun({Name, Content, Output}, Acc) ->
  31. process_unit_test(Content, Output, Acc, Group, Name)
  32. end, GroupAcc, Assertions)
  33. end, [], tests()),
  34. case Failures of
  35. [] ->
  36. io:format("All source parser tests PASS~n~n");
  37. _ ->
  38. io:format("Source parser unit test failures~n"),
  39. [io:format(" Test: ~s.~s~n Expected: ~p~n Actual: ~s~n",
  40. [Group, Name, Expected, Actual])
  41. || {Group, Name, _, {expected, Expected}, {found, Actual}} <- Failures],
  42. throw(failed)
  43. end.
  44. process_unit_test(Content, Output, Acc, Group, Name) ->
  45. Tokens = (catch sources_parser:process_content("dummy_path", Content)),
  46. %%io:format("Tokens are: ~p~n", [Tokens]),
  47. case Tokens of
  48. Output -> Acc;
  49. _ -> [{Group, Name, 'binary', {expected, Output}, {found, Tokens} } | Acc]
  50. end.