couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kxe...@apache.org
Subject [4/5] config commit: updated refs/heads/master to b281825
Date Wed, 04 Feb 2015 15:49:39 GMT
Enforce type verification for config:set/get

Check the type of given default value to make sure it is supported.
Raise error(badarg) from set/get on type missmatch.
Add tests for the feature

COUCHDB-2561


Project: http://git-wip-us.apache.org/repos/asf/couchdb-config/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-config/commit/135af48e
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-config/tree/135af48e
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-config/diff/135af48e

Branch: refs/heads/master
Commit: 135af48ed7cdcdf483b4f7e984e8f7ff278fff03
Parents: e151ae4
Author: ILYA Khlopotov <iilyak@ca.ibm.com>
Authored: Thu Jan 29 13:01:21 2015 -0800
Committer: ILYA Khlopotov <iilyak@ca.ibm.com>
Committed: Wed Feb 4 06:00:10 2015 -0800

----------------------------------------------------------------------
 src/config.erl        | 11 +++++++++--
 test/config_tests.erl | 40 ++++++++++++++++++----------------------
 2 files changed, 27 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/135af48e/src/config.erl
----------------------------------------------------------------------
diff --git a/src/config.erl b/src/config.erl
index dcffe86..a24bb27 100644
--- a/src/config.erl
+++ b/src/config.erl
@@ -139,7 +139,12 @@ get(Section, Key, Default) when is_binary(Section) and is_binary(Key)
->
     ?MODULE:get(binary_to_list(Section), binary_to_list(Key), Default);
 get(Section, Key, Default) when is_list(Section), is_list(Key) ->
     case ets:lookup(?MODULE, {Section, Key}) of
-        [] -> Default;
+        [] when Default == undefined -> Default;
+        [] when is_boolean(Default) -> Default;
+        [] when is_float(Default) -> Default;
+        [] when is_integer(Default) -> Default;
+        [] when is_list(Default) -> Default;
+        [] -> error(badarg);
         [{_, Match}] -> Match
     end.
 
@@ -155,7 +160,9 @@ set(Sec, Key, Val, Persist, Reason) when is_binary(Sec) and is_binary(Key)
->
     ?MODULE:set(binary_to_list(Sec), binary_to_list(Key), Val, Persist, Reason);
 set(Section, Key, Value, Persist, Reason)
         when is_list(Section), is_list(Key), is_list(Value) ->
-    gen_server:call(?MODULE, {set, Section, Key, Value, Persist, Reason}).
+    gen_server:call(?MODULE, {set, Section, Key, Value, Persist, Reason});
+set(_Sec, _Key, _Val, _Persist, _Reason) ->
+    error(badarg).
 
 
 delete(Section, Key) when is_binary(Section) and is_binary(Key) ->

http://git-wip-us.apache.org/repos/asf/couchdb-config/blob/135af48e/test/config_tests.erl
----------------------------------------------------------------------
diff --git a/test/config_tests.erl b/test/config_tests.erl
index 2d3968c..c823d3e 100644
--- a/test/config_tests.erl
+++ b/test/config_tests.erl
@@ -115,8 +115,9 @@ config_get_tests() ->
                 should_return_undefined_atom_on_missed_section(),
                 should_return_undefined_atom_on_missed_option(),
                 should_return_custom_default_value_on_missed_option(),
-                should_only_return_default_on_missed_option()%,
-                %%should_get_binary_option()
+                should_only_return_default_on_missed_option(),
+                should_fail_to_get_binary_value(),
+                should_return_any_supported_default()
             ]
         }
     }.
@@ -129,8 +130,8 @@ config_set_tests() ->
             fun setup/0, fun teardown/1,
             [
                 should_update_option(),
-                should_create_new_section()%,
-                %%should_set_binary_option()
+                should_create_new_section(),
+                should_fail_to_set_binary_value()
             ]
         }
     }.
@@ -143,8 +144,7 @@ config_del_tests() ->
             fun setup/0, fun teardown/1,
             [
                 should_return_undefined_atom_after_option_deletion(),
-                should_be_ok_on_deleting_unknown_options()%,
-                %%should_delete_binary_option()
+                should_be_ok_on_deleting_unknown_options()
             ]
         }
     }.
@@ -243,10 +243,17 @@ should_only_return_default_on_missed_option() ->
     ?_assertEqual("0",
                   config:get("httpd", "port", "bar")).
 
-should_get_binary_option() ->
-    ?_assertEqual(<<"baz">>,
+should_fail_to_get_binary_value() ->
+    ?_assertException(error, badarg,
                   config:get(<<"foo">>, <<"bar">>, <<"baz">>)).
 
+should_return_any_supported_default() ->
+    Values = [undefined, "list", true, false, 0.1, 1],
+    Tests = [{lists:flatten(io_lib:format("for type(~p)", [V])), V}
+        || V <- Values],
+    [{T, ?_assertEqual(V, config:get(<<"foo">>, <<"bar">>, V))}
+        || {T, V} <- Tests].
+
 should_update_option() ->
     ?_assertEqual("severe",
         begin
@@ -262,12 +269,9 @@ should_create_new_section() ->
             config:get("new_section", "bizzle")
         end).
 
-should_set_binary_option() ->
-    ?_assertEqual(<<"baz">>,
-        begin
-            ok = config:set(<<"foo">>, <<"bar">>, <<"baz">>,
false),
-            config:get(<<"foo">>, <<"bar">>)
-        end).
+should_fail_to_set_binary_value() ->
+    ?_assertException(error, badarg,
+        config:set(<<"foo">>, <<"bar">>, <<"baz">>, false)).
 
 should_return_undefined_atom_after_option_deletion() ->
     ?_assertEqual(undefined,
@@ -279,14 +283,6 @@ should_return_undefined_atom_after_option_deletion() ->
 should_be_ok_on_deleting_unknown_options() ->
     ?_assertEqual(ok, config:delete("zoo", "boo", false)).
 
-should_delete_binary_option() ->
-    ?_assertEqual(undefined,
-        begin
-            ok = config:set(<<"foo">>, <<"bar">>, <<"baz">>,
false),
-            ok = config:delete(<<"foo">>, <<"bar">>, false),
-            config:get(<<"foo">>, <<"bar">>)
-        end).
-
 should_ensure_in_defaults(_, _) ->
     ?_test(begin
         ?assertEqual("500",


Mime
View raw message