From c4e9232f297ffda4443477c589f29052178d2c87 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Wed, 22 Oct 2025 12:27:05 -0500 Subject: feat: undead-buffers: Add `cj/make-buffer-undead` function and tests Introduce a new function `cj/make-buffer-undead` that appends a buffer name to the `cj/undead-buffer-list`, preventing it from being killed. This comes along with a suite of tests to check various scenarios and edge cases for handling undead buffers. Additionally, add tests for related functions: `cj/kill-buffer-or-bury-alive`, `cj/kill-buffer-and-window`, and others to ensure they correctly manage buffers, particularly with undead-status considerations. Refactor `undead-buffer-list` naming for consistency and clarity in the module. --- ...est-undead-buffers-kill-buffer-or-bury-alive.el | 138 +++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 tests/test-undead-buffers-kill-buffer-or-bury-alive.el (limited to 'tests/test-undead-buffers-kill-buffer-or-bury-alive.el') diff --git a/tests/test-undead-buffers-kill-buffer-or-bury-alive.el b/tests/test-undead-buffers-kill-buffer-or-bury-alive.el new file mode 100644 index 00000000..60b776e4 --- /dev/null +++ b/tests/test-undead-buffers-kill-buffer-or-bury-alive.el @@ -0,0 +1,138 @@ +;;; test-undead-buffers-kill-buffer-or-bury-alive.el --- Tests for cj/kill-buffer-or-bury-alive -*- lexical-binding: t; -*- + +;;; Commentary: +;; Tests for the cj/kill-buffer-or-bury-alive function from undead-buffers.el + +;;; Code: + +(require 'ert) +(require 'undead-buffers) +(require 'testutil-general) + +;;; Setup and Teardown + +(defun test-kill-buffer-or-bury-alive-setup () + "Setup for kill-buffer-or-bury-alive tests." + (cj/create-test-base-dir)) + +(defun test-kill-buffer-or-bury-alive-teardown () + "Teardown for kill-buffer-or-bury-alive tests." + (cj/delete-test-base-dir)) + +;;; Normal Cases + +(ert-deftest test-kill-buffer-or-bury-alive-regular-buffer-should-kill () + "Killing a regular buffer not in undead list should kill it." + (test-kill-buffer-or-bury-alive-setup) + (unwind-protect + (let ((buf (generate-new-buffer "*test-regular*"))) + (should (buffer-live-p buf)) + (cj/kill-buffer-or-bury-alive buf) + (should-not (buffer-live-p buf))) + (test-kill-buffer-or-bury-alive-teardown))) + +(ert-deftest test-kill-buffer-or-bury-alive-undead-buffer-should-bury () + "Killing an undead buffer should bury it instead." + (test-kill-buffer-or-bury-alive-setup) + (unwind-protect + (let ((orig (copy-sequence cj/undead-buffer-list)) + (buf (generate-new-buffer "*test-undead*"))) + (unwind-protect + (progn + (add-to-list 'cj/undead-buffer-list "*test-undead*") + (should (buffer-live-p buf)) + (cj/kill-buffer-or-bury-alive buf) + (should (buffer-live-p buf))) + (setq cj/undead-buffer-list orig) + (when (buffer-live-p buf) (kill-buffer buf)))) + (test-kill-buffer-or-bury-alive-teardown))) + +(ert-deftest test-kill-buffer-or-bury-alive-with-prefix-arg-should-add-to-undead-list () + "Calling with prefix arg should add buffer to undead list." + (test-kill-buffer-or-bury-alive-setup) + (unwind-protect + (let ((orig (copy-sequence cj/undead-buffer-list)) + (buf (generate-new-buffer "*test-prefix*"))) + (unwind-protect + (progn + (with-current-buffer buf + (let ((current-prefix-arg '(4))) + (cj/kill-buffer-or-bury-alive buf))) + (should (member "*test-prefix*" cj/undead-buffer-list)) + (should (buffer-live-p buf))) + (setq cj/undead-buffer-list orig) + (when (buffer-live-p buf) (kill-buffer buf)))) + (test-kill-buffer-or-bury-alive-teardown))) + +(ert-deftest test-kill-buffer-or-bury-alive-scratch-buffer-should-bury () + "The *scratch* buffer (in default list) should be buried." + (test-kill-buffer-or-bury-alive-setup) + (unwind-protect + (let ((scratch (get-buffer-create "*scratch*"))) + (should (buffer-live-p scratch)) + (cj/kill-buffer-or-bury-alive scratch) + (should (buffer-live-p scratch))) + (test-kill-buffer-or-bury-alive-teardown))) + +;;; Boundary Cases + +(ert-deftest test-kill-buffer-or-bury-alive-buffer-by-name-string-should-work () + "Passing buffer name as string should work." + (test-kill-buffer-or-bury-alive-setup) + (unwind-protect + (let ((buf (generate-new-buffer "*test-string*"))) + (should (buffer-live-p buf)) + (cj/kill-buffer-or-bury-alive "*test-string*") + (should-not (buffer-live-p buf))) + (test-kill-buffer-or-bury-alive-teardown))) + +(ert-deftest test-kill-buffer-or-bury-alive-buffer-by-buffer-object-should-work () + "Passing buffer object should work." + (test-kill-buffer-or-bury-alive-setup) + (unwind-protect + (let ((buf (generate-new-buffer "*test-object*"))) + (should (buffer-live-p buf)) + (cj/kill-buffer-or-bury-alive buf) + (should-not (buffer-live-p buf))) + (test-kill-buffer-or-bury-alive-teardown))) + +(ert-deftest test-kill-buffer-or-bury-alive-modified-undead-buffer-should-bury-without-prompt () + "Modified undead buffer should be buried without save prompt." + (test-kill-buffer-or-bury-alive-setup) + (unwind-protect + (let ((orig (copy-sequence cj/undead-buffer-list)) + (buf (generate-new-buffer "*test-modified*"))) + (unwind-protect + (progn + (add-to-list 'cj/undead-buffer-list "*test-modified*") + (with-current-buffer buf + (insert "some text") + (set-buffer-modified-p t)) + (cj/kill-buffer-or-bury-alive buf) + (should (buffer-live-p buf))) + (setq cj/undead-buffer-list orig) + (when (buffer-live-p buf) + (set-buffer-modified-p nil) + (kill-buffer buf)))) + (test-kill-buffer-or-bury-alive-teardown))) + +;;; Error Cases + +(ert-deftest test-kill-buffer-or-bury-alive-nonexistent-buffer-should-error () + "Passing a non-existent buffer name should error." + (test-kill-buffer-or-bury-alive-setup) + (unwind-protect + (should-error (cj/kill-buffer-or-bury-alive "*nonexistent-buffer-xyz*")) + (test-kill-buffer-or-bury-alive-teardown))) + +(ert-deftest test-kill-buffer-or-bury-alive-killed-buffer-object-should-error () + "Passing a killed buffer object should error." + (test-kill-buffer-or-bury-alive-setup) + (unwind-protect + (let ((buf (generate-new-buffer "*test-killed*"))) + (kill-buffer buf) + (should-error (cj/kill-buffer-or-bury-alive buf))) + (test-kill-buffer-or-bury-alive-teardown))) + +(provide 'test-undead-buffers-kill-buffer-or-bury-alive) +;;; test-undead-buffers-kill-buffer-or-bury-alive.el ends here -- cgit v1.2.3