Просмотр исходного кода

Issue #6

Applied patch from Dan Milstein to fix a crash when rendering floats.

Added a baseline failing test case for the problem.



git-svn-id: http://erlydtl.googlecode.com/svn/trunk@147 a5195066-8e3e-0410-a82a-05b01b1b9875
colm.dougan 16 лет назад
Родитель
Сommit
bb48da29c4
3 измененных файлов с 36 добавлено и 9 удалено
  1. 3 3
      src/erlydtl/erlydtl_compiler.erl
  2. 7 0
      src/erlydtl/erlydtl_filters.erl
  3. 26 6
      src/tests/erlydtl_unittests.erl

+ 3 - 3
src/erlydtl/erlydtl_compiler.erl

@@ -536,11 +536,11 @@ resolve_scoped_variable_ast(VarName, Context) ->
         end, undefined, Context#dtl_context.local_scopes).
         end, undefined, Context#dtl_context.local_scopes).
 
 
 format(Ast, Context) ->
 format(Ast, Context) ->
-    auto_escape(format_integer_ast(Ast), Context).
+    auto_escape(format_number_ast(Ast), Context).
 
 
 
 
-format_integer_ast(Ast) ->
-    erl_syntax:application(erl_syntax:atom(erlydtl_filters), erl_syntax:atom(format_integer),
+format_number_ast(Ast) ->
+    erl_syntax:application(erl_syntax:atom(erlydtl_filters), erl_syntax:atom(format_number),
         [Ast]).
         [Ast]).
 
 
 
 

+ 7 - 0
src/erlydtl/erlydtl_filters.erl

@@ -112,6 +112,13 @@ format_integer(Input) when is_integer(Input) ->
 format_integer(Input) ->
 format_integer(Input) ->
     Input.
     Input.
 
 
+format_number(Input) when is_integer(Input) ->
+    integer_to_list(Input);
+format_number(Input) when is_float(Input) ->
+    io_lib:format("~p", [Input]);
+format_number(Input) ->
+    Input.
+
 join([Input], Separator) when is_list(Input) ->
 join([Input], Separator) when is_list(Input) ->
     string:join(Input, Separator).
     string:join(Input, Separator).
 
 

+ 26 - 6
src/tests/erlydtl_unittests.erl

@@ -4,13 +4,24 @@
 
 
 tests() ->
 tests() ->
     [
     [
+        {"vars", [
+                {"string",
+                    <<"String value is: {{ var1 }}">>,
+                    [{var1, "foo"}], <<"String value is: foo">>},
+                {"int",
+                    <<"The magic number is: {{ var1 }}">>,
+                    [{var1, 42}], <<"The magic number is: 42">>},
+                {"float",
+                    <<"The price of milk is: {{ var1 }}">>,
+                    [{var1, 0.42}], <<"The price of milk is: 0.42">>}
+            ]},
         {"comment", [
         {"comment", [
-                {"Comment block is excised",
-                    <<"Bob {% comment %}(moron){% endcomment %} Loblaw">>,
-                    [], <<"Bob  Loblaw">>},
-                {"Inline comment is excised",
-                    <<"You're {# not #} a very nice person">>,
-                    [], <<"You're  a very nice person">>}
+                {"comment block is excised",
+                    <<"bob {% comment %}(moron){% endcomment %} loblaw">>,
+                    [], <<"bob  loblaw">>},
+                {"inline comment is excised",
+                    <<"you're {# not #} a very nice person">>,
+                    [], <<"you're  a very nice person">>}
             ]},
             ]},
         {"autoescape", [
         {"autoescape", [
                 {"Autoescape works",
                 {"Autoescape works",
@@ -235,6 +246,15 @@ tests() ->
                     <<"Ben &amp; Jerry&#039;s &lt;=&gt; &quot;The World&#039;s Best Ice Cream&quot;">>},
                     <<"Ben &amp; Jerry&#039;s &lt;=&gt; &quot;The World&#039;s Best Ice Cream&quot;">>},
                 {"|format_integer",
                 {"|format_integer",
                     <<"{{ var1|format_integer }}">>, [{var1, 28}], <<"28">>},
                     <<"{{ var1|format_integer }}">>, [{var1, 28}], <<"28">>},
+                {"|format_number 1",
+                    <<"{{ var1|format_number }}">>, [{var1, 28}], <<"28">>},
+                {"|format_number 2",
+                    <<"{{ var1|format_number }}">>, [{var1, 23.77}], <<"23.77">>},
+                {"|format_number 3",
+                    <<"{{ var1|format_number }}">>, [{var1, "28.77"}], <<"28.77">>},
+                {"|format_number 4",
+                    <<"{{ var1|format_number }}">>, [{var1, "23.77"}], <<"23.77">>},
+ 
                 {"|join:\", \"",
                 {"|join:\", \"",
                     <<"{{ var1|join:\", \" }}">>, [{var1, ["Liberte", "Egalite", "Fraternite"]}],
                     <<"{{ var1|join:\", \" }}">>, [{var1, ["Liberte", "Egalite", "Fraternite"]}],
                     <<"Liberte, Egalite, Fraternite">>},
                     <<"Liberte, Egalite, Fraternite">>},