diff options
| author | Richard Hansen | 2022-12-17 18:51:33 -0500 |
|---|---|---|
| committer | Eli Zaretskii | 2022-12-24 09:14:23 +0200 |
| commit | 286c48137f69fa96b80d197da90c69a42df604a3 (patch) | |
| tree | 424c7049a3b32a421b755230d40d3a0d19424f01 | |
| parent | 823c49cea851158bc4db5ab133ecd9bf3d0791d7 (diff) | |
| download | emacs-286c48137f69fa96b80d197da90c69a42df604a3.tar.gz 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)
| -rw-r--r-- | lisp/emacs-lisp/ert-x.el | 31 | ||||
| -rw-r--r-- | 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 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." | |||
| 102 | (indent 1)) | 102 | (indent 1)) |
| 103 | `(ert--call-with-test-buffer ,name-form (lambda () ,@body))) | 103 | `(ert--call-with-test-buffer ,name-form (lambda () ,@body))) |
| 104 | 104 | ||
| 105 | (cl-defmacro ert-with-test-buffer-selected ((&key name) | 105 | (cl-defmacro ert-with-buffer-selected (buffer-or-name &body body) |
| 106 | &body body) | 106 | "Display a buffer in a temporary selected window and run BODY. |
| 107 | "Create a test buffer, switch to it, and run BODY. | 107 | |
| 108 | If BUFFER-OR-NAME is nil, the current buffer is used. | ||
| 108 | 109 | ||
| 109 | This extends `ert-with-test-buffer' by displaying the test | 110 | The buffer is made the current buffer, and the temporary window |
| 110 | buffer (whose name is derived from NAME) in a temporary window. | 111 | becomes the `selected-window', before BODY is evaluated. The |
| 111 | The temporary window becomes the `selected-window' before BODY is | 112 | modification hooks `before-change-functions' and |
| 112 | evaluated. The modification hooks `before-change-functions' and | ||
| 113 | `after-change-functions' are not inhibited during the evaluation | 113 | `after-change-functions' are not inhibited during the evaluation |
| 114 | of BODY, which makes it easier to use `execute-kbd-macro' to | 114 | of BODY, which makes it easier to use `execute-kbd-macro' to |
| 115 | simulate user interaction. The window configuration is restored | 115 | simulate user interaction. The window configuration is restored |
| 116 | before returning, even if BODY exits nonlocally. The return | 116 | before returning, even if BODY exits nonlocally. The return |
| 117 | value is the last form in BODY." | 117 | value is the last form in BODY." |
| 118 | (declare (debug ((":name" form) body)) (indent 1)) | 118 | (declare (debug (form body)) (indent 1)) |
| 119 | `(ert-with-test-buffer (:name ,name) | 119 | `(save-window-excursion |
| 120 | (save-window-excursion | 120 | (with-current-buffer (or ,buffer-or-name (current-buffer)) |
| 121 | (with-selected-window (display-buffer (current-buffer)) | 121 | (with-selected-window (display-buffer (current-buffer)) |
| 122 | ,@body)))) | 122 | ,@body)))) |
| 123 | 123 | ||
| 124 | (cl-defmacro ert-with-test-buffer-selected ((&key name) &body body) | ||
| 125 | "Create a test buffer, switch to it, and run BODY. | ||
| 126 | |||
| 127 | This combines `ert-with-test-buffer' and | ||
| 128 | `ert-with-buffer-selected'. The return value is the last form in | ||
| 129 | BODY." | ||
| 130 | (declare (debug ((":name" form) body)) (indent 1)) | ||
| 131 | `(ert-with-test-buffer (:name ,name) | ||
| 132 | (ert-with-buffer-selected (current-buffer) | ||
| 133 | ,@body))) | ||
| 134 | |||
| 124 | ;;;###autoload | 135 | ;;;###autoload |
| 125 | (defun ert-kill-all-test-buffers () | 136 | (defun ert-kill-all-test-buffers () |
| 126 | "Kill all test buffers that are still live." | 137 | "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 f14d54cd9f7..1cfd218592a 100644 --- a/test/lisp/emacs-lisp/ert-x-tests.el +++ b/test/lisp/emacs-lisp/ert-x-tests.el | |||
| @@ -82,6 +82,40 @@ | |||
| 82 | (should-not (buffer-live-p buffer-1)) | 82 | (should-not (buffer-live-p buffer-1)) |
| 83 | (should (buffer-live-p buffer-2)))))) | 83 | (should (buffer-live-p buffer-2)))))) |
| 84 | 84 | ||
| 85 | (ert-deftest ert-test-with-buffer-selected/current () | ||
| 86 | (let ((origbuf (current-buffer))) | ||
| 87 | (ert-with-test-buffer () | ||
| 88 | (let ((buf (current-buffer))) | ||
| 89 | (should (not (eq buf origbuf))) | ||
| 90 | (with-current-buffer origbuf | ||
| 91 | (ert-with-buffer-selected buf | ||
| 92 | (should (eq (current-buffer) buf)))))))) | ||
| 93 | |||
| 94 | (ert-deftest ert-test-with-buffer-selected/selected () | ||
| 95 | (ert-with-test-buffer () | ||
| 96 | (ert-with-buffer-selected (current-buffer) | ||
| 97 | (should (eq (window-buffer) (current-buffer)))))) | ||
| 98 | |||
| 99 | (ert-deftest ert-test-with-buffer-selected/nil-buffer () | ||
| 100 | (ert-with-test-buffer () | ||
| 101 | (let ((buf (current-buffer))) | ||
| 102 | (ert-with-buffer-selected nil | ||
| 103 | (should (eq (window-buffer) buf)))))) | ||
| 104 | |||
| 105 | (ert-deftest ert-test-with-buffer-selected/modification-hooks () | ||
| 106 | (ert-with-test-buffer () | ||
| 107 | (ert-with-buffer-selected (current-buffer) | ||
| 108 | (should (null inhibit-modification-hooks))))) | ||
| 109 | |||
| 110 | (ert-deftest ert-test-with-buffer-selected/read-only () | ||
| 111 | (ert-with-test-buffer () | ||
| 112 | (ert-with-buffer-selected (current-buffer) | ||
| 113 | (should (null inhibit-read-only)) | ||
| 114 | (should (null buffer-read-only))))) | ||
| 115 | |||
| 116 | (ert-deftest ert-test-with-buffer-selected/return-value () | ||
| 117 | (should (equal (ert-with-buffer-selected nil "foo") "foo"))) | ||
| 118 | |||
| 85 | (ert-deftest ert-test-with-test-buffer-selected/selected () | 119 | (ert-deftest ert-test-with-test-buffer-selected/selected () |
| 86 | (ert-with-test-buffer-selected () | 120 | (ert-with-test-buffer-selected () |
| 87 | (should (eq (window-buffer) (current-buffer))))) | 121 | (should (eq (window-buffer) (current-buffer))))) |