2024-06-23 14:56:19 -05:00
|
|
|
|
;;;; activity-servist/tests/activity-streams: Testing activity-streams.
|
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-06-23 14:56:19 -05:00
|
|
|
|
(defpackage :activity-servist/tests/activity-streams
|
2024-06-22 15:07:58 -05:00
|
|
|
|
(:use :cl :lisp-unit2)
|
2024-06-23 14:56:19 -05:00
|
|
|
|
(:nicknames "AS/T/AS")
|
2024-06-22 17:44:08 -05:00
|
|
|
|
(:export :run :run-with-summary))
|
2024-06-22 15:07:58 -05:00
|
|
|
|
|
2024-06-23 14:56:19 -05:00
|
|
|
|
(in-package :activity-servist/tests/activity-streams)
|
2024-06-22 15:07:58 -05:00
|
|
|
|
|
|
|
|
|
(defun run ()
|
2024-06-23 14:56:19 -05:00
|
|
|
|
"Run all ACTIVITY-STREAMS tests."
|
|
|
|
|
(lisp-unit2:run-tests :package :activity-servist/tests/activity-streams))
|
2024-06-22 17:44:08 -05:00
|
|
|
|
|
|
|
|
|
(defun run-with-summary ()
|
2024-06-23 14:56:19 -05:00
|
|
|
|
"Run tests with summary for ACTIVITY-STREAMS."
|
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-06-23 14:56:19 -05:00
|
|
|
|
`(asdf:system-relative-pathname :activity-servist/tests/activity-streams ,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-06-23 14:56:19 -05:00
|
|
|
|
(as/as: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-06-23 14:56:19 -05:00
|
|
|
|
(relative-pathname "t/activity-streams/activity-vocabulary/")))
|