-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest_js.pl
140 lines (109 loc) · 4.64 KB
/
test_js.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2014-2018, VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(test_js,
[ test_js/0
]).
/** <module> Test JavaScript Pengines interaction
This module scripts the browser and Node.js interaction with a Pengines server.
The set of test cases is based on BDD conventions and is ran by the Mocha test
runner. The browser test environment uses headless Chromium driven through
the debug protocol using the Puppeteer NPM package. The tests were previously
run using SlimerJS (fragile) and before that using PhantomJS (not maintained).
To run this code, install all required dependencies using NPM:
% npm install
*/
% setup paths to load relevant packages from development environment
:- asserta(user:file_search_path(foreign, '../http')).
:- asserta(user:file_search_path(foreign, '../clib')).
:- asserta(user:file_search_path(foreign, '../sgml')).
:- asserta(user:file_search_path(library, '.')).
:- asserta(user:file_search_path(library, '..')).
:- asserta(user:file_search_path(library, '../sgml')).
:- asserta(user:file_search_path(library, '../plunit')).
:- asserta(user:file_search_path(library, '../clib')).
% Hack: auto-loading this does not work.
:- [library(charsio)].
:- [charsio:library(memfile)].
:- debug(pengine(delay)).
:- use_module(library(plunit)).
:- use_module(library(pengines)).
:- use_module(library(pengines_sandbox)).
:- use_module(pengine_sandbox:library(pengines)).
:- use_module(library(process)).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_files)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/json)).
:- use_module(library(http/jquery)).
:- pengine_application(swish).
:- use_module(swish:library(pengines_io)).
pengines:prepare_module(Module, swish, _Options) :-
pengines_io:pengine_bind_io_to_html(Module).
% Necessary for browser tests.
:- set_setting_default(http:cors, [*]).
% :- debug(http(request)).
test_js :-
run_tests([ js_pengines
]).
run_test_script(Script, Status):-
pengine_server_port(Port),
process_create(path(node), [Script, Port],
[stdin(std), stdout(std), stderr(std), process(PID)]),
process_wait(PID, Status).
:- begin_tests(js_pengines,
[ setup(start_pengine_server(_Port)),
cleanup(stop_pengine_server)
]).
test(browser):-
run_test_script('test_js/browser.js', Status),
assertion(Status == exit(0)).
test(node):-
run_test_script('test_js/node.js', Status),
assertion(Status == exit(0)).
:- end_tests(js_pengines).
/*******************************
* HTTP SERVER *
*******************************/
:- http_handler(/, http_reply_from_files(web, []), [prefix]).
:- dynamic
pengine_server_port/1.
pengine_server(URL) :-
start_pengine_server(Port),
format(atom(URL), 'http://localhost:~d', [Port]).
start_pengine_server(Port) :-
pengine_server_port(Port),
!.
start_pengine_server(Port) :-
http_server(http_dispatch, [port(Port)]),
asserta(pengine_server_port(Port)).
stop_pengine_server :-
retract(pengine_server_port(Port)),
!,
http_stop_server(Port, []).
stop_pengine_server.