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