diff --git a/doc/misc/ert.texi b/doc/misc/ert.texi index 566fa03bf3f..c15df506089 100644 --- a/doc/misc/ert.texi +++ b/doc/misc/ert.texi @@ -1093,10 +1093,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: @@ -1109,6 +1110,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) @@ -1133,23 +1144,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 524f02bb36d..5d124197d19 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 a9b43d319a3..b07e693f927 100644 --- a/test/lisp/simple-tests.el +++ b/test/lisp/simple-tests.el @@ -1148,7 +1148,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" @@ -1216,7 +1216,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) @@ -1238,7 +1238,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)) @@ -1250,7 +1250,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) @@ -1281,7 +1281,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)