Give test-completion's PREDICATE full alist entry
[emacs.git] / test / src / minibuf-tests.el
blob98b8614ddff053d81950e72a465b77c0cb3f2dd5
1 ;;; minibuf-tests.el --- tests for minibuf.c functions -*- lexical-binding: t -*-
3 ;; Copyright (C) 2016 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20 ;;; Code:
22 (require 'ert)
25 ;;; Support functions for `try-completion', `all-completion', and
26 ;;; `test-completion' tests.
28 (defun minibuf-tests--strings-to-symbol-list (list)
29 (mapcar #'intern list))
30 (defun minibuf-tests--strings-to-symbol-alist (list)
31 (let ((num 0))
32 (mapcar (lambda (str) (cons (intern str) (cl-incf num))) list)))
33 (defun minibuf-tests--strings-to-string-alist (list)
34 (let ((num 0))
35 (mapcar (lambda (str) (cons str (cl-incf num))) list)))
36 (defun minibuf-tests--strings-to-obarray (list)
37 (let ((ob (make-vector 7 0)))
38 (mapc (lambda (str) (intern str ob)) list)
39 ob))
40 (defun minibuf-tests--strings-to-string-hashtable (list)
41 (let ((ht (make-hash-table :test #'equal))
42 (num 0))
43 (mapc (lambda (str) (puthash str (cl-incf num) ht)) list)
44 ht))
45 (defun minibuf-tests--strings-to-symbol-hashtable (list)
46 (let ((ht (make-hash-table :test #'equal))
47 (num 0))
48 (mapc (lambda (str) (puthash (intern str) (cl-incf num) ht)) list)
49 ht))
51 ;;; Functions that produce a predicate (for *-completion functions)
52 ;;; which always returns non-nil for a given collection.
54 (defun minibuf-tests--memq-of-collection (collection)
55 (lambda (elt) (memq elt collection)))
56 (defun minibuf-tests--part-of-obarray (ob)
57 (lambda (sym) (eq (intern-soft (symbol-name sym) ob) sym)))
58 (defun minibuf-tests--part-of-hashtable (table)
59 (lambda (k v) (equal (gethash k table) v)))
62 ;;; Testing functions that are agnostic to type of COLLECTION.
64 (defun minibuf-tests--try-completion (xform-collection)
65 (let* ((abcdef (funcall xform-collection '("abc" "def")))
66 (+abba (funcall xform-collection '("abc" "abba" "def"))))
67 (should (equal (try-completion "a" abcdef) "abc"))
68 (should (equal (try-completion "a" +abba) "ab"))
69 (should (equal (try-completion "abc" +abba) t))
70 (should (equal (try-completion "abcd" +abba) nil))))
72 (defun minibuf-tests--try-completion-pred (xform-collection collection-member)
73 (let* ((abcdef (funcall xform-collection '("abc" "def")))
74 (abcdef-member (funcall collection-member abcdef))
75 (+abba (funcall xform-collection '("abc" "abba" "def")))
76 (+abba-member (funcall collection-member +abba)))
77 (should (equal (try-completion "a" abcdef abcdef-member) "abc"))
78 (should (equal (try-completion "a" +abba +abba-member) "ab"))
79 (should (equal (try-completion "abc" +abba +abba-member) t))
80 (should (equal (try-completion "abcd" +abba +abba-member) nil))
81 (should-not (try-completion "a" abcdef #'ignore))
82 (should-not (try-completion "a" +abba #'ignore))
83 (should-not (try-completion "abc" +abba #'ignore))
84 (should-not (try-completion "abcd" +abba #'ignore))))
86 (defun minibuf-tests--try-completion-regexp (xform-collection)
87 (let ((abcdef (funcall xform-collection '("abc" "def")))
88 (+abba (funcall xform-collection '("abc" "abba" "def"))))
89 (let ((completion-regexp-list '(".")))
90 (should (equal (try-completion "a" abcdef) "abc"))
91 (should (equal (try-completion "a" +abba) "ab"))
92 (should (equal (try-completion "abc" +abba) t))
93 (should (equal (try-completion "abcd" +abba) nil)))
94 (let ((completion-regexp-list '("X")))
95 (should-not (try-completion "a" abcdef))
96 (should-not (try-completion "a" +abba))
97 (should-not (try-completion "abc" +abba))
98 (should-not (try-completion "abcd" +abba)))))
100 (defun minibuf-tests--all-completions (xform-collection)
101 (let* ((abcdef (funcall xform-collection '("abc" "def")))
102 (+abba (funcall xform-collection '("abc" "abba" "def"))))
103 (should (equal (all-completions "a" abcdef) '("abc")))
104 (should (equal (all-completions "a" +abba) '("abc" "abba")))
105 (should (equal (all-completions "abc" +abba) '("abc")))
106 (should (equal (all-completions "abcd" +abba) nil))))
108 (defun minibuf-tests--all-completions-pred (xform-collection collection-member)
109 (let* ((abcdef (funcall xform-collection '("abc" "def")))
110 (abcdef-member (funcall collection-member abcdef))
111 (+abba (funcall xform-collection '("abc" "abba" "def")))
112 (+abba-member (funcall collection-member +abba)))
113 (should (equal (all-completions "a" abcdef abcdef-member) '("abc")))
114 (should (equal (all-completions "a" +abba +abba-member) '("abc" "abba")))
115 (should (equal (all-completions "abc" +abba +abba-member) '("abc")))
116 (should (equal (all-completions "abcd" +abba +abba-member) nil))
117 (should-not (all-completions "a" abcdef #'ignore))
118 (should-not (all-completions "a" +abba #'ignore))
119 (should-not (all-completions "abc" +abba #'ignore))
120 (should-not (all-completions "abcd" +abba #'ignore))))
122 (defun minibuf-tests--all-completions-regexp (xform-collection)
123 (let ((abcdef (funcall xform-collection '("abc" "def")))
124 (+abba (funcall xform-collection '("abc" "abba" "def"))))
125 (let ((completion-regexp-list '(".")))
126 (should (equal (all-completions "a" abcdef) '("abc")))
127 (should (equal (all-completions "a" +abba) '("abc" "abba")))
128 (should (equal (all-completions "abc" +abba) '("abc")))
129 (should (equal (all-completions "abcd" +abba) nil)))
130 (let ((completion-regexp-list '("X")))
131 (should-not (all-completions "a" abcdef))
132 (should-not (all-completions "a" +abba))
133 (should-not (all-completions "abc" +abba))
134 (should-not (all-completions "abcd" +abba)))))
136 (defun minibuf-tests--test-completion (xform-collection)
137 (let* ((abcdef (funcall xform-collection '("abc" "def")))
138 (+abba (funcall xform-collection '("abc" "abba" "def"))))
139 (should (test-completion "abc" abcdef))
140 (should (test-completion "def" +abba))
141 (should (test-completion "abba" +abba))
142 (should-not (test-completion "abcd" +abba))))
144 (defun minibuf-tests--test-completion-pred (xform-collection collection-member)
145 (let* ((abcdef (funcall xform-collection '("abc" "def")))
146 (abcdef-member (funcall collection-member abcdef))
147 (+abba (funcall xform-collection '("abc" "abba" "def")))
148 (+abba-member (funcall collection-member +abba)))
149 (should (test-completion "abc" abcdef abcdef-member))
150 (should (test-completion "def" +abba +abba-member))
151 (should (test-completion "abba" +abba +abba-member))
152 (should-not (test-completion "abcd" +abba +abba-member))
153 (should-not (test-completion "abc" abcdef #'ignore))
154 (should-not (test-completion "def" +abba #'ignore))
155 (should-not (test-completion "abba" +abba #'ignore))
156 (should-not (test-completion "abcd" +abba #'ignore))))
158 (defun minibuf-tests--test-completion-regexp (xform-collection)
159 (let ((abcdef (funcall xform-collection '("abc" "def")))
160 (+abba (funcall xform-collection '("abc" "abba" "def"))))
161 (let ((completion-regexp-list '(".")))
162 (should (test-completion "abc" abcdef))
163 (should (test-completion "def" +abba))
164 (should (test-completion "abba" +abba))
165 (should-not (test-completion "abcd" +abba)))
166 (let ((completion-regexp-list '("X")))
167 (should-not (test-completion "abc" abcdef))
168 (should-not (test-completion "def" +abba))
169 (should-not (test-completion "abba" +abba))
170 (should-not (test-completion "abcd" +abba)))))
173 ;;; Tests for `try-completion'.
174 (ert-deftest try-completion-string-list ()
175 (minibuf-tests--try-completion #'identity))
176 (ert-deftest try-completion-string-list-predicate ()
177 (minibuf-tests--try-completion-pred
178 #'identity #'minibuf-tests--memq-of-collection))
179 (ert-deftest try-completion-string-list-completion-regexp ()
180 (minibuf-tests--try-completion-regexp #'identity))
182 (ert-deftest try-completion-symbol-list ()
183 (minibuf-tests--try-completion
184 #'minibuf-tests--strings-to-symbol-list))
185 (ert-deftest try-completion-symbol-list-predicate ()
186 (minibuf-tests--try-completion-pred
187 #'minibuf-tests--strings-to-symbol-list
188 #'minibuf-tests--memq-of-collection))
189 (ert-deftest try-completion-symbol-list-completion-regexp ()
190 (minibuf-tests--try-completion-regexp
191 #'minibuf-tests--strings-to-symbol-list))
193 (ert-deftest try-completion-symbol-alist ()
194 (minibuf-tests--try-completion
195 #'minibuf-tests--strings-to-symbol-alist))
196 (ert-deftest try-completion-symbol-alist-predicate ()
197 (minibuf-tests--try-completion-pred
198 #'minibuf-tests--strings-to-symbol-alist
199 #'minibuf-tests--memq-of-collection))
200 (ert-deftest try-completion-symbol-alist-completion-regexp ()
201 (minibuf-tests--try-completion-regexp
202 #'minibuf-tests--strings-to-symbol-alist))
204 (ert-deftest try-completion-string-alist ()
205 (minibuf-tests--try-completion
206 #'minibuf-tests--strings-to-string-alist))
207 (ert-deftest try-completion-string-alist-predicate ()
208 (minibuf-tests--try-completion-pred
209 #'minibuf-tests--strings-to-string-alist
210 #'minibuf-tests--memq-of-collection))
211 (ert-deftest try-completion-string-alist-completion-regexp ()
212 (minibuf-tests--try-completion-regexp
213 #'minibuf-tests--strings-to-string-alist))
215 (ert-deftest try-completion-obarray ()
216 (minibuf-tests--try-completion
217 #'minibuf-tests--strings-to-obarray))
218 (ert-deftest try-completion-obarray-predicate ()
219 (minibuf-tests--try-completion-pred
220 #'minibuf-tests--strings-to-obarray
221 #'minibuf-tests--part-of-obarray))
222 (ert-deftest try-completion-obarray-completion-regexp ()
223 (minibuf-tests--try-completion-regexp
224 #'minibuf-tests--strings-to-obarray))
226 (ert-deftest try-completion-string-hashtable ()
227 (minibuf-tests--try-completion
228 #'minibuf-tests--strings-to-string-hashtable))
229 (ert-deftest try-completion-string-hashtable-predicate ()
230 (minibuf-tests--try-completion-pred
231 #'minibuf-tests--strings-to-string-hashtable
232 #'minibuf-tests--part-of-hashtable))
233 (ert-deftest try-completion-string-hashtable-completion-regexp ()
234 (minibuf-tests--try-completion-regexp
235 #'minibuf-tests--strings-to-string-hashtable))
237 (ert-deftest try-completion-symbol-hashtable ()
238 (minibuf-tests--try-completion
239 #'minibuf-tests--strings-to-symbol-hashtable))
240 (ert-deftest try-completion-symbol-hashtable-predicate ()
241 (minibuf-tests--try-completion-pred
242 #'minibuf-tests--strings-to-symbol-hashtable
243 #'minibuf-tests--part-of-hashtable))
244 (ert-deftest try-completion-symbol-hashtable-completion-regexp ()
245 (minibuf-tests--try-completion-regexp
246 #'minibuf-tests--strings-to-symbol-hashtable))
249 ;;; Tests for `all-completions'.
251 (ert-deftest all-completions-string-list ()
252 (minibuf-tests--all-completions #'identity))
253 (ert-deftest all-completions-string-list-predicate ()
254 (minibuf-tests--all-completions-pred
255 #'identity #'minibuf-tests--memq-of-collection))
256 (ert-deftest all-completions-string-list-completion-regexp ()
257 (minibuf-tests--all-completions-regexp #'identity))
259 (ert-deftest all-completions-symbol-list ()
260 (minibuf-tests--all-completions
261 #'minibuf-tests--strings-to-symbol-list))
262 (ert-deftest all-completions-symbol-list-predicate ()
263 (minibuf-tests--all-completions-pred
264 #'minibuf-tests--strings-to-symbol-list
265 #'minibuf-tests--memq-of-collection))
266 (ert-deftest all-completions-symbol-list-completion-regexp ()
267 (minibuf-tests--all-completions-regexp
268 #'minibuf-tests--strings-to-symbol-list))
270 (ert-deftest all-completions-symbol-alist ()
271 (minibuf-tests--all-completions
272 #'minibuf-tests--strings-to-symbol-alist))
273 (ert-deftest all-completions-symbol-alist-predicate ()
274 (minibuf-tests--all-completions-pred
275 #'minibuf-tests--strings-to-symbol-alist
276 #'minibuf-tests--memq-of-collection))
277 (ert-deftest all-completions-symbol-alist-completion-regexp ()
278 (minibuf-tests--all-completions-regexp
279 #'minibuf-tests--strings-to-symbol-alist))
281 (ert-deftest all-completions-string-alist ()
282 (minibuf-tests--all-completions
283 #'minibuf-tests--strings-to-string-alist))
284 (ert-deftest all-completions-string-alist-predicate ()
285 (minibuf-tests--all-completions-pred
286 #'minibuf-tests--strings-to-string-alist
287 #'minibuf-tests--memq-of-collection))
288 (ert-deftest all-completions-string-alist-completion-regexp ()
289 (minibuf-tests--all-completions-regexp
290 #'minibuf-tests--strings-to-string-alist))
292 (ert-deftest all-completions-obarray ()
293 (minibuf-tests--all-completions
294 #'minibuf-tests--strings-to-obarray))
295 (ert-deftest all-completions-obarray-predicate ()
296 (minibuf-tests--all-completions-pred
297 #'minibuf-tests--strings-to-obarray
298 #'minibuf-tests--part-of-obarray))
299 (ert-deftest all-completions-obarray-completion-regexp ()
300 (minibuf-tests--all-completions-regexp
301 #'minibuf-tests--strings-to-obarray))
303 (ert-deftest all-completions-string-hashtable ()
304 (minibuf-tests--all-completions
305 #'minibuf-tests--strings-to-string-hashtable))
306 (ert-deftest all-completions-string-hashtable-predicate ()
307 (minibuf-tests--all-completions-pred
308 #'minibuf-tests--strings-to-string-hashtable
309 #'minibuf-tests--part-of-hashtable))
310 (ert-deftest all-completions-string-hashtable-completion-regexp ()
311 (minibuf-tests--all-completions-regexp
312 #'minibuf-tests--strings-to-string-hashtable))
314 (ert-deftest all-completions-symbol-hashtable ()
315 (minibuf-tests--all-completions
316 #'minibuf-tests--strings-to-symbol-hashtable))
317 (ert-deftest all-completions-symbol-hashtable-predicate ()
318 (minibuf-tests--all-completions-pred
319 #'minibuf-tests--strings-to-symbol-hashtable
320 #'minibuf-tests--part-of-hashtable))
321 (ert-deftest all-completions-symbol-hashtable-completion-regexp ()
322 (minibuf-tests--all-completions-regexp
323 #'minibuf-tests--strings-to-symbol-hashtable))
326 ;;; Tests for `test-completion'.
328 (ert-deftest test-completion-string-list ()
329 (minibuf-tests--test-completion #'identity))
330 (ert-deftest test-completion-string-list-predicate ()
331 (minibuf-tests--test-completion-pred
332 #'identity #'minibuf-tests--memq-of-collection))
333 (ert-deftest test-completion-string-list-completion-regexp ()
334 (minibuf-tests--test-completion-regexp #'identity))
336 (ert-deftest test-completion-symbol-list ()
337 (minibuf-tests--test-completion
338 #'minibuf-tests--strings-to-symbol-list))
339 (ert-deftest test-completion-symbol-list-predicate ()
340 (minibuf-tests--test-completion-pred
341 #'minibuf-tests--strings-to-symbol-list
342 #'minibuf-tests--memq-of-collection))
343 (ert-deftest test-completion-symbol-list-completion-regexp ()
344 (minibuf-tests--test-completion-regexp
345 #'minibuf-tests--strings-to-symbol-list))
347 (ert-deftest test-completion-symbol-alist ()
348 (minibuf-tests--test-completion
349 #'minibuf-tests--strings-to-symbol-alist))
350 (ert-deftest test-completion-symbol-alist-predicate ()
351 (minibuf-tests--test-completion-pred
352 #'minibuf-tests--strings-to-symbol-alist
353 #'minibuf-tests--memq-of-collection))
354 (ert-deftest test-completion-symbol-alist-completion-regexp ()
355 (minibuf-tests--test-completion-regexp
356 #'minibuf-tests--strings-to-symbol-alist))
358 (ert-deftest test-completion-string-alist ()
359 (minibuf-tests--test-completion
360 #'minibuf-tests--strings-to-string-alist))
361 (ert-deftest test-completion-string-alist-predicate ()
362 (minibuf-tests--test-completion-pred
363 #'minibuf-tests--strings-to-string-alist
364 #'minibuf-tests--memq-of-collection))
365 (ert-deftest test-completion-string-alist-completion-regexp ()
366 (minibuf-tests--test-completion-regexp
367 #'minibuf-tests--strings-to-string-alist))
369 (ert-deftest test-completion-obarray ()
370 (minibuf-tests--test-completion
371 #'minibuf-tests--strings-to-obarray))
372 (ert-deftest test-completion-obarray-predicate ()
373 (minibuf-tests--test-completion-pred
374 #'minibuf-tests--strings-to-obarray
375 #'minibuf-tests--part-of-obarray))
376 (ert-deftest test-completion-obarray-completion-regexp ()
377 (minibuf-tests--test-completion-regexp
378 #'minibuf-tests--strings-to-obarray))
380 (ert-deftest test-completion-string-hashtable ()
381 (minibuf-tests--test-completion
382 #'minibuf-tests--strings-to-string-hashtable))
383 (ert-deftest test-completion-string-hashtable-predicate ()
384 (minibuf-tests--test-completion-pred
385 #'minibuf-tests--strings-to-string-hashtable
386 #'minibuf-tests--part-of-hashtable))
387 (ert-deftest test-completion-string-hashtable-completion-regexp ()
388 (minibuf-tests--test-completion-regexp
389 #'minibuf-tests--strings-to-string-hashtable))
391 (ert-deftest test-completion-symbol-hashtable ()
392 (minibuf-tests--test-completion
393 #'minibuf-tests--strings-to-symbol-hashtable))
394 (ert-deftest test-completion-symbol-hashtable-predicate ()
395 (minibuf-tests--test-completion-pred
396 #'minibuf-tests--strings-to-symbol-hashtable
397 ;; The predicate recieves a string as the key in this case.
398 (lambda (table)
399 (let ((in-table (minibuf-tests--part-of-hashtable table)))
400 (lambda (k v) (funcall in-table (intern k) v))))))
401 (ert-deftest test-completion-symbol-hashtable-completion-regexp ()
402 (minibuf-tests--test-completion-regexp
403 #'minibuf-tests--strings-to-symbol-hashtable))
406 ;;; minibuf-tests.el ends here