]> git.eshelyaron.com Git - emacs.git/commitdiff
Support selecting buffer in ert-with-test-buffer
authorStefan Kangas <stefankangas@gmail.com>
Mon, 24 Feb 2025 17:54:23 +0000 (18:54 +0100)
committerEshel Yaron <me@eshelyaron.com>
Wed, 26 Feb 2025 09:35:45 +0000 (10:35 +0100)
* 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)

doc/misc/ert.texi
lisp/emacs-lisp/ert-x.el
test/lisp/emacs-lisp/ert-x-tests.el
test/lisp/progmodes/hideshow-tests.el
test/lisp/simple-tests.el
test/lisp/whitespace-tests.el

index 42832ab3b88325896efbeb3fb4b9a174793d77e3..19726d3ca2f9ae891459a8dbd6b4055e9c26cdb5 100644 (file)
@@ -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
index 6a8e7b97f47c66d6e43fecb095075ea224c5df1d..33585850a97d104d299aab693ea39ddbfd64b7ca 100644 (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 ()
index 5d4719514099f91b49e0f4298aceb0f649389c89..aa0edf470590032f2da463ce5f63850bd665a120 100644 (file)
   (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 ()
index 8768ab79eed0ca81fae38df1a64e442d82457c7c..59b8522d6149ffc52d6893a1f18148d3763a3f24 100644 (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)
index be9a1573bf5e9111442944379b260ad807846da1..57804ea2103c4d81c2d05a05c48960da470da5a1 100644 (file)
@@ -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" "A<POINT>B" "1" "2" "") "\n"))
              (read-only-mode 1)
@@ -1191,7 +1191,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))
@@ -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 "<POINT>")
       (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<POINT>\n1\n")
       (setq last-command #'ignore)
index 4bd87325104e58a25d364f09c2e91731bc41a7aa..a33338c660fea9fb23531a089fd0be824464c983 100644 (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)