This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
/
which-key-tests.el
267 lines (248 loc) · 9.92 KB
/
which-key-tests.el
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
;;; which-key-tests.el --- Tests for which-key.el -*- lexical-binding: t; -*-
;; Copyright (C) 2017-2021 Free Software Foundation, Inc.
;; Author: Justin Burkett <[email protected]>
;; Maintainer: Justin Burkett <[email protected]>
;; This program 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.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Tests for which-key.el
;;; Code:
(require 'which-key)
(require 'ert)
(ert-deftest which-key-test--keymap-based-bindings ()
(let ((map (make-sparse-keymap))
(prefix-map (make-sparse-keymap)))
(define-key prefix-map "x" #'ignore)
(define-key map "\C-a" 'complete)
(define-key map "\C-b" prefix-map)
(which-key-add-keymap-based-replacements map
"C-a" '("mycomplete" . complete)
"C-b" "mymap"
"C-c" "mymap2")
(define-key map "\C-ca" 'foo)
(should (equal
(which-key--get-keymap-bindings map)
'(("C-a" . "mycomplete")
("C-b" . "group:mymap")
("C-c" . "group:mymap2"))))))
(ert-deftest which-key-test--named-prefix-keymap ()
(define-prefix-command 'which-key-test--named-map)
(let ((map (make-sparse-keymap)))
(define-key map "\C-a" 'which-key-test--named-map)
(should (equal
(which-key--get-keymap-bindings map)
'(("C-a" . "which-key-test--named-map"))))))
(ert-deftest which-key-test--prefix-declaration ()
"Test `which-key-declare-prefixes' and
`which-key-declare-prefixes-for-mode'. See Bug #109."
(let* ((major-mode 'test-mode)
which-key-replacement-alist)
(which-key-add-key-based-replacements
"SPC C-c" '("complete" . "complete title")
"SPC C-k" "cancel")
(which-key-add-major-mode-key-based-replacements 'test-mode
"C-c C-c" '("complete" . "complete title")
"C-c C-k" "cancel")
(should (equal
(which-key--maybe-replace '("SPC C-k" . ""))
'("SPC C-k" . "cancel")))
(should (equal
(which-key--maybe-replace '("C-c C-c" . ""))
'("C-c C-c" . "complete")))))
(ert-deftest which-key-test--maybe-replace ()
"Test `which-key--maybe-replace'. See #154"
(let ((which-key-replacement-alist
'((("C-c [a-d]" . nil) . ("C-c a" . "c-c a"))
(("C-c .+" . nil) . ("C-c *" . "c-c *"))))
(test-mode-1 't)
(test-mode-2 'nil)
which-key-allow-multiple-replacements)
(which-key-add-key-based-replacements
"C-c ." "test ."
"SPC ." "SPC ."
"C-c \\" "regexp quoting"
"C-c [" "bad regexp"
"SPC t1" (lambda (kb)
(cons (car kb)
(if test-mode-1
"[x] test mode"
"[ ] test mode")))
"SPC t2" (lambda (kb)
(cons (car kb)
(if test-mode-2
"[x] test mode"
"[ ] test mode"))))
(should (equal
(which-key--maybe-replace '("C-c g" . "test"))
'("C-c *" . "c-c *")))
(should (equal
(which-key--maybe-replace '("C-c b" . "test"))
'("C-c a" . "c-c a")))
(should (equal
(which-key--maybe-replace '("C-c ." . "not test ."))
'("C-c ." . "test .")))
(should (not
(equal
(which-key--maybe-replace '("C-c +" . "not test ."))
'("C-c ." . "test ."))))
(should (equal
(which-key--maybe-replace '("C-c [" . "orig bad regexp"))
'("C-c [" . "bad regexp")))
(should (equal
(which-key--maybe-replace '("C-c \\" . "pre quoting"))
'("C-c \\" . "regexp quoting")))
;; see #155
(should (equal
(which-key--maybe-replace '("SPC . ." . "don't replace"))
'("SPC . ." . "don't replace")))
(should (equal
(which-key--maybe-replace '("SPC t 1" . "test mode"))
'("SPC t 1" . "[x] test mode")))
(should (equal
(which-key--maybe-replace '("SPC t 2" . "test mode"))
'("SPC t 2" . "[ ] test mode")))))
(ert-deftest which-key-test--maybe-replace-multiple ()
"Test `which-key-allow-multiple-replacements'. See #156."
(let ((which-key-replacement-alist
'(((nil . "helm") . (nil . "HLM"))
((nil . "projectile") . (nil . "PRJTL"))))
(which-key-allow-multiple-replacements t))
(should (equal
(which-key--maybe-replace '("C-c C-c" . "helm-x"))
'("C-c C-c" . "HLM-x")))
(should (equal
(which-key--maybe-replace '("C-c C-c" . "projectile-x"))
'("C-c C-c" . "PRJTL-x")))
(should (equal
(which-key--maybe-replace '("C-c C-c" . "helm-projectile-x"))
'("C-c C-c" . "HLM-PRJTL-x")))))
(ert-deftest which-key-test--key-extraction ()
"Test `which-key--extract-key'. See #161."
(should (equal (which-key--extract-key "SPC a") "a"))
(should (equal (which-key--extract-key "C-x a") "a"))
(should (equal (which-key--extract-key "<left> b a") "a"))
(should (equal (which-key--extract-key "<left> a .. c") "a .. c"))
(should (equal (which-key--extract-key "M-a a .. c") "a .. c")))
(ert-deftest which-key-test--get-keymap-bindings ()
(skip-unless (require 'evil nil t))
(defvar evil-local-mode)
(defvar evil-state)
(declare-function evil-define-key* "ext:evil")
(let ((map (make-sparse-keymap))
(evil-local-mode t)
(evil-state 'normal)
which-key-replacement-alist)
(define-key map [which-key-a] '(which-key "blah"))
(define-key map "b" #'ignore)
(define-key map "c" "c")
(define-key map "dd" "dd")
(define-key map "eee" "eee")
(define-key map "f" [123 45 6])
(define-key map (kbd "M-g g") "M-gg")
(evil-define-key* 'normal map (kbd "C-h") "C-h-normal")
(evil-define-key* 'insert map (kbd "C-h") "C-h-insert")
(should (equal
(sort (which-key--get-keymap-bindings map)
(lambda (a b) (string-lessp (car a) (car b))))
'(("M-g" . "prefix")
("c" . "c")
("d" . "prefix")
("e" . "prefix")
("f" . "{ - C-f"))))
(should (equal
(sort (which-key--get-keymap-bindings map nil nil nil nil t)
(lambda (a b) (string-lessp (car a) (car b))))
'(("C-h" . "C-h-normal")
("M-g" . "prefix")
("c" . "c")
("d" . "prefix")
("e" . "prefix")
("f" . "{ - C-f"))))
(should (equal
(sort (which-key--get-keymap-bindings map nil nil nil t)
(lambda (a b) (string-lessp (car a) (car b))))
'(("M-g g" . "M-gg")
("c" . "c")
("d d" . "dd")
("e e e" . "eee")
("f" . "{ - C-f"))))))
(ert-deftest which-key-test--nil-replacement ()
(let ((which-key-replacement-alist
'(((nil . "winum-select-window-[1-9]") . t))))
(should (equal
(which-key--maybe-replace '("C-c C-c" . "winum-select-window-1"))
'()))))
(ert-deftest which-key-test--key-sorting ()
(let ((keys '(("a" . "z")
("A" . "Z")
("b" . "y")
("B" . "Y")
("p" . "prefix")
("SPC" . "x")
("C-a" . "w"))))
(let ((which-key-sort-uppercase-first t))
(should
(equal
(mapcar #'car (sort (copy-sequence keys) #'which-key-key-order))
'("SPC" "A" "B" "a" "b" "p" "C-a"))))
(let (which-key-sort-uppercase-first)
(should
(equal
(mapcar #'car (sort (copy-sequence keys) #'which-key-key-order))
'("SPC" "a" "b" "p" "A" "B" "C-a"))))
(let ((which-key-sort-uppercase-first t))
(should
(equal
(mapcar #'car (sort (copy-sequence keys) #'which-key-key-order-alpha))
'("SPC" "A" "a" "B" "b" "p" "C-a"))))
(let (which-key-sort-uppercase-first)
(should
(equal
(mapcar #'car (sort (copy-sequence keys) #'which-key-key-order-alpha))
'("SPC" "a" "A" "b" "B" "p" "C-a"))))
(let ((which-key-sort-uppercase-first t))
(should
(equal
(mapcar #'car (sort (copy-sequence keys)
#'which-key-prefix-then-key-order))
'("SPC" "A" "B" "a" "b" "C-a" "p"))))
(let (which-key-sort-uppercase-first)
(should
(equal
(mapcar #'car (sort (copy-sequence keys)
#'which-key-prefix-then-key-order))
'("SPC" "a" "b" "A" "B" "C-a" "p"))))
(let ((which-key-sort-uppercase-first t))
(should
(equal
(mapcar 'car (sort (copy-sequence keys)
#'which-key-prefix-then-key-order-reverse))
'("p" "SPC" "A" "B" "a" "b" "C-a"))))
(let (which-key-sort-uppercase-first)
(should
(equal
(mapcar #'car (sort (copy-sequence keys)
#'which-key-prefix-then-key-order-reverse))
'("p" "SPC" "a" "b" "A" "B" "C-a"))))
(let ((which-key-sort-uppercase-first t))
(should
(equal
(mapcar #'car (sort (copy-sequence keys)
#'which-key-description-order))
'("p" "C-a" "SPC" "b" "B" "a" "A"))))
(let (which-key-sort-uppercase-first)
(should
(equal
(mapcar #'car (sort (copy-sequence keys)
#'which-key-description-order))
'("p" "C-a" "SPC" "b" "B" "a" "A"))))))
(provide 'which-key-tests)
;;; which-key-tests.el ends here