guile-rdf/test-modules/online.scm

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-node
127
    (rdf-triple-object
128
      (car
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
  (define (find-rest node)
138
    (pk 'rest node)
139
    (rdf-triple-object
140
      (car
141
        (filter
142
          (lambda (triple)
143
            (and (equal? (rdf-triple-subject triple) node)
144
                 (equal? (rdf-triple-predicate triple)
145
                         (string-append "http://www.w3.org/1999/02/22-rdf-syntax-ns"
146
                                        "#rest"))))
147
          manifest))))
148
149
  (define (find-first node)
150
    (rdf-triple-object
151
      (car
152
        (filter
153
          (lambda (triple)
154
            (and (equal? (rdf-triple-subject triple) node)
155
                 (equal? (rdf-triple-predicate triple)
156
                         (string-append "http://www.w3.org/1999/02/22-rdf-syntax-ns"
157
                                        "#first"))))
158
          manifest))))
159
160
  (define tests
161
    (let loop ((tests-node tests-node) (tests '()))
162
      (let ((first (find-first tests-node))
163
            (tests-node (find-rest tests-node)))
164
        (if (blank-node? tests-node)
165
            (loop tests-node (cons first tests))
166
            tests))))
167
168
  (cdr
169
    (fold
170
      (lambda (test result)
171
        (let* ((num (car result))
172
               (result (cdr result))
173
               (test-predicates (filter
174
                                  (lambda (t)
175
                                    (equal? (rdf-triple-subject t) test))
176
                                  manifest))
177
               (name (lexical->value (car (get-objects test-predicates "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#name"))))
178
               (description (lexical->value (car (get-objects test-predicates "http://www.w3.org/2000/01/rdf-schema#comment")))))
179
          (cons (+ 1 num)
180
                (cons (make-test-case test num name description
181
                                      (filter
182
                                        (lambda (t)
183
                                          (equal? (rdf-triple-subject t) test))
184
                                        manifest)
185
                                      #f #f)
186
                      result))))
187
      `(,num . ())
188
      tests)))
189
190
(define (run-test-suite manifest expected-failures driver)
191
  "Run a test suite described by @var{manifest}."
192
  (let* ((plan (reverse (get-test-plan manifest))))
193
    ((test-driver-init driver) plan)
194
    ((test-driver-finalize driver) (run-tests plan expected-failures driver))))
195
196
(define (run-test-suites manifests expected-failures driver)
197
  "Run multiple test suites described by @var{manifests}."
198
  (let* ((plan
199
           (fold
200
             (lambda (manifest plan)
201
               (append plan (reverse (get-test-plan
202
                                       manifest #:num (+ (length plan) 1)))))
203
             '()
204
             manifests)))
205
    ((test-driver-init driver) plan)
206
    ((test-driver-finalize driver) (run-tests plan expected-failures driver))))
207