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