summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp
diff options
context:
space:
mode:
authorRichard Hansen <rhansen@rhansen.org>2022-12-17 18:51:33 -0500
committerEli Zaretskii <eliz@gnu.org>2022-12-24 09:14:23 +0200
commit286c48137f69fa96b80d197da90c69a42df604a3 (patch)
tree424c7049a3b32a421b755230d40d3a0d19424f01 /lisp/emacs-lisp
parent823c49cea851158bc4db5ab133ecd9bf3d0791d7 (diff)
downloademacs-286c48137f69fa96b80d197da90c69a42df604a3.tar.gz
emacs-286c48137f69fa96b80d197da90c69a42df604a3.tar.bz2
emacs-286c48137f69fa96b80d197da90c69a42df604a3.zip
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/current) (ert-test-with-buffer-selected/selected) (ert-test-with-buffer-selected/nil-buffer) (ert-test-with-buffer-selected/modification-hooks) (ert-test-with-buffer-selected/read-only) (ert-test-with-buffer-selected/return-value): Add tests. (Bug#60189)
Diffstat (limited to 'lisp/emacs-lisp')
-rw-r--r--lisp/emacs-lisp/ert-x.el31
1 files changed, 21 insertions, 10 deletions
diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el
index 5f1c5c26acd..0614313809c 100644
--- a/lisp/emacs-lisp/ert-x.el
+++ b/lisp/emacs-lisp/ert-x.el
@@ -102,25 +102,36 @@ the name of the test and the result of NAME-FORM."
(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.
+
+If BUFFER-OR-NAME is nil, the current buffer is used.
-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
+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."