]> code.delx.au - gnu-emacs-elpa/blob - company-tests.el
company-update-candidates: Don't be fooled by a few prefix completions
[gnu-emacs-elpa] / company-tests.el
1 ;;; company-tests.el --- company-mode tests -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2011, 2013-2014 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
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
23 ;;; Commentary:
24 ;;
25
26 ;;; Code:
27
28 (require 'ert)
29 (require 'company)
30 (require 'company-keywords)
31 (require 'company-clang)
32
33 (defun company--column (&optional pos)
34 (car (company--col-row pos)))
35
36 ;;; Core
37
38 (ert-deftest company-sorted-keywords ()
39 "Test that keywords in `company-keywords-alist' are in alphabetical order."
40 (dolist (pair company-keywords-alist)
41 (when (consp (cdr pair))
42 (let ((prev (cadr pair)))
43 (dolist (next (cddr pair))
44 (should (not (equal prev next)))
45 (should (string< prev next))
46 (setq prev next))))))
47
48 (ert-deftest company-good-prefix ()
49 (let ((company-minimum-prefix-length 5)
50 company-abort-manual-when-too-short
51 company--manual-action ;idle begin
52 (company-selection-changed t)) ;has no effect
53 (should (eq t (company--good-prefix-p "!@#$%")))
54 (should (eq nil (company--good-prefix-p "abcd")))
55 (should (eq nil (company--good-prefix-p 'stop)))
56 (should (eq t (company--good-prefix-p '("foo" . 5))))
57 (should (eq nil (company--good-prefix-p '("foo" . 4))))
58 (should (eq t (company--good-prefix-p '("foo" . t))))))
59
60 (ert-deftest company--manual-prefix-set-and-unset ()
61 (with-temp-buffer
62 (insert "ab")
63 (company-mode)
64 (let (company-frontends
65 (company-backends
66 (list (lambda (command &optional arg)
67 (cl-case command
68 (prefix (buffer-substring (point-min) (point)))
69 (candidates '("abc" "abd")))))))
70 (company-manual-begin)
71 (should (equal "ab" company--manual-prefix))
72 (company-abort)
73 (should (null company--manual-prefix)))))
74
75 (ert-deftest company-abort-manual-when-too-short ()
76 (let ((company-minimum-prefix-length 5)
77 (company-abort-manual-when-too-short t)
78 (company-selection-changed t)) ;has not effect
79 (let ((company--manual-action nil)) ;idle begin
80 (should (eq t (company--good-prefix-p "!@#$%")))
81 (should (eq t (company--good-prefix-p '("foo" . 5))))
82 (should (eq t (company--good-prefix-p '("foo" . t)))))
83 (let ((company--manual-action t)
84 (company--manual-prefix "abc")) ;manual begin from this prefix
85 (should (eq t (company--good-prefix-p "!@#$")))
86 (should (eq nil (company--good-prefix-p "ab")))
87 (should (eq nil (company--good-prefix-p 'stop)))
88 (should (eq t (company--good-prefix-p '("foo" . 4))))
89 (should (eq t (company--good-prefix-p "abcd")))
90 (should (eq t (company--good-prefix-p "abc")))
91 (should (eq t (company--good-prefix-p '("bar" . t)))))))
92
93 (ert-deftest company-common-with-non-prefix-completion ()
94 (let ((company-backend #'ignore)
95 (company-prefix "abc")
96 company-candidates
97 company-candidates-length
98 company-candidates-cache
99 company-common)
100 (company-update-candidates '("abc" "def-abc"))
101 (should (null company-common))
102 (company-update-candidates '("abc" "abe-c"))
103 (should (null company-common))
104 (company-update-candidates '("abcd" "abcde" "abcdf"))
105 (should (equal "abcd" company-common))))
106
107 (ert-deftest company-multi-backend-with-lambdas ()
108 (let ((company-backend
109 (list (lambda (command &optional arg &rest ignore)
110 (cl-case command
111 (prefix "z")
112 (candidates '("a" "b"))))
113 (lambda (command &optional arg &rest ignore)
114 (cl-case command
115 (prefix "z")
116 (candidates '("c" "d")))))))
117 (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
118
119 (ert-deftest company-multi-backend-filters-backends-by-prefix ()
120 (let ((company-backend
121 (list (lambda (command &optional arg &rest ignore)
122 (cl-case command
123 (prefix (cons "z" t))
124 (candidates '("a" "b"))))
125 (lambda (command &optional arg &rest ignore)
126 (cl-case command
127 (prefix "t")
128 (candidates '("c" "d"))))
129 (lambda (command &optional arg &rest ignore)
130 (cl-case command
131 (prefix "z")
132 (candidates '("e" "f")))))))
133 (should (equal (company-call-backend 'candidates "z") '("a" "b" "e" "f")))))
134
135 (ert-deftest company-multi-backend-remembers-candidate-backend ()
136 (let ((company-backend
137 (list (lambda (command &optional arg)
138 (cl-case command
139 (ignore-case nil)
140 (annotation "1")
141 (candidates '("a" "c"))
142 (post-completion "13")))
143 (lambda (command &optional arg)
144 (cl-case command
145 (ignore-case t)
146 (annotation "2")
147 (candidates '("b" "d"))
148 (post-completion "42")))
149 (lambda (command &optional arg)
150 (cl-case command
151 (annotation "3")
152 (candidates '("e"))
153 (post-completion "74"))))))
154 (let ((candidates (company-calculate-candidates nil)))
155 (should (equal candidates '("a" "b" "c" "d" "e")))
156 (should (equal t (company-call-backend 'ignore-case)))
157 (should (equal "1" (company-call-backend 'annotation (nth 0 candidates))))
158 (should (equal "2" (company-call-backend 'annotation (nth 1 candidates))))
159 (should (equal "13" (company-call-backend 'post-completion (nth 2 candidates))))
160 (should (equal "42" (company-call-backend 'post-completion (nth 3 candidates))))
161 (should (equal "3" (company-call-backend 'annotation (nth 4 candidates))))
162 (should (equal "74" (company-call-backend 'post-completion (nth 4 candidates)))))))
163
164 (ert-deftest company-multi-backend-handles-keyword-with ()
165 (let ((primo (lambda (command &optional arg)
166 (cl-case command
167 (prefix "a")
168 (candidates '("abb" "abc" "abd")))))
169 (secundo (lambda (command &optional arg)
170 (cl-case command
171 (prefix "a")
172 (candidates '("acc" "acd"))))))
173 (let ((company-backend (list 'ignore 'ignore :with secundo)))
174 (should (null (company-call-backend 'prefix))))
175 (let ((company-backend (list 'ignore primo :with secundo)))
176 (should (equal "a" (company-call-backend 'prefix)))
177 (should (equal '("abb" "abc" "abd" "acc" "acd")
178 (company-call-backend 'candidates "a"))))))
179
180 (ert-deftest company-begin-backend-failure-doesnt-break-company-backends ()
181 (with-temp-buffer
182 (insert "a")
183 (company-mode)
184 (should-error
185 (company-begin-backend (lambda (command &rest ignore))))
186 (let (company-frontends
187 (company-backends
188 (list (lambda (command &optional arg)
189 (cl-case command
190 (prefix "a")
191 (candidates '("a" "ab" "ac")))))))
192 (let (this-command)
193 (company-call 'complete))
194 (should (eq 3 company-candidates-length)))))
195
196 (ert-deftest company-require-match-explicit ()
197 (with-temp-buffer
198 (insert "ab")
199 (company-mode)
200 (let (company-frontends
201 (company-require-match 'company-explicit-action-p)
202 (company-backends
203 (list (lambda (command &optional arg)
204 (cl-case command
205 (prefix (buffer-substring (point-min) (point)))
206 (candidates '("abc" "abd")))))))
207 (let (this-command)
208 (company-complete))
209 (let ((last-command-event ?e))
210 (company-call 'self-insert-command 1))
211 (should (eq 2 company-candidates-length))
212 (should (eq 3 (point))))))
213
214 (ert-deftest company-dont-require-match-when-idle ()
215 (with-temp-buffer
216 (insert "ab")
217 (company-mode)
218 (let (company-frontends
219 (company-minimum-prefix-length 2)
220 (company-require-match 'company-explicit-action-p)
221 (company-backends
222 (list (lambda (command &optional arg)
223 (cl-case command
224 (prefix (buffer-substring (point-min) (point)))
225 (candidates '("abc" "abd")))))))
226 (company-idle-begin (current-buffer) (selected-window)
227 (buffer-chars-modified-tick) (point))
228 (should (eq 2 company-candidates-length))
229 (let ((last-command-event ?e))
230 (company-call 'self-insert-command 1))
231 (should (eq nil company-candidates-length))
232 (should (eq 4 (point))))))
233
234 (ert-deftest company-dont-require-match-if-old-prefix-ended-and-was-a-match ()
235 (with-temp-buffer
236 (insert "ab")
237 (company-mode)
238 (let (company-frontends
239 (company-require-match 'company-explicit-action-p)
240 (company-backends
241 (list (lambda (command &optional arg)
242 (cl-case command
243 (prefix (company-grab-word))
244 (candidates '("abc" "ab" "abd"))
245 (sorted t))))))
246 (let (this-command)
247 (company-complete))
248 (let ((last-command-event ?e))
249 (company-call 'self-insert-command 1))
250 (should (eq 3 company-candidates-length))
251 (should (eq 3 (point)))
252 (let ((last-command-event ? ))
253 (company-call 'self-insert-command 1))
254 (should (null company-candidates-length))
255 (should (eq 4 (point))))))
256
257 (ert-deftest company-should-complete-whitelist ()
258 (with-temp-buffer
259 (insert "ab")
260 (company-mode)
261 (let (company-frontends
262 company-begin-commands
263 (company-backends
264 (list (lambda (command &optional arg)
265 (cl-case command
266 (prefix (buffer-substring (point-min) (point)))
267 (candidates '("abc" "abd")))))))
268 (let ((company-continue-commands nil))
269 (let (this-command)
270 (company-complete))
271 (company-call 'backward-delete-char 1)
272 (should (null company-candidates-length)))
273 (let ((company-continue-commands '(backward-delete-char)))
274 (let (this-command)
275 (company-complete))
276 (company-call 'backward-delete-char 1)
277 (should (eq 2 company-candidates-length))))))
278
279 (ert-deftest company-should-complete-blacklist ()
280 (with-temp-buffer
281 (insert "ab")
282 (company-mode)
283 (let (company-frontends
284 company-begin-commands
285 (company-backends
286 (list (lambda (command &optional arg)
287 (cl-case command
288 (prefix (buffer-substring (point-min) (point)))
289 (candidates '("abc" "abd")))))))
290 (let ((company-continue-commands '(not backward-delete-char)))
291 (let (this-command)
292 (company-complete))
293 (company-call 'backward-delete-char 1)
294 (should (null company-candidates-length)))
295 (let ((company-continue-commands '(not backward-delete-char-untabify)))
296 (let (this-command)
297 (company-complete))
298 (company-call 'backward-delete-char 1)
299 (should (eq 2 company-candidates-length))))))
300
301 (ert-deftest company-auto-complete-explicit ()
302 (with-temp-buffer
303 (insert "ab")
304 (company-mode)
305 (let (company-frontends
306 (company-auto-complete 'company-explicit-action-p)
307 (company-auto-complete-chars '(? ))
308 (company-backends
309 (list (lambda (command &optional arg)
310 (cl-case command
311 (prefix (buffer-substring (point-min) (point)))
312 (candidates '("abcd" "abef")))))))
313 (let (this-command)
314 (company-complete))
315 (let ((last-command-event ? ))
316 (company-call 'self-insert-command 1))
317 (should (string= "abcd " (buffer-string))))))
318
319 (ert-deftest company-no-auto-complete-when-idle ()
320 (with-temp-buffer
321 (insert "ab")
322 (company-mode)
323 (let (company-frontends
324 (company-auto-complete 'company-explicit-action-p)
325 (company-auto-complete-chars '(? ))
326 (company-minimum-prefix-length 2)
327 (company-backends
328 (list (lambda (command &optional arg)
329 (cl-case command
330 (prefix (buffer-substring (point-min) (point)))
331 (candidates '("abcd" "abef")))))))
332 (company-idle-begin (current-buffer) (selected-window)
333 (buffer-chars-modified-tick) (point))
334 (let ((last-command-event ? ))
335 (company-call 'self-insert-command 1))
336 (should (string= "ab " (buffer-string))))))
337
338 (ert-deftest company-clears-explicit-action-when-no-matches ()
339 (with-temp-buffer
340 (company-mode)
341 (let (company-frontends
342 company-backends)
343 (company-call 'manual-begin) ;; fails
344 (should (null company-candidates))
345 (should (null (company-explicit-action-p))))))
346
347 (ert-deftest company-ignore-case-replaces-prefix ()
348 (with-temp-buffer
349 (company-mode)
350 (let (company-frontends
351 company-end-of-buffer-workaround
352 (company-backends
353 (list (lambda (command &optional arg)
354 (cl-case command
355 (prefix (buffer-substring (point-min) (point)))
356 (candidates '("abcd" "abef"))
357 (ignore-case t))))))
358 (insert "A")
359 (let (this-command)
360 (company-complete))
361 (should (string= "ab" (buffer-string)))
362 (delete-char -2)
363 (insert "A") ; hack, to keep it in one test
364 (company-complete-selection)
365 (should (string= "abcd" (buffer-string))))))
366
367 (ert-deftest company-ignore-case-with-keep-prefix ()
368 (with-temp-buffer
369 (insert "AB")
370 (company-mode)
371 (let (company-frontends
372 (company-backends
373 (list (lambda (command &optional arg)
374 (cl-case command
375 (prefix (buffer-substring (point-min) (point)))
376 (candidates '("abcd" "abef"))
377 (ignore-case 'keep-prefix))))))
378 (let (this-command)
379 (company-complete))
380 (company-complete-selection)
381 (should (string= "ABcd" (buffer-string))))))
382
383 (ert-deftest company-non-prefix-completion ()
384 (with-temp-buffer
385 (insert "tc")
386 (company-mode)
387 (let (company-frontends
388 company-end-of-buffer-workaround
389 (company-backends
390 (list (lambda (command &optional arg)
391 (cl-case command
392 (prefix (buffer-substring (point-min) (point)))
393 (candidates '("tea-cup" "teal-color")))))))
394 (let (this-command)
395 (company-complete))
396 (should (string= "tc" (buffer-string)))
397 (company-complete-selection)
398 (should (string= "tea-cup" (buffer-string))))))
399
400 (ert-deftest company-pseudo-tooltip-does-not-get-displaced ()
401 :tags '(interactive)
402 (with-temp-buffer
403 (save-window-excursion
404 (set-window-buffer nil (current-buffer))
405 (save-excursion (insert " ff"))
406 (company-mode)
407 (let ((company-frontends '(company-pseudo-tooltip-frontend))
408 (company-begin-commands '(self-insert-command))
409 (company-backends
410 (list (lambda (c &optional arg)
411 (cl-case c (prefix "") (candidates '("a" "b" "c")))))))
412 (let (this-command)
413 (company-call 'complete))
414 (company-call 'open-line 1)
415 (should (eq 1 (overlay-start company-pseudo-tooltip-overlay)))))))
416
417 (ert-deftest company-pseudo-tooltip-show ()
418 :tags '(interactive)
419 (with-temp-buffer
420 (save-window-excursion
421 (set-window-buffer nil (current-buffer))
422 (insert "aaaa\n bb\nccccccc\nddd")
423 (search-backward "bb")
424 (let ((col (company--column))
425 (company-candidates-length 2)
426 (company-candidates '("123" "45"))
427 (company-backend 'ignore))
428 (company-pseudo-tooltip-show (company--row) col 0)
429 (let ((ov company-pseudo-tooltip-overlay))
430 ;; With margins.
431 (should (eq (overlay-get ov 'company-width) 5))
432 ;; FIXME: Make it 2?
433 (should (eq (overlay-get ov 'company-height) company-tooltip-limit))
434 (should (eq (overlay-get ov 'company-column) col))
435 (should (string= (overlay-get ov 'company-display)
436 "\n 123 \nc 45 c\nddd\n")))))))
437
438 (ert-deftest company-pseudo-tooltip-edit-updates-width ()
439 :tags '(interactive)
440 (with-temp-buffer
441 (set-window-buffer nil (current-buffer))
442 (let ((company-candidates-length 5)
443 (company-candidates '("123" "45" "67" "89" "1011"))
444 (company-backend 'ignore)
445 (company-tooltip-limit 4)
446 (company-tooltip-offset-display 'scrollbar))
447 (company-pseudo-tooltip-show (company--row)
448 (company--column)
449 0)
450 (should (eq (overlay-get company-pseudo-tooltip-overlay 'company-width)
451 6))
452 (company-pseudo-tooltip-edit 4)
453 (should (eq (overlay-get company-pseudo-tooltip-overlay 'company-width)
454 7)))))
455
456 (ert-deftest company-preview-show-with-annotations ()
457 :tags '(interactive)
458 (with-temp-buffer
459 (save-window-excursion
460 (set-window-buffer nil (current-buffer))
461 (save-excursion (insert "\n"))
462 (let ((company-candidates-length 1)
463 (company-candidates '("123")))
464 (company-preview-show-at-point (point))
465 (let* ((ov company-preview-overlay)
466 (str (overlay-get ov 'after-string)))
467 (should (string= str "123"))
468 (should (eq (get-text-property 0 'cursor str) t)))))))
469
470 (ert-deftest company-pseudo-tooltip-show-with-annotations ()
471 :tags '(interactive)
472 (with-temp-buffer
473 (save-window-excursion
474 (set-window-buffer nil (current-buffer))
475 (insert " ")
476 (save-excursion (insert "\n"))
477 (let ((company-candidates-length 2)
478 (company-backend (lambda (action &optional arg &rest _ignore)
479 (when (eq action 'annotation)
480 (cdr (assoc arg '(("123" . "(4)")))))))
481 (company-candidates '("123" "45"))
482 company-tooltip-align-annotations)
483 (company-pseudo-tooltip-show-at-point (point) 0)
484 (let ((ov company-pseudo-tooltip-overlay))
485 ;; With margins.
486 (should (eq (overlay-get ov 'company-width) 8))
487 (should (string= (overlay-get ov 'company-display)
488 "\n 123(4) \n 45 \n")))))))
489
490 (ert-deftest company-pseudo-tooltip-show-with-annotations-right-aligned ()
491 :tags '(interactive)
492 (with-temp-buffer
493 (save-window-excursion
494 (set-window-buffer nil (current-buffer))
495 (insert " ")
496 (save-excursion (insert "\n"))
497 (let ((company-candidates-length 3)
498 (company-backend (lambda (action &optional arg &rest _ignore)
499 (when (eq action 'annotation)
500 (cdr (assoc arg '(("123" . "(4)")
501 ("67" . "(891011)")))))))
502 (company-candidates '("123" "45" "67"))
503 (company-tooltip-align-annotations t))
504 (company-pseudo-tooltip-show-at-point (point) 0)
505 (let ((ov company-pseudo-tooltip-overlay))
506 ;; With margins.
507 (should (eq (overlay-get ov 'company-width) 13))
508 (should (string= (overlay-get ov 'company-display)
509 "\n 123 (4) \n 45 \n 67 (891011) \n")))))))
510
511 (ert-deftest company-create-lines-shows-numbers ()
512 (let ((company-show-numbers t)
513 (company-candidates '("x" "y" "z"))
514 (company-candidates-length 3)
515 (company-backend 'ignore))
516 (should (equal '(" x 1 " " y 2 " " z 3 ")
517 (company--create-lines 0 999)))))
518
519 (ert-deftest company-create-lines-truncates-annotations ()
520 (let* ((ww (company--window-width))
521 (data `(("1" . "(123)")
522 ("2" . nil)
523 ("3" . ,(concat "(" (make-string (- ww 2) ?4) ")"))
524 (,(make-string ww ?4) . "<4>")))
525 (company-candidates (mapcar #'car data))
526 (company-candidates-length 4)
527 (company-tooltip-margin 1)
528 (company-backend (lambda (cmd &optional arg)
529 (when (eq cmd 'annotation)
530 (cdr (assoc arg data)))))
531 company-tooltip-align-annotations)
532 (should (equal (list (format " 1(123)%s " (company-space-string (- ww 8)))
533 (format " 2%s " (company-space-string (- ww 3)))
534 (format " 3(444%s " (make-string (- ww 7) ?4))
535 (format " %s " (make-string (- ww 2) ?4)))
536 (company--create-lines 0 999)))
537 (let ((company-tooltip-align-annotations t))
538 (should (equal (list (format " 1%s(123) " (company-space-string (- ww 8)))
539 (format " 2%s " (company-space-string (- ww 3)))
540 (format " 3 (444%s " (make-string (- ww 8) ?4))
541 (format " %s " (make-string (- ww 2) ?4)))
542 (company--create-lines 0 999))))))
543
544 (ert-deftest company-create-lines-truncates-common-part ()
545 (let* ((ww (company--window-width))
546 (company-candidates-length 2)
547 (company-tooltip-margin 1)
548 (company-backend #'ignore))
549 (let* ((company-common (make-string (- ww 3) ?1))
550 (company-candidates `(,(concat company-common "2")
551 ,(concat company-common "3"))))
552 (should (equal (list (format " %s2 " (make-string (- ww 3) ?1))
553 (format " %s3 " (make-string (- ww 3) ?1)))
554 (company--create-lines 0 999))))
555 (let* ((company-common (make-string (- ww 2) ?1))
556 (company-candidates `(,(concat company-common "2")
557 ,(concat company-common "3"))))
558 (should (equal (list (format " %s " company-common)
559 (format " %s " company-common))
560 (company--create-lines 0 999))))
561 (let* ((company-common (make-string ww ?1))
562 (company-candidates `(,(concat company-common "2")
563 ,(concat company-common "3")))
564 (res (company--create-lines 0 999)))
565 (should (equal (list (format " %s " (make-string (- ww 2) ?1))
566 (format " %s " (make-string (- ww 2) ?1)))
567 res))
568 (should (eq 'company-tooltip-common-selection
569 (get-text-property (- ww 2) 'face
570 (car res))))
571 (should (eq 'company-tooltip-selection
572 (get-text-property (1- ww) 'face
573 (car res))))
574 )))
575
576 (ert-deftest company-create-lines-clears-out-non-printables ()
577 :tags '(interactive)
578 (let (company-show-numbers
579 (company-candidates (list
580 (decode-coding-string "avalis\351e" 'utf-8)
581 "avatar"))
582 (company-candidates-length 2)
583 (company-backend 'ignore))
584 (should (equal '(" avalis‗e "
585 " avatar ")
586 (company--create-lines 0 999)))))
587
588 (ert-deftest company-create-lines-handles-multiple-width ()
589 :tags '(interactive)
590 (let (company-show-numbers
591 (company-candidates '("蛙蛙蛙蛙" "蛙abc"))
592 (company-candidates-length 2)
593 (company-backend 'ignore))
594 (should (equal '(" 蛙蛙蛙蛙 "
595 " 蛙abc ")
596 (company--create-lines 0 999)))))
597
598 (ert-deftest company-create-lines-handles-multiple-width-in-annotation ()
599 (let* (company-show-numbers
600 (alist '(("a" . " ︸") ("b" . " ︸︸")))
601 (company-candidates (mapcar #'car alist))
602 (company-candidates-length 2)
603 (company-backend (lambda (c &optional a)
604 (when (eq c 'annotation)
605 (assoc-default a alist)))))
606 (should (equal '(" a ︸ "
607 " b ︸︸ ")
608 (company--create-lines 0 999)))))
609
610 (ert-deftest company-column-with-composition ()
611 :tags '(interactive)
612 (with-temp-buffer
613 (save-window-excursion
614 (set-window-buffer nil (current-buffer))
615 (insert "lambda ()")
616 (compose-region 1 (1+ (length "lambda")) "\\")
617 (should (= (company--column) 4)))))
618
619 (ert-deftest company-column-with-line-prefix ()
620 :tags '(interactive)
621 (with-temp-buffer
622 (save-window-excursion
623 (set-window-buffer nil (current-buffer))
624 (insert "foo")
625 (put-text-property (point-min) (point) 'line-prefix " ")
626 (should (= (company--column) 5)))))
627
628 (ert-deftest company-column-with-line-prefix-on-empty-line ()
629 :tags '(interactive)
630 (with-temp-buffer
631 (save-window-excursion
632 (set-window-buffer nil (current-buffer))
633 (insert "\n")
634 (forward-char -1)
635 (put-text-property (point-min) (point-max) 'line-prefix " ")
636 (should (= (company--column) 2)))))
637
638 (ert-deftest company-column-with-tabs ()
639 :tags '(interactive)
640 (with-temp-buffer
641 (save-window-excursion
642 (set-window-buffer nil (current-buffer))
643 (insert "|\t|\t|\t(")
644 (let ((tab-width 8))
645 (should (= (company--column) 25))))))
646
647 (ert-deftest company-row-with-header-line-format ()
648 :tags '(interactive)
649 (with-temp-buffer
650 (save-window-excursion
651 (set-window-buffer nil (current-buffer))
652 (should (= (company--row) 0))
653 (setq header-line-format "aaaaaaa")
654 (should (= (company--row) 0)))))
655
656 (ert-deftest company-plainify ()
657 (let ((tab-width 8))
658 (should (equal-including-properties
659 (company-plainify "\tabc\td\t")
660 (concat " "
661 "abc "
662 "d "))))
663 (should (equal-including-properties
664 (company-plainify (propertize "foobar" 'line-prefix "-*-"))
665 "-*-foobar")))
666
667 (ert-deftest company-buffer-lines-with-lines-folded ()
668 :tags '(interactive)
669 (with-temp-buffer
670 (insert (propertize "aaa\nbbb\nccc\nddd\n" 'display "aaa+\n"))
671 (insert "eee\nfff\nggg")
672 (should (equal (company-buffer-lines (point-min) (point-max))
673 '("aaa" "eee" "fff" "ggg")))))
674
675 (ert-deftest company-buffer-lines-with-multiline-display ()
676 :tags '(interactive)
677 (with-temp-buffer
678 (insert (propertize "a" 'display "bbb\nccc\ndddd\n"))
679 (insert "eee\nfff\nggg")
680 (should (equal (company-buffer-lines (point-min) (point-max))
681 '("" "" "" "eee" "fff" "ggg")))))
682
683 (ert-deftest company-modify-line ()
684 (let ((str "-*-foobar"))
685 (should (equal-including-properties
686 (company-modify-line str "zz" 4)
687 "-*-fzzbar"))
688 (should (equal-including-properties
689 (company-modify-line str "xx" 0)
690 "xx-foobar"))
691 (should (equal-including-properties
692 (company-modify-line str "zz" 10)
693 "-*-foobar zz"))))
694
695 (ert-deftest company-scrollbar-bounds ()
696 (should (equal nil (company--scrollbar-bounds 0 3 3)))
697 (should (equal nil (company--scrollbar-bounds 0 4 3)))
698 (should (equal '(0 . 0) (company--scrollbar-bounds 0 1 2)))
699 (should (equal '(1 . 1) (company--scrollbar-bounds 2 2 4)))
700 (should (equal '(2 . 3) (company--scrollbar-bounds 7 4 12)))
701 (should (equal '(1 . 2) (company--scrollbar-bounds 3 4 12)))
702 (should (equal '(1 . 3) (company--scrollbar-bounds 4 5 11))))
703
704 ;;; Async
705
706 (defun company-async-backend (command &optional arg)
707 (pcase command
708 (`prefix "foo")
709 (`candidates
710 (cons :async
711 (lambda (cb)
712 (run-with-timer 0.05 nil
713 #'funcall cb '("abc" "abd")))))))
714
715 (ert-deftest company-call-backend-forces-sync ()
716 (let ((company-backend 'company-async-backend)
717 (company-async-timeout 0.1))
718 (should (equal '("abc" "abd") (company-call-backend 'candidates)))))
719
720 (ert-deftest company-call-backend-errors-on-timeout ()
721 (with-temp-buffer
722 (let* ((company-backend (lambda (command &optional _arg)
723 (pcase command
724 (`candidates (cons :async 'ignore)))))
725 (company-async-timeout 0.1)
726 (err (should-error (company-call-backend 'candidates "foo"))))
727 (should (string-match-p "async timeout" (cadr err))))))
728
729 (ert-deftest company-call-backend-raw-passes-return-value-verbatim ()
730 (let ((company-backend 'company-async-backend))
731 (should (equal "foo" (company-call-backend-raw 'prefix)))
732 (should (equal :async (car (company-call-backend-raw 'candidates "foo"))))
733 (should (equal 'closure (cadr (company-call-backend-raw 'candidates "foo"))))))
734
735 (ert-deftest company-manual-begin-forces-async-candidates-to-sync ()
736 (with-temp-buffer
737 (company-mode)
738 (let (company-frontends
739 company-transformers
740 (company-backends (list 'company-async-backend)))
741 (company-manual-begin)
742 (should (equal "foo" company-prefix))
743 (should (equal '("abc" "abd") company-candidates)))))
744
745 (ert-deftest company-idle-begin-allows-async-candidates ()
746 (with-temp-buffer
747 (company-mode)
748 (let (company-frontends
749 company-transformers
750 (company-backends (list 'company-async-backend)))
751 (company-idle-begin (current-buffer) (selected-window)
752 (buffer-chars-modified-tick) (point))
753 (should (null company-candidates))
754 (sleep-for 0.1)
755 (should (equal "foo" company-prefix))
756 (should (equal '("abc" "abd") company-candidates)))))
757
758 (ert-deftest company-idle-begin-cancels-async-candidates-if-buffer-changed ()
759 (with-temp-buffer
760 (company-mode)
761 (let (company-frontends
762 (company-backends (list 'company-async-backend)))
763 (company-idle-begin (current-buffer) (selected-window)
764 (buffer-chars-modified-tick) (point))
765 (should (null company-candidates))
766 (insert "a")
767 (sleep-for 0.1)
768 (should (null company-candidates)))))
769
770 (ert-deftest company-idle-begin-async-allows-immediate-callbacks ()
771 (with-temp-buffer
772 (company-mode)
773 (let (company-frontends
774 (company-backends
775 (list (lambda (command &optional arg)
776 (pcase command
777 (`prefix (buffer-substring (point-min) (point)))
778 (`candidates
779 (let ((c (all-completions arg '("abc" "def"))))
780 (cons :async
781 (lambda (cb) (funcall cb c)))))
782 (`no-cache t)))))
783 (company-minimum-prefix-length 0))
784 (company-idle-begin (current-buffer) (selected-window)
785 (buffer-chars-modified-tick) (point))
786 (should (equal '("abc" "def") company-candidates))
787 (let ((last-command-event ?a))
788 (company-call 'self-insert-command 1))
789 (should (equal '("abc") company-candidates)))))
790
791 (ert-deftest company-multi-backend-forces-prefix-to-sync ()
792 (with-temp-buffer
793 (let ((company-backend (list 'ignore
794 (lambda (command)
795 (should (eq command 'prefix))
796 (cons :async
797 (lambda (cb)
798 (run-with-timer
799 0.01 nil
800 (lambda () (funcall cb nil))))))
801 (lambda (command)
802 (should (eq command 'prefix))
803 "foo"))))
804 (should (equal "foo" (company-call-backend-raw 'prefix))))
805 (let ((company-backend (list (lambda (_command)
806 (cons :async
807 (lambda (cb)
808 (run-with-timer
809 0.01 nil
810 (lambda () (funcall cb "bar"))))))
811 (lambda (_command)
812 "foo"))))
813 (should (equal "bar" (company-call-backend-raw 'prefix))))))
814
815 (ert-deftest company-multi-backend-merges-deferred-candidates ()
816 (with-temp-buffer
817 (let* ((immediate (lambda (command &optional arg)
818 (pcase command
819 (`prefix "foo")
820 (`candidates
821 (cons :async
822 (lambda (cb) (funcall cb '("f"))))))))
823 (company-backend (list 'ignore
824 (lambda (command &optional arg)
825 (pcase command
826 (`prefix "foo")
827 (`candidates
828 (should (equal arg "foo"))
829 (cons :async
830 (lambda (cb)
831 (run-with-timer
832 0.01 nil
833 (lambda () (funcall cb '("a" "b")))))))))
834 (lambda (command &optional arg)
835 (pcase command
836 (`prefix "foo")
837 (`candidates '("c" "d" "e"))))
838 immediate)))
839 (should (equal :async (car (company-call-backend-raw 'candidates "foo"))))
840 (should (equal '("a" "b" "c" "d" "e" "f")
841 (company-call-backend 'candidates "foo")))
842 (let ((company-backend (list immediate)))
843 (should (equal '("f") (company-call-backend 'candidates "foo")))))))
844
845 ;;; Transformers
846
847 (ert-deftest company-occurrence-prefer-closest-above ()
848 (with-temp-buffer
849 (save-window-excursion
850 (set-window-buffer nil (current-buffer))
851 (insert "foo0
852 foo1
853 ")
854 (save-excursion
855 (insert "
856 foo3
857 foo2"))
858 (let ((company-backend 'company-dabbrev)
859 (company-occurrence-weight-function
860 'company-occurrence-prefer-closest-above))
861 (should (equal '("foo1" "foo0" "foo3" "foo2" "foo4")
862 (company-sort-by-occurrence
863 '("foo0" "foo1" "foo2" "foo3" "foo4"))))))))
864
865 (ert-deftest company-occurrence-prefer-any-closest ()
866 (with-temp-buffer
867 (save-window-excursion
868 (set-window-buffer nil (current-buffer))
869 (insert "foo0
870 foo1
871 ")
872 (save-excursion
873 (insert "
874 foo3
875 foo2"))
876 (let ((company-backend 'company-dabbrev)
877 (company-occurrence-weight-function
878 'company-occurrence-prefer-any-closest))
879 (should (equal '("foo1" "foo3" "foo0" "foo2" "foo4")
880 (company-sort-by-occurrence
881 '("foo0" "foo1" "foo2" "foo3" "foo4"))))))))
882
883 ;;; Template
884
885 (ert-deftest company-template-removed-after-the-last-jump ()
886 (with-temp-buffer
887 (insert "{ }")
888 (goto-char 2)
889 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
890 (save-excursion
891 (dotimes (i 2)
892 (insert " ")
893 (company-template-add-field tpl (point) "foo")))
894 (company-call 'template-forward-field)
895 (should (= 3 (point)))
896 (company-call 'template-forward-field)
897 (should (= 7 (point)))
898 (company-call 'template-forward-field)
899 (should (= 11 (point)))
900 (should (zerop (length (overlay-get tpl 'company-template-fields))))
901 (should (null (overlay-buffer tpl))))))
902
903 (ert-deftest company-template-removed-after-input-and-jump ()
904 (with-temp-buffer
905 (insert "{ }")
906 (goto-char 2)
907 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
908 (save-excursion
909 (insert " ")
910 (company-template-add-field tpl (point) "bar"))
911 (company-call 'template-move-to-first tpl)
912 (should (= 3 (point)))
913 (dolist (c (string-to-list "tee"))
914 (let ((last-command-event c))
915 (company-call 'self-insert-command 1)))
916 (should (string= "{ tee }" (buffer-string)))
917 (should (overlay-buffer tpl))
918 (company-call 'template-forward-field)
919 (should (= 7 (point)))
920 (should (null (overlay-buffer tpl))))))
921
922 (defun company-call (name &rest args)
923 (let* ((maybe (intern (format "company-%s" name)))
924 (command (if (fboundp maybe) maybe name)))
925 (let ((this-command command))
926 (run-hooks 'pre-command-hook))
927 (apply command args)
928 (let ((this-command command))
929 (run-hooks 'post-command-hook))))
930
931 (ert-deftest company-template-c-like-templatify ()
932 (with-temp-buffer
933 (let ((text "foo(int a, short b)"))
934 (insert text)
935 (company-template-c-like-templatify text)
936 (should (equal "foo(arg0, arg1)" (buffer-string)))
937 (should (looking-at "arg0"))
938 (should (equal "int a"
939 (overlay-get (company-template-field-at) 'display))))))
940
941 (ert-deftest company-template-c-like-templatify-trims-after-closing-paren ()
942 (with-temp-buffer
943 (let ((text "foo(int a, short b)!@ #1334 a"))
944 (insert text)
945 (company-template-c-like-templatify text)
946 (should (equal "foo(arg0, arg1)" (buffer-string)))
947 (should (looking-at "arg0")))))
948
949 (ert-deftest company-template-c-like-templatify-generics ()
950 (with-temp-buffer
951 (let ((text "foo<TKey, TValue>(int i, Dict<TKey, TValue>, long l)"))
952 (insert text)
953 (company-template-c-like-templatify text)
954 (should (equal "foo<arg0, arg1>(arg2, arg3, arg4)" (buffer-string)))
955 (should (looking-at "arg0"))
956 (should (equal "TKey" (overlay-get (company-template-field-at) 'display)))
957 (search-forward "arg3")
958 (forward-char -1)
959 (should (equal "Dict<TKey, TValue>"
960 (overlay-get (company-template-field-at) 'display))))))
961
962 ;;; Clang
963
964 (ert-deftest company-clang-objc-templatify ()
965 (with-temp-buffer
966 (let ((text "createBookWithTitle:andAuthor:"))
967 (insert text)
968 (company-clang-objc-templatify text)
969 (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
970 (should (looking-at "arg0"))
971 (should (null (overlay-get (company-template-field-at) 'display))))))