summaryrefslogtreecommitdiff
path: root/test/lisp/kmacro-tests.el
blob: c891072076332a860f2e3dcef2b71189d3e77bb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
;;; kmacro-tests.el --- Tests for kmacro.el       -*- lexical-binding: t; -*-

;; Copyright (C) 2017-2021 Free Software Foundation, Inc.

;; Author: Gemini Lasswell <gazally@runbox.com>

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

(require 'kmacro)
(require 'seq)
(require 'ert)
(require 'ert-x)

;;; Test fixtures:

(defmacro kmacro-tests-with-kmacro-clean-slate (&rest body)
  "Create a clean environment for a kmacro test BODY to run in."
  (declare (debug (body)))
  `(cl-letf* ((kmacro-execute-before-append t)
              (kmacro-ring-max 8)
              (kmacro-repeat-no-prefix t)
              (kmacro-call-repeat-key nil)
              (kmacro-call-repeat-with-arg nil)

              (kbd-macro-termination-hook nil)
              (defining-kbd-macro nil)
              (executing-kbd-macro nil)
              (executing-kbd-macro-index 0)
              (last-kbd-macro nil)

              (kmacro-ring nil)

              (kmacro-counter 0)
              (kmacro-default-counter-format "%d")
              (kmacro-counter-format "%d")
              (kmacro-counter-format-start "%d")
              (kmacro-counter-value-start 0)
              (kmacro-last-counter 0)
              (kmacro-initial-counter-value nil)

              (kmacro-tests-macros nil)
              (kmacro-tests-events nil)
              (kmacro-tests-sequences nil))
     (advice-add 'end-kbd-macro :after #'kmacro-tests-end-macro-advice)
     (advice-add 'read-event :around #'kmacro-tests-read-event-advice )
     (advice-add 'read-key-sequence :around #'kmacro-tests-read-key-sequence-advice)
     (unwind-protect
         (ert-with-test-buffer (:name "")
           (switch-to-buffer (current-buffer))
           ,@body)
       (advice-remove 'read-key-sequence #'kmacro-tests-read-key-sequence-advice)
       (advice-remove 'read-event #'kmacro-tests-read-event-advice)
       (advice-remove 'end-kbd-macro #'kmacro-tests-end-macro-advice))))

(defmacro kmacro-tests-deftest (name _args docstring &rest keys-and-body)
  "Define a kmacro unit test.
NAME is the name of the test, _ARGS should be nil, and DOCSTRING
is required.  To avoid having to duplicate ert's keyword parsing
here, its keywords and values (if any) must be inside a list
after the docstring, preceding the body, here combined with the
body in KEYS-AND-BODY."
  (declare (debug (&define name sexp stringp
                           [&optional (&rest &or [keywordp sexp])]
                           def-body))
           (doc-string 3)
           (indent 2))

  (let* ((keys (when (and (listp (car keys-and-body))
                          (keywordp (caar keys-and-body)))
                 (car keys-and-body)))
         (body (if keys (cdr keys-and-body)
                 keys-and-body)))
    `(ert-deftest ,name ()
       ,docstring ,@keys
       (kmacro-tests-with-kmacro-clean-slate ,@body))))

(defvar kmacro-tests-keymap
  (let ((map (make-sparse-keymap)))
    (dotimes (i 26)
      (define-key map (string (+ ?a i)) 'self-insert-command))
    (dotimes (i 10)
      (define-key map (string (+ ?0 i)) 'self-insert-command))
    ;; Define a few key sequences of different lengths.
    (dolist (item '(("\C-a"     . beginning-of-line)
                    ("\C-b"     . backward-char)
                    ("\C-e"     . end-of-line)
                    ("\C-f"     . forward-char)
                    ("\C-r"     . isearch-backward)
                    ("\C-u"     . universal-argument)
                    ("\C-w"     . kill-region)
                    ("\C-SPC"   . set-mark-command)
                    ("\M-w"     . kill-ring-save)
                    ("\M-x"     . execute-extended-command)
                    ("\C-cd"    . downcase-word)
                    ("\C-cxu"   . upcase-word)
                    ("\C-cxq"   . quoted-insert)
                    ("\C-cxi"   . kmacro-insert-counter)
                    ("\C-x\C-k" . kmacro-keymap)))
      (define-key map (car item) (cdr item)))
    map)
  "Keymap to use for testing keyboard macros.
This is used to obtain consistent results even if tests are run
in an environment with rebound keys.")

(defvar kmacro-tests-events nil
  "Input events used by the kmacro test in progress.")

(defun kmacro-tests-read-event-advice (orig-func &rest args)
  "Pop and return an event from `kmacro-tests-events'.
Return the result of calling ORIG-FUNC with ARGS if
`kmacro-tests-events' is empty, or if a keyboard macro is
running."
  (if (or executing-kbd-macro (null kmacro-tests-events))
      (apply orig-func args)
    (pop kmacro-tests-events)))

(defvar kmacro-tests-sequences nil
  "Input sequences used by the kmacro test in progress.")

(defun kmacro-tests-read-key-sequence-advice (orig-func &rest args)
  "Pop and return a string from `kmacro-tests-sequences'.
Return the result of calling ORIG-FUNC with ARGS if
`kmacro-tests-sequences' is empty, or if a keyboard macro is
running."
  (if (or executing-kbd-macro (null kmacro-tests-sequences))
      (apply orig-func args)
    (pop kmacro-tests-sequences)))

(defvar kmacro-tests-macros nil
  "Keyboard macros (in vector form) used by the kmacro test in progress.")

(defun kmacro-tests-end-macro-advice (&rest _args)
  "Pop a macro from `kmacro-tests-macros' and assign it to `last-kbd-macro'.
If `kmacro-tests-macros' is empty, do nothing."
  (when kmacro-tests-macros
    (setq last-kbd-macro (pop kmacro-tests-macros))))

;;; Some more powerful expectations:

(defmacro kmacro-tests-should-insert (value &rest body)
  "Verify that VALUE is inserted by the execution of BODY.
Execute BODY, then check that the string VALUE was inserted
into the current buffer at point."
  (declare (debug (stringp body))
           (indent 1))
  (let ((g-p (cl-gensym))
        (g-bsize (cl-gensym)))
    `(let ((,g-p (point))
           (,g-bsize (buffer-size)))
       ,@body
       (should (equal (buffer-substring ,g-p (point)) ,value))
       (should (equal (- (buffer-size) ,g-bsize) (length ,value))))))

(defmacro kmacro-tests-should-match-message (value &rest body)
  "Verify that a message matching VALUE is issued while executing BODY.
Execute BODY, and then if there is not a regexp match between
VALUE and any text written to *Messages* during the execution,
cause the current test to fail."
  (declare (debug (form body))
           (indent 1))
  (let ((g-captured-messages (cl-gensym)))
    `(ert-with-message-capture ,g-captured-messages
       ,@body
       (should (string-match-p ,value ,g-captured-messages)))))

;;; Tests:

(kmacro-tests-deftest kmacro-tests-test-insert-counter-01-nil ()
  "`kmacro-insert-counter' adds one to macro counter with nil arg."
  (kmacro-tests-should-insert "0"
    (kmacro-tests-simulate-command '(kmacro-insert-counter nil)))
  (kmacro-tests-should-insert "1"
    (kmacro-tests-simulate-command '(kmacro-insert-counter nil))))

(kmacro-tests-deftest kmacro-tests-test-insert-counter-02-int ()
  "`kmacro-insert-counter' increments by value of list argument."
  (kmacro-tests-should-insert "0"
    (kmacro-tests-simulate-command '(kmacro-insert-counter 2)))
  (kmacro-tests-should-insert "2"
    (kmacro-tests-simulate-command '(kmacro-insert-counter 3)))
  (kmacro-tests-should-insert "5"
    (kmacro-tests-simulate-command '(kmacro-insert-counter nil))))

(kmacro-tests-deftest kmacro-tests-test-insert-counter-03-list ()
  "`kmacro-insert-counter' doesn't increment when given universal argument."
  (kmacro-tests-should-insert "0"
    (kmacro-tests-simulate-command '(kmacro-insert-counter (16))))
  (kmacro-tests-should-insert "0"
    (kmacro-tests-simulate-command '(kmacro-insert-counter (4)))))

(kmacro-tests-deftest kmacro-tests-test-insert-counter-04-neg ()
  "`kmacro-insert-counter' decrements with '- prefix argument"
  (kmacro-tests-should-insert "0"
    (kmacro-tests-simulate-command '(kmacro-insert-counter -)))
  (kmacro-tests-should-insert "-1"
    (kmacro-tests-simulate-command '(kmacro-insert-counter nil))))

(kmacro-tests-deftest kmacro-tests-test-start-format-counter ()
  "`kmacro-insert-counter' uses start value and format."
  (kmacro-tests-simulate-command '(kmacro-set-counter 10))
  (kmacro-tests-should-insert "10"
    (kmacro-tests-simulate-command '(kmacro-insert-counter nil)))
  (kmacro-tests-should-insert "11"
    (kmacro-tests-simulate-command '(kmacro-insert-counter nil)))
  (kmacro-set-format "c=%s")
  (kmacro-tests-simulate-command '(kmacro-set-counter 50))
  (kmacro-tests-should-insert "c=50"
    (kmacro-tests-simulate-command '(kmacro-insert-counter nil))))

(kmacro-tests-deftest kmacro-tests-test-start-macro-when-defining-macro ()
  "Starting a macro while defining a macro does not start a second macro."
  (kmacro-tests-simulate-command '(kmacro-start-macro nil))
  ;; We should now be in the macro-recording state.
  (should defining-kbd-macro)
  (should-not last-kbd-macro)
  ;; Calling it again should leave us in the same state.
  (kmacro-tests-simulate-command '(kmacro-start-macro nil))
  (should defining-kbd-macro)
  (should-not last-kbd-macro))


(kmacro-tests-deftest kmacro-tests-set-macro-counter-while-defining ()
  "Use of the prefix arg with kmacro-start sets kmacro-counter."
  ;; Give kmacro-start-macro an argument.
  (kmacro-tests-simulate-command '(kmacro-start-macro 5))
  (should defining-kbd-macro)
  ;; Verify that the counter is set to that value.
  (kmacro-tests-should-insert "5"
    (kmacro-tests-simulate-command '(kmacro-insert-counter nil)))
  ;; Change it while defining a macro.
  (kmacro-tests-simulate-command '(kmacro-set-counter 1))
  (kmacro-tests-should-insert "1"
    (kmacro-tests-simulate-command '(kmacro-insert-counter nil)))
  ;; Using universal arg to set counter should reset to starting value.
  (kmacro-tests-simulate-command '(kmacro-set-counter (4)) '(4))
  (kmacro-tests-should-insert "5"
    (kmacro-tests-simulate-command '(kmacro-insert-counter nil))))


(kmacro-tests-deftest kmacro-tests-start-insert-counter-appends-to-macro ()
  "Use of the universal arg appends to the previous macro."
  (let ((kmacro-tests-macros (list (string-to-vector "hello"))))
    ;; Start recording a macro.
    (kmacro-tests-simulate-command '(kmacro-start-macro-or-insert-counter nil))
    ;; Make sure we are recording.
    (should defining-kbd-macro)
    ;; Call it again and it should insert the counter.
    (kmacro-tests-should-insert "0"
      (kmacro-tests-simulate-command '(kmacro-start-macro-or-insert-counter nil)))
    ;; We should still be in the recording state.
    (should defining-kbd-macro)
    ;; End recording with repeat count.
    (kmacro-tests-simulate-command '(kmacro-end-or-call-macro 3))
    ;; Recording should be finished.
    (should-not defining-kbd-macro)
    ;; Now use prefix arg to append to the previous macro.
    ;; This should run the previous macro first.
    (kmacro-tests-should-insert "hello"
      (kmacro-tests-simulate-command
       '(kmacro-start-macro-or-insert-counter (4))))
    ;;  Verify that the recording state has changed.
    (should (equal defining-kbd-macro 'append))))

(kmacro-tests-deftest kmacro-tests-end-call-macro-prefix-args ()
  "kmacro-end-call-macro changes behavior based on prefix arg."
  ;; "Record" two macros.
  (dotimes (i 2)
    (kmacro-tests-define-macro (vconcat (format "macro #%d" (1+  i)))))
  ;; With no prefix arg, it should call the second macro.
  (kmacro-tests-should-insert "macro #2"
    (kmacro-tests-simulate-command '(kmacro-end-or-call-macro nil)))
  ;; With universal arg, it should call the first one.
  (kmacro-tests-should-insert "macro #1"
    (kmacro-tests-simulate-command '(kmacro-end-or-call-macro (4)))))

(kmacro-tests-deftest kmacro-tests-end-and-call-macro ()
  "Keyboard command to end and call macro works under various conditions."
  ;; First, try it with no macro to record.
  (setq kmacro-tests-macros '(""))
  (kmacro-tests-simulate-command '(kmacro-start-macro nil))
  (condition-case err
      (kmacro-tests-simulate-command '(kmacro-end-and-call-macro 2) 2)
    (error (should (string= (cadr err)
                            "No kbd macro has been defined"))))

  ;; Check that it stopped defining and that no macro was recorded.
  (should-not defining-kbd-macro)
  (should-not last-kbd-macro)

  ;; Now try it while not recording, but first record a non-nil macro.
  (kmacro-tests-define-macro "macro")
  (kmacro-tests-should-insert "macro"
    (kmacro-tests-simulate-command '(kmacro-end-and-call-macro nil))))

(kmacro-tests-deftest kmacro-tests-end-and-call-macro-mouse ()
  "Commands to end and call macro work under various conditions.
This is a regression test for Bug#24992."
  (cl-letf (((symbol-function #'mouse-set-point) #'ignore))
    ;; First, try it with no macro to record.
    (setq kmacro-tests-macros '(""))
    (kmacro-tests-simulate-command '(kmacro-start-macro nil))
    (condition-case err
        (kmacro-tests-simulate-command '(kmacro-end-call-mouse 2) 2)
      (error (should (string= (cadr err)
                              "No kbd macro has been defined"))))

    ;; Check that it stopped defining and that no macro was recorded.
    (should-not defining-kbd-macro)
    (should-not last-kbd-macro)

    ;; Now try it while not recording, but first record a non-nil macro.
    (kmacro-tests-define-macro "macro")
    (kmacro-tests-should-insert "macro"
      (kmacro-tests-simulate-command '(kmacro-end-call-mouse nil)))))

(kmacro-tests-deftest kmacro-tests-call-macro-hint-and-repeat ()
  "`kmacro-call-macro' gives hint in Messages and sets up repeat keymap.
This is a regression test for: Bug#3412, Bug#11817."
  (kmacro-tests-define-macro [?m])
  (let ((kmacro-call-repeat-key t)
        (kmacro-call-repeat-with-arg t)
        (overriding-terminal-local-map overriding-terminal-local-map)
        (last-input-event ?e))
    (message "")  ; Clear the echo area. (Bug#3412)
    (kmacro-tests-should-match-message "Type e to repeat macro"
      (kmacro-tests-should-insert "mmmmmm"
        (cl-letf (((symbol-function #'this-single-command-keys) (lambda ()
                                                                  [?\C-x ?e])))
          (kmacro-call-macro 3))
        ;; Check that it set up for repeat, and run the repeat.
        (funcall (lookup-key overriding-terminal-local-map "e"))))))

(kmacro-tests-deftest
    kmacro-tests-run-macro-command-recorded-in-macro ()
  "No infinite loop if `kmacro-end-and-call-macro' is recorded in the macro.
\(Bug#15126)"
  (:expected-result :failed)
  (ert-skip "Skipping due to Bug#24921 (an ERT bug)")
  (kmacro-tests-define-macro (vconcat "foo" [return] "\M-x"
                                      "kmacro-end-and-call-macro"))
  (use-local-map kmacro-tests-keymap)
  (kmacro-tests-simulate-command '(kmacro-end-and-call-macro nil)))


(kmacro-tests-deftest kmacro-tests-test-ring-2nd-commands ()
  "2nd macro in ring is displayed and executed normally and on repeat."
  (use-local-map kmacro-tests-keymap)
  ;; Record one macro, with count.
  (push (vconcat "\C-cxi" "\C-u\C-cxi") kmacro-tests-macros)
  (kmacro-tests-simulate-command '(kmacro-start-macro 1))
  (kmacro-tests-simulate-command '(kmacro-end-macro nil))
  ;; Check that execute and display do nothing with no 2nd macro.
  (kmacro-tests-should-insert ""
    (kmacro-tests-simulate-command '(kmacro-call-ring-2nd nil)))
  (kmacro-tests-should-match-message "Only one keyboard macro defined"
    (kmacro-tests-simulate-command '(kmacro-view-ring-2nd)))
  ;; Record another one, with format.
  (kmacro-set-format "=%d=")
  (kmacro-tests-define-macro (vconcat "bar"))
  ;; Execute the first one, mocked up to insert counter.
  ;; Should get default format.
  (kmacro-tests-should-insert "11"
    (kmacro-tests-simulate-command '(kmacro-call-ring-2nd nil)))
  ;; Now display the 2nd ring macro and check result.
  (kmacro-tests-should-match-message "C-c x i C-u C-c x i"
    (kmacro-view-ring-2nd)))

(kmacro-tests-deftest kmacro-tests-fill-ring-and-rotate ()
  "Macro ring can shift one way, shift the other way, swap and pop."
  (cl-letf ((kmacro-ring-max 4))
    ;; Record enough macros that the first one drops off the history.
    (dotimes (n (1+ kmacro-ring-max))
      (kmacro-tests-define-macro (make-vector (1+ n) (+ ?a n))))
    ;; Cycle the ring and check that #2 comes up.
    (kmacro-tests-should-match-message "2*b"
      (kmacro-tests-simulate-command '(kmacro-cycle-ring-next nil)))
    ;; Execute the current macro and check arguments.
    (kmacro-tests-should-insert "bbbb"
      (kmacro-call-macro 2 t))
    ;; Cycle the ring the other way; #5 expected.
    (kmacro-tests-should-match-message "5*e" (kmacro-cycle-ring-previous nil))
    ;; Swapping the top two should give #4.
    (kmacro-tests-should-match-message "4*d" (kmacro-swap-ring))
    ;; Delete the top and expect #5.
    (kmacro-tests-should-match-message "5*e" (kmacro-delete-ring-head))))


(kmacro-tests-deftest kmacro-tests-test-ring-commands-when-no-macros ()
  "Ring commands give appropriate message when no macros exist."
  (dolist (cmd '((kmacro-cycle-ring-next nil)
                 (kmacro-cycle-ring-previous nil)
                 (kmacro-swap-ring)
                 (kmacro-delete-ring-head)
                 (kmacro-view-ring-2nd)
                 (kmacro-call-ring-2nd nil)
                 (kmacro-view-macro)))
    (kmacro-tests-should-match-message "No keyboard macro defined"
      (kmacro-tests-simulate-command cmd))))

(kmacro-tests-deftest kmacro-tests-repeat-on-last-key ()
  "Kmacro commands can be run in sequence without prefix keys."
  (let* ((prefix (where-is-internal 'kmacro-keymap nil t))
         ;; Make a sequence of events to run.
         ;; Comments are expected output of mock macros
         ;; on the first and second run of the sequence (see below).
         (events (mapcar #'kmacro-tests-get-kmacro-key
                         '(kmacro-end-or-call-macro-repeat ;c / b
                           kmacro-end-or-call-macro-repeat ;c / b
                           kmacro-call-ring-2nd-repeat     ;b / a
                           kmacro-cycle-ring-next
                           kmacro-end-or-call-macro-repeat ;a / a
                           kmacro-cycle-ring-previous
                           kmacro-end-or-call-macro-repeat ;c / b
                           kmacro-delete-ring-head
                           kmacro-end-or-call-macro-repeat ;b / a
                           )))
         (kmacro-tests-macros (list [?a] [?b] [?c]))
         ;; What we want kmacro to see as keyboard command sequence
         (first-event (seq-concatenate
                       'vector
                       prefix
                       (vector (kmacro-tests-get-kmacro-key
                                'kmacro-end-or-call-macro-repeat)))))
    (cl-letf
        ;; standardize repeat options
        ((kmacro-repeat-no-prefix t)
         (kmacro-call-repeat-key t)
         (kmacro-call-repeat-with-arg nil))
      ;; "Record" two macros
      (dotimes (_n 2)
        (kmacro-tests-simulate-command '(kmacro-start-macro nil))
        (kmacro-tests-simulate-command '(kmacro-end-macro nil)))
      ;; Start recording #3
      (kmacro-tests-simulate-command '(kmacro-start-macro nil))

      ;; Set up pending keyboard events and a fresh buffer
      ;; kmacro-set-counter is not one of the repeating kmacro
      ;; commands so it should end the sequence.
      (let* ((end-key (kmacro-tests-get-kmacro-key 'kmacro-set-counter))
             (kmacro-tests-events (append events (list end-key))))
        (cl-letf (((symbol-function #'this-single-command-keys)
                   (lambda () first-event)))
          (use-local-map kmacro-tests-keymap)
          (kmacro-tests-should-insert "ccbacb"
            ;; End #3 and launch loop to read events.
            (kmacro-end-or-call-macro-repeat nil))))

      ;; `kmacro-edit-macro-repeat' should also stop the sequence,
      ;; so run it again with that at the end.
      (let* ((end-key (kmacro-tests-get-kmacro-key 'kmacro-edit-macro-repeat))
             (kmacro-tests-events (append events (list end-key))))
        (cl-letf (((symbol-function #'edit-kbd-macro) #'ignore)
                  ((symbol-function #'this-single-command-keys)
                   (lambda () first-event)))
          (use-local-map kmacro-tests-keymap)
          (kmacro-tests-should-insert "bbbbbaaba"
            (kmacro-end-or-call-macro-repeat 3)))))))

(kmacro-tests-deftest kmacro-tests-repeat-view-and-run ()
  "Kmacro view cycles through ring and executes macro just viewed."
  (let* ((prefix (where-is-internal 'kmacro-keymap nil t))
         (kmacro-tests-events
          (mapcar #'kmacro-tests-get-kmacro-key
                  (append (make-list 5 'kmacro-view-macro-repeat)
                          '(kmacro-end-or-call-macro-repeat
                            kmacro-set-counter))))
         ;; Make kmacro see this as keyboard command sequence.
         (first-event (seq-concatenate
                       'vector
                       prefix
                       (vector (kmacro-tests-get-kmacro-key
                                'kmacro-view-macro-repeat))))
         ;; Construct a regexp to match the messages which should be
         ;; produced by repeated view-repeats.
         (macros-regexp (apply #'concat
                               (mapcar (lambda (c) (format ".+%s\n" c))
                                       '("d" "c" "b" "a" "d" "c")))))
    (cl-letf ((kmacro-repeat-no-prefix t)
              (kmacro-call-repeat-key t)
              (kmacro-call-repeat-with-arg nil)
              ((symbol-function #'this-single-command-keys) (lambda ()
                                                              first-event)))
      ;; "Record" some macros.
      (dotimes (n 4)
        (kmacro-tests-define-macro (make-vector 1 (+ ?a n))))

      (use-local-map kmacro-tests-keymap)
      ;; 6 views (the direct call plus the 5 in events) should
      ;; cycle through the ring and get to the second-to-last
      ;; macro defined.
      (kmacro-tests-should-insert "c"
        (kmacro-tests-should-match-message macros-regexp
          (kmacro-tests-simulate-command '(kmacro-view-macro-repeat nil)))))))

(kmacro-tests-deftest kmacro-tests-bind-to-key-when-recording ()
  "Bind to key doesn't bind a key during macro recording."
  (cl-letf ((global-map global-map)
            (saved-binding (key-binding "\C-a"))
            (kmacro-tests-sequences (list "\C-a")))
    (kmacro-tests-simulate-command '(kmacro-start-macro 1))
    (kmacro-bind-to-key nil)
    (should (eq saved-binding (key-binding "\C-a")))))

(kmacro-tests-deftest kmacro-tests-name-or-bind-to-key-when-no-macro ()
  "Bind to key, symbol or register fails when when no macro exists."
  (should-error (kmacro-bind-to-key nil))
  (should-error (kmacro-name-last-macro 'kmacro-tests-symbol-for-test))
  (should-error (kmacro-to-register)))

(kmacro-tests-deftest kmacro-tests-bind-to-key-bad-key-sequence ()
  "Bind to key fails to bind to ^G."
  (let ((global-map global-map)
        (saved-binding (key-binding "\C-g"))
        (kmacro-tests-sequences (list "\C-g")))
    (kmacro-tests-define-macro [1])
    (kmacro-bind-to-key nil)
    (should (eq saved-binding (key-binding "\C-g")))))

(kmacro-tests-deftest kmacro-tests-bind-to-key-with-key-sequence-in-use ()
  "Bind to key respects yes-or-no-p when given already bound key sequence."
  (kmacro-tests-define-macro (vconcat "abaab"))
  (let ((global-map global-map)
        (map (make-sparse-keymap))
        (kmacro-tests-sequences (make-list 2 "\C-hi")))
    (define-key map "\C-hi" 'info)
    (use-local-map map)
    ;; Try the command with yes-or-no-p set up to say no.
    (cl-letf (((symbol-function #'yes-or-no-p)
               (lambda (prompt)
                 (should (string-match-p "info" prompt))
                 (should (string-match-p "C-h i" prompt))
                 nil)))
      (kmacro-bind-to-key nil))

    (should (equal (where-is-internal 'info nil t)
                   (vconcat "\C-hi")))
    ;; Try it again with yes.
    (cl-letf (((symbol-function #' yes-or-no-p)
               (lambda (_prompt) t)))
      (kmacro-bind-to-key nil))

    (should-not (equal (where-is-internal 'info global-map t)
                       (vconcat "\C-hi")))
    (use-local-map nil)
    (kmacro-tests-should-insert "abaab"
      (funcall (key-binding "\C-hi")))))

(kmacro-tests-deftest kmacro-tests-kmacro-bind-to-single-key ()
  "Bind to key uses C-x C-k A when asked to bind to A."
  (let ((global-map global-map)
        (kmacro-tests-macros (list (string-to-vector "\C-cxi"))))
    (use-local-map kmacro-tests-keymap)

    ;; Record a macro with counter and format set.
    (kmacro-set-format "<%d>")
    (kmacro-tests-simulate-command '(kmacro-start-macro-or-insert-counter 5))
    (kmacro-tests-simulate-command '(kmacro-end-macro nil))

    (let ((kmacro-tests-sequences (list "A")))
      (kmacro-bind-to-key nil))

    ;; Record a second macro with different counter and format.
    (kmacro-set-format "%d")
    (kmacro-tests-define-macro [2])

    ;; Check the bound key and run it and verify correct counter
    ;; and format.
    (should (equal (string-to-vector "\C-cxi")
                   (car (kmacro-extract-lambda
                         (key-binding "\C-x\C-kA")))))
    (kmacro-tests-should-insert "<5>"
      (funcall (key-binding "\C-x\C-kA")))))

(kmacro-tests-deftest kmacro-tests-name-last-macro-unable-to-bind ()
  "Name last macro won't bind to symbol which is already bound."
  (kmacro-tests-define-macro [1])
  ;; Set up a test symbol which looks like a function.
  (setplist 'kmacro-tests-symbol-for-test nil)
  (fset 'kmacro-tests-symbol-for-test #'ignore)
  (should-error (kmacro-name-last-macro 'kmacro-tests-symbol-for-test))
  ;; The empty string symbol also can't be bound.
  (should-error (kmacro-name-last-macro (make-symbol ""))))

(kmacro-tests-deftest kmacro-tests-name-last-macro-bind-and-rebind ()
  "Name last macro can rebind a symbol it binds."
  ;; Make sure our symbol is unbound.
  (when (fboundp 'kmacro-tests-symbol-for-test)
    (fmakunbound 'kmacro-tests-symbol-for-test))
  (setplist 'kmacro-tests-symbol-for-test nil)
  ;; Make two macros and bind them to the same symbol.
  (dotimes (i 2)
    (kmacro-tests-define-macro (make-vector (1+ i) (+ ?a i)))
    (kmacro-name-last-macro 'kmacro-tests-symbol-for-test)
    (should (fboundp 'kmacro-tests-symbol-for-test)))

  ;; Now run the function bound to the symbol. Result should be the
  ;; second macro.
  (kmacro-tests-should-insert "bb"
    (kmacro-tests-simulate-command '(kmacro-tests-symbol-for-test))))

(kmacro-tests-deftest kmacro-tests-store-in-register ()
  "Macro can be stored in and retrieved from a register."
  (use-local-map kmacro-tests-keymap)
  ;; Save and restore register 200 so we can use it for the test.
  (let ((saved-reg-contents (get-register 200)))
    (unwind-protect
        (progn
          ;; Define a macro, and save it to a register.
          (kmacro-tests-define-macro (vconcat "a\C-a\C-cxu"))
          (kmacro-to-register 200)
          ;; Then make a new different macro.
          (kmacro-tests-define-macro (vconcat "bb\C-a\C-cxu"))
          ;; When called from the register, result should be first macro.
          (kmacro-tests-should-insert "AAA"
            (kmacro-tests-simulate-command '(jump-to-register 200 3) 3))
          (kmacro-tests-should-insert "a C-a C-c x u"
            (kmacro-tests-simulate-command '(insert-register 200 t) '(4))))
      (set-register 200 saved-reg-contents))))

(kmacro-tests-deftest kmacro-tests-step-edit-act ()
  "Step-edit steps-through a macro with act and act-repeat."
  (kmacro-tests-run-step-edit "he\C-u2lo"
                              :events (make-list 6 'act)
                              :result "hello"
                              :macro-result "he\C-u2lo")

  (kmacro-tests-run-step-edit "f\C-aoo\C-abar"
                              :events (make-list 5 'act-repeat)
                              :result "baroof"
                              :macro-result "f\C-aoo\C-abar"))

(kmacro-tests-deftest kmacro-tests-step-edit-skip ()
  "Step-editing can skip parts of macro."
  (kmacro-tests-run-step-edit "ofoofff"
                              :events '(skip skip-keep skip-keep skip-keep
                                             skip-rest)
                              :result ""
                              :macro-result "foo"))

(kmacro-tests-deftest kmacro-tests-step-edit-quit ()
  "Quit while step-editing leaves macro unchanged."
  (kmacro-tests-run-step-edit "bar"
                              :events '(help insert skip help quit)
                              :sequences '("f" "o" "o" "\C-j")
                              :result "foo"
                              :macro-result "bar"))

(kmacro-tests-deftest kmacro-tests-step-insert ()
  "Step edit can insert in macro."
  (kmacro-tests-run-step-edit "fbazbop"
                              :events '(insert act insert-1 act-repeat)
                              :sequences '("o" "o" "\C-a" "\C-j" "\C-e")
                              :result "foobazbop"
                              :macro-result "oo\C-af\C-ebazbop"))

(kmacro-tests-deftest kmacro-tests-step-edit-replace-digit-argument ()
  "Step-edit replace can replace a numeric argument in a macro.
This is a regression for item 1 in Bug#24991."
  (:expected-result :failed)
  (kmacro-tests-run-step-edit "\C-u3b\C-a\C-cxu"
                              :events '(act replace automatic)
                              :sequences '("8" "x" "\C-j")
                              :result "XXXXXXXX"
                              :macro-result "\C-u8x\C-a\C-cxu"))

(kmacro-tests-deftest kmacro-tests-step-edit-replace ()
  "Step-edit replace and replace-1 can replace parts of a macro."
  (kmacro-tests-run-step-edit "a\C-a\C-cxu"
                              :events '(act act replace)
                              :sequences '("b" "c" "\C-j")
                              :result "bca"
                              :macro-result "a\C-abc")
  (kmacro-tests-run-step-edit "a\C-a\C-cxucd"
                              :events '(act replace-1 automatic)
                              :sequences '("b")
                              :result "abcd"
                              :macro-result "ab\C-cxucd")
  (kmacro-tests-run-step-edit "by"
                              :events '(act replace)
                              :sequences '("a" "r" "\C-j")
                              :result "bar"
                              :macro-result "bar"))

(kmacro-tests-deftest kmacro-tests-step-edit-append ()
  "Step edit append inserts after point, and append-end inserts at end."
  (kmacro-tests-run-step-edit "f-b"
                              :events '(append append-end)
                              :sequences '("o" "o" "\C-j" "a" "r" "\C-j")
                              :result "foo-bar"
                              :macro-result "foo-bar")
  (kmacro-tests-run-step-edit "x"
                              :events '(append)
                              :sequences '("\C-a" "\C-cxu" "\C-e" "y" "\C-j")
                              :result "Xy"
                              :macro-result "x\C-a\C-cxu\C-ey"))

(kmacro-tests-deftest kmacro-tests-append-end-at-end-appends ()
  "Append-end when already at end of macro appends to end of macro.
This is a regression for item 2 in Bug#24991."
  (:expected-result :failed)
  (kmacro-tests-run-step-edit "x"
                              :events '(append-end)
                              :sequences '("\C-a" "\C-cxu" "\C-e" "y" "\C-j")
                              :result "Xy"
                              :macro-result "x\C-a\C-cxu\C-ey"))


(kmacro-tests-deftest kmacro-tests-step-edit-skip-entire ()
  "Skipping a whole macro in step-edit leaves macro unchanged.
This is a regression for item 3 in Bug#24991."
  (:expected-result :failed)
  (kmacro-tests-run-step-edit "xyzzy"
                              :events '(skip-rest)
                              :result ""
                              :macro-result "xyzzy"))

(kmacro-tests-deftest kmacro-tests-step-edit-step-through-negative-argument ()
  "Step edit works on macros using negative universal argument.
This is a regression for item 4 in Bug#24991."
  (:expected-result :failed)
  (kmacro-tests-run-step-edit "boo\C-u-\C-cu"
                              :events '(act-repeat automatic)
                              :result "BOO"
                              :macro-result "boo\C-u-\C-cd"))

(kmacro-tests-deftest kmacro-tests-step-edit-with-quoted-insert ()
  "Stepping through a macro that uses quoted insert leaves macro unchanged.
This is a regression for item 5 in Bug#24991."
  (:expected-result :failed)
  (let ((read-quoted-char-radix 8))
    (kmacro-tests-run-step-edit "\C-cxq17051i there"
                                :events '(act automatic)
                                :result "ḩi there"
                                :macro-result "\C-cxq17051i there")
    (kmacro-tests-run-step-edit "g\C-cxq17051i"
                                :events '(act insert-1 automatic)
                                :sequences '("-")
                                :result "g-ḩi"
                                :macro-result "g-\C-cxq17051i")))

(kmacro-tests-deftest kmacro-tests-step-edit-can-replace-meta-keys ()
  "Replacing C-w with M-w produces the expected result.
This is a regression for item 7 in Bug#24991."
  (:expected-result :failed)
  (kmacro-tests-run-step-edit "abc\C-b\C-b\C-SPC\C-f\C-w\C-e\C-y"
                              :events '(act-repeat act-repeat
                                                   act-repeat act-repeat
                                                   replace automatic)
                              :sequences '("\M-w" "\C-j")
                              :result "abcb"
                              :macro-result "abc\C-b\C-b\C-SPC\C-f\M-w\C-e\C-y")
  (kmacro-tests-should-insert "abcb" (kmacro-call-macro nil)))

(kmacro-tests-deftest kmacro-tests-step-edit-ignores-qr-map-commands ()
  "Unimplemented commands from `query-replace-map' are ignored."
  (kmacro-tests-run-step-edit "yep"
                              :events '(edit-replacement
                                        act-and-show act-and-exit
                                        delete-and-edit
                                        recenter backup
                                        scroll-up scroll-down
                                        scroll-other-window
                                        scroll-other-window-down
                                        exit-prefix
                                        act act act)
                              :result "yep"
                              :macro-result "yep"))

(kmacro-tests-deftest
    kmacro-tests-step-edit-edits-macro-with-extended-command ()
  "Step-editing a macro which uses the minibuffer can change the macro."
  (let ((mac (vconcat [?\M-x] "eval-expression" '[return]
                      "(insert-char (+ ?a \C-e" [?1] "))" '[return]))
        (mac-after (vconcat [?\M-x] "eval-expression" '[return]
                            "(insert-char (+ ?a \C-e" [?2] "))" '[return])))

    (kmacro-tests-run-step-edit mac
                                :events '(act act-repeat
                                              act act-repeat act
                                              replace-1 act-repeat act)
                                :sequences '("2")
                                :result "c"
                                :macro-result mac-after)))

(kmacro-tests-deftest kmacro-tests-step-edit-step-through-isearch ()
  "Step-editing can edit a macro which uses `isearch-backward' (Bug#22488)."
  (:expected-result :failed)
  (let ((mac (vconcat "test Input" '[return]
                      [?\C-r] "inp" '[return] "\C-cxu"))
        (mac-after (vconcat "test input" '[return]
                            [?\C-r] "inp" '[return] "\C-cd")))

    (kmacro-tests-run-step-edit mac
                                :events '(act-repeat act act
                                                     act-repeat act
                                                     replace-1)
                                :sequences '("\C-cd")
                                :result "test input\n"
                                :macro-result mac-after)))

(kmacro-tests-deftest kmacro-tests-step-edit-cleans-up-hook ()
  "Step-editing properly cleans up `post-command-hook.' (Bug #18708)"
  (let (post-command-hook)
    (setq-local post-command-hook '(t))
    (kmacro-tests-run-step-edit "x"
                                :events '(act)
                                :result "x"
                                :macro-result "x")
    (kmacro-tests-simulate-command '(beginning-of-line))))

(cl-defun kmacro-tests-run-step-edit
    (macro &key events sequences result macro-result)
  "Set up and run a test of `kmacro-step-edit-macro'.

Run `kmacro-step-edit-macro' with MACRO defined as a keyboard macro
and `read-event' and `read-key-sequence' set up to return items from
EVENTS and SEQUENCES respectively.  SEQUENCES may be nil, but
EVENTS should not be.  EVENTS should be a list of symbols bound
in `kmacro-step-edit-map' or `query-replace' map, and this function
will do the keymap lookup for you. SEQUENCES should contain
return values for `read-key-sequence'.

Before running the macro, the current buffer will be erased.
RESULT is the string that should be inserted during the
step-editing process, and MACRO-RESULT is the expected value of
`last-kbd-macro' after the editing is complete."

  (let* ((kmacro-tests-events (mapcar #'kmacro-tests-get-kmacro-step-edit-key events))
         (kmacro-tests-sequences sequences))

    (kmacro-tests-define-macro (string-to-vector macro))
    (use-local-map kmacro-tests-keymap)
    (erase-buffer)
    (kmacro-step-edit-macro)
    (when result
      (should (equal result (buffer-string))))
    (when macro-result
      (should (equal last-kbd-macro (string-to-vector macro-result))))))

;;; Utilities:

(defun kmacro-tests-simulate-command (command &optional arg)
  "Call `ert-simulate-command' after setting `current-prefix-arg'.
Sets `current-prefix-arg' to ARG if it is non-nil, otherwise to
the second element of COMMAND, before executing COMMAND using
`ert-simulate-command'."
  (let ((current-prefix-arg (or arg (cadr command))))
    (ert-simulate-command command)))

(defun kmacro-tests-define-macro (mac)
  "Define MAC as a keyboard macro using kmacro commands."
  (push mac kmacro-tests-macros)
  (kmacro-tests-simulate-command '(kmacro-start-macro nil))
  (should defining-kbd-macro)
  (kmacro-tests-simulate-command '(kmacro-end-macro nil))
  (should (equal mac last-kbd-macro)))

(defun kmacro-tests-get-kmacro-key (sym)
  "Look up kmacro command SYM in kmacro's keymap.
Return the integer key value found."
  (aref (where-is-internal sym kmacro-keymap t) 0))

(defun kmacro-tests-get-kmacro-step-edit-key (sym)
  "Return the first key bound to SYM in `kmacro-step-edit-map'."
  (let ((where (aref (where-is-internal sym kmacro-step-edit-map t) 0)))
    (if (consp where)
        (car where)
      where)))

(provide 'kmacro-tests)

;;; kmacro-tests.el ends here