1 ;;; company-tests.el --- company-mode tests -*- lexical-binding: t -*-
3 ;; Copyright (C) 2011, 2013-2014 Free Software Foundation, Inc.
5 ;; Author: Nikolaj Schumacher
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
30 (require 'company-keywords)
31 (require 'company-clang)
33 (defun company--column (&optional pos)
34 (car (company--col-row pos)))
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))
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))))))
60 (ert-deftest company--manual-prefix-set-and-unset ()
64 (let (company-frontends
66 (list (lambda (command &optional arg)
68 (prefix (buffer-substring (point-min) (point)))
69 (candidates '("abc" "abd")))))))
70 (company-manual-begin)
71 (should (equal "ab" company--manual-prefix))
73 (should (null company--manual-prefix)))))
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)))))))
93 (ert-deftest company-multi-backend-with-lambdas ()
94 (let ((company-backend
95 (list (lambda (command &optional arg &rest ignore)
98 (candidates '("a" "b"))))
99 (lambda (command &optional arg &rest ignore)
102 (candidates '("c" "d")))))))
103 (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
105 (ert-deftest company-multi-backend-filters-backends-by-prefix ()
106 (let ((company-backend
107 (list (lambda (command &optional arg &rest ignore)
109 (prefix (cons "z" t))
110 (candidates '("a" "b"))))
111 (lambda (command &optional arg &rest ignore)
114 (candidates '("c" "d"))))
115 (lambda (command &optional arg &rest ignore)
118 (candidates '("e" "f")))))))
119 (should (equal (company-call-backend 'candidates "z") '("a" "b" "e" "f")))))
121 (ert-deftest company-multi-backend-remembers-candidate-backend ()
122 (let ((company-backend
123 (list (lambda (command &optional arg)
127 (candidates '("a" "c"))
128 (post-completion "13")))
129 (lambda (command &optional arg)
133 (candidates '("b" "d"))
134 (post-completion "42")))
135 (lambda (command &optional arg)
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)))))))
150 (ert-deftest company-multi-backend-handles-keyword-with ()
151 (let ((primo (lambda (command &optional arg)
154 (candidates '("abb" "abc" "abd")))))
155 (secundo (lambda (command &optional arg)
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"))))))
166 (ert-deftest company-begin-backend-failure-doesnt-break-company-backends ()
171 (company-begin-backend (lambda (command &rest ignore))))
172 (let (company-frontends
174 (list (lambda (command &optional arg)
177 (candidates '("a" "ab" "ac")))))))
179 (company-call 'complete))
180 (should (eq 3 company-candidates-length)))))
182 (ert-deftest company-require-match-explicit ()
186 (let (company-frontends
187 (company-require-match 'company-explicit-action-p)
189 (list (lambda (command &optional arg)
191 (prefix (buffer-substring (point-min) (point)))
192 (candidates '("abc" "abd")))))))
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))))))
200 (ert-deftest company-dont-require-match-when-idle ()
204 (let (company-frontends
205 (company-minimum-prefix-length 2)
206 (company-require-match 'company-explicit-action-p)
208 (list (lambda (command &optional arg)
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))))))
220 (ert-deftest company-dont-require-match-if-old-prefix-ended-and-was-a-match ()
224 (let (company-frontends
225 (company-require-match 'company-explicit-action-p)
227 (list (lambda (command &optional arg)
229 (prefix (company-grab-word))
230 (candidates '("abc" "ab" "abd"))
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))))))
243 (ert-deftest company-should-complete-whitelist ()
247 (let (company-frontends
248 company-begin-commands
250 (list (lambda (command &optional arg)
252 (prefix (buffer-substring (point-min) (point)))
253 (candidates '("abc" "abd")))))))
254 (let ((company-continue-commands nil))
257 (company-call 'backward-delete-char 1)
258 (should (null company-candidates-length)))
259 (let ((company-continue-commands '(backward-delete-char)))
262 (company-call 'backward-delete-char 1)
263 (should (eq 2 company-candidates-length))))))
265 (ert-deftest company-should-complete-blacklist ()
269 (let (company-frontends
270 company-begin-commands
272 (list (lambda (command &optional arg)
274 (prefix (buffer-substring (point-min) (point)))
275 (candidates '("abc" "abd")))))))
276 (let ((company-continue-commands '(not backward-delete-char)))
279 (company-call 'backward-delete-char 1)
280 (should (null company-candidates-length)))
281 (let ((company-continue-commands '(not backward-delete-char-untabify)))
284 (company-call 'backward-delete-char 1)
285 (should (eq 2 company-candidates-length))))))
287 (ert-deftest company-auto-complete-explicit ()
291 (let (company-frontends
292 (company-auto-complete 'company-explicit-action-p)
293 (company-auto-complete-chars '(? ))
295 (list (lambda (command &optional arg)
297 (prefix (buffer-substring (point-min) (point)))
298 (candidates '("abcd" "abef")))))))
301 (let ((last-command-event ? ))
302 (company-call 'self-insert-command 1))
303 (should (string= "abcd " (buffer-string))))))
305 (ert-deftest company-no-auto-complete-when-idle ()
309 (let (company-frontends
310 (company-auto-complete 'company-explicit-action-p)
311 (company-auto-complete-chars '(? ))
312 (company-minimum-prefix-length 2)
314 (list (lambda (command &optional arg)
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))))))
324 (ert-deftest company-clears-explicit-action-when-no-matches ()
327 (let (company-frontends
329 (company-call 'manual-begin) ;; fails
330 (should (null company-candidates))
331 (should (null (company-explicit-action-p))))))
333 (ert-deftest company-ignore-case-replaces-prefix ()
336 (let (company-frontends
337 company-end-of-buffer-workaround
339 (list (lambda (command &optional arg)
341 (prefix (buffer-substring (point-min) (point)))
342 (candidates '("abcd" "abef"))
347 (should (string= "ab" (buffer-string)))
349 (insert "A") ; hack, to keep it in one test
350 (company-complete-selection)
351 (should (string= "abcd" (buffer-string))))))
353 (ert-deftest company-ignore-case-with-keep-prefix ()
357 (let (company-frontends
359 (list (lambda (command &optional arg)
361 (prefix (buffer-substring (point-min) (point)))
362 (candidates '("abcd" "abef"))
363 (ignore-case 'keep-prefix))))))
366 (company-complete-selection)
367 (should (string= "ABcd" (buffer-string))))))
369 (ert-deftest company-non-prefix-completion ()
373 (let (company-frontends
374 company-end-of-buffer-workaround
376 (list (lambda (command &optional arg)
378 (prefix (buffer-substring (point-min) (point)))
379 (candidates '("tea-cup" "teal-color")))))))
382 (should (string= "tc" (buffer-string)))
383 (company-complete-selection)
384 (should (string= "tea-cup" (buffer-string))))))
386 (ert-deftest company-pseudo-tooltip-does-not-get-displaced ()
389 (save-window-excursion
390 (set-window-buffer nil (current-buffer))
391 (save-excursion (insert " ff"))
393 (let ((company-frontends '(company-pseudo-tooltip-frontend))
394 (company-begin-commands '(self-insert-command))
396 (list (lambda (c &optional arg)
397 (cl-case c (prefix "") (candidates '("a" "b" "c")))))))
399 (company-call 'complete))
400 (company-call 'open-line 1)
401 (should (eq 1 (overlay-start company-pseudo-tooltip-overlay)))))))
403 (ert-deftest company-pseudo-tooltip-show ()
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))
417 (should (eq (overlay-get ov 'company-width) 5))
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 "\n 123 \nc 45 c\nddd\n")))))))
424 (ert-deftest company-pseudo-tooltip-edit-updates-width ()
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)
436 (should (eq (overlay-get company-pseudo-tooltip-overlay 'company-width)
438 (company-pseudo-tooltip-edit 4)
439 (should (eq (overlay-get company-pseudo-tooltip-overlay 'company-width)
442 (ert-deftest company-preview-show-with-annotations ()
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 (str (overlay-get ov 'after-string)))
453 (should (string= str "123"))
454 (should (eq (get-text-property 0 'cursor str) t)))))))
456 (ert-deftest company-pseudo-tooltip-show-with-annotations ()
459 (save-window-excursion
460 (set-window-buffer nil (current-buffer))
462 (save-excursion (insert "\n"))
463 (let ((company-candidates-length 2)
464 (company-backend (lambda (action &optional arg &rest _ignore)
465 (when (eq action 'annotation)
466 (cdr (assoc arg '(("123" . "(4)")))))))
467 (company-candidates '("123" "45"))
468 company-tooltip-align-annotations)
469 (company-pseudo-tooltip-show-at-point (point) 0)
470 (let ((ov company-pseudo-tooltip-overlay))
472 (should (eq (overlay-get ov 'company-width) 8))
473 (should (string= (overlay-get ov 'company-display)
474 "\n 123(4) \n 45 \n")))))))
476 (ert-deftest company-pseudo-tooltip-show-with-annotations-right-aligned ()
479 (save-window-excursion
480 (set-window-buffer nil (current-buffer))
482 (save-excursion (insert "\n"))
483 (let ((company-candidates-length 3)
484 (company-backend (lambda (action &optional arg &rest _ignore)
485 (when (eq action 'annotation)
486 (cdr (assoc arg '(("123" . "(4)")
487 ("67" . "(891011)")))))))
488 (company-candidates '("123" "45" "67"))
489 (company-tooltip-align-annotations t))
490 (company-pseudo-tooltip-show-at-point (point) 0)
491 (let ((ov company-pseudo-tooltip-overlay))
493 (should (eq (overlay-get ov 'company-width) 13))
494 (should (string= (overlay-get ov 'company-display)
495 "\n 123 (4) \n 45 \n 67 (891011) \n")))))))
497 (ert-deftest company-create-lines-shows-numbers ()
498 (let ((company-show-numbers t)
499 (company-candidates '("x" "y" "z"))
500 (company-candidates-length 3)
501 (company-backend 'ignore))
502 (should (equal '(" x 1 " " y 2 " " z 3 ")
503 (company--create-lines 0 999)))))
505 (ert-deftest company-create-lines-truncates-annotations ()
506 (let* ((ww (company--window-width))
507 (data `(("1" . "(123)")
509 ("3" . ,(concat "(" (make-string (- ww 2) ?4) ")"))
510 (,(make-string ww ?4) . "<4>")))
511 (company-candidates (mapcar #'car data))
512 (company-candidates-length 4)
513 (company-tooltip-margin 1)
514 (company-backend (lambda (cmd &optional arg)
515 (when (eq cmd 'annotation)
516 (cdr (assoc arg data)))))
517 company-tooltip-align-annotations)
518 (should (equal (list (format " 1(123)%s " (company-space-string (- ww 8)))
519 (format " 2%s " (company-space-string (- ww 3)))
520 (format " 3(444%s " (make-string (- ww 7) ?4))
521 (format " %s " (make-string (- ww 2) ?4)))
522 (company--create-lines 0 999)))
523 (let ((company-tooltip-align-annotations t))
524 (should (equal (list (format " 1%s(123) " (company-space-string (- ww 8)))
525 (format " 2%s " (company-space-string (- ww 3)))
526 (format " 3 (444%s " (make-string (- ww 8) ?4))
527 (format " %s " (make-string (- ww 2) ?4)))
528 (company--create-lines 0 999))))))
530 (ert-deftest company-create-lines-truncates-common-part ()
531 (let* ((ww (company--window-width))
532 (company-candidates-length 2)
533 (company-tooltip-margin 1)
534 (company-backend #'ignore))
535 (let* ((company-common (make-string (- ww 3) ?1))
536 (company-candidates `(,(concat company-common "2")
537 ,(concat company-common "3"))))
538 (should (equal (list (format " %s2 " (make-string (- ww 3) ?1))
539 (format " %s3 " (make-string (- ww 3) ?1)))
540 (company--create-lines 0 999))))
541 (let* ((company-common (make-string (- ww 2) ?1))
542 (company-candidates `(,(concat company-common "2")
543 ,(concat company-common "3"))))
544 (should (equal (list (format " %s " company-common)
545 (format " %s " company-common))
546 (company--create-lines 0 999))))
547 (let* ((company-common (make-string ww ?1))
548 (company-candidates `(,(concat company-common "2")
549 ,(concat company-common "3")))
550 (res (company--create-lines 0 999)))
551 (should (equal (list (format " %s " (make-string (- ww 2) ?1))
552 (format " %s " (make-string (- ww 2) ?1)))
554 (should (eq 'company-tooltip-common-selection
555 (get-text-property (- ww 2) 'face
557 (should (eq 'company-tooltip-selection
558 (get-text-property (1- ww) 'face
562 (ert-deftest company-create-lines-clears-out-non-printables ()
564 (let (company-show-numbers
565 (company-candidates (list
566 (decode-coding-string "avalis\351e" 'utf-8)
568 (company-candidates-length 2)
569 (company-backend 'ignore))
570 (should (equal '(" avalis‗e "
572 (company--create-lines 0 999)))))
574 (ert-deftest company-create-lines-handles-multiple-width ()
576 (let (company-show-numbers
577 (company-candidates '("蛙蛙蛙蛙" "蛙abc"))
578 (company-candidates-length 2)
579 (company-backend 'ignore))
580 (should (equal '(" 蛙蛙蛙蛙 "
582 (company--create-lines 0 999)))))
584 (ert-deftest company-create-lines-handles-multiple-width-in-annotation ()
585 (let* (company-show-numbers
586 (alist '(("a" . " ︸") ("b" . " ︸︸")))
587 (company-candidates (mapcar #'car alist))
588 (company-candidates-length 2)
589 (company-backend (lambda (c &optional a)
590 (when (eq c 'annotation)
591 (assoc-default a alist)))))
592 (should (equal '(" a ︸ "
594 (company--create-lines 0 999)))))
596 (ert-deftest company-column-with-composition ()
599 (save-window-excursion
600 (set-window-buffer nil (current-buffer))
602 (compose-region 1 (1+ (length "lambda")) "\\")
603 (should (= (company--column) 4)))))
605 (ert-deftest company-column-with-line-prefix ()
608 (save-window-excursion
609 (set-window-buffer nil (current-buffer))
611 (put-text-property (point-min) (point) 'line-prefix " ")
612 (should (= (company--column) 5)))))
614 (ert-deftest company-column-with-line-prefix-on-empty-line ()
617 (save-window-excursion
618 (set-window-buffer nil (current-buffer))
621 (put-text-property (point-min) (point-max) 'line-prefix " ")
622 (should (= (company--column) 2)))))
624 (ert-deftest company-column-with-tabs ()
627 (save-window-excursion
628 (set-window-buffer nil (current-buffer))
629 (insert "|\t|\t|\t(")
631 (should (= (company--column) 25))))))
633 (ert-deftest company-row-with-header-line-format ()
636 (save-window-excursion
637 (set-window-buffer nil (current-buffer))
638 (should (= (company--row) 0))
639 (setq header-line-format "aaaaaaa")
640 (should (= (company--row) 0)))))
642 (ert-deftest company-plainify ()
644 (should (equal-including-properties
645 (company-plainify "\tabc\td\t")
649 (should (equal-including-properties
650 (company-plainify (propertize "foobar" 'line-prefix "-*-"))
653 (ert-deftest company-buffer-lines-with-lines-folded ()
656 (insert (propertize "aaa\nbbb\nccc\nddd\n" 'display "aaa+\n"))
657 (insert "eee\nfff\nggg")
658 (should (equal (company-buffer-lines (point-min) (point-max))
659 '("aaa" "eee" "fff" "ggg")))))
661 (ert-deftest company-buffer-lines-with-multiline-display ()
664 (insert (propertize "a" 'display "bbb\nccc\ndddd\n"))
665 (insert "eee\nfff\nggg")
666 (should (equal (company-buffer-lines (point-min) (point-max))
667 '("" "" "" "eee" "fff" "ggg")))))
669 (ert-deftest company-modify-line ()
670 (let ((str "-*-foobar"))
671 (should (equal-including-properties
672 (company-modify-line str "zz" 4)
674 (should (equal-including-properties
675 (company-modify-line str "xx" 0)
677 (should (equal-including-properties
678 (company-modify-line str "zz" 10)
681 (ert-deftest company-scrollbar-bounds ()
682 (should (equal nil (company--scrollbar-bounds 0 3 3)))
683 (should (equal nil (company--scrollbar-bounds 0 4 3)))
684 (should (equal '(0 . 0) (company--scrollbar-bounds 0 1 2)))
685 (should (equal '(1 . 1) (company--scrollbar-bounds 2 2 4)))
686 (should (equal '(2 . 3) (company--scrollbar-bounds 7 4 12)))
687 (should (equal '(1 . 2) (company--scrollbar-bounds 3 4 12)))
688 (should (equal '(1 . 3) (company--scrollbar-bounds 4 5 11))))
692 (defun company-async-backend (command &optional arg)
698 (run-with-timer 0.05 nil
699 #'funcall cb '("abc" "abd")))))))
701 (ert-deftest company-call-backend-forces-sync ()
702 (let ((company-backend 'company-async-backend)
703 (company-async-timeout 0.1))
704 (should (equal '("abc" "abd") (company-call-backend 'candidates)))))
706 (ert-deftest company-call-backend-errors-on-timeout ()
708 (let* ((company-backend (lambda (command &optional _arg)
710 (`candidates (cons :async 'ignore)))))
711 (company-async-timeout 0.1)
712 (err (should-error (company-call-backend 'candidates "foo"))))
713 (should (string-match-p "async timeout" (cadr err))))))
715 (ert-deftest company-call-backend-raw-passes-return-value-verbatim ()
716 (let ((company-backend 'company-async-backend))
717 (should (equal "foo" (company-call-backend-raw 'prefix)))
718 (should (equal :async (car (company-call-backend-raw 'candidates "foo"))))
719 (should (equal 'closure (cadr (company-call-backend-raw 'candidates "foo"))))))
721 (ert-deftest company-manual-begin-forces-async-candidates-to-sync ()
724 (let (company-frontends
726 (company-backends (list 'company-async-backend)))
727 (company-manual-begin)
728 (should (equal "foo" company-prefix))
729 (should (equal '("abc" "abd") company-candidates)))))
731 (ert-deftest company-idle-begin-allows-async-candidates ()
734 (let (company-frontends
736 (company-backends (list 'company-async-backend)))
737 (company-idle-begin (current-buffer) (selected-window)
738 (buffer-chars-modified-tick) (point))
739 (should (null company-candidates))
741 (should (equal "foo" company-prefix))
742 (should (equal '("abc" "abd") company-candidates)))))
744 (ert-deftest company-idle-begin-cancels-async-candidates-if-buffer-changed ()
747 (let (company-frontends
748 (company-backends (list 'company-async-backend)))
749 (company-idle-begin (current-buffer) (selected-window)
750 (buffer-chars-modified-tick) (point))
751 (should (null company-candidates))
754 (should (null company-candidates)))))
756 (ert-deftest company-idle-begin-async-allows-immediate-callbacks ()
759 (let (company-frontends
761 (list (lambda (command &optional arg)
763 (`prefix (buffer-substring (point-min) (point)))
765 (let ((c (all-completions arg '("abc" "def"))))
767 (lambda (cb) (funcall cb c)))))
769 (company-minimum-prefix-length 0))
770 (company-idle-begin (current-buffer) (selected-window)
771 (buffer-chars-modified-tick) (point))
772 (should (equal '("abc" "def") company-candidates))
773 (let ((last-command-event ?a))
774 (company-call 'self-insert-command 1))
775 (should (equal '("abc") company-candidates)))))
777 (ert-deftest company-multi-backend-forces-prefix-to-sync ()
779 (let ((company-backend (list 'ignore
781 (should (eq command 'prefix))
786 (lambda () (funcall cb nil))))))
788 (should (eq command 'prefix))
790 (should (equal "foo" (company-call-backend-raw 'prefix))))
791 (let ((company-backend (list (lambda (_command)
796 (lambda () (funcall cb "bar"))))))
799 (should (equal "bar" (company-call-backend-raw 'prefix))))))
801 (ert-deftest company-multi-backend-merges-deferred-candidates ()
803 (let* ((immediate (lambda (command &optional arg)
808 (lambda (cb) (funcall cb '("f"))))))))
809 (company-backend (list 'ignore
810 (lambda (command &optional arg)
814 (should (equal arg "foo"))
819 (lambda () (funcall cb '("a" "b")))))))))
820 (lambda (command &optional arg)
823 (`candidates '("c" "d" "e"))))
825 (should (equal :async (car (company-call-backend-raw 'candidates "foo"))))
826 (should (equal '("a" "b" "c" "d" "e" "f")
827 (company-call-backend 'candidates "foo")))
828 (let ((company-backend (list immediate)))
829 (should (equal '("f") (company-call-backend 'candidates "foo")))))))
833 (ert-deftest company-occurrence-prefer-closest-above ()
835 (save-window-excursion
836 (set-window-buffer nil (current-buffer))
844 (let ((company-backend 'company-dabbrev)
845 (company-occurrence-weight-function
846 'company-occurrence-prefer-closest-above))
847 (should (equal '("foo1" "foo0" "foo3" "foo2" "foo4")
848 (company-sort-by-occurrence
849 '("foo0" "foo1" "foo2" "foo3" "foo4"))))))))
851 (ert-deftest company-occurrence-prefer-any-closest ()
853 (save-window-excursion
854 (set-window-buffer nil (current-buffer))
862 (let ((company-backend 'company-dabbrev)
863 (company-occurrence-weight-function
864 'company-occurrence-prefer-any-closest))
865 (should (equal '("foo1" "foo3" "foo0" "foo2" "foo4")
866 (company-sort-by-occurrence
867 '("foo0" "foo1" "foo2" "foo3" "foo4"))))))))
871 (ert-deftest company-template-removed-after-the-last-jump ()
875 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
879 (company-template-add-field tpl (point) "foo")))
880 (company-call 'template-forward-field)
881 (should (= 3 (point)))
882 (company-call 'template-forward-field)
883 (should (= 7 (point)))
884 (company-call 'template-forward-field)
885 (should (= 11 (point)))
886 (should (zerop (length (overlay-get tpl 'company-template-fields))))
887 (should (null (overlay-buffer tpl))))))
889 (ert-deftest company-template-removed-after-input-and-jump ()
893 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
896 (company-template-add-field tpl (point) "bar"))
897 (company-call 'template-move-to-first tpl)
898 (should (= 3 (point)))
899 (dolist (c (string-to-list "tee"))
900 (let ((last-command-event c))
901 (company-call 'self-insert-command 1)))
902 (should (string= "{ tee }" (buffer-string)))
903 (should (overlay-buffer tpl))
904 (company-call 'template-forward-field)
905 (should (= 7 (point)))
906 (should (null (overlay-buffer tpl))))))
908 (defun company-call (name &rest args)
909 (let* ((maybe (intern (format "company-%s" name)))
910 (command (if (fboundp maybe) maybe name)))
911 (let ((this-command command))
912 (run-hooks 'pre-command-hook))
914 (let ((this-command command))
915 (run-hooks 'post-command-hook))))
917 (ert-deftest company-template-c-like-templatify ()
919 (let ((text "foo(int a, short b)"))
921 (company-template-c-like-templatify text)
922 (should (equal "foo(arg0, arg1)" (buffer-string)))
923 (should (looking-at "arg0"))
924 (should (equal "int a"
925 (overlay-get (company-template-field-at) 'display))))))
927 (ert-deftest company-template-c-like-templatify-trims-after-closing-paren ()
929 (let ((text "foo(int a, short b)!@ #1334 a"))
931 (company-template-c-like-templatify text)
932 (should (equal "foo(arg0, arg1)" (buffer-string)))
933 (should (looking-at "arg0")))))
935 (ert-deftest company-template-c-like-templatify-generics ()
937 (let ((text "foo<TKey, TValue>(int i, Dict<TKey, TValue>, long l)"))
939 (company-template-c-like-templatify text)
940 (should (equal "foo<arg0, arg1>(arg2, arg3, arg4)" (buffer-string)))
941 (should (looking-at "arg0"))
942 (should (equal "TKey" (overlay-get (company-template-field-at) 'display)))
943 (search-forward "arg3")
945 (should (equal "Dict<TKey, TValue>"
946 (overlay-get (company-template-field-at) 'display))))))
950 (ert-deftest company-clang-objc-templatify ()
952 (let ((text "createBookWithTitle:andAuthor:"))
954 (company-clang-objc-templatify text)
955 (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
956 (should (looking-at "arg0"))
957 (should (null (overlay-get (company-template-field-at) 'display))))))