-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_plweb.pl
75 lines (50 loc) · 1.39 KB
/
test_plweb.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
:- module(test_plweb,
[ test_links/0
]).
:- use_module(library(ansi_term)).
:- use_module(library(apply)).
:- use_module(library(error)).
:- use_module(library(http/http_client)).
:- use_module(library(http/http_ssl_plugin)). % HTTPS support.
:- use_module(library(uri)).
:- use_module(page).
/** <module> Test various aspects of plweb
@author Wouter Beek
@version 2016/05/02
*/
:- meta_predicate
verbose(0, +, +).
test_links :-
clause(plweb_page:menu(_,L), _),
maplist(test_links, L).
test_links(_=L) :-
is_list(L), !,
maplist(test_links, L).
test_links(_=Uri) :-
is_link(Uri), !,
verbose(test_link(Uri), "Checking URL ‘~a’", [Uri]).
test_links(_).
test_link(Uri) :-
http_get(Uri, _, [status_code(Code)]),
(between(200, 299, Code) -> true ; throw(error(Code))).
% HELPERS %
%! is_link(@Term) is semidet.
is_link(Uri) :-
atom(Uri),
uri_components(Uri, uri_components(Scheme,Auth,_,_,_)),
maplist(atom, [Scheme,Auth]).
%! verbose(:Goal_0, +Format, +Args) is det.
verbose(Goal_0, Format, Args) :-
get_time(Start),
format(Format, Args),
( catch(Goal_0, E, true)
-> ( var(E)
-> get_time(End),
Delta is End - Start,
ansi_format([fg(green)], "~`.t success (~2f sec.)~72|", [Delta])
; message_to_string(E, S),
ansi_format([fg(red)], "~`.t ERROR: ~w~72|", [S])
)
; ansi_format([fg(red)], "~`.t ERROR: (failed)~72|", [])
),
nl.