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

minor clean up. support any map keys, not only atoms, lists and binaries.

Andreas Stenius 10 лет назад
Родитель
Сommit
3b8d4149f7
2 измененных файлов с 57 добавлено и 60 удалено
  1. 22 28
      src/erlydtl_runtime.erl
  2. 35 32
      test/erlydtl_test_defs.erl

+ 22 - 28
src/erlydtl_runtime.erl

@@ -108,38 +108,32 @@ find_value(Key, Tuple) when is_tuple(Tuple) ->
                     undefined
             end
     end;
-find_value(Key, Map) when is_atom(Key) ->
+find_value(Key, Map) ->
     case erlang:is_builtin(erlang, is_map, 1) andalso erlang:is_map(Map) of
-        true ->
-            case maps:find(Key, Map) of
-                error           -> find_value(atom_to_list(Key), Map);
-                {ok, Value}     -> Value
-            end;
-        false ->
-            undefined
+        true  -> find_map_value(Key, Map);
+        false -> undefined
+    end.
+
+find_map_value(Key, Map) when is_atom(Key) ->
+    case maps:find(Key, Map) of
+        error           -> find_map_value(atom_to_list(Key), Map);
+        {ok, Value}     -> Value
     end;
-find_value(Key, Map) when is_list(Key) ->
-    case erlang:is_builtin(erlang, is_map, 1) andalso erlang:is_map(Map) of
-        true ->
-            case maps:find(Key, Map) of
-                error           -> find_value(list_to_binary(Key), Map);
-                {ok, Value}     -> Value
-            end;
-        false ->
-            undefined
+find_map_value(Key, Map) when is_list(Key) ->
+    case maps:find(Key, Map) of
+        error           -> find_map_value(list_to_binary(Key), Map);
+        {ok, Value}     -> Value
     end;
-find_value(Key, Map) when is_binary(Key) ->
-    case erlang:is_builtin(erlang, is_map, 1) andalso erlang:is_map(Map) of
-        true ->
-            case maps:find(Key, Map) of
-                error           -> undefined;
-                {ok, Value}     -> Value
-            end;
-        false ->
-            undefined
+find_map_value(Key, Map) when is_binary(Key) ->
+    case maps:find(Key, Map) of
+        error           -> undefined;
+        {ok, Value}     -> Value
     end;
-find_value(_, _) ->
-    undefined.
+find_map_value(Key, Map) ->
+    case maps:find(Key, Map) of
+        error           -> undefined;
+        {ok, Value}     -> Value
+    end.
 
 fetch_value(Key, Data, Options) ->
     fetch_value(Key, Data, Options, []).

+ 35 - 32
test/erlydtl_test_defs.erl

@@ -12,39 +12,42 @@
 tests() ->
     [def_to_test(G, D) || {G, Ds} <- all_test_defs(), D <- Ds].
 
--define(IF_MAPS(Then, Else),
-        case erlang:is_builtin(erlang, is_map, 1) of
-            true -> Then;
-            false -> Else
-        end).
-
 all_test_defs() ->
-    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">>},
-            {"No spaces",
-             <<"{{var1}}">>,
-             [{var1, "foo"}], <<"foo">>},
-            {"Variable name is a tag name",
-             <<"{{ comment }}">>,
-             [{comment, "Nice work!"}], <<"Nice work!">>},
-            {"list no the attr",
-             <<"{{ content.description }}">>,
-             [{content, "test"}], <<"">>},
-            {"binary no the attr",
-             <<"{{ content.description }}">>,
-             [{content, <<"test">>}], <<"">>}],
-    MapsTests = ?IF_MAPS([{"maps", <<"{{ my_maps.hello }}">>,
-                           [{ my_maps, maps:put(hello, "world", maps:new())}],
-                           <<"world">>
-                          }], []),
-    [{"vars", MapsTests ++ Vars},
+    [{"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">>},
+       {"No spaces",
+        <<"{{var1}}">>,
+        [{var1, "foo"}], <<"foo">>},
+       {"Variable name is a tag name",
+        <<"{{ comment }}">>,
+        [{comment, "Nice work!"}], <<"Nice work!">>},
+       {"list no the attr",
+        <<"{{ content.description }}">>,
+        [{content, "test"}], <<"">>},
+       {"binary no the attr",
+        <<"{{ content.description }}">>,
+        [{content, <<"test">>}], <<"">>}
+      ]},
+     {"maps",
+      case erlang:is_builtin(erlang, is_map, 1) of
+          false -> [];
+          true ->
+              [#test{
+                  title = "simple test",
+                  source = <<"{{ msg.hello }}">>,
+                  render_vars = [{msg, maps:put(hello, "world", maps:new())}],
+                  output = <<"world">>
+                 }
+              ]
+      end},
      {"comment",
       [{"comment block is excised",
         <<"bob {% comment %}(moron){% endcomment %} loblaw">>,