From: Stefan Kangas Date: Mon, 24 Feb 2025 17:54:23 +0000 (+0100) Subject: Support selecting buffer in ert-with-test-buffer X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=6241ed85267f514445162d7713a3b67fca1d6228;p=emacs.git Support selecting buffer in ert-with-test-buffer * lisp/emacs-lisp/ert-x.el (ert-with-test-buffer): Add new keyword argument :selected to make the buffer current and selected. (ert-with-test-buffer-selected): Make obsolete and redefine in terms of ert-with-test-buffer. * doc/misc/ert.texi (Helper Functions): Document above new :selected keyword argument, and remove documentation of ert-with-test-buffer-selected. * test/lisp/emacs-lisp/ert-x-tests.el (ert-test-with-test-buffer-selected/selected) (ert-test-with-test-buffer-selected/modification-hooks) (ert-test-with-test-buffer-selected/read-only) (ert-test-with-test-buffer-selected/return-value) (ert-test-with-test-buffer-selected/buffer-name): * test/lisp/progmodes/hideshow-tests.el (hideshow-tests-with-temp-buffer-selected): * test/lisp/simple-tests.el (kill-whole-line-invisible) (kill-whole-line-read-only, kill-whole-line-after-other-kill) (kill-whole-line-buffer-boundaries) (kill-whole-line-line-boundaries): * test/lisp/whitespace-tests.el (whitespace-tests--with-test-buffer, whitespace-tests--global): Use ert-with-test-buffer instead of ert-with-test-buffer-selected. (cherry picked from commit 13ca18e1f79f9c02ac46735b58bc5126f77f0a77) --- diff --git a/doc/misc/ert.texi b/doc/misc/ert.texi index 42832ab3b88..19726d3ca2f 100644 --- a/doc/misc/ert.texi +++ b/doc/misc/ert.texi @@ -1088,10 +1088,11 @@ for writing tests. @subsection Test Buffers -@defmac ert-with-test-buffer ((&key ((:name name-form))) &body body) +@defmac ert-with-test-buffer ((&key ((:name name-form :selected select-form))) &body body) This macro creates a test buffer and runs @var{body} in that buffer. If @var{body} finishes successfully, the test buffer is killed; if there is -an error, the test buffer is kept around for further inspection. +an error, the test buffer is kept around for further inspection. The +return value is the last form in @var{body}. The test buffer name is derived from the name of the ERT test and the result of @var{NAME-FORM}. Example: @@ -1104,6 +1105,16 @@ result of @var{NAME-FORM}. Example: This uses the test buffer @file{*Test buffer (backtrace-tests--variables): variables*}. + +If @var{select-form} is non-nil, select the buffer after creating it. +This has the same effect as combining @code{ert-with-test-buffer} with +@code{ert-with-buffer-selected}. Example: + +@lisp +(ert-deftest whitespace-tests--global () + (ert-with-test-buffer-selected (:name "global" :selected t) + @dots{})) +@end lisp @end defmac @defmac ert-with-buffer-selected (buffer &body body) @@ -1128,23 +1139,6 @@ value is the last form in @var{body}. Example: This displays a temporary buffer like @file{ *temp*-739785*}. @end defmac -@defmac ert-with-test-buffer-selected ((&key name) &body body) -This creates a test buffer, switches to it, and runs @var{body}. - -It combines @code{ert-with-test-buffer} and -@code{ert-with-buffer-selected}. The return value is the last form in -@var{body}. Example: - -@lisp -(ert-deftest whitespace-tests--global () - (ert-with-test-buffer-selected (:name "global") - @dots{})) -@end lisp - -This displays the test buffer @file{*Test buffer -(whitespace-tests--global): global*}. -@end defmac - @defun ert-kill-all-test-buffers () It kills all test buffers that are still live. @end defun diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el index 6a8e7b97f47..33585850a97 100644 --- a/lisp/emacs-lisp/ert-x.el +++ b/lisp/emacs-lisp/ert-x.el @@ -90,17 +90,28 @@ ERT--THUNK with that buffer as current." (kill-buffer ert--buffer) (remhash ert--buffer ert--test-buffers)))) -(cl-defmacro ert-with-test-buffer ((&key ((:name name-form))) +(cl-defmacro ert-with-test-buffer ((&key ((:name name-form)) + ((:selected select-form))) &body body) "Create a test buffer and run BODY in that buffer. -To be used in ERT tests. If BODY finishes successfully, the test -buffer is killed; if there is an error, the test buffer is kept -around for further inspection. Its name is derived from -the name of the test and the result of NAME-FORM." - (declare (debug ((":name" form) def-body)) +To be used in ERT tests. If BODY finishes successfully, the test buffer +is killed; if there is an error, the test buffer is kept around for +further inspection. The name of the buffer is derived from the name of +the test and the result of NAME-FORM. + +If SELECT-FORM is non-nil, switch to the test buffer before running +BODY, as if body was in `ert-with-buffer-selected'. + +The return value is the last form in BODY." + (declare (debug ((":name" form) (":selected" form) def-body)) (indent 1)) - `(ert--call-with-test-buffer ,name-form (lambda () ,@body))) + `(ert--call-with-test-buffer + ,name-form + ,(if select-form + `(lambda () (ert-with-buffer-selected (current-buffer) + ,@body)) + `(lambda () ,@body)))) (cl-defmacro ert-with-buffer-selected (buffer-or-name &body body) "Display a buffer in a temporary selected window and run BODY. @@ -124,13 +135,12 @@ value is the last form in 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))) +This combines `ert-with-test-buffer' and `ert-with-buffer-selected'. +The return value is the last form in BODY." + (declare (obsolete ert-with-test-buffer "31.1") + (debug ((":name" form) body)) (indent 1)) + `(ert-with-test-buffer (:name ,name :selected t) + ,@body)) ;;;###autoload (defun ert-kill-all-test-buffers () diff --git a/test/lisp/emacs-lisp/ert-x-tests.el b/test/lisp/emacs-lisp/ert-x-tests.el index 5d471951409..aa0edf47059 100644 --- a/test/lisp/emacs-lisp/ert-x-tests.el +++ b/test/lisp/emacs-lisp/ert-x-tests.el @@ -117,24 +117,24 @@ (should (equal (ert-with-buffer-selected nil "foo") "foo"))) (ert-deftest ert-test-with-test-buffer-selected/selected () - (ert-with-test-buffer-selected () + (ert-with-test-buffer (:selected t) (should (eq (window-buffer) (current-buffer))))) (ert-deftest ert-test-with-test-buffer-selected/modification-hooks () - (ert-with-test-buffer-selected () + (ert-with-test-buffer (:selected t) (should (null inhibit-modification-hooks)))) (ert-deftest ert-test-with-test-buffer-selected/read-only () - (ert-with-test-buffer-selected () + (ert-with-test-buffer (:selected t) (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"))) + (should (equal (ert-with-test-buffer (:selected t) "foo") "foo"))) (ert-deftest ert-test-with-test-buffer-selected/buffer-name () (should (equal (ert-with-test-buffer (:name "foo") (buffer-name)) - (ert-with-test-buffer-selected (:name "foo") + (ert-with-test-buffer (:name "foo" :selected t) (buffer-name))))) (ert-deftest ert-filter-string () diff --git a/test/lisp/progmodes/hideshow-tests.el b/test/lisp/progmodes/hideshow-tests.el index 8768ab79eed..59b8522d614 100644 --- a/test/lisp/progmodes/hideshow-tests.el +++ b/test/lisp/progmodes/hideshow-tests.el @@ -46,7 +46,7 @@ always located at the beginning of buffer." BODY is code to be executed within the temp buffer. Point is always located at the beginning of buffer." (declare (indent 1) (debug t)) - `(ert-with-test-buffer-selected () + `(ert-with-test-buffer (:selected t) (,mode) (hs-minor-mode 1) (insert ,contents) diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el index be9a1573bf5..57804ea2103 100644 --- a/test/lisp/simple-tests.el +++ b/test/lisp/simple-tests.el @@ -1101,7 +1101,7 @@ See Bug#21722." (ert-deftest kill-whole-line-invisible () (cl-flet ((test (kill-whole-line-arg &rest expected-lines) (ert-info ((format "%s" kill-whole-line-arg) :prefix "Subtest: ") - (ert-with-test-buffer-selected nil + (ert-with-test-buffer (:selected t) (simple-test--set-buffer-text-point-mark (string-join '("* -2" "hidden" @@ -1169,7 +1169,7 @@ See Bug#21722." (cl-flet ((test (kill-whole-line-arg expected-kill-lines expected-buffer-lines) (ert-info ((format "%s" kill-whole-line-arg) :prefix "Subtest: ") - (ert-with-test-buffer-selected nil + (ert-with-test-buffer (:selected t) (simple-test--set-buffer-text-point-mark (string-join '("-2" "-1" "AB" "1" "2" "") "\n")) (read-only-mode 1) @@ -1191,7 +1191,7 @@ See Bug#21722." (test -9 '("-2" "-1" "AB") '("-2" "-1" "AB" "1" "2" "")))) (ert-deftest kill-whole-line-after-other-kill () - (ert-with-test-buffer-selected nil + (ert-with-test-buffer (:selected t) (simple-test--set-buffer-text-point-mark "AXB") (setq last-command #'ignore) (kill-region (point) (mark)) @@ -1203,7 +1203,7 @@ See Bug#21722." (simple-test--get-buffer-text-point-mark))))) (ert-deftest kill-whole-line-buffer-boundaries () - (ert-with-test-buffer-selected nil + (ert-with-test-buffer (:selected t) (ert-info ("0" :prefix "Subtest: ") (simple-test--set-buffer-text-point-mark "") (should-error (kill-whole-line -1) @@ -1234,7 +1234,7 @@ See Bug#21722." (should (equal "A\n" (car kill-ring)))))) (ert-deftest kill-whole-line-line-boundaries () - (ert-with-test-buffer-selected nil + (ert-with-test-buffer (:selected t) (ert-info ("1a" :prefix "Subtest: ") (simple-test--set-buffer-text-point-mark "-1\n\n1\n") (setq last-command #'ignore) diff --git a/test/lisp/whitespace-tests.el b/test/lisp/whitespace-tests.el index 4bd87325104..a33338c660f 100644 --- a/test/lisp/whitespace-tests.el +++ b/test/lisp/whitespace-tests.el @@ -30,7 +30,7 @@ The buffer is displayed in `selected-window', and nil, `whitespace-mode' is left disabled." (declare (debug ((style form) def-body)) (indent 1)) - `(ert-with-test-buffer-selected () + `(ert-with-test-buffer (:selected t) ;; In case global-*-mode is enabled. (whitespace-mode -1) (font-lock-mode -1) @@ -63,7 +63,7 @@ buffer's content." (unwind-protect (progn (global-whitespace-mode 1) - (ert-with-test-buffer-selected () + (ert-with-test-buffer (:selected t) (normal-mode) (should whitespace-mode) (global-whitespace-mode -1)