summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorVincent Belaïche <vincentb1@users.sourceforge.net>2026-03-29 06:17:55 +0200
committerVincent Belaïche <vincentb1@users.sourceforge.net>2026-04-16 13:30:42 +0200
commit5b6fc8ebfcd6bce6a0e5fb7160ec7a2aeb561baf (patch)
treec7ecc0994c1d75437cd0e3a380c9cdb4948784e4 /test
parent1c4c9d759a2e32856db3fd03e906057c8d81fa0e (diff)
ert-play-keys function
* lisp/emacs-lisp/ert-x.el (ert-play-keys): new defun. * test/lisp/simple-tests.el (undo-test-kill-c-a-then-undo) (undo-test-point-after-forward-kill): Use new function `ert-play-keys' and `(ert-with-test-buffer (:selected t) ...)' rather (with-temp-buffer (switch-to-buffer (current-buffer) ...)'. * test/lisp/erc/erc-scenarios-spelling.el (erc-scenarios-spelling--auto-correct): Use new function `ert-play-keys' and `(ert-with-buffer-selected ...)' rather than `execute-kbd-macro' and `(with-current-buffer ... (set-window-buffer nil (current-buffer) ...)'. * doc/misc/ert.texi (Helper Functions): Document ert-play-keys, and differences between ert-simulate-command, ert-simulate-keys & ert-play-keys. * test/lisp/emacs-lisp/ert-x-tests.el (ert-x-tests-play-keys) (ert-x-tests-simulate-command, ert-x-tests-simulate-keys): New tests.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/emacs-lisp/ert-x-tests.el71
-rw-r--r--test/lisp/erc/erc-scenarios-spelling.el7
-rw-r--r--test/lisp/simple-tests.el46
3 files changed, 96 insertions, 28 deletions
diff --git a/test/lisp/emacs-lisp/ert-x-tests.el b/test/lisp/emacs-lisp/ert-x-tests.el
index c72ab97c557..2823fdba3d7 100644
--- a/test/lisp/emacs-lisp/ert-x-tests.el
+++ b/test/lisp/emacs-lisp/ert-x-tests.el
@@ -293,6 +293,77 @@ desired effect."
(should-error
(ert-with-temp-directory dir :text "foo" nil)))
+(ert-deftest ert-x-tests-play-keys ()
+ "Test `ert-play-keys'.
+Send one symbolic event, some inserted text, and some key event to the
+test buffer, and check all of them are processed."
+ (ert-with-test-buffer (:selected t)
+ (let (verdict-event verdict-key verdict-pre-command-hook verdict-post-command-hook)
+ (let ((pre-command-hook (lambda () (setq verdict-pre-command-hook t)))
+ (post-command-hook (lambda () (setq verdict-post-command-hook t)))
+ (map (let ((map (make-sparse-keymap)))
+ (define-key map [event]
+ (lambda ()
+ (interactive)
+ (setq verdict-event
+ (list t
+ (called-interactively-p 'any)
+ (called-interactively-p 'interactive)))))
+ (define-key map [?$]
+ (lambda ()
+ (interactive)
+ (setq verdict-key
+ (list t
+ (called-interactively-p 'any)
+ (called-interactively-p 'interactive)))))
+ map)))
+ (let ((minor-mode-map-alist (cons (cons t map) minor-mode-map-alist)))
+ (ert-play-keys (vconcat [event] "n'importe $quoi"))))
+ (should (equal verdict-event '(t t nil)))
+ (should (equal verdict-key '(t t nil)))
+ (should (eq verdict-pre-command-hook t))
+ (should (eq verdict-post-command-hook t)))
+ (should (string= "n'importe quoi"
+ (buffer-substring (point-min) (point-max))))))
+
+(ert-deftest ert-x-tests-simulate-command ()
+ "Test `ert-simulate-command'."
+ (ert-with-test-buffer ()
+ (let (verdict-interactive verdict-pre-command-hook verdict-post-command-hook)
+ (let ((pre-command-hook (lambda () (setq verdict-pre-command-hook t)))
+ (post-command-hook (lambda () (setq verdict-post-command-hook t))))
+ (should (eq (ert-simulate-command
+ (list
+ (lambda (x)
+ (interactive (list "un rien"))
+ (insert x)
+ (setq verdict-interactive (list t
+ (called-interactively-p 'any)
+ (called-interactively-p 'interactive)))
+ :ok)
+ "n'importe quoi"))
+ :ok)))
+ (should (equal verdict-interactive '(t nil nil)))
+ (should (eq verdict-pre-command-hook t))
+ (should (eq verdict-post-command-hook t)))
+ (should (string= "n'importe quoi"
+ (buffer-substring (point-min) (point-max))))))
+
+(ert-deftest ert-x-tests-simulate-keys ()
+ "Test `ert-simulate-keys'."
+ (ert-with-test-buffer ()
+ (let* ((map (let ((map (make-sparse-keymap)))
+ (define-key map [?b]
+ (lambda ()
+ (interactive)
+ (insert "r"))) map))
+ (minor-mode-map-alist (cons (cons t map) minor-mode-map-alist)))
+ (ert-simulate-keys
+ (listify-key-sequence "un bien\nn'importe quoi")
+ (should (string= (read-from-minibuffer "Please enter something: ") "un rien")))
+ (should (string= "" (buffer-substring (point-min) (point-max)))))))
+
+
(provide 'ert-x-tests)
;;; ert-x-tests.el ends here
diff --git a/test/lisp/erc/erc-scenarios-spelling.el b/test/lisp/erc/erc-scenarios-spelling.el
index 0495a2a1390..74e6dbb9715 100644
--- a/test/lisp/erc/erc-scenarios-spelling.el
+++ b/test/lisp/erc/erc-scenarios-spelling.el
@@ -61,14 +61,13 @@
(should erc-spelling-mode)
(should flyspell-mode)))
- (with-current-buffer (erc-d-t-wait-for 10 (get-buffer "#chan"))
+ (ert-with-buffer-selected (erc-d-t-wait-for 10 (get-buffer "#chan"))
(should erc-spelling-mode)
(should flyspell-mode)
(funcall expect 10 "<alice> tester, welcome!")
;; Insert a command with one misspelled word.
- (set-window-buffer nil (current-buffer))
- (execute-kbd-macro "\M->/AMSG an/dor /gmsg one fsbot two frob my shoe")
+ (ert-play-keys (vconcat [?\M->] "/AMSG an/dor /gmsg one fsbot two frob my shoe"))
(funcall expect 10 "shoe")
(let* ((ovs (overlays-in erc-input-marker (point)))
@@ -90,7 +89,7 @@
;; Depending on the machine, this should become something
;; like: "/AMSG an/dor /gmsg one fsbot two Rob my shoe".
- (execute-kbd-macro (key-parse "M-TAB"))
+ (ert-play-keys "M-TAB")
(should (equal (overlays-in erc-input-marker (point-max))
(list ov1)))))
diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el
index 12697b2d68e..464cc51c152 100644
--- a/test/lisp/simple-tests.el
+++ b/test/lisp/simple-tests.el
@@ -21,7 +21,7 @@
;;; Code:
-(require 'ert)
+(require 'ert-x)
(eval-when-compile (require 'cl-lib))
(defun simple-test--buffer-substrings ()
@@ -730,36 +730,34 @@ See bug#35036."
;; Test for a regression introduced by undo-auto--boundaries changes.
;; https://lists.gnu.org/r/emacs-devel/2015-11/msg01652.html
(defun undo-test-kill-c-a-then-undo ()
- (with-temp-buffer
- (switch-to-buffer (current-buffer))
- (setq buffer-undo-list nil)
- (insert "a\nb\nc\n")
- (goto-char (point-max))
- ;; We use a keyboard macro because it adds undo events in the same
- ;; way as if a user were involved.
- (funcall (kmacro [left
- ;; Delete "c"
- backspace
- left left left
- ;; Delete "a"
- backspace
- ;; C-/ or undo
- ?\C-/
- ]))
+ (ert-with-test-buffer (:selected t)
+ (setq buffer-undo-list nil)
+ (insert "a\nb\nc\n")
+ (goto-char (point-max))
+ ;; We use a keyboard macro because it adds undo events in the same
+ ;; way as if a user were involved.
+ (ert-play-keys [left
+ ;; Delete "c"
+ backspace
+ left left left
+ ;; Delete "a"
+ backspace
+ ;; C-/ or undo
+ ?\C-/
+ ])
(point)))
(defun undo-test-point-after-forward-kill ()
- (with-temp-buffer
- (switch-to-buffer (current-buffer))
+ (ert-with-test-buffer (:selected t)
(setq buffer-undo-list nil)
(insert "kill word forward")
;; Move to word "word".
(goto-char 6)
- (funcall (kmacro [;; kill-word
- C-delete
- ;; undo
- ?\C-/
- ]))
+ (ert-play-keys [;; kill-word
+ C-delete
+ ;; undo
+ ?\C-/
+ ])
(point)))
(ert-deftest undo-point-in-wrong-place ()