1:- module(onepointfour_basics_checks_throwers,
2 [
3 fail_unless_hard/1 4 ,throw_or_fail_for_case_random/1 5 ,throw_or_fail/5 6 ,throw_2/3 7 ]). 8
9:- use_module(library('onepointfour_basics/dict_settings.pl')). 10
34
46
47fail_unless_hard(soft) :- !,fail.
48fail_unless_hard(hard).
49
57
58throw_or_fail_for_case_random(Tuned) :-
59 fail_unless_hard(Tuned), 60 throw_2(random,"random failure as decided by a call to maybe/1",_).
61
63throw_or_fail(ErrorTerm,Culprit,Tuned,Info,Dict) :-
64 fail_unless_hard(Tuned), 65 get_setting(Dict,name,Name,""), 66 name_to_string(Name,NameStr),
67 (
68 (Info = be(What))
69 ->
70 format(string(Msg),"~s should be ~q",[NameStr,What])
71 ;
72 (Info = be(What,Text))
73 ->
74 format(string(Msg),"~s should be ~q. ~q",[NameStr,What,Text])
75 ;
76 format(string(Msg),"~s should fulfill ~q-ness",[NameStr,Info])
77 ),
78 throw_2(ErrorTerm,Msg,Culprit).
79
82
83throw_2(domain(Expected),Msg,Culprit) :- throw(error(check(domain , Expected , Msg , Culprit) , _)).
84throw_2(type(Expected),Msg,Culprit) :- throw(error(check(type , Expected , Msg , Culprit) , _)).
85throw_2(domain,Msg,Culprit) :- throw(error(check(domain , _Expected , Msg , Culprit) , _)).
86throw_2(type,Msg,Culprit) :- throw(error(check(type , _Expected , Msg , Culprit) , _)).
87throw_2(uninstantiation,Msg,Culprit) :- throw(error(check(uninstantiation , _Expected , Msg , Culprit) , _)). 88throw_2(instantiation,Msg,Culprit) :- throw(error(check(instantiation , _Expected , Msg , Culprit) , _)). 89throw_2(random,Msg,_) :- throw(error(check(random , _Expected , Msg , _Culprit) , _)).
90throw_2(explicit_fail,Msg,Culprit) :- throw(error(check(explicit_fail , _Expected , Msg , Culprit) , _)).
91throw_2(call,Msg,Culprit) :- throw(error(check(call , _Expected , Msg , Culprit) , _)).
92throw_2(hard_check_fails,Msg,Culprit) :- throw(error(check(hard_check_fails , _Expected , Msg , Culprit) , _)).
93throw_2(syntax,Msg,Culprit) :- throw(error(check(syntax , _Expected , Msg , Culprit) , _)).
94throw_2(unknown_condition,Msg,Culprit) :- throw(error(check(unknown_condition , _Expected , Msg , Culprit) , _)).
95
97
98throw_2(passall,Msg,Culprit) :- throw(error(check(passall , _Expected , Msg , Culprit) , _)).
99throw_2(passany,Msg,Culprit) :- throw(error(check(passany , _Expected , Msg , Culprit) , _)).
100throw_2(passnone,Msg,Culprit) :- throw(error(check(passnone , _Expected , Msg , Culprit) , _)).
101throw_2(forall,Msg,Culprit) :- throw(error(check(forall , _Expected , Msg , Culprit) , _)).
102throw_2(forany,Msg,Culprit) :- throw(error(check(forany , _Expected , Msg , Culprit) , _)).
103throw_2(fornone,Msg,Culprit) :- throw(error(check(fornone , _Expected , Msg , Culprit) , _)).
104
106
107throw_2(T,M,C) :-
108 format(string(Msg),"Bug! You forgot a throw_2/3 clause in the source for term '~q', message '~q', culprit '~q'",[T,M,C]),
109 throw(Msg).
110
112
113name_to_string(X , "the value") :- var(X),!.
114name_to_string('' , "the value") :- !.
115name_to_string("" , "the value") :- !.
116name_to_string(X , X ) :- string(X),!.
117name_to_string(X , Str ) :- atom(X),!,atom_string(X,Str).
118name_to_string(X , Str ) :- format(string(Str),"~q",[X])
Some helper predicates for
checks.pl
The homepage for this module is at
https://github.com/dtonhofer/prolog_code/blob/main/unpacked/onepointfour_basics/README_checks.md
*/