Rewrite buffer tests; refactor doom-kill-buffer
This commit is contained in:
parent
7b5d2063f8
commit
4d487c3e0f
2 changed files with 113 additions and 53 deletions
|
@ -155,30 +155,35 @@ popup window/buffer and b) isn't a special buffer."
|
||||||
buffer, but only bury the buffer if it is present in another window.
|
buffer, but only bury the buffer if it is present in another window.
|
||||||
|
|
||||||
See `doom-real-buffer-p' for what 'real' means."
|
See `doom-real-buffer-p' for what 'real' means."
|
||||||
(let* ((buffer (or buffer (current-buffer)))
|
(setq buffer (or buffer (current-buffer)))
|
||||||
(buffer-win (get-buffer-window buffer))
|
(when (and (bufferp buffer) (buffer-live-p buffer))
|
||||||
(only-buffer-window-p (= 1 (length (get-buffer-window-list buffer nil t)))))
|
(let ((buffer-win (get-buffer-window buffer))
|
||||||
;; deal with unsaved buffers
|
(only-buffer-window-p (= 1 (length (get-buffer-window-list buffer nil t)))))
|
||||||
(when (and only-buffer-window-p
|
;; deal with unsaved buffers
|
||||||
(buffer-file-name buffer)
|
(when (and only-buffer-window-p
|
||||||
(buffer-modified-p buffer))
|
(buffer-file-name buffer)
|
||||||
(with-current-buffer buffer
|
(buffer-modified-p buffer))
|
||||||
(if (and (not dont-save)
|
(with-current-buffer buffer
|
||||||
(yes-or-no-p "Buffer is unsaved, save it?"))
|
(if (and (not dont-save)
|
||||||
(save-buffer)
|
(yes-or-no-p "Buffer is unsaved, save it?"))
|
||||||
(set-buffer-modified-p nil))))
|
(save-buffer)
|
||||||
;; deal with dedicated windows
|
(set-buffer-modified-p nil))))
|
||||||
(if (window-dedicated-p buffer-win)
|
;; deal with dedicated windows
|
||||||
(unless (window--delete buffer-win t t)
|
(if buffer-win
|
||||||
(split-window buffer-win)
|
(if (window-dedicated-p buffer-win)
|
||||||
(window--delete buffer-win t t))
|
(unless (window--delete buffer-win t t)
|
||||||
;; cycle to a real buffer
|
(split-window buffer-win)
|
||||||
(doom--cycle-real-buffers -1)
|
(window--delete buffer-win t t))
|
||||||
(when buffer-win
|
;; cycle to a real buffer
|
||||||
(unrecord-window-buffer buffer-win buffer))
|
(with-selected-window buffer-win
|
||||||
(when only-buffer-window-p
|
(doom--cycle-real-buffers -1)
|
||||||
(kill-buffer buffer)))
|
(when buffer-win
|
||||||
(eq (current-buffer) buffer)))
|
(unrecord-window-buffer buffer-win buffer))
|
||||||
|
(when only-buffer-window-p
|
||||||
|
(kill-buffer buffer)))
|
||||||
|
(not (eq (current-buffer) buffer)))
|
||||||
|
(kill-buffer buffer)
|
||||||
|
(not (buffer-live-p buffer))))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun doom-force-kill-buffer (&optional buffer dont-save)
|
(defun doom-force-kill-buffer (&optional buffer dont-save)
|
||||||
|
@ -187,7 +192,7 @@ switched to a real buffer."
|
||||||
(interactive)
|
(interactive)
|
||||||
(let* ((buffer (or buffer (current-buffer)))
|
(let* ((buffer (or buffer (current-buffer)))
|
||||||
(windows (get-buffer-window-list buffer nil t)))
|
(windows (get-buffer-window-list buffer nil t)))
|
||||||
(kill-buffer buffer)
|
(doom-kill-buffer buffer dont-save)
|
||||||
(dolist (win windows)
|
(dolist (win windows)
|
||||||
(with-selected-window win
|
(with-selected-window win
|
||||||
(unless (doom-real-buffer-p)
|
(unless (doom-real-buffer-p)
|
||||||
|
|
|
@ -1,36 +1,91 @@
|
||||||
;;; test/core/autoload/test-buffers.el
|
;;; test/core/autoload/test-buffers.el
|
||||||
|
|
||||||
|
(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))
|
||||||
|
`(let* (,@buffers
|
||||||
|
(buffer-list (list ,@(reverse (mapcar #'car buffers)))))
|
||||||
|
,@body
|
||||||
|
(mapc #'kill-buffer buffer-list))))
|
||||||
|
|
||||||
(def-test-group! core/autoload/buffers
|
(def-test-group! core/autoload/buffers
|
||||||
(ert-deftest get-buffers ()
|
(ert-deftest get-buffers ()
|
||||||
(let ((a (get-buffer-create "*a*"))
|
(with-temp-buffers! (a b c)
|
||||||
(b (get-buffer-create "*b*"))
|
(should (cl-every #'buffer-live-p buffer-list))
|
||||||
(c (get-buffer-create "*c*"))
|
(should (equal buffer-list (list a b c)))
|
||||||
(buffers (doom-buffer-list)))
|
(dolist (buf (list (cons a doom-emacs-dir)
|
||||||
(should buffers)
|
(cons b doom-emacs-dir)
|
||||||
(should (cl-every 'bufferp buffers))
|
(cons c "/tmp/")))
|
||||||
(should (cl-every (lambda (b) (memq b buffers)) (list a b c)))))
|
(with-current-buffer (car buf)
|
||||||
|
(setq-local default-directory (cdr buf))))
|
||||||
|
(with-current-buffer a
|
||||||
|
;; should produce all buffers
|
||||||
|
(let ((buffers (doom-buffer-list)))
|
||||||
|
(should (cl-every (lambda (x) (memq x buffers)) (list a b c))))
|
||||||
|
;; should produce only project buffers
|
||||||
|
(let ((buffers (doom-buffer-list t)))
|
||||||
|
(should (cl-every (lambda (x) (memq x buffers)) (list a b)))
|
||||||
|
(should-not (memq c buffers))))
|
||||||
|
;; If no project is available, just get all buffers
|
||||||
|
(with-current-buffer c
|
||||||
|
(let ((buffers (doom-buffer-list t)))
|
||||||
|
(should (cl-every (lambda (x) (memq x buffers)) (list a b c)))))))
|
||||||
|
|
||||||
|
(ert-deftest get-real-buffers ()
|
||||||
|
(with-temp-buffers! (a b c d)
|
||||||
|
(with-current-buffer c
|
||||||
|
(rename-buffer "*C*"))
|
||||||
|
(with-current-buffer d
|
||||||
|
(doom-popup-mode +1))
|
||||||
|
(let ((buffers (doom-real-buffers-list buffer-list)))
|
||||||
|
(should (= (length buffers) 2))
|
||||||
|
(should (cl-every (lambda (x) (memq x buffers)) (list a b)))
|
||||||
|
(should (cl-notany (lambda (x) (memq x buffers)) (list c d))))))
|
||||||
|
|
||||||
|
(ert-deftest kill-buffers ()
|
||||||
|
(with-temp-buffers! (a b)
|
||||||
|
(doom-kill-buffer a)
|
||||||
|
(should-not (buffer-live-p a))
|
||||||
|
;; modified buffer
|
||||||
|
(with-current-buffer b
|
||||||
|
(set-buffer-modified-p t))
|
||||||
|
(doom-kill-buffer b t)
|
||||||
|
(should-not (buffer-live-p a))))
|
||||||
|
|
||||||
|
(ert-deftest kill-buffer-then-show-real-buffer ()
|
||||||
|
(with-temp-buffers! (a b c)
|
||||||
|
(should (cl-every #'buffer-live-p buffer-list))
|
||||||
|
(switch-to-buffer a)
|
||||||
|
(should (eq (current-buffer) a))
|
||||||
|
(should (eq (selected-window) (get-buffer-window a)))
|
||||||
|
(should (doom-kill-buffer a))
|
||||||
|
(should (eq (current-buffer) b))
|
||||||
|
(should (doom-kill-buffer))
|
||||||
|
(should (eq (current-buffer) c))
|
||||||
|
(doom/kill-this-buffer)
|
||||||
|
(should (eq (current-buffer) (doom-fallback-buffer)))))
|
||||||
|
|
||||||
(ert-deftest matching-buffers ()
|
(ert-deftest matching-buffers ()
|
||||||
(let ((a (get-buffer-create "*a*"))
|
(with-temp-buffers! (a b c)
|
||||||
(b (get-buffer-create "*b*"))
|
(let ((buffers (doom-matching-buffers "^[ac]$")))
|
||||||
(c (get-buffer-create "*c*"))
|
(should (= 2 (length buffers)))
|
||||||
(buffers (doom-matching-buffers "^\\*[ac]\\*$")))
|
(should (cl-every #'bufferp buffers))
|
||||||
(should (= 2 (length buffers)))
|
(should (cl-every (lambda (x) (memq x buffers)) (list a c)))
|
||||||
(should (cl-every 'bufferp buffers))
|
(should (equal (reverse buffers) (doom-matching-buffers "^[ac]$" buffer-list))))))
|
||||||
(should (cl-every (lambda (b) (memq b buffers)) (list a c)))))
|
|
||||||
|
|
||||||
(ert-deftest buffers-in-mode ()
|
(ert-deftest buffers-in-mode ()
|
||||||
(dolist (name (list "*a*" "*b*"))
|
(with-temp-buffers! (a b c d e)
|
||||||
(with-current-buffer (get-buffer-create name)
|
(dolist (buf (list a b))
|
||||||
(emacs-lisp-mode)))
|
(with-current-buffer buf
|
||||||
(dolist (name (list "*c*" "*d*" "*e*"))
|
(emacs-lisp-mode)))
|
||||||
(with-current-buffer (get-buffer-create name)
|
(dolist (buf (list c d e))
|
||||||
(text-mode)))
|
(with-current-buffer buf
|
||||||
(let ((el-buffers (doom-buffers-in-mode 'emacs-lisp-mode))
|
(text-mode)))
|
||||||
(txt-buffers (doom-buffers-in-mode 'text-mode)))
|
(let ((el-buffers (doom-buffers-in-mode 'emacs-lisp-mode))
|
||||||
(should (cl-every 'bufferp (append el-buffers txt-buffers)))
|
(txt-buffers (doom-buffers-in-mode 'text-mode)))
|
||||||
(should (= 2 (length el-buffers)))
|
(should (cl-every #'buffer-live-p (append el-buffers txt-buffers)))
|
||||||
(should (= 3 (length txt-buffers)))))
|
(should (= 2 (length el-buffers)))
|
||||||
|
(should (= 3 (length txt-buffers)))))))
|
||||||
;; TODO
|
|
||||||
)
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue