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
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 @@
?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 @@
?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) ->
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 @@
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 @@
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 @@
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 @@
?_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 @@
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_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",