1 ;;; company-tests.el --- company-mode tests
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/>.
28 (eval-when-compile (require 'cl))
31 (require 'company-keywords)
32 (require 'company-elisp)
33 (require 'company-clang)
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))
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))))))
59 (ert-deftest company--manual-prefix-set-and-unset ()
63 (let (company-frontends
65 (list (lambda (command &optional arg)
67 (prefix (buffer-substring (point-min) (point)))
68 (candidates '("abc" "abd")))))))
69 (company-manual-begin)
70 (should (equal "ab" company--manual-prefix))
72 (should (null company--manual-prefix)))))
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)))))))
92 (ert-deftest company-multi-backend-with-lambdas ()
93 (let ((company-backend
94 (list (lambda (command &optional arg &rest ignore)
97 (candidates '("a" "b"))))
98 (lambda (command &optional arg &rest ignore)
101 (candidates '("c" "d")))))))
102 (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
104 (ert-deftest company-multi-backend-remembers-candidate-backend ()
105 (let ((company-backend
106 (list (lambda (command &optional arg &rest ignore)
110 (candidates '("a" "c"))
111 (post-completion "13")))
112 (lambda (command &optional arg &rest ignore)
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)))))))
126 (ert-deftest company-multi-backend-handles-keyword-with ()
127 (let ((primo (lambda (command &optional arg)
130 (candidates '("abb" "abc" "abd")))))
131 (secundo (lambda (command &optional arg)
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"))))))
142 (ert-deftest company-begin-backend-failure-doesnt-break-company-backends ()
147 (company-begin-backend (lambda (command &rest ignore))))
148 (let (company-frontends
150 (list (lambda (command &optional arg)
153 (candidates '("a" "ab" "ac")))))))
155 (company-call 'complete))
156 (should (eq 3 company-candidates-length)))))
158 (ert-deftest company-require-match-explicit ()
162 (let (company-frontends
163 (company-require-match 'company-explicit-action-p)
165 (list (lambda (command &optional arg)
167 (prefix (buffer-substring (point-min) (point)))
168 (candidates '("abc" "abd")))))))
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))))))
176 (ert-deftest company-dont-require-match-when-idle ()
180 (let (company-frontends
181 (company-require-match 'company-explicit-action-p)
183 (list (lambda (command &optional arg)
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))))))
194 (ert-deftest company-should-complete-whitelist ()
198 (let (company-frontends
199 company-begin-commands
201 (list (lambda (command &optional arg)
203 (prefix (buffer-substring (point-min) (point)))
204 (candidates '("abc" "abd")))))))
205 (let ((company-continue-commands nil))
208 (company-call 'backward-delete-char 1)
209 (should (null company-candidates-length)))
210 (let ((company-continue-commands '(backward-delete-char)))
213 (company-call 'backward-delete-char 1)
214 (should (eq 2 company-candidates-length))))))
216 (ert-deftest company-should-complete-blacklist ()
220 (let (company-frontends
221 company-begin-commands
223 (list (lambda (command &optional arg)
225 (prefix (buffer-substring (point-min) (point)))
226 (candidates '("abc" "abd")))))))
227 (let ((company-continue-commands '(not backward-delete-char)))
230 (company-call 'backward-delete-char 1)
231 (should (null company-candidates-length)))
232 (let ((company-continue-commands '(not backward-delete-char-untabify)))
235 (company-call 'backward-delete-char 1)
236 (should (eq 2 company-candidates-length))))))
238 (ert-deftest company-auto-complete-explicit ()
242 (let (company-frontends
243 (company-auto-complete 'company-explicit-action-p)
244 (company-auto-complete-chars '(? ))
246 (list (lambda (command &optional arg)
248 (prefix (buffer-substring (point-min) (point)))
249 (candidates '("abcd" "abef")))))))
252 (let ((last-command-event ? ))
253 (company-call 'self-insert-command 1))
254 (should (string= "abcd " (buffer-string))))))
256 (ert-deftest company-no-auto-complete-when-idle ()
260 (let (company-frontends
261 (company-auto-complete 'company-explicit-action-p)
262 (company-auto-complete-chars '(? ))
264 (list (lambda (command &optional arg)
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))))))
274 (ert-deftest company-clears-explicit-action-when-no-matches ()
277 (let (company-frontends
279 (company-call 'manual-begin) ;; fails
280 (should (null company-candidates))
281 (should (null (company-explicit-action-p))))))
283 (ert-deftest company-ignore-case-replaces-prefix ()
286 (let (company-frontends
287 company-end-of-buffer-workaround
289 (list (lambda (command &optional arg)
291 (prefix (buffer-substring (point-min) (point)))
292 (candidates '("abcd" "abef"))
297 (should (string= "ab" (buffer-string)))
299 (insert "A") ; hack, to keep it in one test
300 (company-complete-selection)
301 (should (string= "abcd" (buffer-string))))))
303 (ert-deftest company-ignore-case-with-keep-prefix ()
307 (let (company-frontends
309 (list (lambda (command &optional arg)
311 (prefix (buffer-substring (point-min) (point)))
312 (candidates '("abcd" "abef"))
313 (ignore-case 'keep-prefix))))))
316 (company-complete-selection)
317 (should (string= "ABcd" (buffer-string))))))
319 (ert-deftest company-non-prefix-completion ()
323 (let (company-frontends
324 company-end-of-buffer-workaround
326 (list (lambda (command &optional arg)
328 (prefix (buffer-substring (point-min) (point)))
329 (candidates '("tea-cup" "teal-color")))))))
332 (should (string= "tc" (buffer-string)))
333 (company-complete-selection)
334 (should (string= "tea-cup" (buffer-string))))))
336 (ert-deftest company-pseudo-tooltip-does-not-get-displaced ()
339 (save-window-excursion
340 (set-window-buffer nil (current-buffer))
341 (save-excursion (insert " ff"))
343 (let ((company-frontends '(company-pseudo-tooltip-frontend))
344 (company-begin-commands '(self-insert-command))
346 (list (lambda (c &optional arg)
347 (case c (prefix "") (candidates '("a" "b" "c")))))))
349 (company-call 'complete))
350 (company-call 'open-line 1)
351 (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
353 (ert-deftest company-pseudo-tooltip-show ()
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))
367 (should (eq (overlay-get ov 'company-width) 5))
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")))))))
374 (ert-deftest company-preview-show-with-annotations ()
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")))))))
386 (ert-deftest company-pseudo-tooltip-show-with-annotations ()
389 (save-window-excursion
390 (set-window-buffer nil (current-buffer))
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))
402 (should (eq (overlay-get ov 'company-width) 8))
403 (should (string= (overlay-get ov 'company-after)
404 " 123(4) \n 45 \n")))))))
406 (ert-deftest company-pseudo-tooltip-show-with-annotations-right-aligned ()
409 (save-window-excursion
410 (set-window-buffer nil (current-buffer))
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))
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")))))))
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)))))
435 (ert-deftest company-create-lines-truncates-annotations ()
436 (let* ((ww (company--window-width))
437 (data `(("1" . "(123)")
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))))))
460 (ert-deftest company-column-with-composition ()
463 (compose-region 1 (1+ (length "lambda")) "\\")
464 (should (= (company--column) 4))))
466 (ert-deftest company-column-with-line-prefix ()
469 (put-text-property (point-min) (point) 'line-prefix " ")
470 (should (= (company--column) 5))))
472 (ert-deftest company-column-wth-line-prefix-on-empty-line ()
476 (put-text-property (point-min) (point-max) 'line-prefix " ")
477 (should (= (company--column) 2))))
479 (ert-deftest company-plainify ()
481 (should (equal-including-properties
482 (company-plainify "\tabc\td\t")
486 (should (equal-including-properties
487 (company-plainify (propertize "foobar" 'line-prefix "-*-"))
490 (ert-deftest company-modify-line ()
491 (let ((str "-*-foobar"))
492 (should (equal-including-properties
493 (company-modify-line str "zz" 4)
495 (should (equal-including-properties
496 (company-modify-line str "xx" 0)
498 (should (equal-including-properties
499 (company-modify-line str "zz" 10)
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))))
513 (ert-deftest company-template-removed-after-the-last-jump ()
517 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
521 (company-template-add-field tpl (point) "foo")))
522 (company-call 'template-forward-field)
523 (should (= 3 (point)))
524 (company-call 'template-forward-field)
525 (should (= 7 (point)))
526 (company-call 'template-forward-field)
527 (should (= 11 (point)))
528 (should (zerop (length (overlay-get tpl 'company-template-fields))))
529 (should (null (overlay-buffer tpl))))))
531 (ert-deftest company-template-removed-after-input-and-jump ()
535 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
538 (company-template-add-field tpl (point) "bar"))
539 (company-call 'template-move-to-first tpl)
540 (should (= 3 (point)))
541 (dolist (c (string-to-list "tee"))
542 (let ((last-command-event c))
543 (company-call 'self-insert-command 1)))
544 (should (string= "{ tee }" (buffer-string)))
545 (should (overlay-buffer tpl))
546 (company-call 'template-forward-field)
547 (should (= 7 (point)))
548 (should (null (overlay-buffer tpl))))))
550 (defun company-call (name &rest args)
551 (let* ((maybe (intern (format "company-%s" name)))
552 (command (if (fboundp maybe) maybe name)))
553 (let ((this-command command))
554 (run-hooks 'pre-command-hook))
556 (let ((this-command command))
557 (run-hooks 'post-command-hook))))
559 (ert-deftest company-template-c-like-templatify ()
561 (let ((text "foo(int a, short b)"))
563 (company-template-c-like-templatify text)
564 (should (equal "foo(arg0, arg1)" (buffer-string)))
565 (should (looking-at "arg0"))
566 (should (equal "int a"
567 (overlay-get (company-template-field-at) 'display))))))
569 (ert-deftest company-template-c-like-templatify-trims-after-closing-paren ()
571 (let ((text "foo(int a, short b)!@ #1334 a"))
573 (company-template-c-like-templatify text)
574 (should (equal "foo(arg0, arg1)" (buffer-string)))
575 (should (looking-at "arg0")))))
579 (defmacro company-elisp-with-buffer (contents &rest body)
583 (setq major-mode 'emacs-lisp-mode)
584 (re-search-backward "|")
586 (let ((company-elisp-detect-function-context t))
589 (ert-deftest company-elisp-candidates-predicate ()
590 (company-elisp-with-buffer
592 (should (eq (company-elisp--candidates-predicate "ba")
594 (should (eq (let (company-elisp-detect-function-context)
595 (company-elisp--candidates-predicate "ba"))
596 'company-elisp--predicate)))
597 (company-elisp-with-buffer
599 (should (eq (company-elisp--candidates-predicate "foo")
601 (should (eq (let (company-elisp-detect-function-context)
602 (company-elisp--candidates-predicate "foo"))
603 'company-elisp--predicate)))
604 (company-elisp-with-buffer
606 (should (eq (company-elisp--candidates-predicate "b")
607 'company-elisp--predicate))))
609 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
610 (company-elisp-with-buffer
611 "(def foo () \"Doo be doo `ide|"
612 (should (eq 'company-elisp--predicate
613 (company-elisp--candidates-predicate "ide")))))
615 ;; This one's also an integration test.
616 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
617 (let ((company-elisp-detect-function-context t)
618 (obarray [when what whelp])
622 (company-elisp-with-buffer
623 "(let ((foo 7) (wh| )))"
624 (should (equal '("what" "whelp")
625 (company-elisp-candidates "wh"))))
626 (company-elisp-with-buffer
627 "(cond ((null nil) (wh| )))"
628 (should (equal '("when")
629 (company-elisp-candidates "wh"))))))
631 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
632 (loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
633 ("(let (foo (bar|" "bar" boundp)
634 ("(let (foo) (bar|" "bar" fboundp))
636 (eval `(company-elisp-with-buffer
638 (should (eq ',predicate
639 (company-elisp--candidates-predicate ,prefix)))))))
641 (ert-deftest company-elisp-finds-vars ()
642 (let ((obarray [boo bar baz backquote])
646 (should (equal '("bar" "baz")
647 (company-elisp--globals "ba" 'boundp)))))
649 (ert-deftest company-elisp-finds-functions ()
650 (let ((obarray [when what whelp])
653 (should (equal '("when")
654 (company-elisp--globals "wh" 'fboundp)))))
656 (ert-deftest company-elisp-finds-things ()
657 (let ((obarray [when what whelp])
660 (should (equal '("what" "whelp" "when")
661 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
664 (ert-deftest company-elisp-locals-vars ()
665 (company-elisp-with-buffer
666 "(let ((foo 5) (bar 6))
667 (cl-labels ((borg ()))
670 (should (equal '("bar" "baz" "boo")
671 (company-elisp--locals "b" nil)))))
673 (ert-deftest company-elisp-locals-single-var ()
674 (company-elisp-with-buffer
678 (should (equal '("itk" "item")
679 (company-elisp--locals "it" nil)))))
681 (ert-deftest company-elisp-locals-funs ()
682 (company-elisp-with-buffer
683 "(cl-labels ((foo ())
687 (should (equal '("fee" "foo")
688 (sort (company-elisp--locals "f" t) 'string<)))))
690 (ert-deftest company-elisp-locals-skips-current-varlist ()
691 (company-elisp-with-buffer
694 (should (null (company-elisp--locals "f" nil)))))
696 (ert-deftest company-elisp-show-locals-first ()
697 (company-elisp-with-buffer
702 (let ((obarray [float-pi]))
703 (let (company-elisp-show-locals-first)
704 (should (eq nil (company-elisp 'sorted))))
705 (let ((company-elisp-show-locals-first t))
706 (should (eq t (company-elisp 'sorted)))
707 (should (equal '("flee" "floo" "flop" "float-pi")
708 (company-elisp-candidates "fl")))))))
710 (ert-deftest company-elisp-candidates-no-duplicates ()
711 (company-elisp-with-buffer
714 (let ((obarray [float-pi])
715 (company-elisp-show-locals-first t))
716 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
718 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
719 (company-elisp-with-buffer
721 (should (null (company-elisp 'prefix)))))
723 (ert-deftest company-elisp-should-complete-def-call ()
724 (company-elisp-with-buffer
726 (should (equal "defu" (company-elisp 'prefix)))))
728 (ert-deftest company-elisp-should-complete-in-defvar ()
729 ;; It will also complete the var name, at least for now.
730 (company-elisp-with-buffer
732 (should (equal "de" (company-elisp 'prefix)))))
734 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
735 (company-elisp-with-buffer
736 "(defsubst foobar (ba|"
737 (should (null (company-elisp 'prefix)))))
739 (ert-deftest company-elisp-prefix-in-defun-body ()
740 (company-elisp-with-buffer
742 (should (equal "" (company-elisp 'prefix)))))
746 (ert-deftest company-clang-objc-templatify ()
748 (let ((text "createBookWithTitle:andAuthor:"))
750 (company-clang-objc-templatify text)
751 (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
752 (should (looking-at "arg0"))
753 (should (null (overlay-get (company-template-field-at) 'display))))))