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 (ice-9 binary-ports) |
| 21 | #:use-module (nquads tordf) |
| 22 | #:use-module ((rdf entailment rdf) #:prefix rdf:) |
| 23 | #:use-module ((rdf entailment rdfs) #:prefix rdfs:) |
| 24 | #:use-module ((rdf entailment simple) #:prefix simple:) |
| 25 | #:use-module (rdf rdf) |
| 26 | #:use-module ((rdf xsd) #:prefix xsd:) |
| 27 | #:use-module (rnrs bytevectors) |
| 28 | #:use-module (srfi srfi-64) |
| 29 | #:use-module (test-modules test-case) |
| 30 | #:use-module (turtle tordf) |
| 31 | #:use-module (web client) |
| 32 | #:use-module (web response) |
| 33 | #:export (run-test-suite)) |
| 34 | |
| 35 | |
| 36 | (define (run-test test) |
| 37 | (define (entailment-test action-doc action regime vocabulary expected-doc) |
| 38 | "Run an entailment test." |
| 39 | (let ((result (turtle->rdf action-doc action)) |
| 40 | (entails? (match regime |
| 41 | ("simple" simple:entails?) |
| 42 | ("RDF" |
| 43 | (lambda (g e) |
| 44 | (rdf:entails? g e vocabulary))) |
| 45 | ("RDFS" |
| 46 | (lambda (g e) |
| 47 | (rdfs:entails? g e vocabulary))) |
| 48 | (_ (throw 'unknown-regime regime)))) |
| 49 | (consistent-graph? |
| 50 | (match regime |
| 51 | ("simple" simple:consistent-graph?) |
| 52 | ("RDF" (lambda (g) |
| 53 | (rdf:consistent-graph? g vocabulary))) |
| 54 | ("RDFS" (lambda (g) |
| 55 | (rdfs:consistent-graph? g vocabulary)))))) |
| 56 | (if expected-doc |
| 57 | (entails? result expected-doc) |
| 58 | (not (consistent-graph? result))))) |
| 59 | |
| 60 | (match test |
| 61 | (($ test-case type name regime recognized unrecognized action result) |
| 62 | (let ((action-doc (get-test-doc action)) |
| 63 | (vocabulary (make-rdf-vocabulary |
| 64 | (cons* xsd:string rdf:langString recognized) |
| 65 | xsd:order xsd:compatible?)) |
| 66 | (expected-doc (if result |
| 67 | (turtle->rdf (get-test-doc result) result) |
| 68 | #f))) |
| 69 | (match type |
| 70 | ("TestTurtlePositiveSyntax" |
| 71 | (test-assert name (turtle->rdf action-doc action))) |
| 72 | ("TestTurtleNegativeSyntax" |
| 73 | (test-error name #t (turtle->rdf action-doc action))) |
| 74 | ("TestTurtleNegativeEval" |
| 75 | (test-error name #t (turtle->rdf action-doc action))) |
| 76 | ("TestTurtleEval" |
| 77 | (test-assert name (rdf-isomorphic? (turtle->rdf action-doc action) |
| 78 | expected-doc))) |
| 79 | ("TestNQuadsPositiveSyntax" |
| 80 | (test-assert name (nquads->rdf action-doc))) |
| 81 | ("TestNQuadsNegativeSyntax" |
| 82 | (test-error name #t (nquads->rdf action-doc))) |
| 83 | ("PositiveEntailmentTest" |
| 84 | (test-assert name (entailment-test action-doc action regime vocabulary |
| 85 | expected-doc))) |
| 86 | ("NegativeEntailmentTest" |
| 87 | (test-assert name |
| 88 | (not (entailment-test action-doc action regime vocabulary |
| 89 | expected-doc)))) |
| 90 | (_ (throw 'unrecognized-type type))))))) |
| 91 | |
| 92 | (define (get-test-doc url) |
| 93 | "Get a test suite object from the manifest at @var{url}." |
| 94 | |
| 95 | (define cache-filename |
| 96 | (string-append "test-files/" (substring url |
| 97 | (string-length "http://www.w3.org/2013/")))) |
| 98 | |
| 99 | (if (file-exists? cache-filename) |
| 100 | (let ((bv (call-with-input-file cache-filename get-bytevector-all))) |
| 101 | (if (eof-object? bv) |
| 102 | "" |
| 103 | (utf8->string bv))) |
| 104 | (call-with-values |
| 105 | (lambda () |
| 106 | (http-get url)) |
| 107 | (lambda (hdr body) |
| 108 | (if (equal? (response-code hdr) 200) |
| 109 | (if (string? body) |
| 110 | body |
| 111 | (utf8->string body)) |
| 112 | (throw 'error-fetching-test-manifest (response-code hdr))))))) |
| 113 | |
| 114 | (define (run-test-suite manifest expected-failures name) |
| 115 | (test-begin name) |
| 116 | (let ((plan (rdf->test-plan |
| 117 | (turtle->rdf (get-test-doc manifest) manifest) |
| 118 | manifest))) |
| 119 | (for-each run-test plan) |
| 120 | (test-end name))) |
| 121 |