2024-09-03 12:09:18 -05:00
|
|
|
|
;;;; activity-servist/tests/vocab/activity: Testing activity-vocabulary.
|
2024-06-22 15:07:58 -05:00
|
|
|
|
|
|
|
|
|
;; Copyright © 2024 Jaidyn Levesque <jadedctrl@posteo.at>
|
|
|
|
|
;;
|
|
|
|
|
;; This program is free software: you can redistribute it and/or
|
|
|
|
|
;; modify it under the terms of the GNU Affero General Public License
|
|
|
|
|
;; as published by the Free Software Foundation, either version 3 of
|
|
|
|
|
;; the License, or (at your option) any later version.
|
|
|
|
|
;;
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU Affero General Public License for more details.
|
|
|
|
|
;;
|
|
|
|
|
;; You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
2024-09-03 12:09:18 -05:00
|
|
|
|
(defpackage :activity-servist/tests/vocab/activity
|
2024-06-22 15:07:58 -05:00
|
|
|
|
(:use :cl :lisp-unit2)
|
2024-09-04 15:11:43 -05:00
|
|
|
|
(:nicknames "AS/T/V/A")
|
2024-06-22 17:44:08 -05:00
|
|
|
|
(:export :run :run-with-summary))
|
2024-06-22 15:07:58 -05:00
|
|
|
|
|
2024-09-03 12:09:18 -05:00
|
|
|
|
(in-package :activity-servist/tests/vocab/activity)
|
2024-06-22 15:07:58 -05:00
|
|
|
|
|
|
|
|
|
(defun run ()
|
2024-08-21 23:37:11 -05:00
|
|
|
|
"Run all ACTIVITY-VOCABULARY tests."
|
2024-08-24 15:01:24 -05:00
|
|
|
|
(let ((json-ld:*default-json-type* "https://www.w3.org/ns/activitystreams#Object"))
|
2024-09-03 12:09:18 -05:00
|
|
|
|
(lisp-unit2:run-tests :package :activity-servist/tests/vocab/activity)))
|
2024-06-22 17:44:08 -05:00
|
|
|
|
|
|
|
|
|
(defun run-with-summary ()
|
2024-08-21 23:37:11 -05:00
|
|
|
|
"Run tests with summary for ACTIVITY-VOCABULARY."
|
2024-06-22 17:44:08 -05:00
|
|
|
|
(lisp-unit2:with-summary()
|
|
|
|
|
(run)))
|
2024-06-22 15:07:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Util
|
|
|
|
|
;;; ————————————————————————————————————————
|
|
|
|
|
(defmacro relative-pathname (path)
|
|
|
|
|
"Return an absolute path adding the relative PATH to the system’s path."
|
2024-09-03 12:09:18 -05:00
|
|
|
|
`(asdf:system-relative-pathname :activity-servist/tests/vocab/activity ,path))
|
2024-06-22 15:07:58 -05:00
|
|
|
|
|
|
|
|
|
(defmacro define-json-test (path tags)
|
|
|
|
|
"Define a lisp-unit2 test for parsing of the given JSON file.
|
|
|
|
|
We compare the original JSON to that of the parsed-then-reserialized JSON,
|
|
|
|
|
ensuring they are semantically equivalent. White-space and key order are ignored."
|
|
|
|
|
(let ((content (alexandria:read-file-into-string (relative-pathname path))))
|
2024-06-23 21:02:16 -05:00
|
|
|
|
`(define-test ,(intern (format nil "EX-~A" (string-upcase (pathname-name path))))
|
|
|
|
|
(:tags ,tags)
|
2024-06-22 15:07:58 -05:00
|
|
|
|
(assert-equal
|
|
|
|
|
(hash-table-sorted-alist
|
|
|
|
|
(yason:parse ,content))
|
|
|
|
|
(hash-table-sorted-alist
|
|
|
|
|
(yason:parse
|
|
|
|
|
(yason:with-output-to-string* ()
|
|
|
|
|
(yason:encode-object
|
2024-08-21 23:37:11 -05:00
|
|
|
|
(json-ld:parse ,content)))))))))
|
2024-06-22 15:07:58 -05:00
|
|
|
|
|
|
|
|
|
(defun sort-alist (alist predicate)
|
|
|
|
|
"Sort an associative list by its keys."
|
|
|
|
|
(sort alist
|
|
|
|
|
(lambda (cell-a cell-b)
|
|
|
|
|
(apply predicate (list (car cell-a) (car cell-b))))))
|
|
|
|
|
|
2024-06-23 12:18:41 -05:00
|
|
|
|
(defun ensure-list (item)
|
|
|
|
|
"Ensure ITEM is either a list or the sole member of a new list."
|
|
|
|
|
(if (listp item)
|
|
|
|
|
item
|
|
|
|
|
(list item)))
|
|
|
|
|
|
2024-06-22 15:07:58 -05:00
|
|
|
|
(defun hash-table-sorted-alist (table &optional (predicate #'string<))
|
|
|
|
|
"Return a sorted associative list containing the keys and values of TABLE.
|
|
|
|
|
Any nested hash-tables found as values are also sorted, recursively."
|
|
|
|
|
(sort-alist
|
|
|
|
|
(mapcar (lambda (cell)
|
|
|
|
|
(cons (car cell)
|
2024-06-23 12:18:41 -05:00
|
|
|
|
(mapcar (lambda (cell-item)
|
|
|
|
|
(if (hash-table-p cell-item)
|
|
|
|
|
(hash-table-sorted-alist cell-item)
|
|
|
|
|
cell-item))
|
|
|
|
|
(ensure-list (cdr cell)))))
|
2024-06-22 15:07:58 -05:00
|
|
|
|
(alexandria:hash-table-alist table))
|
|
|
|
|
predicate))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Test definitions
|
|
|
|
|
;;; ————————————————————————————————————————
|
|
|
|
|
;; Define a test for each ActivityVocabulary type’s example JSON.
|
|
|
|
|
;; Examples are taken from the spec:
|
|
|
|
|
;; https://www.w3.org/TR/activitystreams-vocabulary/
|
|
|
|
|
(mapcar (lambda (file)
|
2024-06-23 14:56:19 -05:00
|
|
|
|
(eval `(define-json-test ,file '(:activity-vocabulary))))
|
2024-06-22 15:07:58 -05:00
|
|
|
|
(uiop:directory-files
|
2024-09-03 12:09:18 -05:00
|
|
|
|
(relative-pathname "t/vocab/activity/")))
|