Subject: [PATCH] ert-x: Refactor `ert-with-test-buffer-selected'

From: Richard Hansen <[email-masked]>
Date: Mon, 19 Dec 2022 00:22:19 -0500

I think the attached patches can go on emacs-29 because 
`ert-with-test-buffer-selected' was added in 29.

Patch 1:

     ; ert-x: Add test for buffer read-only state

     This test should have been included with commit
     29b7d740006fe2190a729bd1c30ccab9356cee36.

Patch 2:

     ; ert-x: Simplify `ert-with-test-buffer-selected'

Patch 3:

     ert-x: Move window selection logic to its own macro

     * lisp/emacs-lisp/ert-x.el (ert-with-buffer-selected): New macro to
     temporarily display a buffer in a selected window and evaluate a body.
     (ert-with-test-buffer-selected): Use the new macro.
     * test/lisp/whitespace-tests.el (ert-test-with-buffer-selected/*): Add
     tests.
From 25540c6d797b57e2c3514592fc3c0be87b80166a Mon Sep 17 00:00:00 2001
From: Richard Hansen <[email-masked]>
Date: Sat, 17 Dec 2022 18:26:33 -0500
Subject: [PATCH 1/3] ; ert-x: Add test for buffer read-only state

This test should have been included with commit
29b7d740006fe2190a729bd1c30ccab9356cee36.
---
 test/lisp/emacs-lisp/ert-x-tests.el | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/test/lisp/emacs-lisp/ert-x-tests.el b/test/lisp/emacs-lisp/ert-x-tests.el
index 63e7cd7608..f14d54cd9f 100644
--- a/test/lisp/emacs-lisp/ert-x-tests.el
+++ b/test/lisp/emacs-lisp/ert-x-tests.el
@@ -90,6 +90,11 @@ ert-test-with-test-buffer-selected/modification-hooks
   (ert-with-test-buffer-selected ()
     (should (null inhibit-modification-hooks))))
 
+(ert-deftest ert-test-with-test-buffer-selected/read-only ()
+  (ert-with-test-buffer-selected ()
+    (should (null inhibit-read-only))
+    (should (null buffer-read-only))))
+
 (ert-deftest ert-test-with-test-buffer-selected/return-value ()
   (should (equal (ert-with-test-buffer-selected () "foo") "foo")))
 
-- 
2.39.0

From 81ceb7a8c6678f30b0b7adf06452a1da5eb0f505 Mon Sep 17 00:00:00 2001
From: Richard Hansen <[email-masked]>
Date: Sat, 17 Dec 2022 18:18:39 -0500
Subject: [PATCH 2/3] ; ert-x: Simplify `ert-with-test-buffer-selected'

---
 lisp/emacs-lisp/ert-x.el | 28 +++++-----------------------
 1 file changed, 5 insertions(+), 23 deletions(-)

diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el
index 49f2a1d696..5f1c5c26ac 100644
--- a/lisp/emacs-lisp/ert-x.el
+++ b/lisp/emacs-lisp/ert-x.el
@@ -115,29 +115,11 @@ ert-with-test-buffer-selected
 simulate user interaction.  The window configuration is restored
 before returning, even if BODY exits nonlocally.  The return
 value is the last form in BODY."
-  (declare (debug ((":name" form) def-body))
-           (indent 1))
-  (let ((ret (make-symbol "ert--with-test-buffer-selected-ret")))
-    `(save-window-excursion
-       (let (,ret)
-         (ert-with-test-buffer (:name ,name)
-           (with-current-buffer-window (current-buffer)
-               `(display-buffer-below-selected
-                 (body-function
-                  . ,(lambda (window)
-                       (select-window window t)
-                       ;; body-function is intended to initialize the
-                       ;; contents of a temporary read-only buffer, so
-                       ;; it is executed with some convenience
-                       ;; changes.  Undo those changes so that the
-                       ;; test buffer behaves more like an ordinary
-                       ;; buffer while the body executes.
-                       (let ((inhibit-modification-hooks nil)
-                             (inhibit-read-only nil)
-                             (buffer-read-only nil))
-                         (setq ,ret (progn ,@body))))))
-             nil))
-         ,ret))))
+  (declare (debug ((":name" form) body)) (indent 1))
+  `(ert-with-test-buffer (:name ,name)
+     (save-window-excursion
+       (with-selected-window (display-buffer (current-buffer))
+         ,@body))))
 
 ;;;###autoload
 (defun ert-kill-all-test-buffers ()
-- 
2.39.0

From ea54be12b97b459a7e9ac9274f7594b9f80b8f31 Mon Sep 17 00:00:00 2001
From: Richard Hansen <[email-masked]>
Date: Sat, 17 Dec 2022 18:51:33 -0500
Subject: [PATCH 3/3] ert-x: Move window selection logic to its own macro

* lisp/emacs-lisp/ert-x.el (ert-with-buffer-selected): New macro to
temporarily display a buffer in a selected window and evaluate a body.
(ert-with-test-buffer-selected): Use the new macro.
* test/lisp/whitespace-tests.el (ert-test-with-buffer-selected/*): Add
tests.
---
 lisp/emacs-lisp/ert-x.el            | 31 +++++++++++++++++---------
 test/lisp/emacs-lisp/ert-x-tests.el | 34 +++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 10 deletions(-)

diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el
index 5f1c5c26ac..0614313809 100644
--- a/lisp/emacs-lisp/ert-x.el
+++ b/lisp/emacs-lisp/ert-x.el
@@ -102,25 +102,36 @@ ert-with-test-buffer
            (indent 1))
   `(ert--call-with-test-buffer ,name-form (lambda () ,@body)))
 
-(cl-defmacro ert-with-test-buffer-selected ((&key name)
-                                            &body body)
-  "Create a test buffer, switch to it, and run BODY.
+(cl-defmacro ert-with-buffer-selected (buffer-or-name &body body)
+  "Display a buffer in a temporary selected window and run BODY.
 
-This extends `ert-with-test-buffer' by displaying the test
-buffer (whose name is derived from NAME) in a temporary window.
-The temporary window becomes the `selected-window' before BODY is
-evaluated.  The modification hooks `before-change-functions' and
+If BUFFER-OR-NAME is nil, the current buffer is used.
+
+The buffer is made the current buffer, and the temporary window
+becomes the `selected-window', before BODY is evaluated.  The
+modification hooks `before-change-functions' and
 `after-change-functions' are not inhibited during the evaluation
 of BODY, which makes it easier to use `execute-kbd-macro' to
 simulate user interaction.  The window configuration is restored
 before returning, even if BODY exits nonlocally.  The return
 value is the last form in BODY."
-  (declare (debug ((":name" form) body)) (indent 1))
-  `(ert-with-test-buffer (:name ,name)
-     (save-window-excursion
+  (declare (debug (form body)) (indent 1))
+  `(save-window-excursion
+     (with-current-buffer (or ,buffer-or-name (current-buffer))
        (with-selected-window (display-buffer (current-buffer))
          ,@body))))
 
+(cl-defmacro ert-with-test-buffer-selected ((&key name) &body body)
+  "Create a test buffer, switch to it, and run BODY.
+
+This combines `ert-with-test-buffer' and
+`ert-with-buffer-selected'.  The return value is the last form in
+BODY."
+  (declare (debug ((":name" form) body)) (indent 1))
+  `(ert-with-test-buffer (:name ,name)
+     (ert-with-buffer-selected (current-buffer)
+       ,@body)))
+
 ;;;###autoload
 (defun ert-kill-all-test-buffers ()
   "Kill all test buffers that are still live."
diff --git a/test/lisp/emacs-lisp/ert-x-tests.el b/test/lisp/emacs-lisp/ert-x-tests.el
index f14d54cd9f..1cfd218592 100644
--- a/test/lisp/emacs-lisp/ert-x-tests.el
+++ b/test/lisp/emacs-lisp/ert-x-tests.el
@@ -82,6 +82,40 @@ ert-test-test-buffers
         (should-not (buffer-live-p buffer-1))
         (should (buffer-live-p buffer-2))))))
 
+(ert-deftest ert-test-with-buffer-selected/current ()
+  (let ((origbuf (current-buffer)))
+    (ert-with-test-buffer ()
+      (let ((buf (current-buffer)))
+        (should (not (eq buf origbuf)))
+        (with-current-buffer origbuf
+          (ert-with-buffer-selected buf
+            (should (eq (current-buffer) buf))))))))
+
+(ert-deftest ert-test-with-buffer-selected/selected ()
+  (ert-with-test-buffer ()
+    (ert-with-buffer-selected (current-buffer)
+      (should (eq (window-buffer) (current-buffer))))))
+
+(ert-deftest ert-test-with-buffer-selected/nil-buffer ()
+  (ert-with-test-buffer ()
+    (let ((buf (current-buffer)))
+      (ert-with-buffer-selected nil
+        (should (eq (window-buffer) buf))))))
+
+(ert-deftest ert-test-with-buffer-selected/modification-hooks ()
+  (ert-with-test-buffer ()
+    (ert-with-buffer-selected (current-buffer)
+      (should (null inhibit-modification-hooks)))))
+
+(ert-deftest ert-test-with-buffer-selected/read-only ()
+  (ert-with-test-buffer ()
+    (ert-with-buffer-selected (current-buffer)
+      (should (null inhibit-read-only))
+      (should (null buffer-read-only)))))
+
+(ert-deftest ert-test-with-buffer-selected/return-value ()
+  (should (equal (ert-with-buffer-selected nil "foo") "foo")))
+
 (ert-deftest ert-test-with-test-buffer-selected/selected ()
   (ert-with-test-buffer-selected ()
     (should (eq (window-buffer) (current-buffer)))))
-- 
2.39.0