aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorPhilip Kaludercic2024-06-18 21:45:58 +0200
committerPhilip Kaludercic2024-06-18 21:45:58 +0200
commitfa4203300fde6820a017bf1089652fb95759d68c (patch)
treedfc47ca47d04feddd8af5d510c2795d67622db60 /test
parenta7dff8c53dde18c703f470cde9ad033cffe8c766 (diff)
parentcc0a3a5f65bda4d3a34cfcd8070540aa1b36f84d (diff)
downloademacs-fa4203300fde6820a017bf1089652fb95759d68c.tar.gz
emacs-fa4203300fde6820a017bf1089652fb95759d68c.zip
Merge remote-tracking branch 'origin/feature/which-key-in-core'
Diffstat (limited to 'test')
-rw-r--r--test/lisp/which-key-tests.el267
1 files changed, 267 insertions, 0 deletions
diff --git a/test/lisp/which-key-tests.el b/test/lisp/which-key-tests.el
new file mode 100644
index 00000000000..1f2b1965ec3
--- /dev/null
+++ b/test/lisp/which-key-tests.el
@@ -0,0 +1,267 @@
1;;; which-key-tests.el --- Tests for which-key.el -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2017-2021 Free Software Foundation, Inc.
4
5;; Author: Justin Burkett <justin@burkett.cc>
6;; Maintainer: Justin Burkett <justin@burkett.cc>
7
8;; This program is free software; you can redistribute it and/or modify
9;; it under the terms of the GNU General Public License as published by
10;; the Free Software Foundation, either version 3 of the License, or
11;; (at your option) any later version.
12
13;; This program is distributed in the hope that it will be useful,
14;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;; GNU General Public License for more details.
17
18;; You should have received a copy of the GNU General Public License
19;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21;;; Commentary:
22
23;; Tests for which-key.el
24
25;;; Code:
26
27(require 'which-key)
28(require 'ert)
29
30(ert-deftest which-key-test--keymap-based-bindings ()
31 (let ((map (make-sparse-keymap))
32 (prefix-map (make-sparse-keymap)))
33 (define-key prefix-map "x" #'ignore)
34 (define-key map "\C-a" 'complete)
35 (define-key map "\C-b" prefix-map)
36 (which-key-add-keymap-based-replacements map
37 "C-a" '("mycomplete" . complete)
38 "C-b" "mymap"
39 "C-c" "mymap2")
40 (define-key map "\C-ca" 'foo)
41 (should (equal
42 (which-key--get-keymap-bindings map)
43 '(("C-a" . "mycomplete")
44 ("C-b" . "group:mymap")
45 ("C-c" . "group:mymap2"))))))
46
47(ert-deftest which-key-test--named-prefix-keymap ()
48 (define-prefix-command 'which-key-test--named-map)
49 (let ((map (make-sparse-keymap)))
50 (define-key map "\C-a" 'which-key-test--named-map)
51 (should (equal
52 (which-key--get-keymap-bindings map)
53 '(("C-a" . "which-key-test--named-map"))))))
54
55(ert-deftest which-key-test--prefix-declaration ()
56 "Test `which-key-declare-prefixes' and
57`which-key-declare-prefixes-for-mode'. See Bug #109."
58 (let* ((major-mode 'test-mode)
59 which-key-replacement-alist)
60 (which-key-add-key-based-replacements
61 "SPC C-c" '("complete" . "complete title")
62 "SPC C-k" "cancel")
63 (which-key-add-major-mode-key-based-replacements 'test-mode
64 "C-c C-c" '("complete" . "complete title")
65 "C-c C-k" "cancel")
66 (should (equal
67 (which-key--maybe-replace '("SPC C-k" . ""))
68 '("SPC C-k" . "cancel")))
69 (should (equal
70 (which-key--maybe-replace '("C-c C-c" . ""))
71 '("C-c C-c" . "complete")))))
72
73(ert-deftest which-key-test--maybe-replace ()
74 "Test `which-key--maybe-replace'. See #154"
75 (let ((which-key-replacement-alist
76 '((("C-c [a-d]" . nil) . ("C-c a" . "c-c a"))
77 (("C-c .+" . nil) . ("C-c *" . "c-c *"))))
78 (test-mode-1 't)
79 (test-mode-2 'nil)
80 which-key-allow-multiple-replacements)
81 (which-key-add-key-based-replacements
82 "C-c ." "test ."
83 "SPC ." "SPC ."
84 "C-c \\" "regexp quoting"
85 "C-c [" "bad regexp"
86 "SPC t1" (lambda (kb)
87 (cons (car kb)
88 (if test-mode-1
89 "[x] test mode"
90 "[ ] test mode")))
91 "SPC t2" (lambda (kb)
92 (cons (car kb)
93 (if test-mode-2
94 "[x] test mode"
95 "[ ] test mode"))))
96 (should (equal
97 (which-key--maybe-replace '("C-c g" . "test"))
98 '("C-c *" . "c-c *")))
99 (should (equal
100 (which-key--maybe-replace '("C-c b" . "test"))
101 '("C-c a" . "c-c a")))
102 (should (equal
103 (which-key--maybe-replace '("C-c ." . "not test ."))
104 '("C-c ." . "test .")))
105 (should (not
106 (equal
107 (which-key--maybe-replace '("C-c +" . "not test ."))
108 '("C-c ." . "test ."))))
109 (should (equal
110 (which-key--maybe-replace '("C-c [" . "orig bad regexp"))
111 '("C-c [" . "bad regexp")))
112 (should (equal
113 (which-key--maybe-replace '("C-c \\" . "pre quoting"))
114 '("C-c \\" . "regexp quoting")))
115 ;; see #155
116 (should (equal
117 (which-key--maybe-replace '("SPC . ." . "don't replace"))
118 '("SPC . ." . "don't replace")))
119 (should (equal
120 (which-key--maybe-replace '("SPC t 1" . "test mode"))
121 '("SPC t 1" . "[x] test mode")))
122 (should (equal
123 (which-key--maybe-replace '("SPC t 2" . "test mode"))
124 '("SPC t 2" . "[ ] test mode")))))
125
126(ert-deftest which-key-test--maybe-replace-multiple ()
127 "Test `which-key-allow-multiple-replacements'. See #156."
128 (let ((which-key-replacement-alist
129 '(((nil . "helm") . (nil . "HLM"))
130 ((nil . "projectile") . (nil . "PRJTL"))))
131 (which-key-allow-multiple-replacements t))
132 (should (equal
133 (which-key--maybe-replace '("C-c C-c" . "helm-x"))
134 '("C-c C-c" . "HLM-x")))
135 (should (equal
136 (which-key--maybe-replace '("C-c C-c" . "projectile-x"))
137 '("C-c C-c" . "PRJTL-x")))
138 (should (equal
139 (which-key--maybe-replace '("C-c C-c" . "helm-projectile-x"))
140 '("C-c C-c" . "HLM-PRJTL-x")))))
141
142(ert-deftest which-key-test--key-extraction ()
143 "Test `which-key--extract-key'. See #161."
144 (should (equal (which-key--extract-key "SPC a") "a"))
145 (should (equal (which-key--extract-key "C-x a") "a"))
146 (should (equal (which-key--extract-key "<left> b a") "a"))
147 (should (equal (which-key--extract-key "<left> a .. c") "a .. c"))
148 (should (equal (which-key--extract-key "M-a a .. c") "a .. c")))
149
150(ert-deftest which-key-test--get-keymap-bindings ()
151 (skip-unless (require 'evil nil t))
152 (defvar evil-local-mode)
153 (defvar evil-state)
154 (declare-function evil-define-key* "ext:evil")
155 (let ((map (make-sparse-keymap))
156 (evil-local-mode t)
157 (evil-state 'normal)
158 which-key-replacement-alist)
159 (define-key map [which-key-a] '(which-key "blah"))
160 (define-key map "b" #'ignore)
161 (define-key map "c" "c")
162 (define-key map "dd" "dd")
163 (define-key map "eee" "eee")
164 (define-key map "f" [123 45 6])
165 (define-key map (kbd "M-g g") "M-gg")
166 (evil-define-key* 'normal map (kbd "C-h") "C-h-normal")
167 (evil-define-key* 'insert map (kbd "C-h") "C-h-insert")
168 (should (equal
169 (sort (which-key--get-keymap-bindings map)
170 (lambda (a b) (string-lessp (car a) (car b))))
171 '(("M-g" . "prefix")
172 ("c" . "c")
173 ("d" . "prefix")
174 ("e" . "prefix")
175 ("f" . "{ - C-f"))))
176 (should (equal
177 (sort (which-key--get-keymap-bindings map nil nil nil nil t)
178 (lambda (a b) (string-lessp (car a) (car b))))
179 '(("C-h" . "C-h-normal")
180 ("M-g" . "prefix")
181 ("c" . "c")
182 ("d" . "prefix")
183 ("e" . "prefix")
184 ("f" . "{ - C-f"))))
185 (should (equal
186 (sort (which-key--get-keymap-bindings map nil nil nil t)
187 (lambda (a b) (string-lessp (car a) (car b))))
188 '(("M-g g" . "M-gg")
189 ("c" . "c")
190 ("d d" . "dd")
191 ("e e e" . "eee")
192 ("f" . "{ - C-f"))))))
193
194(ert-deftest which-key-test--nil-replacement ()
195 (let ((which-key-replacement-alist
196 '(((nil . "winum-select-window-[1-9]") . t))))
197 (should (equal
198 (which-key--maybe-replace '("C-c C-c" . "winum-select-window-1"))
199 '()))))
200
201(ert-deftest which-key-test--key-sorting ()
202 (let ((keys '(("a" . "z")
203 ("A" . "Z")
204 ("b" . "y")
205 ("B" . "Y")
206 ("p" . "prefix")
207 ("SPC" . "x")
208 ("C-a" . "w"))))
209 (let ((which-key-sort-uppercase-first t))
210 (should
211 (equal
212 (mapcar #'car (sort (copy-sequence keys) #'which-key-key-order))
213 '("SPC" "A" "B" "a" "b" "p" "C-a"))))
214 (let (which-key-sort-uppercase-first)
215 (should
216 (equal
217 (mapcar #'car (sort (copy-sequence keys) #'which-key-key-order))
218 '("SPC" "a" "b" "p" "A" "B" "C-a"))))
219 (let ((which-key-sort-uppercase-first t))
220 (should
221 (equal
222 (mapcar #'car (sort (copy-sequence keys) #'which-key-key-order-alpha))
223 '("SPC" "A" "a" "B" "b" "p" "C-a"))))
224 (let (which-key-sort-uppercase-first)
225 (should
226 (equal
227 (mapcar #'car (sort (copy-sequence keys) #'which-key-key-order-alpha))
228 '("SPC" "a" "A" "b" "B" "p" "C-a"))))
229 (let ((which-key-sort-uppercase-first t))
230 (should
231 (equal
232 (mapcar #'car (sort (copy-sequence keys)
233 #'which-key-prefix-then-key-order))
234 '("SPC" "A" "B" "a" "b" "C-a" "p"))))
235 (let (which-key-sort-uppercase-first)
236 (should
237 (equal
238 (mapcar #'car (sort (copy-sequence keys)
239 #'which-key-prefix-then-key-order))
240 '("SPC" "a" "b" "A" "B" "C-a" "p"))))
241 (let ((which-key-sort-uppercase-first t))
242 (should
243 (equal
244 (mapcar 'car (sort (copy-sequence keys)
245 #'which-key-prefix-then-key-order-reverse))
246 '("p" "SPC" "A" "B" "a" "b" "C-a"))))
247 (let (which-key-sort-uppercase-first)
248 (should
249 (equal
250 (mapcar #'car (sort (copy-sequence keys)
251 #'which-key-prefix-then-key-order-reverse))
252 '("p" "SPC" "a" "b" "A" "B" "C-a"))))
253 (let ((which-key-sort-uppercase-first t))
254 (should
255 (equal
256 (mapcar #'car (sort (copy-sequence keys)
257 #'which-key-description-order))
258 '("p" "C-a" "SPC" "b" "B" "a" "A"))))
259 (let (which-key-sort-uppercase-first)
260 (should
261 (equal
262 (mapcar #'car (sort (copy-sequence keys)
263 #'which-key-description-order))
264 '("p" "C-a" "SPC" "b" "B" "a" "A"))))))
265
266(provide 'which-key-tests)
267;;; which-key-tests.el ends here