diff options
Diffstat (limited to 't')
| -rw-r--r-- | t/tests.lisp | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/t/tests.lisp b/t/tests.lisp new file mode 100644 index 0000000..6baf5ed --- /dev/null +++ b/t/tests.lisp @@ -0,0 +1,140 @@ +;;; A small self-contained test runner, so the project needs nothing +;;; beyond ASDF/UIOP. + +(defpackage #:wallpaper-switcher/tests + (:use #:cl #:wallpaper-switcher) + (:export #:run-tests)) + +(in-package #:wallpaper-switcher/tests) + +(defvar *tests* '()) +(defvar *failures* 0) + +(defmacro deftest (name &body body) + `(progn + (defun ,name () ,@body) + (pushnew ',name *tests*) + ',name)) + +(defmacro check (form) + `(unless ,form + (incf *failures*) + (format t "~& FAIL: ~S~%" ',form))) + +(defmacro check-equal (expected form) + (let ((e (gensym)) (v (gensym))) + `(let ((,e ,expected) (,v ,form)) + (unless (equal ,e ,v) + (incf *failures*) + (format t "~& FAIL: ~S~% expected ~S~% got ~S~%" ',form ,e ,v))))) + +(defun run-tests () + (let ((*failures* 0)) + (dolist (test (reverse *tests*)) + (format t "~&~A~%" test) + (handler-case (funcall test) + (error (e) + (incf *failures*) + (format t " ERROR: ~A~%" e)))) + (format t "~&~D test~:P, ~D failure~:P~%" (length *tests*) *failures*) + (zerop *failures*))) + +(defun call-with-temp-dir (fn) + (let ((dir (uiop:ensure-directory-pathname + (format nil "~Awallpaper-switcher-test-~36R/" + (namestring (uiop:temporary-directory)) + (random (expt 36 8)))))) + (ensure-directories-exist dir) + (unwind-protect (funcall fn dir) + (uiop:delete-directory-tree dir :validate t)))) + +(defmacro with-temp-dir ((var) &body body) + `(call-with-temp-dir (lambda (,var) ,@body))) + +(defun touch (dir name) + (with-open-file (s (merge-pathnames name dir) :direction :output + :if-does-not-exist :create) + (declare (ignorable s)))) + +;;; Tests + +(deftest test-shell-quote + (check-equal "'plain'" (shell-quote "plain")) + (check-equal "'with space'" (shell-quote "with space")) + (check-equal "'it'\\''s'" (shell-quote "it's")) + (check-equal "''" (shell-quote ""))) + +(deftest test-xml-escape + (check-equal "a & b <c> "d" 'e'" + (xml-escape "a & b <c> \"d\" 'e'"))) + +(deftest test-menu-label + (check-equal "sunset" (menu-label #p"/x/sunset.png")) + (check-equal "my__wall" (menu-label #p"/x/my_wall.webp"))) + +(deftest test-expand-path + (let ((home (string-right-trim "/" (namestring (user-homedir-pathname))))) + (check-equal (concatenate 'string home "/.wallpapers") (expand-path "~/.wallpapers")) + (check-equal (concatenate 'string home "/pics") (expand-path "$HOME/pics")) + (check-equal (concatenate 'string home "/pics") (expand-path "${HOME}/pics")) + (check-equal "/abs/path" (expand-path "/abs/path")))) + +(deftest test-parse-config + (let ((c (parse-config-string + "# comment + +directory = /srv/walls +command = feh --bg-fill %f +extensions = .PNG, jpg webp"))) + (check-equal "/srv/walls" (config-directory c)) + (check-equal "feh --bg-fill %f" (config-command c)) + (check-equal '("png" "jpg" "webp") (config-extensions c)))) + +(deftest test-parse-config-warns-on-unknown-key + (let ((warned nil)) + (handler-bind ((warning (lambda (w) (setf warned t) (muffle-warning w)))) + (parse-config-string "colour = blue")) + (check warned))) + +(deftest test-config-defaults + (let ((c (default-config))) + (check-equal "~/.wallpapers" (config-directory c)) + (check-equal '("png" "webp") (config-extensions c)) + (check (search "swaybg" (config-command c))))) + +(deftest test-build-command + (check-equal "sh -c 'swaybg -i '\\''/w/a b.png'\\'' 100%'" + (build-command "swaybg -i %f 100%%" #p"/w/a b.png"))) + +(deftest test-find-wallpapers + (with-temp-dir (dir) + (dolist (n '("b.webp" "A.png" "c.PNG" "notes.txt" "noext")) + (touch dir n)) + (ensure-directories-exist (merge-pathnames "sub/" dir)) + (touch (merge-pathnames "sub/" dir) "nested.png") + (check-equal '("A.png" "b.webp" "c.PNG") + (mapcar #'file-namestring + (find-wallpapers (namestring dir) '("png" "webp")))))) + +(deftest test-find-wallpapers-missing-dir + (check-equal nil (find-wallpapers "/nonexistent/wallpaper-switcher/" '("png")))) + +(deftest test-render-menu + (with-temp-dir (dir) + (touch dir "tom & jerry.png") + (let ((xml (with-output-to-string (s) + (render-menu (make-config :directory (namestring dir) + :command "swaybg -i %f") + s)))) + (check (search "<openbox_pipe_menu>" xml)) + (check (search "label=\"tom & jerry\"" xml)) + (check (search "<action name=\"Execute\"><command>sh -c 'swaybg -i '\\''" + xml)) + (check (search "</openbox_pipe_menu>" xml))))) + +(deftest test-render-menu-empty + (with-temp-dir (dir) + (let ((xml (with-output-to-string (s) + (render-menu (make-config :directory (namestring dir)) s)))) + (check (search "<separator label=\"No wallpapers in" xml)) + (check (not (search "<item" xml)))))) |
