sources_parser_tests.erl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. -module(sources_parser_tests).
  2. -include_lib("eunit/include/eunit.hrl").
  3. all_sources_parser_test_() ->
  4. [{Title, [test_fun(Test) || Test <- Tests]}
  5. || {Title, Tests} <- test_defs()].
  6. all_sources_parser_ext_test_() ->
  7. [test_ext_fun(Test) || Test <- ext_test_defs()].
  8. test_fun({Name, Content, Output}) ->
  9. {Name, fun () ->
  10. Tokens = (catch sources_parser:process_content("dummy_path", Content)),
  11. ?assertMatch(Output, Tokens)
  12. end}.
  13. test_defs() ->
  14. [{"trans",
  15. [{"block with no trans",
  16. <<"<html>{% block main %} {% endblock %}</html>">>,
  17. []},
  18. {"block with trans",
  19. <<"<html>{% block main %} {% trans \"Hello\" %} {% endblock %}</html>">>,
  20. [{"Hello",{"dummy_path",1,33}}]},
  21. {"for with trans",
  22. <<"<html>{% block main %} {%for thing in things %}{% trans \"Hello inside a for\" %} {% endfor %} {% endblock %}</html>">>,
  23. [{"Hello inside a for",{"dummy_path",1,57}}]},
  24. {"if with trans",
  25. <<"<html>{% block content %}{% if thing %} {% trans \"Hello inside an if\" %} {% endif %} {% endblock %}</html>">>,
  26. [{"Hello inside an if",{"dummy_path",1,50}}]},
  27. {"if with trans inside a for",
  28. <<"<html>{% block content %}{%for thin in things %}{% if thing %} {% trans \"Hello inside an if inside a for\" %} {% endif %} {% endfor %}{% endblock %}</html>">>,
  29. [{"Hello inside an if inside a for",{"dummy_path",1,73}}]},
  30. {"if and else both with trans",
  31. <<"<html>{% block content %}{% if thing %} {% trans \"Hello inside an if\" %} {% else %} {% trans \"Hello inside an else\" %} {% endif %} {% endblock %}</html>">>,
  32. [ {"Hello inside an else",{"dummy_path",1,94}}, {"Hello inside an if",{"dummy_path",1,50}}]}
  33. ]}
  34. ].
  35. test_ext_fun({Name, Tpl, {Fields, Output}}) ->
  36. {Name, fun() ->
  37. Tokens = [sources_parser:phrase_info(Fields, P)
  38. || P <- sources_parser:parse_content("dummy_path", Tpl)],
  39. ?assertEqual(Output, Tokens)
  40. end}.
  41. ext_test_defs() ->
  42. [{"trans with inline comments",
  43. <<"{#TrAnSlATORs: hi!#}{%trans 'phrase'%}">>,
  44. {[msgid, comment], [["phrase", "TrAnSlATORs: hi!"]]}},
  45. {"trans with comments",
  46. <<"{%comment%}translators: com{{me}}nt{%endcomment%}{%trans 'phrase'%}">>,
  47. {[msgid, comment], [["phrase", "translators: com{{ me }}nt"]]}},
  48. {"blocktrans with comments",
  49. <<"{%comment%}translators: comment{%endcomment%}{%blocktrans with a=b%}B={{b}}{%endblocktrans%}">>,
  50. {[msgid, comment], [["B={{ b }}", "translators: comment"]]}}
  51. ].