summaryrefslogtreecommitdiff
path: root/test/lisp/repeat-tests.el
blob: a1f9bbb1739d87207e18a8c29532efe6acc64a25 (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
;;; repeat-tests.el --- Tests for repeat.el          -*- lexical-binding: t; -*-

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

;; Author: Juri Linkov <juri@linkov.net>

;; 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/>.

;;; Code:

(require 'ert)
(require 'repeat)

(defvar repeat-tests-calls nil)

(defun repeat-tests-call-a (&optional arg)
  (interactive "p")
  (push `(,arg a) repeat-tests-calls))

(defun repeat-tests-call-b (&optional arg)
  (interactive "p")
  (push `(,arg b) repeat-tests-calls))

(defvar repeat-tests-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "C-x w a") 'repeat-tests-call-a)
    map)
  "Keymap for keys that initiate repeating sequences.")

(defvar repeat-tests-repeat-map
  (let ((map (make-sparse-keymap)))
    (define-key map "a" 'repeat-tests-call-a)
    (define-key map "b" 'repeat-tests-call-b)
    map)
  "Keymap for repeating sequences.")
(put 'repeat-tests-call-a 'repeat-map 'repeat-tests-repeat-map)
(put 'repeat-tests-call-b 'repeat-map 'repeat-tests-repeat-map)

(defmacro with-repeat-mode (&rest body)
  "Create environment for testing `repeat-mode'."
  `(unwind-protect
      (progn
        (repeat-mode +1)
        (with-temp-buffer
          (save-window-excursion
            ;; `execute-kbd-macro' applied to window only
            (set-window-buffer nil (current-buffer))
            (use-local-map repeat-tests-map)
            ,@body)))
    (repeat-mode -1)))

(defun repeat-tests--check (keys calls inserted)
  (setq repeat-tests-calls nil)
  (delete-region (point-min) (point-max))
  (execute-kbd-macro (kbd keys))
  (should (equal (nreverse repeat-tests-calls) calls))
  ;; Check for self-inserting keys
  (should (equal (buffer-string) inserted)))

(ert-deftest repeat-tests-exit-key ()
  (with-repeat-mode
   (let ((repeat-echo-function 'ignore))
     (let ((repeat-exit-key nil))
       (repeat-tests--check
        "C-x w a b a b RET c"
        '((1 a) (1 b) (1 a) (1 b)) "\nc"))
     (let ((repeat-exit-key [return]))
       (repeat-tests--check
        "C-x w a b a b <return> c"
        '((1 a) (1 b) (1 a) (1 b)) "c")))))

(ert-deftest repeat-tests-keep-prefix ()
  (with-repeat-mode
   (let ((repeat-echo-function 'ignore))
     (repeat-tests--check
      "C-x w a b a b c"
      '((1 a) (1 b) (1 a) (1 b)) "c")
     (let ((repeat-keep-prefix nil))
       (repeat-tests--check
        "C-2 C-x w a b a b c"
        '((2 a) (1 b) (1 a) (1 b)) "c")
       (repeat-tests--check
        "C-2 C-x w a C-3 c"
        '((2 a)) "ccc"))
     ;; TODO: fix and uncomment
     ;; (let ((repeat-keep-prefix t))
     ;;   (repeat-tests--check
     ;;    "C-2 C-x w a b a b c"
     ;;    '((2 a) (2 b) (2 a) (2 b)) "c")
     ;;   (repeat-tests--check
     ;;    "C-2 C-x w a C-1 C-2 b a C-3 C-4 b c"
     ;;    '((2 a) (12 b) (12 a) (34 b)) "c"))
     )))

;; TODO: :tags '(:expensive-test)  for repeat-exit-timeout

(provide 'repeat-tests)
;;; repeat-tests.el ends here