unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob b230958b365839193e0b12c832452157595f0035 8053 bytes (raw)
name: test/src/keymap-tests.el 	 # note: path name is non-authoritative(*)

  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
 
;;; keymap-tests.el --- Test suite for src/keymap.c -*- lexical-binding: t -*-

;; Copyright (C) 2015-2020 Free Software Foundation, Inc.

;; Author: Juanma Barranquero <lekktu@gmail.com>
;;         Stefan Kangas <stefankangas@gmail.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/>.

;;; Code:

(require 'ert)

(defun keymap-tests--make-keymap-test (fun)
  (should (eq (car (funcall fun)) 'keymap))
  (should (proper-list-p (funcall fun)))
  (should (equal (car (last (funcall fun "foo"))) "foo")))

(ert-deftest keymap-make-keymap ()
  (keymap-tests--make-keymap-test #'make-keymap)
  (should (char-table-p (cadr (make-keymap)))))

(ert-deftest keymap-make-sparse-keymap ()
  (keymap-tests--make-keymap-test #'make-sparse-keymap))

(ert-deftest keymap-keymapp ()
  (should (keymapp (make-keymap)))
  (should (keymapp (make-sparse-keymap)))
  (should-not (keymapp '(foo bar))))

(ert-deftest keymap-keymap-parent ()
  (should-not (keymap-parent (make-keymap)))
  (should-not (keymap-parent (make-sparse-keymap)))
  (let ((map (make-keymap)))
    (set-keymap-parent map help-mode-map)
    (should (equal (keymap-parent map) help-mode-map))))

(ert-deftest keymap-copy-keymap/is-equal ()
  (should (equal (copy-keymap help-mode-map) help-mode-map)))

(ert-deftest keymap-copy-keymap/is-not-eq ()
  (should-not (eq (copy-keymap help-mode-map) help-mode-map)))

(ert-deftest keymap-lookup-key ()
  (let ((map (make-keymap)))
    (define-key map [?a] 'foo)
    (should (eq (lookup-key map [?a]) 'foo))))

(ert-deftest describe-buffer-bindings/header-in-current-buffer ()
  "Header should be inserted into the current buffer.
https://debbugs.gnu.org/39149#31"
  (with-temp-buffer
    (describe-buffer-bindings (current-buffer))
    (should (string-match (rx bol "key" (+ space) "binding" eol)
                          (buffer-string)))))

(ert-deftest describe-buffer-bindings/returns-nil ()
  "Should return nil."
  (with-temp-buffer
    (should (eq (describe-buffer-bindings (current-buffer)) nil))))

(ert-deftest keymap-store_in_keymap-XFASTINT-on-non-characters ()
  "Check for bug fixed in \"Fix assertion violation in define-key\",
commit 86c19714b097aa477d339ed99ffb5136c755a046."
  (let ((def (lookup-key Buffer-menu-mode-map [32])))
    (unwind-protect
        (progn
          (should-not (eq def 'undefined))
          ;; This will cause an assertion violation if the bug is present.
          ;; We could run an inferior Emacs process and check for the return
          ;; status, but in some environments an assertion failure triggers
          ;; an abort dialog that requires user intervention anyway.
          (define-key Buffer-menu-mode-map [(32 . 32)] 'undefined)
          (should (eq (lookup-key Buffer-menu-mode-map [32]) 'undefined)))
      (define-key Buffer-menu-mode-map [32] def))))

\f
;;;; where-is-internal

(defun keymap-tests--command-1 () (interactive) nil)
(defun keymap-tests--command-2 () (interactive) nil)
(put 'keymap-tests--command-1 :advertised-binding [?y])

(ert-deftest keymap-where-is-internal ()
  (let ((map (make-sparse-keymap)))
    (define-key map "x" 'keymap-tests--command-1)
    (define-key map "y" 'keymap-tests--command-1)
    (should (equal (where-is-internal 'keymap-tests--command-1 map)
                   '([?y] [?x])))))

(ert-deftest keymap-where-is-internal/firstonly-t ()
  (let ((map (make-sparse-keymap)))
    (define-key map "x" 'keymap-tests--command-1)
    (define-key map "y" 'keymap-tests--command-1)
    (should (equal (where-is-internal 'keymap-tests--command-1 map t)
                   [?y]))))

(ert-deftest keymap-where-is-internal/menu-item ()
  (let ((map (make-sparse-keymap)))
    (define-key map [menu-bar foobar cmd1]
      '(menu-item "Run Command 1" keymap-tests--command-1
                  :help "Command 1 Help"))
    (define-key map "x" 'keymap-tests--command-1)
    (should (equal (where-is-internal 'keymap-tests--command-1 map)
                   '([?x] [menu-bar foobar cmd1])))
    (should (equal (where-is-internal 'keymap-tests--command-1 map t) [?x]))))


(ert-deftest keymap-where-is-internal/advertised-binding ()
  ;; Make sure order does not matter.
  (dolist (keys '(("x" . "y") ("y" . "x")))
    (let ((map (make-sparse-keymap)))
      (define-key map (car keys) 'keymap-tests--command-1)
      (define-key map (cdr keys) 'keymap-tests--command-1)
      (should (equal (where-is-internal 'keymap-tests--command-1 map t) [121])))))

(ert-deftest keymap-where-is-internal/advertised-binding-respect-remap ()
  (let ((map (make-sparse-keymap)))
    (define-key map "x" 'next-line)
    (define-key map [remap keymap-tests--command-1] 'next-line)
    (define-key map "y" 'keymap-tests--command-1)
    (should (equal (where-is-internal 'keymap-tests--command-1 map t) [?x]))))

(ert-deftest keymap-where-is-internal/remap ()
  (let ((map (make-keymap)))
    (define-key map (kbd "x") 'foo)
    (define-key map (kbd "y") 'bar)
    (define-key map [remap foo] 'bar)
    (should (equal (where-is-internal 'foo map t) [?y]))
    (should (equal (where-is-internal 'bar map t) [?y]))))

(defvar keymap-tests-minor-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "x" 'keymap-tests--command-2)
    map))

(defvar keymap-tests-major-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "x" 'keymap-tests--command-1)
    map))

(define-minor-mode keymap-tests-minor-mode "Test.")

(define-derived-mode keymap-tests-major-mode nil "Test.")

(ert-deftest keymap-where-is-internal/shadowed ()
  (with-temp-buffer
    (keymap-tests-major-mode)
    (keymap-tests-minor-mode)
    (should-not (where-is-internal 'keymap-tests--command-1 nil t))
    (should (equal (where-is-internal 'keymap-tests--command-2 nil t) [120]))))

(ert-deftest keymap-where-is-internal/preferred-modifier-is-a-string ()
  "Make sure we don't crash when `where-is-preferred-modifier' is not a symbol."
  (should
   (equal (let ((where-is-preferred-modifier "alt"))
            (where-is-internal 'execute-extended-command global-map t))
          [#x8000078])))

\f
;;;; describe_vector

(ert-deftest help--describe-vector/bug-9293-one-shadowed-in-range ()
  "Check that we only show a range if shadowed by the same command."
  (let ((orig-map (let ((map (make-keymap)))
                    (define-key map "e" 'foo)
                    (define-key map "f" 'foo)
                    (define-key map "g" 'foo)
                    (define-key map "h" 'foo)
                    map))
        (shadow-map (let ((map (make-keymap)))
                      (define-key map "f" 'bar)
                      map)))
    (with-temp-buffer
      (help--describe-vector (cadr orig-map) nil #'help--describe-command
                             t shadow-map orig-map t)
      (should (equal (buffer-string)
                     "
e		foo
f		foo  (binding currently shadowed)
g .. h		foo
")))))

\f
;;;; apropos-internal

(ert-deftest keymap-apropos-internal ()
  (should (equal (apropos-internal "^next-line$") '(next-line)))
  (should (>= (length (apropos-internal "^help")) 100))
  (should-not (apropos-internal "^test-a-missing-symbol-foo-bar-zut$")))

(ert-deftest keymap-apropos-internal/predicate ()
  (should (equal (apropos-internal "^next-line$" #'commandp) '(next-line)))
  (should (>= (length (apropos-internal "^help" #'commandp)) 15))
  (should-not (apropos-internal "^next-line$" #'keymapp)))

(provide 'keymap-tests)

;;; keymap-tests.el ends here

debug log:

solving b230958b36 ...
found b230958b36 in https://yhetil.org/emacs-bugs/CADwFkmnLKf1jRfny6k_o9hO9kvrKfz3JhkS0DM8qPh8DmZgOiA@mail.gmail.com/
found e3dd8420d7 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 e3dd8420d7b12f9acf51093b43ddebf1553ab106	test/src/keymap-tests.el

applying [1/1] https://yhetil.org/emacs-bugs/CADwFkmnLKf1jRfny6k_o9hO9kvrKfz3JhkS0DM8qPh8DmZgOiA@mail.gmail.com/
diff --git a/test/src/keymap-tests.el b/test/src/keymap-tests.el
index e3dd8420d7..b230958b36 100644

Checking patch test/src/keymap-tests.el...
Applied patch test/src/keymap-tests.el cleanly.

index at:
100644 b230958b365839193e0b12c832452157595f0035	test/src/keymap-tests.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).