erlydtl_translation_tests.erl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. -module(erlydtl_translation_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. test_fun({Name, Template, Variables, Options, Output}) ->
  7. {Name, fun () ->
  8. Tokens = (catch compile_and_render(Template, Variables, Options)),
  9. ?assertMatch(Output, Tokens)
  10. end}.
  11. compile_and_render(Template, Variables, Options) ->
  12. {ok, test} = erlydtl:compile_template(Template, test),
  13. {ok, R} = test:render(Variables, Options),
  14. iolist_to_binary(R).
  15. test_defs() ->
  16. [
  17. {"trans", [
  18. {"simple", "{% trans \"hello\" %}", [], [], <<"hello">>},
  19. {"with_fun", "{% trans \"text\" %}", [], [{translation_fun, fun(_ID, _L) -> "hola" end}], <<"hola">>},
  20. {"with_fun_utf8", "{% trans \"text\" %}", [],
  21. [{translation_fun, fun(_ID, _L) -> <<"привет"/utf8>> end}], <<"привет"/utf8>>}
  22. ]},
  23. {"blocktrans", [
  24. {"simple", "{% blocktrans %} hello {% endblocktrans %}", [], [], <<" hello ">>},
  25. {"with_fun", "{% blocktrans %} hello {% endblocktrans %}", [],
  26. [{translation_fun, fun(_ID, _L) -> "hola" end}], <<"hola">>},
  27. {"s_param_no_fun", "{% blocktrans %} hello {{ p }} {% endblocktrans %}", [{p, "mundo"}],
  28. [], <<" hello mundo ">>},
  29. {"s_param", "{% blocktrans %} hello {{ p }} {% endblocktrans %}", [{p, "mundo"}],
  30. [{translation_fun, fun(_ID, _L) -> "hola {{ p }}" end}], <<"hola mundo">>},
  31. {"b_param", "{% blocktrans %} hello {{ p }} {% endblocktrans %}", [{p, <<"mundo">>}],
  32. [{translation_fun, fun(_ID, _L) -> "hola {{ p }}" end}], <<"hola mundo">>},
  33. {"i_param", "{% blocktrans %} hello {{ p }} {% endblocktrans %}", [{p, 1}],
  34. [{translation_fun, fun(_ID, _L) -> "hola {{ p }}" end}], <<"hola 1">>},
  35. {"f_param", "{% blocktrans %} hello {{ p }} {% endblocktrans %}", [{p, 3.1415}],
  36. [{translation_fun, fun(_ID, _L) -> "hola {{ p }}" end}], <<"hola 3.1415">>},
  37. {"b_xss", "{% blocktrans %} hello {{ p }} {% endblocktrans %}",
  38. [{p, <<"<script>alert('pwnd');</script>">>}],
  39. [{translation_fun, fun(_ID, _L) -> "hola {{ p }}" end}],
  40. <<"hola &lt;script&gt;alert(&#039;pwnd&#039;);&lt;/script&gt;">>},
  41. {"s_xss", "{% blocktrans %} hello {{ p }} {% endblocktrans %}",
  42. [{p, "<script>alert('pwnd');</script>"}],
  43. [{translation_fun, fun(_ID, _L) -> "hola {{ p }}" end}],
  44. <<"hola &lt;script&gt;alert(&#039;pwnd&#039;);&lt;/script&gt;">>},
  45. {"b_autoecape_off",
  46. "{% autoescape off %}{% blocktrans %} hello {{ p }} {% endblocktrans %}{% endautoescape %}",
  47. [{p, <<"<script>alert('pwnd');</script>">>}],
  48. [{translation_fun, fun(_ID, _L) -> "hola {{ p }}" end}],
  49. <<"hola <script>alert('pwnd');</script>">>},
  50. {"b_autoecape_nested",
  51. "{% autoescape off %}{% autoescape on %}{% blocktrans %} hello {{ p }} {% endblocktrans %}{% endautoescape %}{% endautoescape %}",
  52. [{p, <<"<script>alert('pwnd');</script>">>}],
  53. [{translation_fun, fun(_ID, _L) -> "hola {{ p }}" end}],
  54. <<"hola &lt;script&gt;alert(&#039;pwnd&#039;);&lt;/script&gt;">>},
  55. {"term_hack_", "{% blocktrans %} hello {{ p }} {% endblocktrans %}",
  56. [{p, {"<script>alert('pwnd');</script>"}}],
  57. [{translation_fun, fun(_ID, _L) -> "hola {{ p }}" end}],
  58. <<"hola {&quot;&lt;script&gt;alert(&#039;pwnd&#039;);&lt;/script&gt;&quot;}">>},
  59. {"plural_2",
  60. "{% blocktrans count counter=p %} hello world {% plural %} hello {{ p }} worlds {% endblocktrans %}",
  61. [{p, 2}],
  62. [{translation_fun, fun({" hello world ", {" hello {{ p }} worlds ", 2}}, _L) ->
  63. "hola {{ p }} mundos"
  64. end}],
  65. <<"hola 2 mundos">>}
  66. ]}
  67. ].