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.
This commit is contained in:
Stefan Kangas 2025-02-24 18:54:23 +01:00
parent 637c73d3a8
commit 13ca18e1f7
6 changed files with 50 additions and 46 deletions

View file

@ -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

View file

@ -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 ()

View file

@ -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 ()

View file

@ -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)

View file

@ -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" "A<POINT>B" "1" "2" "") "\n"))
(read-only-mode 1)
@ -1238,7 +1238,7 @@ See Bug#21722."
(test -9 '("-2" "-1" "AB") '("<POINT>-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 "A<POINT>X<MARK>B")
(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 "<POINT>")
(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<POINT>\n1\n")
(setq last-command #'ignore)

View file

@ -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)