online.scm
1 | ;;;; Copyright (C) 2020 Julien Lepiller <julien@lepiller.eu> |
2 | ;;;; |
3 | ;;;; This library is free software; you can redistribute it and/or |
4 | ;;;; modify it under the terms of the GNU Lesser General Public |
5 | ;;;; License as published by the Free Software Foundation; either |
6 | ;;;; version 3 of the License, or (at your option) any later version. |
7 | ;;;; |
8 | ;;;; This library is distributed in the hope that it will be useful, |
9 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | ;;;; Lesser General Public License for more details. |
12 | ;;;; |
13 | ;;;; You should have received a copy of the GNU Lesser General Public |
14 | ;;;; License along with this library; if not, write to the Free Software |
15 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
16 | ;;;; |
17 | |
18 | (define-module (test-modules online) |
19 | #:use-module (ice-9 match) |
20 | #:use-module (rnrs bytevectors) |
21 | #:use-module (srfi srfi-1) |
22 | #:use-module (test-modules result) |
23 | #:use-module (rdf rdf) |
24 | #:use-module (srfi srfi-1) |
25 | #:use-module (turtle tordf) |
26 | #:use-module (web client) |
27 | #:use-module (web response) |
28 | #:export (run-test-suite |
29 | run-test-suites)) |
30 | |
31 | (define (get-objects triples predicate) |
32 | (map |
33 | rdf-triple-object |
34 | (filter |
35 | (lambda (t) |
36 | (equal? (rdf-triple-predicate t) predicate)) |
37 | triples))) |
38 | |
39 | (define (lexical->value value) |
40 | (cond |
41 | ((rdf-literal? value) |
42 | (rdf-literal-lexical-form value)) |
43 | ((blank-node? value) |
44 | (string-append "_:" (number->string value))) |
45 | (else value))) |
46 | |
47 | (define (execute-test test) |
48 | (let* ((predicates (test-case-document test)) |
49 | (type (car (get-objects predicates "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"))) |
50 | (type (substring type (string-length "http://www.w3.org/ns/rdftest#"))) |
51 | (action (car (get-objects predicates "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#action")))) |
52 | (turtle->rdf (get-test-doc action) action))) |
53 | |
54 | (define (run-test test) |
55 | (let* ((predicates (test-case-document test)) |
56 | (type (car (get-objects predicates "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"))) |
57 | (type (substring type (string-length "http://www.w3.org/ns/rdftest#"))) |
58 | (result |
59 | (catch #t |
60 | (lambda () |
61 | (execute-test test)) |
62 | (lambda (key . value) |
63 | (cons key value))))) |
64 | (cond |
65 | ((member type '("TestTurtlePositiveSyntax" "TestTriGPositiveSyntax" |
66 | "TestNTriplesPositiveSyntax" "TestNQuadsPositiveSyntax")) |
67 | (match result |
68 | (((? symbol? key) . value) |
69 | (update-test-case test |
70 | #:result 'fail |
71 | #:reason (format #f "failed with ~a: ~a" key value))) |
72 | (_ (update-test-case test #:result 'pass)))) |
73 | ((member type '("TestTurtleNegativeSyntax" "TestTriGNegativeSyntax" |
74 | "TestNTriplesNegativeSyntax" "TestNQuadsNegativeSyntax" |
75 | "TestXMLNegativeSyntax")) |
76 | (match result |
77 | (((? symbol? key) . value) (update-test-case test #:result 'pass)) |
78 | (_ (update-test-case test |
79 | #:result 'fail |
80 | #:reason "Expected failure but got success")))) |
81 | (else |
82 | (update-test-case test |
83 | #:result 'skip |
84 | #:reason (format #f "Unrecognized test type: ~a" type)))))) |
85 | |
86 | (define (run-tests tests expected-failures driver) |
87 | "Run all the tests of the @var{tests} test suite, using identifiers starting |
88 | from @var{id}. Return is undefined." |
89 | (fold |
90 | (lambda (test results) |
91 | (let* ((result (run-test test)) |
92 | (result |
93 | (if (assoc-ref expected-failures (test-case-id test)) |
94 | (update-test-case result |
95 | #:result (cond |
96 | ((equal? 'skip (test-case-result result)) |
97 | 'skip) |
98 | ((equal? 'fail (test-case-result result)) |
99 | 'xfail) |
100 | ((equal? 'pass (test-case-result result)) |
101 | 'xpass)) |
102 | #:reason (assoc-ref expected-failures (test-case-id test))) |
103 | result))) |
104 | ((test-driver-print driver) result) |
105 | (cons result results))) |
106 | '() |
107 | tests)) |
108 | |
109 | (define (get-test-doc url) |
110 | "Get a test suite object from the manifest at @var{url}." |
111 | (call-with-values |
112 | (lambda () |
113 | (http-get url)) |
114 | (lambda (hdr body) |
115 | (if (equal? (response-code hdr) 200) |
116 | (if (string? body) |
117 | body |
118 | (utf8->string body)) |
119 | (throw 'error-fetching-test-manifest (response-code hdr)))))) |
120 | |
121 | (define* (get-test-plan url #:key (num 1)) |
122 | (define document (get-test-doc url)) |
123 | |
124 | (define manifest (turtle->rdf document url)) |
125 | |
126 | (define tests |
127 | (map |
128 | rdf-triple-object |
129 | (filter |
130 | (lambda (triple) |
131 | (and (equal? (rdf-triple-subject triple) url) |
132 | (equal? (rdf-triple-predicate triple) |
133 | (string-append "http://www.w3.org/2001/sw/DataAccess/" |
134 | "tests/test-manifest#entries")))) |
135 | manifest))) |
136 | |
137 | (cdr |
138 | (fold |
139 | (lambda (test result) |
140 | (let* ((num (car result)) |
141 | (result (cdr result)) |
142 | (test-predicates (filter |
143 | (lambda (t) |
144 | (equal? (rdf-triple-subject t) test)) |
145 | manifest)) |
146 | (name (lexical->value (car (get-objects test-predicates "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#name")))) |
147 | (description (lexical->value (car (get-objects test-predicates "http://www.w3.org/2000/01/rdf-schema#comment"))))) |
148 | (cons (+ 1 num) |
149 | (cons (make-test-case test num name description |
150 | (filter |
151 | (lambda (t) |
152 | (equal? (rdf-triple-subject t) test)) |
153 | manifest) |
154 | #f #f) |
155 | result)))) |
156 | `(,num . ()) |
157 | tests))) |
158 | |
159 | (define (run-test-suite manifest expected-failures driver) |
160 | "Run a test suite described by @var{manifest}." |
161 | (let* ((plan (reverse (get-test-plan manifest)))) |
162 | ((test-driver-init driver) plan) |
163 | ((test-driver-finalize driver) (run-tests plan expected-failures driver)))) |
164 | |
165 | (define (run-test-suites manifests expected-failures driver) |
166 | "Run multiple test suites described by @var{manifests}." |
167 | (let* ((plan |
168 | (fold |
169 | (lambda (manifest plan) |
170 | (append plan (reverse (get-test-plan |
171 | manifest #:num (+ (length plan) 1))))) |
172 | '() |
173 | manifests))) |
174 | ((test-driver-init driver) plan) |
175 | ((test-driver-finalize driver) (run-tests plan expected-failures driver)))) |
176 |