]> code.delx.au - gnu-emacs-elpa/blob - test/core-tests.el
Revert "Remove completions without annotations when considering duplicates"
[gnu-emacs-elpa] / test / core-tests.el
1 ;;; core-tests.el --- company-mode tests -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Dmitry Gutov
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 (require 'company-tests)
23
24 (ert-deftest company-good-prefix ()
25 (let ((company-minimum-prefix-length 5)
26 company-abort-manual-when-too-short
27 company--manual-action ;idle begin
28 (company-selection-changed t)) ;has no effect
29 (should (eq t (company--good-prefix-p "!@#$%")))
30 (should (eq nil (company--good-prefix-p "abcd")))
31 (should (eq nil (company--good-prefix-p 'stop)))
32 (should (eq t (company--good-prefix-p '("foo" . 5))))
33 (should (eq nil (company--good-prefix-p '("foo" . 4))))
34 (should (eq t (company--good-prefix-p '("foo" . t))))))
35
36 (ert-deftest company--manual-prefix-set-and-unset ()
37 (with-temp-buffer
38 (insert "ab")
39 (company-mode)
40 (let (company-frontends
41 (company-backends
42 (list (lambda (command &optional _)
43 (cl-case command
44 (prefix (buffer-substring (point-min) (point)))
45 (candidates '("abc" "abd")))))))
46 (company-manual-begin)
47 (should (equal "ab" company--manual-prefix))
48 (company-abort)
49 (should (null company--manual-prefix)))))
50
51 (ert-deftest company-abort-manual-when-too-short ()
52 (let ((company-minimum-prefix-length 5)
53 (company-abort-manual-when-too-short t)
54 (company-selection-changed t)) ;has not effect
55 (let ((company--manual-action nil)) ;idle begin
56 (should (eq t (company--good-prefix-p "!@#$%")))
57 (should (eq t (company--good-prefix-p '("foo" . 5))))
58 (should (eq t (company--good-prefix-p '("foo" . t)))))
59 (let ((company--manual-action t)
60 (company--manual-prefix "abc")) ;manual begin from this prefix
61 (should (eq t (company--good-prefix-p "!@#$")))
62 (should (eq nil (company--good-prefix-p "ab")))
63 (should (eq nil (company--good-prefix-p 'stop)))
64 (should (eq t (company--good-prefix-p '("foo" . 4))))
65 (should (eq t (company--good-prefix-p "abcd")))
66 (should (eq t (company--good-prefix-p "abc")))
67 (should (eq t (company--good-prefix-p '("bar" . t)))))))
68
69 (ert-deftest company-common-with-non-prefix-completion ()
70 (let ((company-backend #'ignore)
71 (company-prefix "abc")
72 company-candidates
73 company-candidates-length
74 company-candidates-cache
75 company-common)
76 (company-update-candidates '("abc" "def-abc"))
77 (should (null company-common))
78 (company-update-candidates '("abc" "abe-c"))
79 (should (null company-common))
80 (company-update-candidates '("abcd" "abcde" "abcdf"))
81 (should (equal "abcd" company-common))))
82
83 (ert-deftest company-multi-backend-with-lambdas ()
84 (let ((company-backend
85 (list (lambda (command &optional _ &rest _r)
86 (cl-case command
87 (prefix "z")
88 (candidates '("a" "b"))))
89 (lambda (command &optional _ &rest _r)
90 (cl-case command
91 (prefix "z")
92 (candidates '("c" "d")))))))
93 (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
94
95 (ert-deftest company-multi-backend-filters-backends-by-prefix ()
96 (let ((company-backend
97 (list (lambda (command &optional _ &rest _r)
98 (cl-case command
99 (prefix (cons "z" t))
100 (candidates '("a" "b"))))
101 (lambda (command &optional _ &rest _r)
102 (cl-case command
103 (prefix "t")
104 (candidates '("c" "d"))))
105 (lambda (command &optional _ &rest _r)
106 (cl-case command
107 (prefix "z")
108 (candidates '("e" "f")))))))
109 (should (equal (company-call-backend 'candidates "z") '("a" "b" "e" "f")))))
110
111 (ert-deftest company-multi-backend-remembers-candidate-backend ()
112 (let ((company-backend
113 (list (lambda (command &optional _)
114 (cl-case command
115 (ignore-case nil)
116 (annotation "1")
117 (candidates '("a" "c"))
118 (post-completion "13")))
119 (lambda (command &optional _)
120 (cl-case command
121 (ignore-case t)
122 (annotation "2")
123 (candidates '("b" "d"))
124 (post-completion "42")))
125 (lambda (command &optional _)
126 (cl-case command
127 (annotation "3")
128 (candidates '("e"))
129 (post-completion "74"))))))
130 (let ((candidates (company-calculate-candidates nil)))
131 (should (equal candidates '("a" "b" "c" "d" "e")))
132 (should (equal t (company-call-backend 'ignore-case)))
133 (should (equal "1" (company-call-backend 'annotation (nth 0 candidates))))
134 (should (equal "2" (company-call-backend 'annotation (nth 1 candidates))))
135 (should (equal "13" (company-call-backend 'post-completion (nth 2 candidates))))
136 (should (equal "42" (company-call-backend 'post-completion (nth 3 candidates))))
137 (should (equal "3" (company-call-backend 'annotation (nth 4 candidates))))
138 (should (equal "74" (company-call-backend 'post-completion (nth 4 candidates)))))))
139
140 (ert-deftest company-multi-backend-handles-keyword-with ()
141 (let ((primo (lambda (command &optional _)
142 (cl-case command
143 (prefix "a")
144 (candidates '("abb" "abc" "abd")))))
145 (secundo (lambda (command &optional _)
146 (cl-case command
147 (prefix "a")
148 (candidates '("acc" "acd"))))))
149 (let ((company-backend (list 'ignore 'ignore :with secundo)))
150 (should (null (company-call-backend 'prefix))))
151 (let ((company-backend (list 'ignore primo :with secundo)))
152 (should (equal "a" (company-call-backend 'prefix)))
153 (should (equal '("abb" "abc" "abd" "acc" "acd")
154 (company-call-backend 'candidates "a"))))))
155
156 (ert-deftest company-begin-backend-failure-doesnt-break-company-backends ()
157 (with-temp-buffer
158 (insert "a")
159 (company-mode)
160 (should-error
161 (company-begin-backend #'ignore))
162 (let (company-frontends
163 (company-backends
164 (list (lambda (command &optional _)
165 (cl-case command
166 (prefix "a")
167 (candidates '("a" "ab" "ac")))))))
168 (let (this-command)
169 (company-call 'complete))
170 (should (eq 3 company-candidates-length)))))
171
172 (ert-deftest company-require-match-explicit ()
173 (with-temp-buffer
174 (insert "ab")
175 (company-mode)
176 (let (company-frontends
177 (company-require-match 'company-explicit-action-p)
178 (company-backends
179 (list (lambda (command &optional _)
180 (cl-case command
181 (prefix (buffer-substring (point-min) (point)))
182 (candidates '("abc" "abd")))))))
183 (let (this-command)
184 (company-complete))
185 (let ((last-command-event ?e))
186 (company-call 'self-insert-command 1))
187 (should (eq 2 company-candidates-length))
188 (should (eq 3 (point))))))
189
190 (ert-deftest company-dont-require-match-when-idle ()
191 (with-temp-buffer
192 (insert "ab")
193 (company-mode)
194 (let (company-frontends
195 (company-minimum-prefix-length 2)
196 (company-require-match 'company-explicit-action-p)
197 (company-backends
198 (list (lambda (command &optional _)
199 (cl-case command
200 (prefix (buffer-substring (point-min) (point)))
201 (candidates '("abc" "abd")))))))
202 (company-idle-begin (current-buffer) (selected-window)
203 (buffer-chars-modified-tick) (point))
204 (should (eq 2 company-candidates-length))
205 (let ((last-command-event ?e))
206 (company-call 'self-insert-command 1))
207 (should (eq nil company-candidates-length))
208 (should (eq 4 (point))))))
209
210 (ert-deftest company-dont-require-match-if-was-a-match-and-old-prefix-ended ()
211 (with-temp-buffer
212 (insert "ab")
213 (company-mode)
214 (let (company-frontends
215 company-auto-complete
216 (company-require-match t)
217 (company-backends
218 (list (lambda (command &optional _)
219 (cl-case command
220 (prefix (company-grab-word))
221 (candidates '("abc" "ab" "abd"))
222 (sorted t))))))
223 (let (this-command)
224 (company-complete))
225 (let ((last-command-event ?e))
226 (company-call 'self-insert-command 1))
227 (should (eq 3 company-candidates-length))
228 (should (eq 3 (point)))
229 (let ((last-command-event ? ))
230 (company-call 'self-insert-command 1))
231 (should (null company-candidates-length))
232 (should (eq 4 (point))))))
233
234 (ert-deftest company-dont-require-match-if-was-a-match-and-new-prefix-is-stop ()
235 (with-temp-buffer
236 (company-mode)
237 (insert "c")
238 (let (company-frontends
239 (company-require-match t)
240 (company-backends
241 (list (lambda (command &optional _)
242 (cl-case command
243 (prefix (if (> (point) 2)
244 'stop
245 (buffer-substring (point-min) (point))))
246 (candidates '("a" "b" "c")))))))
247 (let (this-command)
248 (company-complete))
249 (should (eq 3 company-candidates-length))
250 (let ((last-command-event ?e))
251 (company-call 'self-insert-command 1))
252 (should (not company-candidates)))))
253
254 (ert-deftest company-should-complete-whitelist ()
255 (with-temp-buffer
256 (insert "ab")
257 (company-mode)
258 (let (company-frontends
259 company-begin-commands
260 (company-backends
261 (list (lambda (command &optional _)
262 (cl-case command
263 (prefix (buffer-substring (point-min) (point)))
264 (candidates '("abc" "abd")))))))
265 (let ((company-continue-commands nil))
266 (let (this-command)
267 (company-complete))
268 (company-call 'backward-delete-char 1)
269 (should (null company-candidates-length)))
270 (let ((company-continue-commands '(backward-delete-char)))
271 (let (this-command)
272 (company-complete))
273 (company-call 'backward-delete-char 1)
274 (should (eq 2 company-candidates-length))))))
275
276 (ert-deftest company-should-complete-blacklist ()
277 (with-temp-buffer
278 (insert "ab")
279 (company-mode)
280 (let (company-frontends
281 company-begin-commands
282 (company-backends
283 (list (lambda (command &optional _)
284 (cl-case command
285 (prefix (buffer-substring (point-min) (point)))
286 (candidates '("abc" "abd")))))))
287 (let ((company-continue-commands '(not backward-delete-char)))
288 (let (this-command)
289 (company-complete))
290 (company-call 'backward-delete-char 1)
291 (should (null company-candidates-length)))
292 (let ((company-continue-commands '(not backward-delete-char-untabify)))
293 (let (this-command)
294 (company-complete))
295 (company-call 'backward-delete-char 1)
296 (should (eq 2 company-candidates-length))))))
297
298 (ert-deftest company-auto-complete-explicit ()
299 (with-temp-buffer
300 (insert "ab")
301 (company-mode)
302 (let (company-frontends
303 (company-auto-complete 'company-explicit-action-p)
304 (company-auto-complete-chars '(? ))
305 (company-backends
306 (list (lambda (command &optional _)
307 (cl-case command
308 (prefix (buffer-substring (point-min) (point)))
309 (candidates '("abcd" "abef")))))))
310 (let (this-command)
311 (company-complete))
312 (let ((last-command-event ? ))
313 (company-call 'self-insert-command 1))
314 (should (string= "abcd " (buffer-string))))))
315
316 (ert-deftest company-no-auto-complete-when-idle ()
317 (with-temp-buffer
318 (insert "ab")
319 (company-mode)
320 (let (company-frontends
321 (company-auto-complete 'company-explicit-action-p)
322 (company-auto-complete-chars '(? ))
323 (company-minimum-prefix-length 2)
324 (company-backends
325 (list (lambda (command &optional _)
326 (cl-case command
327 (prefix (buffer-substring (point-min) (point)))
328 (candidates '("abcd" "abef")))))))
329 (company-idle-begin (current-buffer) (selected-window)
330 (buffer-chars-modified-tick) (point))
331 (let ((last-command-event ? ))
332 (company-call 'self-insert-command 1))
333 (should (string= "ab " (buffer-string))))))
334
335 (ert-deftest company-clears-explicit-action-when-no-matches ()
336 (with-temp-buffer
337 (company-mode)
338 (let (company-frontends
339 company-backends)
340 (company-call 'manual-begin) ;; fails
341 (should (null company-candidates))
342 (should (null (company-explicit-action-p))))))
343
344 (ert-deftest company-ignore-case-replaces-prefix ()
345 (with-temp-buffer
346 (company-mode)
347 (let (company-frontends
348 (company-backends
349 (list (lambda (command &optional _)
350 (cl-case command
351 (prefix (buffer-substring (point-min) (point)))
352 (candidates '("abcd" "abef"))
353 (ignore-case t))))))
354 (insert "A")
355 (let (this-command)
356 (company-complete))
357 (should (string= "ab" (buffer-string)))
358 (delete-char -2)
359 (insert "A") ; hack, to keep it in one test
360 (company-complete-selection)
361 (should (string= "abcd" (buffer-string))))))
362
363 (ert-deftest company-ignore-case-with-keep-prefix ()
364 (with-temp-buffer
365 (insert "AB")
366 (company-mode)
367 (let (company-frontends
368 (company-backends
369 (list (lambda (command &optional _)
370 (cl-case command
371 (prefix (buffer-substring (point-min) (point)))
372 (candidates '("abcd" "abef"))
373 (ignore-case 'keep-prefix))))))
374 (let (this-command)
375 (company-complete))
376 (company-complete-selection)
377 (should (string= "ABcd" (buffer-string))))))
378
379 (ert-deftest company-non-prefix-completion ()
380 (with-temp-buffer
381 (insert "tc")
382 (company-mode)
383 (let (company-frontends
384 (company-backends
385 (list (lambda (command &optional _)
386 (cl-case command
387 (prefix (buffer-substring (point-min) (point)))
388 (candidates '("tea-cup" "teal-color")))))))
389 (let (this-command)
390 (company-complete))
391 (should (string= "tc" (buffer-string)))
392 (company-complete-selection)
393 (should (string= "tea-cup" (buffer-string))))))
394
395 (defvar ct-sorted nil)
396
397 (defun ct-equal-including-properties (list1 list2)
398 (or (and (not list1) (not list2))
399 (and (ert-equal-including-properties (car list1) (car list2))
400 (ct-equal-including-properties (cdr list1) (cdr list2)))))
401
402 (ert-deftest company-strips-duplicates-returns-nil ()
403 (should (null (company--preprocess-candidates nil))))
404
405 (ert-deftest company-strips-duplicates-within-groups ()
406 (let* ((kvs '(("a" . "b")
407 ("a" . nil)
408 ("a" . "b")
409 ("a" . "c")
410 ("a" . "b")
411 ("b" . "c")
412 ("b" . nil)
413 ("a" . "b")))
414 (fn (lambda (kvs)
415 (mapcar (lambda (kv) (propertize (car kv) 'ann (cdr kv)))
416 kvs)))
417 (company-backend
418 (lambda (command &optional arg)
419 (pcase command
420 (`prefix "")
421 (`sorted ct-sorted)
422 (`duplicates t)
423 (`annotation (get-text-property 0 'ann arg)))))
424 (reference '(("a" . "b")
425 ("a" . nil)
426 ("a" . "c")
427 ("b" . "c")
428 ("b" . nil)
429 ("a" . "b"))))
430 (let ((ct-sorted t))
431 (should (ct-equal-including-properties
432 (company--preprocess-candidates (funcall fn kvs))
433 (funcall fn reference))))
434 (should (ct-equal-including-properties
435 (company--preprocess-candidates (funcall fn kvs))
436 (funcall fn (butlast reference))))))
437
438 ;;; Row and column
439
440 (ert-deftest company-column-with-composition ()
441 :tags '(interactive)
442 (with-temp-buffer
443 (save-window-excursion
444 (set-window-buffer nil (current-buffer))
445 (insert "lambda ()")
446 (compose-region 1 (1+ (length "lambda")) "\\")
447 (should (= (company--column) 4)))))
448
449 (ert-deftest company-column-with-line-prefix ()
450 :tags '(interactive)
451 (with-temp-buffer
452 (save-window-excursion
453 (set-window-buffer nil (current-buffer))
454 (insert "foo")
455 (put-text-property (point-min) (point) 'line-prefix " ")
456 (should (= (company--column) 5)))))
457
458 (ert-deftest company-column-with-line-prefix-on-empty-line ()
459 :tags '(interactive)
460 (with-temp-buffer
461 (save-window-excursion
462 (set-window-buffer nil (current-buffer))
463 (insert "\n")
464 (forward-char -1)
465 (put-text-property (point-min) (point-max) 'line-prefix " ")
466 (should (= (company--column) 2)))))
467
468 (ert-deftest company-column-with-tabs ()
469 :tags '(interactive)
470 (with-temp-buffer
471 (save-window-excursion
472 (set-window-buffer nil (current-buffer))
473 (insert "|\t|\t|\t(")
474 (let ((tab-width 8))
475 (should (= (company--column) 25))))))
476
477 (ert-deftest company-row-with-header-line-format ()
478 :tags '(interactive)
479 (with-temp-buffer
480 (save-window-excursion
481 (set-window-buffer nil (current-buffer))
482 (should (= (company--row) 0))
483 (setq header-line-format "aaaaaaa")
484 (should (= (company--row) 0)))))