sources_parser_tests.erl 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. -module(sources_parser_tests).
  2. -include_lib("eunit/include/eunit.hrl").
  3. -include("include/erlydtl_ext.hrl").
  4. all_sources_parser_test_() ->
  5. [{Title, [test_fun(Test) || Test <- Tests]}
  6. || {Title, Tests} <- test_defs()].
  7. test_fun({Name, Content, Output}) ->
  8. {Name, fun () ->
  9. Tokens = (catch sources_parser:process_content("dummy_path", Content)),
  10. ?assertMatch(Output, Tokens)
  11. end}.
  12. test_defs() ->
  13. [{"trans",
  14. [{"block with no trans",
  15. <<"<html>{% block main %} {% endblock %}</html>">>,
  16. []},
  17. {"block with trans",
  18. <<"<html>{% block main %} {% trans \"Hello\" %} {% endblock %}</html>">>,
  19. [{"Hello",{"dummy_path",1,33}}]},
  20. {"for with trans",
  21. <<"<html>{% block main %} {%for thing in things %}{% trans \"Hello inside a for\" %} {% endfor %} {% endblock %}</html>">>,
  22. [{"Hello inside a for",{"dummy_path",1,57}}]},
  23. {"if with trans",
  24. <<"<html>{% block content %}{% if thing %} {% trans \"Hello inside an if\" %} {% endif %} {% endblock %}</html>">>,
  25. [{"Hello inside an if",{"dummy_path",1,50}}]},
  26. {"if with trans inside a for",
  27. <<"<html>{% block content %}{%for thin in things %}{% if thing %} {% trans \"Hello inside an if inside a for\" %} {% endif %} {% endfor %}{% endblock %}</html>">>,
  28. [{"Hello inside an if inside a for",{"dummy_path",1,73}}]},
  29. {"if and else both with trans",
  30. <<"<html>{% block content %}{% if thing %} {% trans \"Hello inside an if\" %} {% else %} {% trans \"Hello inside an else\" %} {% endif %} {% endblock %}</html>">>,
  31. [{"Hello inside an else",{"dummy_path",1,94}}, {"Hello inside an if",{"dummy_path",1,50}}]}
  32. ]}
  33. ].
  34. all_sources_parser_ext_test_() ->
  35. [test_ext_fun(Test) || Test <- ext_test_defs()].
  36. test_ext_fun({Name, Tpl, {Fields, Output}}) ->
  37. {Name, fun() ->
  38. Tokens = [sources_parser:phrase_info(Fields, P)
  39. || P <- sources_parser:parse_content("dummy_path", Tpl)],
  40. ?assertEqual(Output, Tokens)
  41. end}.
  42. ext_test_defs() ->
  43. [{"trans with inline comments",
  44. <<"{#TrAnSlATORs: hi!#}{%trans 'phrase'%}">>,
  45. {[msgid, comment], [["phrase", "TrAnSlATORs: hi!"]]}},
  46. {"trans with comments",
  47. <<"{%comment%}translators: com{{me}}nt{%endcomment%}{%trans 'phrase'%}">>,
  48. {[msgid, comment], [["phrase", "translators: com{{ me }}nt"]]}},
  49. {"blocktrans with comments",
  50. <<"{%comment%}translators: comment{%endcomment%}{%blocktrans with a=b%}B={{b}}{%endblocktrans%}">>,
  51. {[msgid, comment], [["B={{ b }}", "translators: comment"]]}}
  52. ].
  53. unparser_test_() ->
  54. [test_unparser_fun(Test) || Test <- unparser_test_defs()].
  55. test_unparser_fun({Name, Tpl}) ->
  56. {Name, fun() ->
  57. %% take input Tpl value, parse it, "unparse" it, then parse it again.
  58. %% the both parsed values should be equvialent, even if the source versions
  59. %% are not an exact match (there can be whitespace differences)
  60. {ok, Dpt} = erlydtl_compiler:do_parse_template(
  61. Tpl, #dtl_context{}),
  62. Unparsed = erlydtl_unparser:unparse(Dpt),
  63. {ok, DptU} = erlydtl_compiler:do_parse_template(
  64. Unparsed, #dtl_context{}),
  65. compare_tree(Dpt, DptU)
  66. end}.
  67. unparser_test_defs() ->
  68. [{"comment tag", <<"here it is: {# this is my comment #} <-- it was right there.">>}
  69. ].
  70. compare_tree([], []) -> ok;
  71. compare_tree([H1|T1], [H2|T2]) ->
  72. compare_token(H1, H2),
  73. compare_tree(T1, T2).
  74. compare_token({'extends', Value1}, {'extends', Value2}) ->
  75. ?assertEqual(Value1, Value2);
  76. compare_token({'autoescape', OnOrOff1, Contents1}, {'autoescape', OnOrOff2, Contents2}) ->
  77. ?assertEqual(OnOrOff1, OnOrOff2),
  78. compare_tree(Contents1, Contents2);
  79. compare_token({'block', Identifier1, Contents1}, {'block', Identifier2, Contents2}) ->
  80. compare_identifier(Identifier1, Identifier2),
  81. compare_tree(Contents1, Contents2);
  82. compare_token({'blocktrans', Args1, Contents1}, {'blocktrans', Args2, Contents2}) ->
  83. compare_args(Args1, Args2),
  84. compare_tree(Contents1, Contents2);
  85. compare_token({'call', Identifier1}, {'call', Identifier2}) ->
  86. compare_identifier(Identifier1, Identifier2);
  87. compare_token({'call', Identifier1, With1}, {'call', Identifier2, With2}) ->
  88. ?assertEqual(With1, With2),
  89. compare_identifier(Identifier1, Identifier2);
  90. compare_token({'comment', Contents1}, {'comment', Contents2}) ->
  91. compare_tree(Contents1, Contents2);
  92. compare_token({'comment_tag', _Pos, Text1}, {'comment_tag', _Pos, Text2}) ->
  93. ?assertEqual(Text1, Text2);
  94. compare_token({'cycle', Names1}, {'cycle', Names2}) ->
  95. compare_tree(Names1, Names2);
  96. compare_token({'cycle_compat', Names1}, {'cycle_compat', Names2}) ->
  97. compare_cycle_compat_names(Names1, Names2);
  98. compare_token({'date', 'now', Value1}, {'date', 'now', Value2}) ->
  99. compare_value(Value1, Value2);
  100. compare_token({'filter', FilterList1, Contents1}, {'filter', FilterList2, Contents2}) ->
  101. compare_filters(FilterList1, FilterList2),
  102. compare_tree(Contents1, Contents2);
  103. compare_token({'firstof', Vars1}, {'firstof', Vars2}) ->
  104. compare_tree(Vars1, Vars2);
  105. %% TODO...
  106. %% compare_token({'for', {'in', IteratorList, Identifier}, Contents}, {'for', {'in', IteratorList, Identifier}, Contents}) -> ok;
  107. %% compare_token({'for', {'in', IteratorList, Identifier}, Contents, EmptyPartsContents}, {'for', {'in', IteratorList, Identifier}, Contents, EmptyPartsContents}) -> ok;
  108. compare_token({'if', Expression1, Contents1}, {'if', Expression2, Contents2}) ->
  109. compare_expression(Expression1, Expression2),
  110. compare_tree(Contents1, Contents2);
  111. %% compare_token({'ifchanged', Expression, IfContents}, {'ifchanged', Expression, IfContents}) -> ok;
  112. %% compare_token({'ifchangedelse', Expression, IfContents, ElseContents}, {'ifchangedelse', Expression, IfContents, ElseContents}) -> ok;
  113. %% compare_token({'ifelse', Expression, IfContents, ElseContents}, {'ifelse', Expression, IfContents, ElseContents}) -> ok;
  114. %% compare_token({'ifequal', [Arg1, Arg2], Contents}, {'ifequal', [Arg1, Arg2], Contents}) -> ok;
  115. %% compare_token({'ifequalelse', [Arg1, Arg2], IfContents, ElseContents}, {'ifequalelse', [Arg1, Arg2], IfContents, ElseContents}) -> ok;
  116. %% compare_token({'ifnotequal', [Arg1, Arg2], Contents}, {'ifnotequal', [Arg1, Arg2], Contents}) -> ok;
  117. %% compare_token({'ifnotequalelse', [Arg1, Arg2], IfContents, ElseContents}, {'ifnotequalelse', [Arg1, Arg2], IfContents, ElseContents}) -> ok;
  118. %% compare_token({'include', Value, []}, {'include', Value, []}) -> ok;
  119. %% compare_token({'include', Value, Args}, {'include', Value, Args}) -> ok;
  120. %% compare_token({'include_only', Value, []}, {'include_only', Value, []}) -> ok;
  121. %% compare_token({'include_only', Value, Args}, {'include_only', Value, Args}) -> ok;
  122. %% compare_token({'regroup', {Variable, Identifier1, Identifier2}, Contents}, {'regroup', {Variable, Identifier1, Identifier2}, Contents}) -> ok;
  123. %% compare_token({'spaceless', Contents}, {'spaceless', Contents}) -> ok;
  124. %% compare_token({'ssi', Arg}, {'ssi', Arg}) -> ok;
  125. %% compare_token({'ssi_parsed', Arg}, {'ssi_parsed', Arg}) -> ok;
  126. compare_token({'string', _, String1}, {'string', _, String2}) ->
  127. ?assertEqual(String1, String2);
  128. %% compare_token({'tag', Identifier, []}, {'tag', Identifier, []}) -> ok;
  129. %% compare_token({'tag', Identifier, Args}, {'tag', Identifier, Args}) -> ok;
  130. %% compare_token({'templatetag', Identifier}, {'templatetag', Identifier}) -> ok;
  131. %% compare_token({'trans', Value}, {'trans', Value}) -> ok;
  132. %% compare_token({'widthratio', Numerator, Denominator, Scale}, {'widthratio', Numerator, Denominator, Scale}) -> ok;
  133. %% compare_token({'with', Args, Contents}, {'with', Args, Contents}) -> ok;
  134. compare_token(ValueToken1, ValueToken2) ->
  135. compare_value(ValueToken1, ValueToken2).
  136. compare_identifier({identifier, _, Name1}, {identifier, _, Name2}) ->
  137. ?assertEqual(Name1, Name2).
  138. compare_filters(FilterList1, FilterList2) ->
  139. [compare_filter(F1, F2)
  140. || {F1, F2} <- lists:zip(FilterList1, FilterList2)].
  141. compare_filter([Identifier1], [Identifier2]) ->
  142. compare_identifier(Identifier1, Identifier2);
  143. compare_filter([Identifier1, Arg1], [Identifier2, Arg2]) ->
  144. compare_identifier(Identifier1, Identifier2),
  145. compare_value(Arg1, Arg2).
  146. compare_expression({'expr', _, Arg11, Arg12}, {'expr', _, Arg21, Arg22}) ->
  147. compare_value(Arg11, Arg21),
  148. compare_value(Arg12, Arg22);
  149. compare_expression({'expr', "not", Expr1}, {'expr', "not", Expr2}) ->
  150. compare_expression(Expr1, Expr2);
  151. compare_expression(Other1, Other2) ->
  152. compare_value(Other1, Other2).
  153. compare_value({'string_literal', _, Value1}, {'string_literal', _, Value2}) ->
  154. ?assertEqual(Value1, Value2);
  155. compare_value({'number_literal', _, Value1}, {'number_literal', _, Value2}) ->
  156. ?assertEqual(Value1, Value2);
  157. compare_value({'apply_filter', Variable1, Filter1}, {'apply_filter', Variable2, Filter2}) ->
  158. compare_value(Variable1, Variable2),
  159. compare_filter(Filter1, Filter2);
  160. compare_value({'attribute', {Variable1, Identifier1}}, {'attribute', {Variable2, Identifier2}}) ->
  161. compare_value(Variable1, Variable2),
  162. compare_identifier(Identifier1, Identifier2);
  163. compare_value({'variable', Identifier1}, {'variable', Identifier2}) ->
  164. compare_identifier(Identifier1, Identifier2).
  165. compare_args(Args1, Args2) ->
  166. [compare_arg(A1, A2)
  167. || {A1, A2} <- lists:zip(Args1, Args2)].
  168. compare_arg({{identifier, _, Name1}, Value1}, {{identifier, _, Name2}, Value2}) ->
  169. ?assertEqual(Name1, Name2),
  170. compare_value(Value1, Value2).
  171. compare_cycle_compat_names(Names1, Names2) ->
  172. [compare_identifier(N1, N2)
  173. || {N1, N2} <- lists:zip(Names1, Names2)].