Commit cb4a997b authored by Siebers, Michael's avatar Siebers, Michael
Browse files

Updated get_dict_typed/4 and dependent predicates and tests

parent 55a47daa
......@@ -23,25 +23,25 @@
% @error type_error(dict, Parameters) if Parameters is no dict
% @error existence_error(key, Key, Parameters) if Parameters does not contain
% the required key Key
% @error instantiation_error if any used dict value is unbound
% @error instantiation_error if dict is unbound
% @error key_instantiation_error(abs_path) if the dict value under abs_path is unbound
% @error uninstantiation_error(Result|More) if Result or More are bound
% @error type_error(Type, KeyName) if the dict value under KeyName is of a wrong
% type, expected Type
% @error key_type_error(KeyName, Type, Value) if the dict value Value under
% KeyName is of a wrong type,
% expected Type
% @error unknown_key(KeyName, Dict) if the dict contains an unknown key KeyName
% @error implementation_error(Cause) if generating the explanation itself throws
% an exception
do_handle_explain(Parameters, Result, More) :-
must_be(dict, Parameters), % might throw type_error(dict, Parameters)
% or instantiation_error
AbsPath = Parameters.abs_path, % extract absolute path; or
% existence_error(key, abs_path, Parameters)
must_be(atom, AbsPath), % might throw instantiation_error or
% type_error(atom, AbsPath)
once(( get_dict(limit, Parameters, Limit)
( get_dict_typed(abs_path, Parameters, atom, AbsPath) % might throw error
-> true
; existence_error(key, abs_path, Parameters)
),
once(( get_dict_typed(limit, Parameters, integer, Limit)
; Limit = 5
)),
must_be(integer, Limit), % might throw instantiation_error or
% type_error(int, Limit)
( get_dict(Key,Parameters,_),
\+ member(Key, [abs_path, limit])
-> throw(error(unknown_key(Key,Parameters),_))
......
......@@ -10,15 +10,20 @@
% @error type_error(dict, Dict) if Dict is no dict
% @error existence_error(key, abs_path, Dict) if Dict does not contain the
% key abs_path
% @error instantiation_error if the dict value under abs_path is unbound
% @error key_instantiation_error(abs_path) if the dict value under abs_path is unbound
% @error key_type_error(abs_path, atom, AbsPath) if the dict value AbsPath under
% the key abs_path is not an atom
% @error unknown_key(KeyName, Dict) if the dict contains any KeyName which is
% not abs_path
% @error implementation_error(Cause) if checking the irrelevance itself throws
% an error (Cause)
do_handle_irrelevant_file(Parameters,AbsPath) :-
get_dict_typed(Parameters, abs_path, atom, AbsPath),
(
get_dict_typed(abs_path, Parameters, atom, AbsPath)
-> true
; existence_error(key, abs_path, Parameters)
),
( (get_dict(Key,Parameters,_), Key\= abs_path )
-> throw(error(unknown_key(Key,Parameters),_))
; true
......
......@@ -153,14 +153,15 @@ test(do_handle_explain_missingkey,
web_api:do_handle_explain(Parameters,_Result,_More).
% do_handle_explain(Dict, Result, More) throws error(instantiation_error,_)
% if the dict value under abs_path is unbound. Takes precedence over type_error,
% unknown_key, implementation_error, and not_irrelevant.
% do_handle_explain(Dict, Result, More) throws
% error(key_instantiation_error(abs_path),_) if the dict value under abs_path
% is unbound. Takes precedence over type_error, unknown_key, implementation_error,
% and not_irrelevant.
test(do_handle_explain_instantiation,
[
forall(member(Parameters,
[t{abs_path:_},t{abs_path:_,another_key:some_atom}])),
error(instantiation_error),
error(key_instantiation_error(abs_path)),
setup(setup_explain_throws(some_error)),
cleanup(cleanup_explain_throws)
]) :-
......@@ -182,7 +183,8 @@ test(do_handle_explain_uninstantiation,
web_api:do_handle_explain(t{abs_path:'some_path'},Result,_More).
% do_handle_explain(Dict, Result, More) throws error(type_error(Type, KeyName),_)
% do_handle_explain(Dict, Result, More) throws
% error(key_type_error(KeyName, Type, Value),_)
% if the dict value under KeyName is not of type Type. Takes precedence over
% unknown_key, implementation_error, and not_irrelevant.
test(do_handle_explain_type,
......@@ -196,7 +198,7 @@ test(do_handle_explain_type,
another_key:some_atom})
)
),
error(type_error(atom, OtherTypeValue)),
error(key_type_error(abs_path,atom, OtherTypeValue)),
setup(setup_explain_throws(some_error)),
cleanup(cleanup_explain_throws)
]) :-
......@@ -210,7 +212,7 @@ test(do_handle_explain_type,
another_key:some_atom})
)
),
error(type_error(integer, OtherTypeValue)),
error(key_type_error(limit, integer, OtherTypeValue)),
setup(setup_explain_throws(some_error)),
cleanup(cleanup_explain_throws)
]) :-
......
......@@ -95,7 +95,7 @@ test(text_to_string_type,
* get_dict_typed/4 *
********************/
% get_dict_typed(+Dict, +Key, +Type, -Value) succeeds with the correct value on
% get_dict_typed(+Key, +Dict, +Type, -Value) succeeds with the correct value on
% correct input
test(get_dict_typed_happy,
[ forall((
......@@ -108,46 +108,46 @@ test(get_dict_typed_happy,
true(ExpectedValue =@= ActualValue)
]) :-
Dict = RandomDict.put([Key=ExpectedValue]),
web_api:get_dict_typed(Dict, Key, Type, ActualValue).
web_api:get_dict_typed(Key, Dict, Type, ActualValue).
% get_dict_typed(+Dict, +Key, +Type, -Value) throws instantiation_error if Dict,
% get_dict_typed(+Key, +Dict, +Type, -Value) throws instantiation_error if Dict,
% Key, or Type is unbound.
test(get_dict_typed_instantiation_Dict,
[ error(instantiation_error)
]) :-
web_api:get_dict_typed(_, some_key, some_type, _ActualValue).
web_api:get_dict_typed(some_key, _, some_type, _ActualValue).
test(get_dict_typed_instantiation_Key,
[ error(instantiation_error)
]) :-
web_api:get_dict_typed(_{some_key: some_value}, _, some_type, _ActualValue).
web_api:get_dict_typed(_Key, _{some_key: some_value}, some_type, _ActualValue).
test(get_dict_typed_instantiation_Type,
[ error(instantiation_error)
]) :-
web_api:get_dict_typed(_{some_key: some_value}, some_key, _, _ActualValue).
web_api:get_dict_typed(some_key, _{some_key: some_value}, _, _ActualValue).
% get_dict_typed(+Dict, +Key, +Type, -Value) throws type_error(dict, Dict) if
% get_dict_typed(+Key, +Dict, +Type, -Value) throws type_error(dict, Dict) if
% Dict is no dict.
test(get_dict_typed_type_Dict,
[ forall(type_not_dict(NoDict)),
error(type_error(dict, NoDict))
]) :-
web_api:get_dict_typed(NoDict, some_key, some_type, _ActualValue).
web_api:get_dict_typed(some_key, NoDict, some_type, _ActualValue).
% get_dict_typed(+Dict, +Key, +Type, -Value) throws
% existence_error(key, Key, Dict) if Dict does not contain the key Key.
% get_dict_typed(+Key, +Dict, +Type, -Value) fails if Dict does not contain the
% key Key.
test(get_dict_typed_existence,
[ forall((
type_is(dict, Dict)
)),
error(existence_error(key, some_key, Dict))
fail
]) :-
web_api:get_dict_typed(Dict, some_key, some_type, _ActualValue).
web_api:get_dict_typed(some_key, Dict, some_type, _ActualValue).
% get_dict_typed(+Dict, +Key, +Type, -Value) throws key_instantiation_error(Key)
% get_dict_typed(+Key, +Dict, +Type, -Value) throws key_instantiation_error(Key)
% if Value is insufficiently instantiated
test(get_dict_typed_key_instantiation,
[ forall((
......@@ -158,10 +158,10 @@ test(get_dict_typed_key_instantiation,
error(key_instantiation_error(Key))
]) :-
Dict = RandomDict.put([Key=_]),
web_api:get_dict_typed(Dict, Key, Type, _ActualValue).
web_api:get_dict_typed(Key, Dict, Type, _ActualValue).
% get_dict_typed(+Dict, +Key, +Type, -Value) throws
% get_dict_typed(+Key, +Dict, +Type, -Value) throws
% key_type_error(Key, Type, Value) if Value is not of type Type.
test(get_dict_typed_key_type_error,
[ forall((
......@@ -173,6 +173,6 @@ test(get_dict_typed_key_type_error,
error(key_type_error(Key, Type, Value))
]) :-
Dict = RandomDict.put([Key=Value]),
web_api:get_dict_typed(Dict, Key, Type, _ActualValue).
web_api:get_dict_typed(Key, Dict, Type, _ActualValue).
:- end_tests('web_api -> helpers').
......@@ -112,7 +112,7 @@ test(do_handle_irrelevant_file_instantiation,
% error(key_type_error(abs_path, atom, AbsPath),_) if the dict value under
% abs_path is not an atom. Takes precedence over unknown_key and
% implementation_error.
test(do_handle_irrelevant_file_type,
test(do_handle_irrelevant_file_keytype,
[
forall((
type_is(dict, RandomDict),
......
......@@ -315,26 +315,25 @@ text_to_string(Text, String) :-
format(string(String), '~s', [Text]).
%! get_dict_typed(+Dict, +Key, +Type, -Value) is det
%! get_dict_typed(+Key, +Dict, +Type, -Value) is det
%
% Unify the value associated with Key in Dict with Value. Throws an existence
% error if Key does not appear in Dict. Additionally, validates whether Value
% Unify the value associated with Key in Dict with Value. Fails silently if Key
% does not appear in Dict. If the Key is present, validates whether Value
% is bound and of type Type. Type definitions are taken from must_be/2.
%
% @see get_dict/3
% @see must_be/2
% @error instantiation_error if Dict Key, or Type is unbound
% @error type_error(dict, Dict) if Dict is no dict
% @error existence_error(key, Key, Dict) if Dict does not contain the key Key
% @error key_instantiation_error(Key) if Value is insufficiently instantiated
% @error key_type_error(Key, Type, Value) if Value is not of type Type
get_dict_typed(Dict, Key, Type, Value) :-
get_dict_typed(Key, Dict, Type, Value) :-
( nonvar(Key)
-> true
; throw(error(instantiation_error,_))
),
must_be(dict,Dict),
Value = Dict.Key,
get_dict(Key, Dict, Value),
( nonvar(Value)
-> true
; throw(error(key_instantiation_error(Key), _))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment