1 ;;; kmacro-tests.el --- Tests for kmacro.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2017 Free Software Foundation, Inc.
5 ;; Author: Gemini Lasswell <gazally@runbox.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
33 (defmacro kmacro-tests-with-kmacro-clean-slate
(&rest body
)
34 "Create a clean environment for a kmacro test BODY to run in."
35 (declare (debug (body)))
36 `(cl-letf* ((kmacro-execute-before-append t
)
38 (kmacro-repeat-no-prefix t
)
39 (kmacro-call-repeat-key nil
)
40 (kmacro-call-repeat-with-arg nil
)
42 (kbd-macro-termination-hook nil
)
43 (defining-kbd-macro nil
)
44 (executing-kbd-macro nil
)
45 (executing-kbd-macro-index 0)
51 (kmacro-default-counter-format "%d")
52 (kmacro-counter-format "%d")
53 (kmacro-counter-format-start "%d")
54 (kmacro-counter-value-start 0)
55 (kmacro-last-counter 0)
56 (kmacro-initial-counter-value nil
)
58 (kmacro-tests-macros nil
)
59 (kmacro-tests-events nil
)
60 (kmacro-tests-sequences nil
))
61 (advice-add 'end-kbd-macro
:after
#'kmacro-tests-end-macro-advice
)
62 (advice-add 'read-event
:around
#'kmacro-tests-read-event-advice
)
63 (advice-add 'read-key-sequence
:around
#'kmacro-tests-read-key-sequence-advice
)
65 (ert-with-test-buffer (:name
"")
66 (switch-to-buffer (current-buffer))
68 (advice-remove 'read-key-sequence
#'kmacro-tests-read-key-sequence-advice
)
69 (advice-remove 'read-event
#'kmacro-tests-read-event-advice
)
70 (advice-remove 'end-kbd-macro
#'kmacro-tests-end-macro-advice
))))
72 (defmacro kmacro-tests-deftest
(name _args docstring
&rest keys-and-body
)
73 "Define a kmacro unit test.
74 NAME is the name of the test, _ARGS should be nil, and DOCSTRING
75 is required. To avoid having to duplicate ert's keyword parsing
76 here, its keywords and values (if any) must be inside a list
77 after the docstring, preceding the body, here combined with the
78 body in KEYS-AND-BODY."
79 (declare (debug (&define name sexp stringp
80 [&optional
(&rest
&or
[keywordp sexp
])]
85 (let* ((keys (when (and (listp (car keys-and-body
))
86 (keywordp (caar keys-and-body
)))
88 (body (if keys
(cdr keys-and-body
)
90 `(ert-deftest ,name
()
92 (kmacro-tests-with-kmacro-clean-slate ,@body
))))
94 (defvar kmacro-tests-keymap
95 (let ((map (make-sparse-keymap)))
97 (define-key map
(string (+ ?a i
)) 'self-insert-command
))
99 (define-key map
(string (+ ?
0 i
)) 'self-insert-command
))
100 ;; Define a few key sequences of different lengths.
101 (dolist (item '(("\C-a" . beginning-of-line
)
102 ("\C-b" . backward-char
)
103 ("\C-e" . end-of-line
)
104 ("\C-f" . forward-char
)
105 ("\C-r" . isearch-backward
)
106 ("\C-u" . universal-argument
)
107 ("\C-w" . kill-region
)
108 ("\C-SPC" . set-mark-command
)
109 ("\M-w" . kill-ring-save
)
110 ("\M-x" . execute-extended-command
)
111 ("\C-cd" . downcase-word
)
112 ("\C-cxu" . upcase-word
)
113 ("\C-cxq" . quoted-insert
)
114 ("\C-cxi" . kmacro-insert-counter
)
115 ("\C-x\C-k" . kmacro-keymap
)))
116 (define-key map
(car item
) (cdr item
)))
118 "Keymap to use for testing keyboard macros.
119 This is used to obtain consistent results even if tests are run
120 in an environment with rebound keys.")
122 (defvar kmacro-tests-events nil
123 "Input events used by the kmacro test in progress.")
125 (defun kmacro-tests-read-event-advice (orig-func &rest args
)
126 "Pop and return an event from `kmacro-tests-events'.
127 Return the result of calling ORIG-FUNC with ARGS if
128 `kmacro-tests-events' is empty, or if a keyboard macro is
130 (if (or executing-kbd-macro
(null kmacro-tests-events
))
131 (apply orig-func args
)
132 (pop kmacro-tests-events
)))
134 (defvar kmacro-tests-sequences nil
135 "Input sequences used by the kmacro test in progress.")
137 (defun kmacro-tests-read-key-sequence-advice (orig-func &rest args
)
138 "Pop and return a string from `kmacro-tests-sequences'.
139 Return the result of calling ORIG-FUNC with ARGS if
140 `kmacro-tests-sequences' is empty, or if a keyboard macro is
142 (if (or executing-kbd-macro
(null kmacro-tests-sequences
))
143 (apply orig-func args
)
144 (pop kmacro-tests-sequences
)))
146 (defvar kmacro-tests-macros nil
147 "Keyboard macros (in vector form) used by the kmacro test in progress.")
149 (defun kmacro-tests-end-macro-advice (&rest _args
)
150 "Pop a macro from `kmacro-tests-macros' and assign it to `last-kbd-macro'.
151 If `kmacro-tests-macros' is empty, do nothing."
152 (when kmacro-tests-macros
153 (setq last-kbd-macro
(pop kmacro-tests-macros
))))
155 ;;; Some more powerful expectations:
157 (defmacro kmacro-tests-should-insert
(value &rest body
)
158 "Verify that VALUE is inserted by the execution of BODY.
159 Execute BODY, then check that the string VALUE was inserted
160 into the current buffer at point."
161 (declare (debug (stringp body
))
163 (let ((g-p (cl-gensym))
164 (g-bsize (cl-gensym)))
165 `(let ((,g-p
(point))
166 (,g-bsize
(buffer-size)))
168 (should (equal (buffer-substring ,g-p
(point)) ,value
))
169 (should (equal (- (buffer-size) ,g-bsize
) (length ,value
))))))
171 (defmacro kmacro-tests-should-match-message
(value &rest body
)
172 "Verify that a message matching VALUE is issued while executing BODY.
173 Execute BODY, and then if there is not a regexp match between
174 VALUE and any text written to *Messages* during the execution,
175 cause the current test to fail."
176 (declare (debug (form body
))
178 (let ((g-captured-messages (cl-gensym)))
179 `(ert-with-message-capture ,g-captured-messages
181 (should (string-match-p ,value
,g-captured-messages
)))))
185 (kmacro-tests-deftest kmacro-tests-test-insert-counter-01-nil
()
186 "`kmacro-insert-counter' adds one to macro counter with nil arg."
187 (kmacro-tests-should-insert "0"
188 (kmacro-tests-simulate-command '(kmacro-insert-counter nil
)))
189 (kmacro-tests-should-insert "1"
190 (kmacro-tests-simulate-command '(kmacro-insert-counter nil
))))
192 (kmacro-tests-deftest kmacro-tests-test-insert-counter-02-int
()
193 "`kmacro-insert-counter' increments by value of list argument."
194 (kmacro-tests-should-insert "0"
195 (kmacro-tests-simulate-command '(kmacro-insert-counter 2)))
196 (kmacro-tests-should-insert "2"
197 (kmacro-tests-simulate-command '(kmacro-insert-counter 3)))
198 (kmacro-tests-should-insert "5"
199 (kmacro-tests-simulate-command '(kmacro-insert-counter nil
))))
201 (kmacro-tests-deftest kmacro-tests-test-insert-counter-03-list
()
202 "`kmacro-insert-counter' doesn't increment when given universal argument."
203 (kmacro-tests-should-insert "0"
204 (kmacro-tests-simulate-command '(kmacro-insert-counter (16))))
205 (kmacro-tests-should-insert "0"
206 (kmacro-tests-simulate-command '(kmacro-insert-counter (4)))))
208 (kmacro-tests-deftest kmacro-tests-test-insert-counter-04-neg
()
209 "`kmacro-insert-counter' decrements with '- prefix argument"
210 (kmacro-tests-should-insert "0"
211 (kmacro-tests-simulate-command '(kmacro-insert-counter -
)))
212 (kmacro-tests-should-insert "-1"
213 (kmacro-tests-simulate-command '(kmacro-insert-counter nil
))))
215 (kmacro-tests-deftest kmacro-tests-test-start-format-counter
()
216 "`kmacro-insert-counter' uses start value and format."
217 (kmacro-tests-simulate-command '(kmacro-set-counter 10))
218 (kmacro-tests-should-insert "10"
219 (kmacro-tests-simulate-command '(kmacro-insert-counter nil
)))
220 (kmacro-tests-should-insert "11"
221 (kmacro-tests-simulate-command '(kmacro-insert-counter nil
)))
222 (kmacro-set-format "c=%s")
223 (kmacro-tests-simulate-command '(kmacro-set-counter 50))
224 (kmacro-tests-should-insert "c=50"
225 (kmacro-tests-simulate-command '(kmacro-insert-counter nil
))))
227 (kmacro-tests-deftest kmacro-tests-test-start-macro-when-defining-macro
()
228 "Starting a macro while defining a macro does not start a second macro."
229 (kmacro-tests-simulate-command '(kmacro-start-macro nil
))
230 ;; We should now be in the macro-recording state.
231 (should defining-kbd-macro
)
232 (should-not last-kbd-macro
)
233 ;; Calling it again should leave us in the same state.
234 (kmacro-tests-simulate-command '(kmacro-start-macro nil
))
235 (should defining-kbd-macro
)
236 (should-not last-kbd-macro
))
239 (kmacro-tests-deftest kmacro-tests-set-macro-counter-while-defining
()
240 "Use of the prefix arg with kmacro-start sets kmacro-counter."
241 ;; Give kmacro-start-macro an argument.
242 (kmacro-tests-simulate-command '(kmacro-start-macro 5))
243 (should defining-kbd-macro
)
244 ;; Verify that the counter is set to that value.
245 (kmacro-tests-should-insert "5"
246 (kmacro-tests-simulate-command '(kmacro-insert-counter nil
)))
247 ;; Change it while defining a macro.
248 (kmacro-tests-simulate-command '(kmacro-set-counter 1))
249 (kmacro-tests-should-insert "1"
250 (kmacro-tests-simulate-command '(kmacro-insert-counter nil
)))
251 ;; Using universal arg to set counter should reset to starting value.
252 (kmacro-tests-simulate-command '(kmacro-set-counter (4)) '(4))
253 (kmacro-tests-should-insert "5"
254 (kmacro-tests-simulate-command '(kmacro-insert-counter nil
))))
257 (kmacro-tests-deftest kmacro-tests-start-insert-counter-appends-to-macro
()
258 "Use of the universal arg appends to the previous macro."
259 (let ((kmacro-tests-macros (list (string-to-vector "hello"))))
260 ;; Start recording a macro.
261 (kmacro-tests-simulate-command '(kmacro-start-macro-or-insert-counter nil
))
262 ;; Make sure we are recording.
263 (should defining-kbd-macro
)
264 ;; Call it again and it should insert the counter.
265 (kmacro-tests-should-insert "0"
266 (kmacro-tests-simulate-command '(kmacro-start-macro-or-insert-counter nil
)))
267 ;; We should still be in the recording state.
268 (should defining-kbd-macro
)
269 ;; End recording with repeat count.
270 (kmacro-tests-simulate-command '(kmacro-end-or-call-macro 3))
271 ;; Recording should be finished.
272 (should-not defining-kbd-macro
)
273 ;; Now use prefix arg to append to the previous macro.
274 ;; This should run the previous macro first.
275 (kmacro-tests-should-insert "hello"
276 (kmacro-tests-simulate-command
277 '(kmacro-start-macro-or-insert-counter (4))))
278 ;; Verify that the recording state has changed.
279 (should (equal defining-kbd-macro
'append
))))
281 (kmacro-tests-deftest kmacro-tests-end-call-macro-prefix-args
()
282 "kmacro-end-call-macro changes behavior based on prefix arg."
283 ;; "Record" two macros.
285 (kmacro-tests-define-macro (vconcat (format "macro #%d" (1+ i
)))))
286 ;; With no prefix arg, it should call the second macro.
287 (kmacro-tests-should-insert "macro #2"
288 (kmacro-tests-simulate-command '(kmacro-end-or-call-macro nil
)))
289 ;; With universal arg, it should call the first one.
290 (kmacro-tests-should-insert "macro #1"
291 (kmacro-tests-simulate-command '(kmacro-end-or-call-macro (4)))))
293 (kmacro-tests-deftest kmacro-tests-end-and-call-macro
()
294 "Keyboard command to end and call macro works under various conditions."
295 ;; First, try it with no macro to record.
296 (setq kmacro-tests-macros
'(""))
297 (kmacro-tests-simulate-command '(kmacro-start-macro nil
))
299 (kmacro-tests-simulate-command '(kmacro-end-and-call-macro 2) 2)
300 (error (should (string= (cadr err
)
301 "No kbd macro has been defined"))))
303 ;; Check that it stopped defining and that no macro was recorded.
304 (should-not defining-kbd-macro
)
305 (should-not last-kbd-macro
)
307 ;; Now try it while not recording, but first record a non-nil macro.
308 (kmacro-tests-define-macro "macro")
309 (kmacro-tests-should-insert "macro"
310 (kmacro-tests-simulate-command '(kmacro-end-and-call-macro nil
))))
312 (kmacro-tests-deftest kmacro-tests-end-and-call-macro-mouse
()
313 "Commands to end and call macro work under various conditions.
314 This is a regression test for Bug#24992."
315 (:expected-result
:failed
)
316 (cl-letf (((symbol-function #'mouse-set-point
) #'ignore
))
317 ;; First, try it with no macro to record.
318 (setq kmacro-tests-macros
'(""))
319 (kmacro-tests-simulate-command '(kmacro-start-macro nil
))
321 (kmacro-tests-simulate-command '(kmacro-end-call-mouse 2) 2)
322 (error (should (string= (cadr err
)
323 "No kbd macro has been defined"))))
325 ;; Check that it stopped defining and that no macro was recorded.
326 (should-not defining-kbd-macro
)
327 (should-not last-kbd-macro
)
329 ;; Now try it while not recording, but first record a non-nil macro.
330 (kmacro-tests-define-macro "macro")
331 (kmacro-tests-should-insert "macro"
332 (kmacro-tests-simulate-command '(kmacro-end-call-mouse nil
)))))
334 (kmacro-tests-deftest kmacro-tests-call-macro-hint-and-repeat
()
335 "`kmacro-call-macro' gives hint in Messages and sets up repeat keymap.
336 This is a regression test for: Bug#3412, Bug#11817."
337 (kmacro-tests-define-macro [?m
])
338 (let ((kmacro-call-repeat-key t
)
339 (kmacro-call-repeat-with-arg t
)
340 (overriding-terminal-local-map overriding-terminal-local-map
)
341 (last-input-event ?e
))
342 (message "") ; Clear the echo area. (Bug#3412)
343 (kmacro-tests-should-match-message "Type e to repeat macro"
344 (kmacro-tests-should-insert "mmmmmm"
345 (cl-letf (((symbol-function #'this-single-command-keys
) (lambda ()
347 (kmacro-call-macro 3))
348 ;; Check that it set up for repeat, and run the repeat.
349 (funcall (lookup-key overriding-terminal-local-map
"e"))))))
351 (kmacro-tests-deftest
352 kmacro-tests-run-macro-command-recorded-in-macro
()
353 "No infinite loop if `kmacro-end-and-call-macro' is recorded in the macro.
355 (:expected-result
:failed
)
356 (ert-skip "Skipping due to Bug#24921 (an ERT bug)")
357 (kmacro-tests-define-macro (vconcat "foo" [return] "\M-x"
358 "kmacro-end-and-call-macro"))
359 (use-local-map kmacro-tests-keymap)
360 (kmacro-tests-simulate-command '(kmacro-end-and-call-macro nil)))
363 (kmacro-tests-deftest kmacro-tests-test-ring-2nd-commands ()
364 "2nd macro in ring is displayed and executed normally and on repeat."
365 (use-local-map kmacro-tests-keymap)
366 ;; Record one macro, with count.
367 (push (vconcat "\C-cxi" "\C-u\C-cxi") kmacro-tests-macros)
368 (kmacro-tests-simulate-command '(kmacro-start-macro 1))
369 (kmacro-tests-simulate-command '(kmacro-end-macro nil))
370 ;; Check that execute and display do nothing with no 2nd macro.
371 (kmacro-tests-should-insert ""
372 (kmacro-tests-simulate-command '(kmacro-call-ring-2nd nil)))
373 (kmacro-tests-should-match-message "Only one keyboard macro defined"
374 (kmacro-tests-simulate-command '(kmacro-view-ring-2nd)))
375 ;; Record another one, with format.
376 (kmacro-set-format "=%d=")
377 (kmacro-tests-define-macro (vconcat "bar"))
378 ;; Execute the first one, mocked up to insert counter.
379 ;; Should get default format.
380 (kmacro-tests-should-insert "11"
381 (kmacro-tests-simulate-command '(kmacro-call-ring-2nd nil)))
382 ;; Now display the 2nd ring macro and check result.
383 (kmacro-tests-should-match-message "C-c x i C-u C-c x i"
384 (kmacro-view-ring-2nd)))
386 (kmacro-tests-deftest kmacro-tests-fill-ring-and-rotate ()
387 "Macro ring can shift one way, shift the other way, swap and pop."
388 (cl-letf ((kmacro-ring-max 4))
389 ;; Record enough macros that the first one drops off the history.
390 (dotimes (n (1+ kmacro-ring-max))
391 (kmacro-tests-define-macro (make-vector (1+ n) (+ ?a n))))
392 ;; Cycle the ring and check that #2 comes up.
393 (kmacro-tests-should-match-message "2*b"
394 (kmacro-tests-simulate-command '(kmacro-cycle-ring-next nil)))
395 ;; Execute the current macro and check arguments.
396 (kmacro-tests-should-insert "bbbb"
397 (kmacro-call-macro 2 t))
398 ;; Cycle the ring the other way; #5 expected.
399 (kmacro-tests-should-match-message "5*e" (kmacro-cycle-ring-previous nil))
400 ;; Swapping the top two should give #4.
401 (kmacro-tests-should-match-message "4*d" (kmacro-swap-ring))
402 ;; Delete the top and expect #5.
403 (kmacro-tests-should-match-message "5*e" (kmacro-delete-ring-head))))
406 (kmacro-tests-deftest kmacro-tests-test-ring-commands-when-no-macros ()
407 "Ring commands give appropriate message when no macros exist."
408 (dolist (cmd '((kmacro-cycle-ring-next nil)
409 (kmacro-cycle-ring-previous nil)
411 (kmacro-delete-ring-head)
412 (kmacro-view-ring-2nd)
413 (kmacro-call-ring-2nd nil)
414 (kmacro-view-macro)))
415 (kmacro-tests-should-match-message "No keyboard macro defined"
416 (kmacro-tests-simulate-command cmd))))
418 (kmacro-tests-deftest kmacro-tests-repeat-on-last-key ()
419 "Kmacro commands can be run in sequence without prefix keys."
420 (let* ((prefix (where-is-internal 'kmacro-keymap nil t))
421 ;; Make a sequence of events to run.
422 ;; Comments are expected output of mock macros
423 ;; on the first and second run of the sequence (see below).
424 (events (mapcar #'kmacro-tests-get-kmacro-key
425 '(kmacro-end-or-call-macro-repeat ;c / b
426 kmacro-end-or-call-macro-repeat ;c / b
427 kmacro-call-ring-2nd-repeat ;b / a
428 kmacro-cycle-ring-next
429 kmacro-end-or-call-macro-repeat ;a / a
430 kmacro-cycle-ring-previous
431 kmacro-end-or-call-macro-repeat ;c / b
432 kmacro-delete-ring-head
433 kmacro-end-or-call-macro-repeat ;b / a
435 (kmacro-tests-macros (list [?a] [?b] [?c]))
436 ;; What we want kmacro to see as keyboard command sequence
437 (first-event (seq-concatenate
440 (vector (kmacro-tests-get-kmacro-key
441 'kmacro-end-or-call-macro-repeat)))))
443 ;; standardize repeat options
444 ((kmacro-repeat-no-prefix t)
445 (kmacro-call-repeat-key t)
446 (kmacro-call-repeat-with-arg nil))
447 ;; "Record" two macros
449 (kmacro-tests-simulate-command '(kmacro-start-macro nil))
450 (kmacro-tests-simulate-command '(kmacro-end-macro nil)))
451 ;; Start recording #3
452 (kmacro-tests-simulate-command '(kmacro-start-macro nil))
454 ;; Set up pending keyboard events and a fresh buffer
455 ;; kmacro-set-counter is not one of the repeating kmacro
456 ;; commands so it should end the sequence.
457 (let* ((end-key (kmacro-tests-get-kmacro-key 'kmacro-set-counter))
458 (kmacro-tests-events (append events (list end-key))))
459 (cl-letf (((symbol-function #'this-single-command-keys)
460 (lambda () first-event)))
461 (use-local-map kmacro-tests-keymap)
462 (kmacro-tests-should-insert "ccbacb"
463 ;; End #3 and launch loop to read events.
464 (kmacro-end-or-call-macro-repeat nil))))
466 ;; `kmacro-edit-macro-repeat' should also stop the sequence,
467 ;; so run it again with that at the end.
468 (let* ((end-key (kmacro-tests-get-kmacro-key 'kmacro-edit-macro-repeat))
469 (kmacro-tests-events (append events (list end-key))))
470 (cl-letf (((symbol-function #'edit-kbd-macro) #'ignore)
471 ((symbol-function #'this-single-command-keys)
472 (lambda () first-event)))
473 (use-local-map kmacro-tests-keymap)
474 (kmacro-tests-should-insert "bbbbbaaba"
475 (kmacro-end-or-call-macro-repeat 3)))))))
477 (kmacro-tests-deftest kmacro-tests-repeat-view-and-run ()
478 "Kmacro view cycles through ring and executes macro just viewed."
479 (let* ((prefix (where-is-internal 'kmacro-keymap nil t))
481 (mapcar #'kmacro-tests-get-kmacro-key
482 (append (make-list 5 'kmacro-view-macro-repeat)
483 '(kmacro-end-or-call-macro-repeat
484 kmacro-set-counter))))
485 ;; Make kmacro see this as keyboard command sequence.
486 (first-event (seq-concatenate
489 (vector (kmacro-tests-get-kmacro-key
490 'kmacro-view-macro-repeat))))
491 ;; Construct a regexp to match the messages which should be
492 ;; produced by repeated view-repeats.
493 (macros-regexp (apply #'concat
494 (mapcar (lambda (c) (format ".+%s\n" c))
495 '("d" "c" "b" "a" "d" "c")))))
496 (cl-letf ((kmacro-repeat-no-prefix t)
497 (kmacro-call-repeat-key t)
498 (kmacro-call-repeat-with-arg nil)
499 ((symbol-function #'this-single-command-keys) (lambda ()
501 ;; "Record" some macros.
503 (kmacro-tests-define-macro (make-vector 1 (+ ?a n))))
505 (use-local-map kmacro-tests-keymap)
506 ;; 6 views (the direct call plus the 5 in events) should
507 ;; cycle through the ring and get to the second-to-last
509 (kmacro-tests-should-insert "c"
510 (kmacro-tests-should-match-message macros-regexp
511 (kmacro-tests-simulate-command '(kmacro-view-macro-repeat nil)))))))
513 (kmacro-tests-deftest kmacro-tests-bind-to-key-when-recording ()
514 "Bind to key doesn't bind a key during macro recording."
515 (cl-letf ((global-map global-map)
516 (saved-binding (key-binding "\C-a"))
517 (kmacro-tests-sequences (list "\C-a")))
518 (kmacro-tests-simulate-command '(kmacro-start-macro 1))
519 (kmacro-bind-to-key nil)
520 (should (eq saved-binding (key-binding "\C-a")))))
522 (kmacro-tests-deftest kmacro-tests-name-or-bind-to-key-when-no-macro ()
523 "Bind to key, symbol or register fails when when no macro exists."
524 (should-error (kmacro-bind-to-key nil))
525 (should-error (kmacro-name-last-macro 'kmacro-tests-symbol-for-test))
526 (should-error (kmacro-to-register)))
528 (kmacro-tests-deftest kmacro-tests-bind-to-key-bad-key-sequence ()
529 "Bind to key fails to bind to ^G."
530 (let ((global-map global-map)
531 (saved-binding (key-binding "\C-g"))
532 (kmacro-tests-sequences (list "\C-g")))
533 (kmacro-tests-define-macro [1])
534 (kmacro-bind-to-key nil)
535 (should (eq saved-binding (key-binding "\C-g")))))
537 (kmacro-tests-deftest kmacro-tests-bind-to-key-with-key-sequence-in-use ()
538 "Bind to key respects yes-or-no-p when given already bound key sequence."
539 (kmacro-tests-define-macro (vconcat "abaab"))
540 (let ((global-map global-map)
541 (map (make-sparse-keymap))
542 (kmacro-tests-sequences (make-list 2 "\C-hi")))
543 (define-key map "\C-hi" 'info)
545 ;; Try the command with yes-or-no-p set up to say no.
546 (cl-letf (((symbol-function #'yes-or-no-p)
548 (should (string-match-p "info" prompt))
549 (should (string-match-p "C-h i" prompt))
551 (kmacro-bind-to-key nil))
553 (should (equal (where-is-internal 'info nil t)
555 ;; Try it again with yes.
556 (cl-letf (((symbol-function #' yes-or-no-p)
557 (lambda (_prompt) t)))
558 (kmacro-bind-to-key nil))
560 (should-not (equal (where-is-internal 'info global-map t)
563 (kmacro-tests-should-insert "abaab"
564 (funcall (key-binding "\C-hi")))))
566 (kmacro-tests-deftest kmacro-tests-kmacro-bind-to-single-key ()
567 "Bind to key uses C-x C-k A when asked to bind to A."
568 (let ((global-map global-map)
569 (kmacro-tests-macros (list (string-to-vector "\C-cxi"))))
570 (use-local-map kmacro-tests-keymap)
572 ;; Record a macro with counter and format set.
573 (kmacro-set-format "<%d>")
574 (kmacro-tests-simulate-command '(kmacro-start-macro-or-insert-counter 5))
575 (kmacro-tests-simulate-command '(kmacro-end-macro nil))
577 (let ((kmacro-tests-sequences (list "A")))
578 (kmacro-bind-to-key nil))
580 ;; Record a second macro with different counter and format.
581 (kmacro-set-format "%d")
582 (kmacro-tests-define-macro [2])
584 ;; Check the bound key and run it and verify correct counter
586 (should (equal (string-to-vector "\C-cxi")
587 (car (kmacro-extract-lambda
588 (key-binding "\C-x\C-kA")))))
589 (kmacro-tests-should-insert "<5>"
590 (funcall (key-binding "\C-x\C-kA")))))
592 (kmacro-tests-deftest kmacro-tests-name-last-macro-unable-to-bind ()
593 "Name last macro won't bind to symbol which is already bound."
594 (kmacro-tests-define-macro [1])
595 ;; Set up a test symbol which looks like a function.
596 (setplist 'kmacro-tests-symbol-for-test nil)
597 (fset 'kmacro-tests-symbol-for-test #'ignore)
598 (should-error (kmacro-name-last-macro 'kmacro-tests-symbol-for-test))
599 ;; The empty string symbol also can't be bound.
600 (should-error (kmacro-name-last-macro (make-symbol ""))))
602 (kmacro-tests-deftest kmacro-tests-name-last-macro-bind-and-rebind ()
603 "Name last macro can rebind a symbol it binds."
604 ;; Make sure our symbol is unbound.
605 (when (fboundp 'kmacro-tests-symbol-for-test)
606 (fmakunbound 'kmacro-tests-symbol-for-test))
607 (setplist 'kmacro-tests-symbol-for-test nil)
608 ;; Make two macros and bind them to the same symbol.
610 (kmacro-tests-define-macro (make-vector (1+ i) (+ ?a i)))
611 (kmacro-name-last-macro 'kmacro-tests-symbol-for-test)
612 (should (fboundp 'kmacro-tests-symbol-for-test)))
614 ;; Now run the function bound to the symbol. Result should be the
616 (kmacro-tests-should-insert "bb"
617 (kmacro-tests-simulate-command '(kmacro-tests-symbol-for-test))))
619 (kmacro-tests-deftest kmacro-tests-store-in-register ()
620 "Macro can be stored in and retrieved from a register."
621 (use-local-map kmacro-tests-keymap)
622 ;; Save and restore register 200 so we can use it for the test.
623 (let ((saved-reg-contents (get-register 200)))
626 ;; Define a macro, and save it to a register.
627 (kmacro-tests-define-macro (vconcat "a\C-a\C-cxu"))
628 (kmacro-to-register 200)
629 ;; Then make a new different macro.
630 (kmacro-tests-define-macro (vconcat "bb\C-a\C-cxu"))
631 ;; When called from the register, result should be first macro.
632 (kmacro-tests-should-insert "AAA"
633 (kmacro-tests-simulate-command '(jump-to-register 200 3) 3))
634 (kmacro-tests-should-insert "a C-a C-c x u"
635 (kmacro-tests-simulate-command '(insert-register 200 t) '(4))))
636 (set-register 200 saved-reg-contents))))
638 (kmacro-tests-deftest kmacro-tests-step-edit-act ()
639 "Step-edit steps-through a macro with act and act-repeat."
640 (kmacro-tests-run-step-edit "he\C-u2lo"
641 :events (make-list 6 'act)
643 :macro-result "he\C-u2lo")
645 (kmacro-tests-run-step-edit "f\C-aoo\C-abar"
646 :events (make-list 5 'act-repeat)
648 :macro-result "f\C-aoo\C-abar"))
650 (kmacro-tests-deftest kmacro-tests-step-edit-skip ()
651 "Step-editing can skip parts of macro."
652 (kmacro-tests-run-step-edit "ofoofff"
653 :events '(skip skip-keep skip-keep skip-keep
656 :macro-result "foo"))
658 (kmacro-tests-deftest kmacro-tests-step-edit-quit ()
659 "Quit while step-editing leaves macro unchanged."
660 (kmacro-tests-run-step-edit "bar"
661 :events '(help insert skip help quit)
662 :sequences '("f" "o" "o" "\C-j")
664 :macro-result "bar"))
666 (kmacro-tests-deftest kmacro-tests-step-insert ()
667 "Step edit can insert in macro."
668 (kmacro-tests-run-step-edit "fbazbop"
669 :events '(insert act insert-1 act-repeat)
670 :sequences '("o" "o" "\C-a" "\C-j" "\C-e")
672 :macro-result "oo\C-af\C-ebazbop"))
674 (kmacro-tests-deftest kmacro-tests-step-edit-replace-digit-argument ()
675 "Step-edit replace can replace a numeric argument in a macro.
676 This is a regression for item 1 in Bug#24991."
677 (:expected-result :failed)
678 (kmacro-tests-run-step-edit "\C-u3b\C-a\C-cxu"
679 :events '(act replace automatic)
680 :sequences '("8" "x" "\C-j")
682 :macro-result "\C-u8x\C-a\C-cxu"))
684 (kmacro-tests-deftest kmacro-tests-step-edit-replace ()
685 "Step-edit replace and replace-1 can replace parts of a macro."
686 (kmacro-tests-run-step-edit "a\C-a\C-cxu"
687 :events '(act act replace)
688 :sequences '("b" "c" "\C-j")
690 :macro-result "a\C-abc")
691 (kmacro-tests-run-step-edit "a\C-a\C-cxucd"
692 :events '(act replace-1 automatic)
695 :macro-result "ab\C-cxucd")
696 (kmacro-tests-run-step-edit "by"
697 :events '(act replace)
698 :sequences '("a" "r" "\C-j")
700 :macro-result "bar"))
702 (kmacro-tests-deftest kmacro-tests-step-edit-append ()
703 "Step edit append inserts after point, and append-end inserts at end."
704 (kmacro-tests-run-step-edit "f-b"
705 :events '(append append-end)
706 :sequences '("o" "o" "\C-j" "a" "r" "\C-j")
708 :macro-result "foo-bar")
709 (kmacro-tests-run-step-edit "x"
711 :sequences '("\C-a" "\C-cxu" "\C-e" "y" "\C-j")
713 :macro-result "x\C-a\C-cxu\C-ey"))
715 (kmacro-tests-deftest kmacro-tests-append-end-at-end-appends ()
716 "Append-end when already at end of macro appends to end of macro.
717 This is a regression for item 2 in Bug#24991."
718 (:expected-result :failed)
719 (kmacro-tests-run-step-edit "x"
720 :events '(append-end)
721 :sequences '("\C-a" "\C-cxu" "\C-e" "y" "\C-j")
723 :macro-result "x\C-a\C-cxu\C-ey"))
726 (kmacro-tests-deftest kmacro-tests-step-edit-skip-entire ()
727 "Skipping a whole macro in step-edit leaves macro unchanged.
728 This is a regression for item 3 in Bug#24991."
729 (:expected-result :failed)
730 (kmacro-tests-run-step-edit "xyzzy"
733 :macro-result "xyzzy"))
735 (kmacro-tests-deftest kmacro-tests-step-edit-step-through-negative-argument ()
736 "Step edit works on macros using negative universal argument.
737 This is a regression for item 4 in Bug#24991."
738 (:expected-result :failed)
739 (kmacro-tests-run-step-edit "boo\C-u-\C-cu"
740 :events '(act-repeat automatic)
742 :macro-result "boo\C-u-\C-cd"))
744 (kmacro-tests-deftest kmacro-tests-step-edit-with-quoted-insert ()
745 "Stepping through a macro that uses quoted insert leaves macro unchanged.
746 This is a regression for item 5 in Bug#24991."
747 (:expected-result :failed)
748 (let ((read-quoted-char-radix 8))
749 (kmacro-tests-run-step-edit "\C-cxq17051i there"
750 :events '(act automatic)
752 :macro-result "\C-cxq17051i there")
753 (kmacro-tests-run-step-edit "g\C-cxq17051i"
754 :events '(act insert-1 automatic)
757 :macro-result "g-\C-cxq17051i")))
759 (kmacro-tests-deftest kmacro-tests-step-edit-can-replace-meta-keys ()
760 "Replacing C-w with M-w produces the expected result.
761 This is a regression for item 7 in Bug#24991."
762 (:expected-result :failed)
763 (kmacro-tests-run-step-edit "abc\C-b\C-b\C-SPC\C-f\C-w\C-e\C-y"
764 :events '(act-repeat act-repeat
765 act-repeat act-repeat
767 :sequences '("\M-w" "\C-j")
769 :macro-result "abc\C-b\C-b\C-SPC\C-f\M-w\C-e\C-y")
770 (kmacro-tests-should-insert "abcb" (kmacro-call-macro nil)))
772 (kmacro-tests-deftest kmacro-tests-step-edit-ignores-qr-map-commands ()
773 "Unimplemented commands from `query-replace-map' are ignored."
774 (kmacro-tests-run-step-edit "yep"
775 :events '(edit-replacement
776 act-and-show act-and-exit
779 scroll-up scroll-down
781 scroll-other-window-down
785 :macro-result "yep"))
787 (kmacro-tests-deftest
788 kmacro-tests-step-edit-edits-macro-with-extended-command ()
789 "Step-editing a macro which uses the minibuffer can change the macro."
790 (let ((mac (vconcat [?\M-x] "eval-expression" '[return]
791 "(insert-char (+ ?a \C-e" [?
1] "))" '[return]))
792 (mac-after (vconcat [?\M-x] "eval-expression" '[return]
793 "(insert-char (+ ?a \C-e" [?
2] "))" '[return])))
795 (kmacro-tests-run-step-edit mac
796 :events '(act act-repeat
798 replace-1 act-repeat act)
801 :macro-result mac-after)))
803 (kmacro-tests-deftest kmacro-tests-step-edit-step-through-isearch ()
804 "Step-editing can edit a macro which uses `isearch-backward' (Bug#22488)."
805 (:expected-result :failed)
806 (let ((mac (vconcat "test Input" '[return]
807 [?\C-r
] "inp" '[return] "\C-cxu"))
808 (mac-after (vconcat "test input" '[return]
809 [?\C-r
] "inp" '[return] "\C-cd")))
811 (kmacro-tests-run-step-edit mac
812 :events '(act-repeat act act
815 :sequences '("\C-cd")
816 :result "test input\n"
817 :macro-result mac-after)))
819 (kmacro-tests-deftest kmacro-tests-step-edit-cleans-up-hook ()
820 "Step-editing properly cleans up `post-command-hook.' (Bug #18708)"
821 (let (post-command-hook)
822 (setq-local post-command-hook '(t))
823 (kmacro-tests-run-step-edit "x"
827 (kmacro-tests-simulate-command '(beginning-of-line))))
829 (cl-defun kmacro-tests-run-step-edit
830 (macro &key events sequences result macro-result)
831 "Set up and run a test of `kmacro-step-edit-macro'.
833 Run `kmacro-step-edit-macro' with MACRO defined as a keyboard macro
834 and `read-event' and `read-key-sequence' set up to return items from
835 EVENTS and SEQUENCES respectively. SEQUENCES may be nil, but
836 EVENTS should not be. EVENTS should be a list of symbols bound
837 in `kmacro-step-edit-map' or `query-replace' map, and this function
838 will do the keymap lookup for you. SEQUENCES should contain
839 return values for `read-key-sequence'.
841 Before running the macro, the current buffer will be erased.
842 RESULT is the string that should be inserted during the
843 step-editing process, and MACRO-RESULT is the expected value of
844 `last-kbd-macro' after the editing is complete."
846 (let* ((kmacro-tests-events (mapcar #'kmacro-tests-get-kmacro-step-edit-key events))
847 (kmacro-tests-sequences sequences))
849 (kmacro-tests-define-macro (string-to-vector macro))
850 (use-local-map kmacro-tests-keymap)
852 (kmacro-step-edit-macro)
854 (should (equal result (buffer-string))))
856 (should (equal last-kbd-macro (string-to-vector macro-result))))))
860 (defun kmacro-tests-simulate-command (command &optional arg)
861 "Call `ert-simulate-command' after setting `current-prefix-arg'.
862 Sets `current-prefix-arg' to ARG if it is non-nil, otherwise to
863 the second element of COMMAND, before executing COMMAND using
864 `ert-simulate-command'."
865 (let ((current-prefix-arg (or arg (cadr command))))
866 (ert-simulate-command command)))
868 (defun kmacro-tests-define-macro (mac)
869 "Define MAC as a keyboard macro using kmacro commands."
870 (push mac kmacro-tests-macros)
871 (kmacro-tests-simulate-command '(kmacro-start-macro nil))
872 (should defining-kbd-macro)
873 (kmacro-tests-simulate-command '(kmacro-end-macro nil))
874 (should (equal mac last-kbd-macro)))
876 (defun kmacro-tests-get-kmacro-key (sym)
877 "Look up kmacro command SYM in kmacro's keymap.
878 Return the integer key value found."
879 (aref (where-is-internal sym kmacro-keymap t) 0))
881 (defun kmacro-tests-get-kmacro-step-edit-key (sym)
882 "Return the first key bound to SYM in `kmacro-step-edit-map'."
883 (let ((where (aref (where-is-internal sym kmacro-step-edit-map t) 0)))
888 (provide 'kmacro-tests)
890 ;;; kmacro-tests.el ends here