]> rtime.felk.cvut.cz Git - sojka/company-mode.git/blob - company-tests.el
Implement async operation for grouped backends
[sojka/company-mode.git] / 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 (eval-when-compile (require 'cl))
29 (require 'ert)
30 (require 'company)
31 (require 'company-keywords)
32 (require 'company-elisp)
33 (require 'company-clang)
34
35 ;;; Core
36
37 (ert-deftest company-sorted-keywords ()
38   "Test that keywords in `company-keywords-alist' are in alphabetical order."
39   (dolist (pair company-keywords-alist)
40     (when (consp (cdr pair))
41       (let ((prev (cadr pair)))
42         (dolist (next (cddr pair))
43           (should (not (equal prev next)))
44           (should (string< prev next))
45           (setq prev next))))))
46
47 (ert-deftest company-good-prefix ()
48   (let ((company-minimum-prefix-length 5)
49         company-abort-manual-when-too-short
50         company--manual-action            ;idle begin
51         (company-selection-changed t))    ;has no effect
52     (should (eq t (company--good-prefix-p "!@#$%")))
53     (should (eq nil (company--good-prefix-p "abcd")))
54     (should (eq nil (company--good-prefix-p 'stop)))
55     (should (eq t (company--good-prefix-p '("foo" . 5))))
56     (should (eq nil (company--good-prefix-p '("foo" . 4))))
57     (should (eq t (company--good-prefix-p '("foo" . t))))))
58
59 (ert-deftest company--manual-prefix-set-and-unset ()
60   (with-temp-buffer
61     (insert "ab")
62     (company-mode)
63     (let (company-frontends
64           (company-backends
65            (list (lambda (command &optional arg)
66                    (case command
67                      (prefix (buffer-substring (point-min) (point)))
68                      (candidates '("abc" "abd")))))))
69       (company-manual-begin)
70       (should (equal "ab" company--manual-prefix))
71       (company-abort)
72       (should (null company--manual-prefix)))))
73
74 (ert-deftest company-abort-manual-when-too-short ()
75   (let ((company-minimum-prefix-length 5)
76         (company-abort-manual-when-too-short t)
77         (company-selection-changed t))    ;has not effect
78     (let ((company--manual-action nil))   ;idle begin
79       (should (eq t (company--good-prefix-p "!@#$%")))
80       (should (eq t (company--good-prefix-p '("foo" . 5))))
81       (should (eq t (company--good-prefix-p '("foo" . t)))))
82     (let ((company--manual-action t)
83           (company--manual-prefix "abc")) ;manual begin from this prefix
84       (should (eq t (company--good-prefix-p "!@#$")))
85       (should (eq nil (company--good-prefix-p "ab")))
86       (should (eq nil (company--good-prefix-p 'stop)))
87       (should (eq t (company--good-prefix-p '("foo" . 4))))
88       (should (eq t (company--good-prefix-p "abcd")))
89       (should (eq t (company--good-prefix-p "abc")))
90       (should (eq t (company--good-prefix-p '("bar" . t)))))))
91
92 (ert-deftest company-multi-backend-with-lambdas ()
93   (let ((company-backend
94          (list (lambda (command &optional arg &rest ignore)
95                  (case command
96                    (prefix "z")
97                    (candidates '("a" "b"))))
98                (lambda (command &optional arg &rest ignore)
99                  (case command
100                    (prefix "z")
101                    (candidates '("c" "d")))))))
102     (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
103
104 (ert-deftest company-multi-backend-remembers-candidate-backend ()
105   (let ((company-backend
106          (list (lambda (command &optional arg &rest ignore)
107                  (case command
108                    (ignore-case nil)
109                    (annotation "1")
110                    (candidates '("a" "c"))
111                    (post-completion "13")))
112                (lambda (command &optional arg &rest ignore)
113                  (case command
114                    (ignore-case t)
115                    (annotation "2")
116                    (candidates '("b" "d"))
117                    (post-completion "42"))))))
118     (let ((candidates (company-calculate-candidates nil)))
119       (should (equal candidates '("a" "b" "c" "d")))
120       (should (equal t (company-call-backend 'ignore-case)))
121       (should (equal "1" (company-call-backend 'annotation (nth 0 candidates))))
122       (should (equal "2" (company-call-backend 'annotation (nth 1 candidates))))
123       (should (equal "13" (company-call-backend 'post-completion (nth 2 candidates))))
124       (should (equal "42" (company-call-backend 'post-completion (nth 3 candidates)))))))
125
126 (ert-deftest company-multi-backend-handles-keyword-with ()
127   (let ((primo (lambda (command &optional arg)
128                  (case command
129                    (prefix "a")
130                    (candidates '("abb" "abc" "abd")))))
131         (secundo (lambda (command &optional arg)
132                    (case command
133                      (prefix "a")
134                      (candidates '("acc" "acd"))))))
135     (let ((company-backend (list 'ignore 'ignore :with secundo)))
136       (should (null (company-call-backend 'prefix))))
137     (let ((company-backend (list 'ignore primo :with secundo)))
138       (should (equal "a" (company-call-backend 'prefix)))
139       (should (equal '("abb" "abc" "abd" "acc" "acd")
140                      (company-call-backend 'candidates "a"))))))
141
142 (ert-deftest company-begin-backend-failure-doesnt-break-company-backends ()
143   (with-temp-buffer
144     (insert "a")
145     (company-mode)
146     (should-error
147      (company-begin-backend (lambda (command &rest ignore))))
148     (let (company-frontends
149           (company-backends
150            (list (lambda (command &optional arg)
151                    (case command
152                      (prefix "a")
153                      (candidates '("a" "ab" "ac")))))))
154       (let (this-command)
155         (company-call 'complete))
156       (should (eq 3 company-candidates-length)))))
157
158 (ert-deftest company-require-match-explicit ()
159   (with-temp-buffer
160     (insert "ab")
161     (company-mode)
162     (let (company-frontends
163           (company-require-match 'company-explicit-action-p)
164           (company-backends
165            (list (lambda (command &optional arg)
166                    (case command
167                      (prefix (buffer-substring (point-min) (point)))
168                      (candidates '("abc" "abd")))))))
169       (let (this-command)
170         (company-complete))
171       (let ((last-command-event ?e))
172         (company-call 'self-insert-command 1))
173       (should (eq 2 company-candidates-length))
174       (should (eq 3 (point))))))
175
176 (ert-deftest company-dont-require-match-when-idle ()
177   (with-temp-buffer
178     (insert "ab")
179     (company-mode)
180     (let (company-frontends
181           (company-require-match 'company-explicit-action-p)
182           (company-backends
183            (list (lambda (command &optional arg)
184                    (case command
185                      (prefix (buffer-substring (point-min) (point)))
186                      (candidates '("abc" "abd")))))))
187       (company-idle-begin (current-buffer) (selected-window)
188                           (buffer-chars-modified-tick) (point))
189       (let ((last-command-event ?e))
190         (company-call 'self-insert-command 1))
191       (should (eq nil company-candidates-length))
192       (should (eq 4 (point))))))
193
194 (ert-deftest company-should-complete-whitelist ()
195   (with-temp-buffer
196     (insert "ab")
197     (company-mode)
198     (let (company-frontends
199           company-begin-commands
200           (company-backends
201            (list (lambda (command &optional arg)
202                    (case command
203                      (prefix (buffer-substring (point-min) (point)))
204                      (candidates '("abc" "abd")))))))
205       (let ((company-continue-commands nil))
206         (let (this-command)
207           (company-complete))
208         (company-call 'backward-delete-char 1)
209         (should (null company-candidates-length)))
210       (let ((company-continue-commands '(backward-delete-char)))
211         (let (this-command)
212           (company-complete))
213         (company-call 'backward-delete-char 1)
214         (should (eq 2 company-candidates-length))))))
215
216 (ert-deftest company-should-complete-blacklist ()
217   (with-temp-buffer
218     (insert "ab")
219     (company-mode)
220     (let (company-frontends
221           company-begin-commands
222           (company-backends
223            (list (lambda (command &optional arg)
224                    (case command
225                      (prefix (buffer-substring (point-min) (point)))
226                      (candidates '("abc" "abd")))))))
227       (let ((company-continue-commands '(not backward-delete-char)))
228         (let (this-command)
229           (company-complete))
230         (company-call 'backward-delete-char 1)
231         (should (null company-candidates-length)))
232       (let ((company-continue-commands '(not backward-delete-char-untabify)))
233         (let (this-command)
234           (company-complete))
235         (company-call 'backward-delete-char 1)
236         (should (eq 2 company-candidates-length))))))
237
238 (ert-deftest company-auto-complete-explicit ()
239   (with-temp-buffer
240     (insert "ab")
241     (company-mode)
242     (let (company-frontends
243           (company-auto-complete 'company-explicit-action-p)
244           (company-auto-complete-chars '(? ))
245           (company-backends
246            (list (lambda (command &optional arg)
247                    (case command
248                      (prefix (buffer-substring (point-min) (point)))
249                      (candidates '("abcd" "abef")))))))
250       (let (this-command)
251         (company-complete))
252       (let ((last-command-event ? ))
253         (company-call 'self-insert-command 1))
254       (should (string= "abcd " (buffer-string))))))
255
256 (ert-deftest company-no-auto-complete-when-idle ()
257   (with-temp-buffer
258     (insert "ab")
259     (company-mode)
260     (let (company-frontends
261           (company-auto-complete 'company-explicit-action-p)
262           (company-auto-complete-chars '(? ))
263           (company-backends
264            (list (lambda (command &optional arg)
265                    (case command
266                      (prefix (buffer-substring (point-min) (point)))
267                      (candidates '("abcd" "abef")))))))
268       (company-idle-begin (current-buffer) (selected-window)
269                           (buffer-chars-modified-tick) (point))
270       (let ((last-command-event ? ))
271         (company-call 'self-insert-command 1))
272       (should (string= "ab " (buffer-string))))))
273
274 (ert-deftest company-clears-explicit-action-when-no-matches ()
275   (with-temp-buffer
276     (company-mode)
277     (let (company-frontends
278           company-backends)
279       (company-call 'manual-begin) ;; fails
280       (should (null company-candidates))
281       (should (null (company-explicit-action-p))))))
282
283 (ert-deftest company-ignore-case-replaces-prefix ()
284   (with-temp-buffer
285     (company-mode)
286     (let (company-frontends
287           company-end-of-buffer-workaround
288           (company-backends
289            (list (lambda (command &optional arg)
290                    (case command
291                      (prefix (buffer-substring (point-min) (point)))
292                      (candidates '("abcd" "abef"))
293                      (ignore-case t))))))
294       (insert "A")
295       (let (this-command)
296         (company-complete))
297       (should (string= "ab" (buffer-string)))
298       (delete-char -2)
299       (insert "A") ; hack, to keep it in one test
300       (company-complete-selection)
301       (should (string= "abcd" (buffer-string))))))
302
303 (ert-deftest company-ignore-case-with-keep-prefix ()
304   (with-temp-buffer
305     (insert "AB")
306     (company-mode)
307     (let (company-frontends
308           (company-backends
309            (list (lambda (command &optional arg)
310                    (case command
311                      (prefix (buffer-substring (point-min) (point)))
312                      (candidates '("abcd" "abef"))
313                      (ignore-case 'keep-prefix))))))
314       (let (this-command)
315         (company-complete))
316       (company-complete-selection)
317       (should (string= "ABcd" (buffer-string))))))
318
319 (ert-deftest company-non-prefix-completion ()
320   (with-temp-buffer
321     (insert "tc")
322     (company-mode)
323     (let (company-frontends
324           company-end-of-buffer-workaround
325           (company-backends
326            (list (lambda (command &optional arg)
327                    (case command
328                      (prefix (buffer-substring (point-min) (point)))
329                      (candidates '("tea-cup" "teal-color")))))))
330       (let (this-command)
331         (company-complete))
332       (should (string= "tc" (buffer-string)))
333       (company-complete-selection)
334       (should (string= "tea-cup" (buffer-string))))))
335
336 (ert-deftest company-pseudo-tooltip-does-not-get-displaced ()
337   :tags '(interactive)
338   (with-temp-buffer
339     (save-window-excursion
340       (set-window-buffer nil (current-buffer))
341       (save-excursion (insert " ff"))
342       (company-mode)
343       (let ((company-frontends '(company-pseudo-tooltip-frontend))
344             (company-begin-commands '(self-insert-command))
345             (company-backends
346              (list (lambda (c &optional arg)
347                      (case c (prefix "") (candidates '("a" "b" "c")))))))
348         (let (this-command)
349           (company-call 'complete))
350         (company-call 'open-line 1)
351         (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
352
353 (ert-deftest company-pseudo-tooltip-show ()
354   :tags '(interactive)
355   (with-temp-buffer
356     (save-window-excursion
357     (set-window-buffer nil (current-buffer))
358     (insert "aaaa\n  bb\nccccccc\nddd")
359     (search-backward "bb")
360     (let ((col (company--column))
361           (company-candidates-length 2)
362           (company-candidates '("123" "45"))
363           (company-backend 'ignore))
364       (company-pseudo-tooltip-show (company--row) col 0)
365       (let ((ov company-pseudo-tooltip-overlay))
366         ;; With margins.
367         (should (eq (overlay-get ov 'company-width) 5))
368         ;; FIXME: Make it 2?
369         (should (eq (overlay-get ov 'company-height) company-tooltip-limit))
370         (should (eq (overlay-get ov 'company-column) col))
371         (should (string= (overlay-get ov 'company-after)
372                          "  123 \nc 45  c\nddd\n")))))))
373
374 (ert-deftest company-preview-show-with-annotations ()
375   :tags '(interactive)
376   (with-temp-buffer
377     (save-window-excursion
378       (set-window-buffer nil (current-buffer))
379       (save-excursion (insert "\n"))
380       (let ((company-candidates-length 1)
381             (company-candidates '("123")))
382         (company-preview-show-at-point (point))
383         (let ((ov company-preview-overlay))
384           (should (string= (overlay-get ov 'display) "123\n")))))))
385
386 (ert-deftest company-pseudo-tooltip-show-with-annotations ()
387   :tags '(interactive)
388   (with-temp-buffer
389     (save-window-excursion
390       (set-window-buffer nil (current-buffer))
391       (insert " ")
392       (save-excursion (insert "\n"))
393       (let ((company-candidates-length 2)
394             (company-backend (lambda (action &optional arg &rest _ignore)
395                                (when (eq action 'annotation)
396                                  (cdr (assoc arg '(("123" . "(4)")))))))
397             (company-candidates '("123" "45"))
398             company-tooltip-align-annotations)
399         (company-pseudo-tooltip-show-at-point (point))
400         (let ((ov company-pseudo-tooltip-overlay))
401           ;; With margins.
402           (should (eq (overlay-get ov 'company-width) 8))
403           (should (string= (overlay-get ov 'company-after)
404                            " 123(4) \n 45     \n")))))))
405
406 (ert-deftest company-pseudo-tooltip-show-with-annotations-right-aligned ()
407   :tags '(interactive)
408   (with-temp-buffer
409     (save-window-excursion
410       (set-window-buffer nil (current-buffer))
411       (insert " ")
412       (save-excursion (insert "\n"))
413       (let ((company-candidates-length 3)
414             (company-backend (lambda (action &optional arg &rest _ignore)
415                                (when (eq action 'annotation)
416                                  (cdr (assoc arg '(("123" . "(4)")
417                                                    ("67" . "(891011)")))))))
418             (company-candidates '("123" "45" "67"))
419             (company-tooltip-align-annotations t))
420         (company-pseudo-tooltip-show-at-point (point))
421         (let ((ov company-pseudo-tooltip-overlay))
422           ;; With margins.
423           (should (eq (overlay-get ov 'company-width) 13))
424           (should (string= (overlay-get ov 'company-after)
425                            " 123     (4) \n 45          \n 67 (891011) \n")))))))
426
427 (ert-deftest company-create-lines-shows-numbers ()
428   (let ((company-show-numbers t)
429         (company-candidates '("x" "y" "z"))
430         (company-candidates-length 3)
431         (company-backend 'ignore))
432     (should (equal '(" x 1 " " y 2 " " z 3 ")
433                    (company--create-lines 0 999)))))
434
435 (ert-deftest company-create-lines-truncates-annotations ()
436   (let* ((ww (company--window-width))
437          (data `(("1" . "(123)")
438                  ("2" . nil)
439                  ("3" . ,(concat "(" (make-string (- ww 2) ?4) ")"))
440                  (,(make-string ww ?4) . "<4>")))
441          (company-candidates (mapcar #'car data))
442          (company-candidates-length 4)
443          (company-tooltip-margin 1)
444          (company-backend (lambda (cmd &optional arg)
445                             (when (eq cmd 'annotation)
446                               (cdr (assoc arg data)))))
447          company-tooltip-align-annotations)
448     (should (equal (list (format " 1(123)%s " (company-space-string (- ww 8)))
449                          (format " 2%s " (company-space-string (- ww 3)))
450                          (format " 3(444%s " (make-string (- ww 7) ?4))
451                          (format " %s " (make-string (- ww 2) ?4)))
452                    (company--create-lines 0 999)))
453     (let ((company-tooltip-align-annotations t))
454       (should (equal (list (format " 1%s(123) " (company-space-string (- ww 8)))
455                            (format " 2%s " (company-space-string (- ww 3)))
456                            (format " 3 (444%s " (make-string (- ww 8) ?4))
457                            (format " %s " (make-string (- ww 2) ?4)))
458                      (company--create-lines 0 999))))))
459
460 (ert-deftest company-column-with-composition ()
461   (with-temp-buffer
462     (insert "lambda ()")
463     (compose-region 1 (1+ (length "lambda")) "\\")
464     (should (= (company--column) 4))))
465
466 (ert-deftest company-column-with-line-prefix ()
467   (with-temp-buffer
468     (insert "foo")
469     (put-text-property (point-min) (point) 'line-prefix "  ")
470     (should (= (company--column) 5))))
471
472 (ert-deftest company-column-wth-line-prefix-on-empty-line ()
473   (with-temp-buffer
474     (insert "\n")
475     (forward-char -1)
476     (put-text-property (point-min) (point-max) 'line-prefix "  ")
477     (should (= (company--column) 2))))
478
479 (ert-deftest company-plainify ()
480   (let ((tab-width 8))
481     (should (equal-including-properties
482              (company-plainify "\tabc\td\t")
483              (concat "        "
484                      "abc     "
485                      "d       "))))
486   (should (equal-including-properties
487            (company-plainify (propertize "foobar" 'line-prefix "-*-"))
488            "-*-foobar")))
489
490 (ert-deftest company-modify-line ()
491   (let ((str "-*-foobar"))
492     (should (equal-including-properties
493              (company-modify-line str "zz" 4)
494              "-*-fzzbar"))
495     (should (equal-including-properties
496              (company-modify-line str "xx" 0)
497              "xx-foobar"))
498     (should (equal-including-properties
499              (company-modify-line str "zz" 10)
500              "-*-foobar zz"))))
501
502 (ert-deftest company-scrollbar-bounds ()
503   (should (equal nil (company--scrollbar-bounds 0 3 3)))
504   (should (equal nil (company--scrollbar-bounds 0 4 3)))
505   (should (equal '(0 . 0) (company--scrollbar-bounds 0 1 2)))
506   (should (equal '(1 . 1) (company--scrollbar-bounds 2 2 4)))
507   (should (equal '(2 . 3) (company--scrollbar-bounds 7 4 12)))
508   (should (equal '(1 . 2) (company--scrollbar-bounds 3 4 12)))
509   (should (equal '(1 . 3) (company--scrollbar-bounds 4 5 11))))
510
511 ;;; Async
512
513 (defun company-async-backend (command &optional arg)
514   (pcase command
515     (`prefix "foo")
516     (`candidates
517      (cons :async
518            (lambda (cb)
519              (run-with-timer 0.05 nil
520                              #'funcall cb '("abc" "abd")))))))
521
522 (ert-deftest company-call-backend-forces-sync ()
523   (let ((company-backend 'company-async-backend)
524         (company-async-timeout 0.1))
525     (should (equal '("abc" "abd") (company-call-backend 'candidates)))))
526
527 (ert-deftest company-call-backend-errors-on-timeout ()
528   (with-temp-buffer
529     (let* ((company-backend (lambda (command &optional _arg)
530                               (pcase command
531                                 (`candidates (cons :async 'ignore)))))
532            (company-async-timeout 0.1)
533            (err (should-error (company-call-backend 'candidates "foo"))))
534       (should (string-match-p "async timeout" (cadr err))))))
535
536 (ert-deftest company-call-backend-raw-passes-return-value-verbatim ()
537   (let ((company-backend 'company-async-backend))
538     (should (equal "foo" (company-call-backend-raw 'prefix)))
539     (should (equal :async (car (company-call-backend-raw 'candidates "foo"))))
540     (should (equal 'closure (cadr (company-call-backend-raw 'candidates "foo"))))))
541
542 (ert-deftest company-manual-begin-forces-async-candidates-to-sync ()
543   (with-temp-buffer
544     (company-mode)
545     (let (company-frontends
546           (company-backends (list 'company-async-backend)))
547       (company-manual-begin)
548       (should (equal "foo" company-prefix))
549       (should (equal '("abc" "abd") company-candidates)))))
550
551 (ert-deftest company-idle-begin-allows-async-candidates ()
552   (with-temp-buffer
553     (company-mode)
554     (let (company-frontends
555           (company-backends (list 'company-async-backend)))
556       (company-idle-begin (current-buffer) (selected-window)
557                           (buffer-chars-modified-tick) (point))
558       (should (null company-candidates))
559       (sleep-for 0.1)
560       (should (equal "foo" company-prefix))
561       (should (equal '("abc" "abd") company-candidates)))))
562
563 (ert-deftest company-idle-begin-cancels-async-candidates-if-buffer-changed ()
564   (with-temp-buffer
565     (company-mode)
566     (let (company-frontends
567           (company-backends (list 'company-async-backend)))
568       (company-idle-begin (current-buffer) (selected-window)
569                           (buffer-chars-modified-tick) (point))
570       (should (null company-candidates))
571       (insert "a")
572       (sleep-for 0.1)
573       (should (null company-prefix))
574       (should (null company-candidates)))))
575
576 (ert-deftest company-idle-begin-async-allows-immediate-callbacks ()
577   (with-temp-buffer
578     (company-mode)
579     (let (company-frontends
580           (company-backends
581            (list (lambda (command &optional arg)
582                    (pcase command
583                      (`prefix (buffer-substring (point-min) (point)))
584                      (`candidates
585                       (let ((c (all-completions arg '("abc" "def"))))
586                         (cons :async
587                               (lambda (cb) (funcall cb c)))))
588                      (`no-cache t)))))
589           (company-minimum-prefix-length 0))
590       (company-idle-begin (current-buffer) (selected-window)
591                           (buffer-chars-modified-tick) (point))
592       (should (equal '("abc" "def") company-candidates))
593       (let ((last-command-event ?a))
594         (company-call 'self-insert-command 1))
595       (should (equal '("abc") company-candidates)))))
596
597 (ert-deftest company-multi-backend-forces-prefix-to-sync ()
598   (with-temp-buffer
599     (let ((company-backend (list 'ignore
600                                  (lambda (command)
601                                    (should (eq command 'prefix))
602                                    (cons :async
603                                          (lambda (cb)
604                                            (run-with-timer
605                                             0.01 nil
606                                             (lambda () (funcall cb nil))))))
607                                  (lambda (command)
608                                    (should (eq command 'prefix))
609                                    "foo"))))
610       (should (equal "foo" (company-call-backend-raw 'prefix))))
611     (let ((company-backend (list (lambda (_command)
612                                    (cons :async
613                                          (lambda (cb)
614                                            (run-with-timer
615                                             0.01 nil
616                                             (lambda () (funcall cb "bar"))))))
617                                  (lambda (_command)
618                                    "foo"))))
619       (should (equal "bar" (company-call-backend-raw 'prefix))))))
620
621 (ert-deftest company-multi-backend-merges-deferred-candidates ()
622   (with-temp-buffer
623     (let* ((immediate (lambda (command &optional arg)
624                         (pcase command
625                           (`prefix "foo")
626                           (`candidates
627                            (cons :async
628                                  (lambda (cb) (funcall cb '("f"))))))))
629            (company-backend (list 'ignore
630                                   (lambda (command &optional arg)
631                                     (pcase command
632                                       (`prefix "foo")
633                                       (`candidates
634                                        (should (equal arg "foo"))
635                                        (cons :async
636                                              (lambda (cb)
637                                                (run-with-timer
638                                                 0.01 nil
639                                                 (lambda () (funcall cb '("a" "b")))))))))
640                                   (lambda (command &optional arg)
641                                     (pcase command
642                                       (`prefix "foo")
643                                       (`candidates '("c" "d" "e"))))
644                                   immediate)))
645       (should (equal :async (car (company-call-backend-raw 'candidates "foo"))))
646       (should (equal '("a" "b" "c" "d" "e" "f")
647                      (company-call-backend 'candidates "foo")))
648       (let ((company-backend (list immediate)))
649         (should (equal '("f") (company-call-backend 'candidates "foo")))))))
650
651 ;;; Template
652
653 (ert-deftest company-template-removed-after-the-last-jump ()
654   (with-temp-buffer
655     (insert "{ }")
656     (goto-char 2)
657     (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
658       (save-excursion
659         (dotimes (i 2)
660           (insert " ")
661           (company-template-add-field tpl (point) "foo")))
662       (company-call 'template-forward-field)
663       (should (= 3 (point)))
664       (company-call 'template-forward-field)
665       (should (= 7 (point)))
666       (company-call 'template-forward-field)
667       (should (= 11 (point)))
668       (should (zerop (length (overlay-get tpl 'company-template-fields))))
669       (should (null (overlay-buffer tpl))))))
670
671 (ert-deftest company-template-removed-after-input-and-jump ()
672   (with-temp-buffer
673     (insert "{ }")
674     (goto-char 2)
675     (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
676       (save-excursion
677         (insert " ")
678         (company-template-add-field tpl (point) "bar"))
679       (company-call 'template-move-to-first tpl)
680       (should (= 3 (point)))
681       (dolist (c (string-to-list "tee"))
682         (let ((last-command-event c))
683           (company-call 'self-insert-command 1)))
684       (should (string= "{ tee }" (buffer-string)))
685       (should (overlay-buffer tpl))
686       (company-call 'template-forward-field)
687       (should (= 7 (point)))
688       (should (null (overlay-buffer tpl))))))
689
690 (defun company-call (name &rest args)
691   (let* ((maybe (intern (format "company-%s" name)))
692          (command (if (fboundp maybe) maybe name)))
693     (let ((this-command command))
694       (run-hooks 'pre-command-hook))
695     (apply command args)
696     (let ((this-command command))
697       (run-hooks 'post-command-hook))))
698
699 (ert-deftest company-template-c-like-templatify ()
700   (with-temp-buffer
701     (let ((text "foo(int a, short b)"))
702       (insert text)
703       (company-template-c-like-templatify text)
704       (should (equal "foo(arg0, arg1)" (buffer-string)))
705       (should (looking-at "arg0"))
706       (should (equal "int a"
707                      (overlay-get (company-template-field-at) 'display))))))
708
709 (ert-deftest company-template-c-like-templatify-trims-after-closing-paren ()
710   (with-temp-buffer
711     (let ((text "foo(int a, short b)!@ #1334 a"))
712       (insert text)
713       (company-template-c-like-templatify text)
714       (should (equal "foo(arg0, arg1)" (buffer-string)))
715       (should (looking-at "arg0")))))
716
717 ;;; Clang
718
719 (ert-deftest company-clang-objc-templatify ()
720   (with-temp-buffer
721     (let ((text "createBookWithTitle:andAuthor:"))
722       (insert text)
723       (company-clang-objc-templatify text)
724       (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
725       (should (looking-at "arg0"))
726       (should (null (overlay-get (company-template-field-at) 'display))))))