Refactor and fix unit tests, plus isolate them better

This commit is contained in:
Henrik Lissner 2017-12-31 11:45:02 -05:00
parent 5c74814860
commit 8ad2666f8f
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
14 changed files with 146 additions and 117 deletions

View file

@ -74,17 +74,30 @@ If neither is available, run all tests in all enabled modules."
(defmacro def-test! (name &rest body)
"Define a namespaced ERT test."
(declare (indent defun) (doc-string 2))
(unless (plist-get body :disabled)
(when (plist-get body :skip)
(push '(ert-skip nil) body))
(let (plist)
(while (keywordp (car body))
(push (pop body) plist))
(setq plist (reverse plist))
(when (plist-get plist :skip)
(setq body `((ert-skip nil) ,@body)))
(when-let* ((modes (doom-enlist (plist-get plist :minor-mode))))
(dolist (mode modes)
(setq body `((with-minor-mode! ,mode ,@body)))))
(when-let* ((before (plist-get plist :before)))
(setq body `(,@before ,@body)))
(when-let* ((after (plist-get plist :after)))
(setq body `(,@body @after)))
`(ert-deftest
,(cl-loop with path = (file-relative-name (file-name-sans-extension load-file-name)
doom-emacs-dir)
for (rep . with) in '(("/test/" . "/") ("/" . ":"))
do (setq path (replace-regexp-in-string rep with path t t))
finally return (intern (format "%s::%s" path name))) ()
()
,@body)))
finally return (intern (format "%s::%s" path name)))
()
(with-temp-buffer
(save-mark-and-excursion
(save-window-excursion
,@body))))))
(defmacro should-buffer! (initial expected &rest body)
"Test that a buffer with INITIAL text, run BODY, then test it against EXPECTED.
@ -144,3 +157,10 @@ marker. e.g. {2} can be retrieved with (point! 2)."
((symbolp index) (symbol-name index))
((stringp index) index))
marker-list)))
(defmacro with-minor-mode! (mode &rest body)
"TODO"
(declare (indent defun))
`(progn (,mode +1)
,@body
(,mode -1)))

View file

@ -1,24 +1,23 @@
;; -*- no-byte-compile: t; -*-
;;; core/test/autoload-buffers.el
(defmacro -with-temp-buffers! (buffer-args &rest body)
(defmacro with-temp-buffers!! (buffer-args &rest body)
(declare (indent defun))
(let (buffers)
(dolist (bsym buffer-args)
(push `(,bsym (get-buffer-create ,(symbol-name bsym)))
buffers))
`(save-window-excursion
(cl-flet ((buffer-list
(lambda ()
(cl-remove-if-not #'buffer-live-p (list ,@(reverse (mapcar #'car buffers)))))))
(let* (persp-mode
,@buffers)
,@body
(mapc #'kill-buffer (buffer-list)))))))
`(cl-flet ((buffer-list
(lambda ()
(cl-remove-if-not #'buffer-live-p (list ,@(reverse (mapcar #'car buffers)))))))
(let* (persp-mode
,@buffers)
,@body
(mapc #'kill-buffer (buffer-list))))))
;;
(def-test! get-buffers
(-with-temp-buffers! (a b c)
(with-temp-buffers!! (a b c)
(should (cl-every #'buffer-live-p (buffer-list)))
(should (equal (buffer-list) (list a b c)))
(dolist (buf (list (cons a doom-emacs-dir)
@ -26,6 +25,7 @@
(cons c "/tmp/")))
(with-current-buffer (car buf)
(setq-local default-directory (cdr buf))))
(projectile-mode +1)
(with-current-buffer a
;; should produce all buffers
(let ((buffers (doom-buffer-list)))
@ -37,11 +37,12 @@
;; If no project is available, just get all buffers
(with-current-buffer c
(let ((buffers (doom-project-buffer-list)))
(should (cl-every (lambda (x) (memq x buffers)) (list a b c)))))))
(should (cl-every (lambda (x) (memq x buffers)) (list a b c)))))
(projectile-mode -1)))
(def-test! real-buffers
(let (doom-real-buffer-functions)
(-with-temp-buffers! (a b c d)
(with-temp-buffers!! (a b c d)
(dolist (buf (list a b))
(with-current-buffer buf
(setq-local buffer-file-name "x")))
@ -62,7 +63,7 @@
;; `doom-visible-buffers'
;; `doom-buried-buffers'
(def-test! visible-buffers-and-windows
(-with-temp-buffers! (a b c d)
(with-temp-buffers!! (a b c d)
(switch-to-buffer a)
(should (eq (current-buffer) a))
(should (eq (selected-window) (get-buffer-window a)))
@ -77,7 +78,7 @@
;; `doom-matching-buffers'
(def-test! matching-buffers
(-with-temp-buffers! (a b c)
(with-temp-buffers!! (a b c)
(let ((buffers (doom-matching-buffers "^[ac]$")))
(should (= 2 (length buffers)))
(should (cl-every #'bufferp buffers))
@ -86,7 +87,7 @@
;; `doom-buffers-in-mode'
(def-test! buffers-in-mode
(-with-temp-buffers! (a b c d e)
(with-temp-buffers!! (a b c d e)
(dolist (buf (list a b))
(with-current-buffer buf
(emacs-lisp-mode)))
@ -101,7 +102,7 @@
;; `doom-kill-buffer'
(def-test! kill-buffer
(-with-temp-buffers! (a b)
(with-temp-buffers!! (a b)
(doom-kill-buffer a)
(should-not (buffer-live-p a))
;; modified buffer
@ -112,7 +113,7 @@
;; `doom--cycle-real-buffers'
(def-test! kill-buffer-then-show-real-buffer
(-with-temp-buffers! (a b c d)
(with-temp-buffers!! (a b c d)
(dolist (buf (list a b d))
(with-current-buffer buf
(setq-local buffer-file-name "x")))

View file

@ -2,18 +2,16 @@
;;; core/test/autoload-debug.el
(def-test! what-face
(with-temp-buffer
(insert (propertize "Hello " 'face 'font-lock-keyword-face))
(insert "world")
(insert (propertize "Hello " 'face 'font-lock-keyword-face))
(insert "world")
(should (equal (doom/what-face (point-min)) '((font-lock-keyword-face) ())))
(should-not (doom/what-face (point-max)))))
(should (equal (doom/what-face (point-min)) '((font-lock-keyword-face) ())))
(should-not (doom/what-face (point-max))))
(def-test! what-face-overlays
(with-temp-buffer
(insert "Hello world")
(let ((ov (make-overlay 1 6)))
(overlay-put ov 'face 'font-lock-keyword-face))
(insert "Hello world")
(let ((ov (make-overlay 1 6)))
(overlay-put ov 'face 'font-lock-keyword-face))
(should (equal (doom/what-face (point-min)) '(() (font-lock-keyword-face))))
(should-not (doom/what-face (point-max)))))
(should (equal (doom/what-face (point-min)) '(() (font-lock-keyword-face))))
(should-not (doom/what-face (point-max))))

View file

@ -1,31 +1,21 @@
;; -*- no-byte-compile: t; -*-
;;; core/test/autoload-package.el
(defun -new-package (name version &optional reqs)
(defun -pkg (name version &optional reqs)
(package-desc-create :name name :version version :reqs reqs))
(defmacro -with-temp-packages! (&rest forms)
"Run FORMS in the context of a temporary package setup (as in, it won't
affects your Emacs packages)."
`(let* ((doom-local-dir ,(expand-file-name "test/.local/" doom-emacs-dir))
(doom-packages-dir (concat doom-local-dir "packages/"))
(doom-etc-dir (concat doom-local-dir "etc/"))
(doom-cache-dir (concat doom-local-dir "cache/"))
(package-user-dir (expand-file-name "elpa" doom-packages-dir))
(quelpa-dir (expand-file-name "quelpa" doom-packages-dir))
package-alist
package-archive-contents
package-initialize)
(package-initialize)
,@forms))
(defmacro -with-packages! (packages package-descs &rest body)
`(let ((doom-packages ,packages)
(defmacro with-packages!! (packages package-descs &rest body)
`(let* ((doom-packages-dir ,(expand-file-name "packages/" (file-name-directory load-file-name)))
(package-user-dir ,(expand-file-name "elpa" doom-packages-dir))
(quelpa-dir ,(expand-file-name "quelpa" doom-packages-dir)))
;; (make-directory doom-packages-dir t)
(let ((doom-packages ,packages)
(package-alist ,package-descs)
doom-core-packages)
(cl-letf (((symbol-function 'doom-initialize-packages) (lambda (&rest _)))
((symbol-function 'package-installed-p) (lambda (name &rest _) (assq name package-alist))))
,@body)))
(cl-letf (((symbol-function 'doom-initialize-packages) (lambda (&rest _))))
,@body))
;; (delete-directory doom-packages-dir t)
))
;;
@ -33,18 +23,19 @@ affects your Emacs packages)."
;;
(def-test! backend-detection
(let ((package-alist `((doom-dummy ,(-new-package 'doom-dummy '(20160405 1234)))))
(let ((package-alist `((doom-dummy ,(-pkg 'doom-dummy '(20160405 1234)))))
(quelpa-cache '((doom-quelpa-dummy :fetcher github :repo "hlissner/does-not-exist")))
(quelpa-initialized-p t))
(should (eq (doom-package-backend 'doom-dummy) 'elpa))
(should (eq (doom-package-backend 'doom-quelpa-dummy) 'quelpa))))
(should (eq (doom-package-backend 'doom-quelpa-dummy) 'quelpa))
(should (eq (doom-package-backend 'org) 'emacs))))
(def-test! elpa-outdated-detection
(let* ((doom--last-refresh (current-time))
(package-alist
`((doom-dummy ,(-new-package 'doom-dummy '(20160405 1234)))))
`((doom-dummy ,(-pkg 'doom-dummy '(20160405 1234)))))
(package-archive-contents
`((doom-dummy ,(-new-package 'doom-dummy '(20170405 1234))))))
`((doom-dummy ,(-pkg 'doom-dummy '(20170405 1234))))))
(cl-letf (((symbol-function 'package-refresh-contents) (lambda (&rest _))))
(should (equal (doom-package-outdated-p 'doom-dummy)
'(doom-dummy (20160405 1234) (20170405 1234)))))))
@ -53,7 +44,7 @@ affects your Emacs packages)."
(def-test! get-packages
(let ((quelpa-initialized-p t))
(-with-packages!
(with-packages!!
'((doom-dummy))
'((doom-dummy nil)
(doom-dummy-unwanted nil)
@ -63,17 +54,17 @@ affects your Emacs packages)."
(def-test! orphaned-packages
"Test `doom-get-orphaned-packages', which gets a list of packages that are
no longer enabled or depended on."
(-with-packages!
(with-packages!!
'((doom-dummy))
`((doom-dummy ,(-new-package 'doom-dummy '(20160405 1234) '((doom-dummy-dep (1 0)))))
(doom-dummy-unwanted ,(-new-package 'doom-dummy-unwanted '(20160601 1234)))
(doom-dummy-dep ,(-new-package 'doom-dummy-dep '(20160301 1234))))
`((doom-dummy ,(-pkg 'doom-dummy '(20160405 1234) '((doom-dummy-dep (1 0)))))
(doom-dummy-unwanted ,(-pkg 'doom-dummy-unwanted '(20160601 1234)))
(doom-dummy-dep ,(-pkg 'doom-dummy-dep '(20160301 1234))))
(should (equal (doom-get-orphaned-packages) '(doom-dummy-unwanted)))))
(def-test! missing-packages
"Test `doom-get-missing-packages, which gets a list of enabled packages that
aren't installed."
(-with-packages!
(with-packages!!
'((doom-dummy) (doom-dummy-installed))
`((doom-dummy-installed ,(-new-package 'doom-dummy-installed '(20160405 1234))))
`((doom-dummy-installed ,(-pkg 'doom-dummy-installed '(20160405 1234))))
(should (equal (doom-get-missing-packages) '((doom-dummy))))))

View file

@ -147,10 +147,13 @@
;; --- Settings ---------------------------
(def-setting! :-test-setting (x) x)
(def-test! set
(should (assq :-test-setting doom-settings))
(should (set! :-test-setting t))
(let ((inhibit-message t))
(should-not (set! :non-existant-setting (error "This shouldn't trigger")))))
(eval-and-compile
(let (doom-settings)
(def-setting! :-test-setting (x) `(setq result ,x))
(should (assq :-test-setting doom-settings))
(let ((inhibit-message t)
result)
(set! :-test-setting t)
(should result)
(set! :non-existant-setting (error "This shouldn't trigger"))))))

View file

@ -1,15 +1,20 @@
;; -*- no-byte-compile: t; -*-
;;; ../core/test/core-projects.el
(projectile-mode +1)
(require 'projectile)
;;
;; `doom-project-p'
(def-test! project-p
:minor-mode projectile-mode
(let ((default-directory doom-emacs-dir))
(should (doom-project-p)))
(let ((default-directory (expand-file-name "~")))
(should-not (doom-project-p))))
;; `doom-project-p'
(def-test! project-root
:minor-mode projectile-mode
;; Should resolve to project root
(let ((default-directory doom-core-dir))
(should (equal (doom-project-root) doom-emacs-dir)))
@ -17,12 +22,16 @@
(let ((default-directory (expand-file-name "~")))
(should (equal (doom-project-root) default-directory))))
;; `doom-project-expand'
(def-test! project-expand
:minor-mode projectile-mode
(let ((default-directory doom-core-dir))
(should (equal (doom-project-expand "init.el")
(expand-file-name "init.el" (doom-project-root))))))
;; `doom-project-has!'
(def-test! project-has!
:minor-mode projectile-mode
(let ((default-directory doom-core-dir))
;; Resolve from project root
(should (doom-project-has! "init.el"))
@ -32,4 +41,3 @@
(should (doom-project-has! (and "init.el" (or "LICENSE" "does-not-exist"))))
;; Should resolve relative paths from `default-directory'
(should (doom-project-has! (and "./core.el" "../init.el")))))

View file

@ -1,9 +1,9 @@
;; -*- no-byte-compile: t; -*-
;;; ../core/test/core-ui.el
(defmacro -with-temp-windows! (&rest body)
(defmacro with-temp-windows!! (&rest body)
(declare (indent defun))
`(save-window-excursion
`(progn
(delete-other-windows)
(cl-flet ((split-window (symbol-function #'split-window-horizontally)))
(let ((a (get-buffer-create "a"))
@ -17,15 +17,14 @@
(let ((doom-major-mode-names '((text-mode . "abc")
(lisp-mode . (lambda () "xyz"))
(js-mode . t))))
(with-temp-buffer
(text-mode)
(should (equal mode-name "abc"))
(lisp-mode)
(should (equal mode-name "xyz"))
(should-error (js-mode)))))
(text-mode)
(should (equal mode-name "abc"))
(lisp-mode)
(should (equal mode-name "xyz"))
(should-error (js-mode))))
(def-test! protect-visible-buffers
(-with-temp-windows!
(with-temp-windows!!
(let ((kill-buffer-query-functions '(doom|protect-visible-buffers)))
(switch-to-buffer a) (split-window)
(switch-to-buffer b) (split-window)
@ -35,7 +34,7 @@
(should (kill-buffer)))))
(def-test! *quit-window
(-with-temp-windows!
(with-temp-windows!!
(let (kill-buffer-query-functions)
(switch-to-buffer a) (split-window)
(switch-to-buffer b)