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--explicit-action)
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))))))
56 (ert-deftest company-multi-backend-with-lambdas ()
57 (let ((company-backend
58 (list (lambda (command &optional arg &rest ignore)
61 (candidates '("a" "b"))))
62 (lambda (command &optional arg &rest ignore)
65 (candidates '("c" "d")))))))
66 (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
68 (ert-deftest company-multi-backend-remembers-candidate-backend ()
69 (let ((company-backend
70 (list (lambda (command &optional arg &rest ignore)
74 (candidates '("a" "c"))
75 (post-completion "13")))
76 (lambda (command &optional arg &rest ignore)
80 (candidates '("b" "d"))
81 (post-completion "42"))))))
82 (let ((candidates (company-calculate-candidates nil)))
83 (should (equal candidates '("a" "b" "c" "d")))
84 (should (equal t (company-call-backend 'ignore-case)))
85 (should (equal "1" (company-call-backend 'annotation (nth 0 candidates))))
86 (should (equal "2" (company-call-backend 'annotation (nth 1 candidates))))
87 (should (equal "13" (company-call-backend 'post-completion (nth 2 candidates))))
88 (should (equal "42" (company-call-backend 'post-completion (nth 3 candidates)))))))
90 (ert-deftest company-multi-backend-handles-keyword-with ()
91 (let ((primo (lambda (command &optional arg)
94 (candidates '("abb" "abc" "abd")))))
95 (secundo (lambda (command &optional arg)
98 (candidates '("acc" "acd"))))))
99 (let ((company-backend (list 'ignore 'ignore :with secundo)))
100 (should (null (company-call-backend 'prefix))))
101 (let ((company-backend (list 'ignore primo :with secundo)))
102 (should (equal "a" (company-call-backend 'prefix)))
103 (should (equal '("abb" "abc" "abd" "acc" "acd")
104 (company-call-backend 'candidates "a"))))))
106 (ert-deftest company-begin-backend-failure-doesnt-break-company-backends ()
111 (company-begin-backend (lambda (command &rest ignore))))
112 (let (company-frontends
114 (list (lambda (command &optional arg)
117 (candidates '("a" "ab" "ac")))))))
119 (company-call 'complete))
120 (should (eq 3 company-candidates-length)))))
122 (ert-deftest company-require-match-explicit ()
126 (let (company-frontends
127 (company-require-match 'company-explicit-action-p)
129 (list (lambda (command &optional arg)
131 (prefix (buffer-substring (point-min) (point)))
132 (candidates '("abc" "abd")))))))
135 (let ((last-command-event ?e))
136 (company-call 'self-insert-command 1))
137 (should (eq 2 company-candidates-length))
138 (should (eq 3 (point))))))
140 (ert-deftest company-dont-require-match-when-idle ()
144 (let (company-frontends
145 (company-require-match 'company-explicit-action-p)
147 (list (lambda (command &optional arg)
149 (prefix (buffer-substring (point-min) (point)))
150 (candidates '("abc" "abd")))))))
151 (company-idle-begin (current-buffer) (selected-window)
152 (buffer-chars-modified-tick) (point))
153 (let ((last-command-event ?e))
154 (company-call 'self-insert-command 1))
155 (should (eq nil company-candidates-length))
156 (should (eq 4 (point))))))
158 (ert-deftest company-should-complete-whitelist ()
162 (let (company-frontends
163 company-begin-commands
165 (list (lambda (command &optional arg)
167 (prefix (buffer-substring (point-min) (point)))
168 (candidates '("abc" "abd")))))))
169 (let ((company-continue-commands nil))
172 (company-call 'backward-delete-char 1)
173 (should (null company-candidates-length)))
174 (let ((company-continue-commands '(backward-delete-char)))
177 (company-call 'backward-delete-char 1)
178 (should (eq 2 company-candidates-length))))))
180 (ert-deftest company-should-complete-blacklist ()
184 (let (company-frontends
185 company-begin-commands
187 (list (lambda (command &optional arg)
189 (prefix (buffer-substring (point-min) (point)))
190 (candidates '("abc" "abd")))))))
191 (let ((company-continue-commands '(not backward-delete-char)))
194 (company-call 'backward-delete-char 1)
195 (should (null company-candidates-length)))
196 (let ((company-continue-commands '(not backward-delete-char-untabify)))
199 (company-call 'backward-delete-char 1)
200 (should (eq 2 company-candidates-length))))))
202 (ert-deftest company-auto-complete-explicit ()
206 (let (company-frontends
207 (company-auto-complete 'company-explicit-action-p)
208 (company-auto-complete-chars '(? ))
210 (list (lambda (command &optional arg)
212 (prefix (buffer-substring (point-min) (point)))
213 (candidates '("abcd" "abef")))))))
216 (let ((last-command-event ? ))
217 (company-call 'self-insert-command 1))
218 (should (string= "abcd " (buffer-string))))))
220 (ert-deftest company-no-auto-complete-when-idle ()
224 (let (company-frontends
225 (company-auto-complete 'company-explicit-action-p)
226 (company-auto-complete-chars '(? ))
228 (list (lambda (command &optional arg)
230 (prefix (buffer-substring (point-min) (point)))
231 (candidates '("abcd" "abef")))))))
232 (company-idle-begin (current-buffer) (selected-window)
233 (buffer-chars-modified-tick) (point))
234 (let ((last-command-event ? ))
235 (company-call 'self-insert-command 1))
236 (should (string= "ab " (buffer-string))))))
238 (ert-deftest company-clears-explicit-action-when-no-matches ()
241 (let (company-frontends
243 (company-call 'manual-begin) ;; fails
244 (should (null company-candidates))
245 (should (null (company-explicit-action-p))))))
247 (ert-deftest company-ignore-case-replaces-prefix ()
250 (let (company-frontends
251 company-end-of-buffer-workaround
253 (list (lambda (command &optional arg)
255 (prefix (buffer-substring (point-min) (point)))
256 (candidates '("abcd" "abef"))
261 (should (string= "ab" (buffer-string)))
263 (insert "A") ; hack, to keep it in one test
264 (company-complete-selection)
265 (should (string= "abcd" (buffer-string))))))
267 (ert-deftest company-ignore-case-with-keep-prefix ()
271 (let (company-frontends
273 (list (lambda (command &optional arg)
275 (prefix (buffer-substring (point-min) (point)))
276 (candidates '("abcd" "abef"))
277 (ignore-case 'keep-prefix))))))
280 (company-complete-selection)
281 (should (string= "ABcd" (buffer-string))))))
283 (ert-deftest company-non-prefix-completion ()
287 (let (company-frontends
288 company-end-of-buffer-workaround
290 (list (lambda (command &optional arg)
292 (prefix (buffer-substring (point-min) (point)))
293 (candidates '("tea-cup" "teal-color")))))))
296 (should (string= "tc" (buffer-string)))
297 (company-complete-selection)
298 (should (string= "tea-cup" (buffer-string))))))
300 (ert-deftest company-pseudo-tooltip-does-not-get-displaced ()
303 (save-window-excursion
304 (set-window-buffer nil (current-buffer))
305 (save-excursion (insert " ff"))
307 (let ((company-frontends '(company-pseudo-tooltip-frontend))
308 (company-begin-commands '(self-insert-command))
310 (list (lambda (c &optional arg)
311 (case c (prefix "") (candidates '("a" "b" "c")))))))
313 (company-call 'complete))
314 (company-call 'open-line 1)
315 (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
317 (ert-deftest company-pseudo-tooltip-show ()
320 (save-window-excursion
321 (set-window-buffer nil (current-buffer))
322 (insert "aaaa\n bb\nccccccc\nddd")
323 (search-backward "bb")
324 (let ((col (company--column))
325 (company-candidates-length 2)
326 (company-candidates '("123" "45"))
327 (company-backend 'ignore))
328 (company-pseudo-tooltip-show (company--row) col 0)
329 (let ((ov company-pseudo-tooltip-overlay))
331 (should (eq (overlay-get ov 'company-width) 5))
333 (should (eq (overlay-get ov 'company-height) company-tooltip-limit))
334 (should (eq (overlay-get ov 'company-column) col))
335 (should (string= (overlay-get ov 'company-after)
336 " 123 \nc 45 c\nddd\n")))))))
338 (ert-deftest company-preview-show-with-annotations ()
341 (save-window-excursion
342 (set-window-buffer nil (current-buffer))
343 (save-excursion (insert "\n"))
344 (let ((company-candidates-length 1)
345 (company-candidates '("123")))
346 (company-preview-show-at-point (point))
347 (let ((ov company-preview-overlay))
348 (should (string= (overlay-get ov 'display) "123\n")))))))
350 (ert-deftest company-pseudo-tooltip-show-with-annotations ()
353 (save-window-excursion
354 (set-window-buffer nil (current-buffer))
356 (save-excursion (insert "\n"))
357 (let ((company-candidates-length 2)
358 (company-backend (lambda (action &optional arg &rest _ignore)
359 (when (eq action 'annotation)
360 (cdr (assoc arg '(("123" . "(4)")))))))
361 (company-candidates '("123" "45"))
362 company-tooltip-align-annotations)
363 (company-pseudo-tooltip-show-at-point (point))
364 (let ((ov company-pseudo-tooltip-overlay))
366 (should (eq (overlay-get ov 'company-width) 8))
367 (should (string= (overlay-get ov 'company-after)
368 " 123(4) \n 45 \n")))))))
370 (ert-deftest company-pseudo-tooltip-show-with-annotations-right-aligned ()
373 (save-window-excursion
374 (set-window-buffer nil (current-buffer))
376 (save-excursion (insert "\n"))
377 (let ((company-candidates-length 3)
378 (company-backend (lambda (action &optional arg &rest _ignore)
379 (when (eq action 'annotation)
380 (cdr (assoc arg '(("123" . "(4)")
381 ("67" . "(891011)")))))))
382 (company-candidates '("123" "45" "67"))
383 (company-tooltip-align-annotations t))
384 (company-pseudo-tooltip-show-at-point (point))
385 (let ((ov company-pseudo-tooltip-overlay))
387 (should (eq (overlay-get ov 'company-width) 13))
388 (should (string= (overlay-get ov 'company-after)
389 " 123 (4) \n 45 \n 67 (891011) \n")))))))
391 (ert-deftest company-create-lines-shows-numbers ()
392 (let ((company-show-numbers t)
393 (company-candidates '("x" "y" "z"))
394 (company-candidates-length 3)
395 (company-backend 'ignore))
396 (should (equal '(" x 1 " " y 2 " " z 3 ")
397 (company--create-lines 0 999)))))
399 (ert-deftest company-create-lines-truncates-annotations ()
400 (let* ((ww (company--window-width))
401 (data `(("1" . "(123)")
403 ("3" . ,(concat "(" (make-string (- ww 2) ?4) ")"))
404 (,(make-string ww ?4) . "<4>")))
405 (company-candidates (mapcar #'car data))
406 (company-candidates-length 4)
407 (company-tooltip-margin 1)
408 (company-backend (lambda (cmd &optional arg)
409 (when (eq cmd 'annotation)
410 (cdr (assoc arg data)))))
411 company-tooltip-align-annotations)
412 (should (equal (list (format " 1(123)%s " (company-space-string (- ww 8)))
413 (format " 2%s " (company-space-string (- ww 3)))
414 (format " 3(444%s " (make-string (- ww 7) ?4))
415 (format " %s " (make-string (- ww 2) ?4)))
416 (company--create-lines 0 999)))
417 (let ((company-tooltip-align-annotations t))
418 (should (equal (list (format " 1%s(123) " (company-space-string (- ww 8)))
419 (format " 2%s " (company-space-string (- ww 3)))
420 (format " 3 (444%s " (make-string (- ww 8) ?4))
421 (format " %s " (make-string (- ww 2) ?4)))
422 (company--create-lines 0 999))))))
424 (ert-deftest company-column-with-composition ()
427 (compose-region 1 (1+ (length "lambda")) "\\")
428 (should (= (company--column) 4))))
430 (ert-deftest company-column-with-line-prefix ()
433 (put-text-property (point-min) (point) 'line-prefix " ")
434 (should (= (company--column) 5))))
436 (ert-deftest company-column-wth-line-prefix-on-empty-line ()
440 (put-text-property (point-min) (point-max) 'line-prefix " ")
441 (should (= (company--column) 2))))
443 (ert-deftest company-plainify ()
445 (should (equal-including-properties
446 (company-plainify "\tabc\td\t")
450 (should (equal-including-properties
451 (company-plainify (propertize "foobar" 'line-prefix "-*-"))
454 (ert-deftest company-modify-line ()
455 (let ((str "-*-foobar"))
456 (should (equal-including-properties
457 (company-modify-line str "zz" 4)
459 (should (equal-including-properties
460 (company-modify-line str "xx" 0)
462 (should (equal-including-properties
463 (company-modify-line str "zz" 10)
466 (ert-deftest company-scrollbar-bounds ()
467 (should (equal nil (company--scrollbar-bounds 0 3 3)))
468 (should (equal nil (company--scrollbar-bounds 0 4 3)))
469 (should (equal '(0 . 0) (company--scrollbar-bounds 0 1 2)))
470 (should (equal '(1 . 1) (company--scrollbar-bounds 2 2 4)))
471 (should (equal '(2 . 3) (company--scrollbar-bounds 7 4 12)))
472 (should (equal '(1 . 2) (company--scrollbar-bounds 3 4 12)))
473 (should (equal '(1 . 3) (company--scrollbar-bounds 4 5 11))))
477 (ert-deftest company-template-removed-after-the-last-jump ()
481 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
485 (company-template-add-field tpl (point) "foo")))
486 (company-call 'template-forward-field)
487 (should (= 3 (point)))
488 (company-call 'template-forward-field)
489 (should (= 7 (point)))
490 (company-call 'template-forward-field)
491 (should (= 11 (point)))
492 (should (zerop (length (overlay-get tpl 'company-template-fields))))
493 (should (null (overlay-buffer tpl))))))
495 (ert-deftest company-template-removed-after-input-and-jump ()
499 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
502 (company-template-add-field tpl (point) "bar"))
503 (company-call 'template-move-to-first tpl)
504 (should (= 3 (point)))
505 (dolist (c (string-to-list "tee"))
506 (let ((last-command-event c))
507 (company-call 'self-insert-command 1)))
508 (should (string= "{ tee }" (buffer-string)))
509 (should (overlay-buffer tpl))
510 (company-call 'template-forward-field)
511 (should (= 7 (point)))
512 (should (null (overlay-buffer tpl))))))
514 (defun company-call (name &rest args)
515 (let* ((maybe (intern (format "company-%s" name)))
516 (command (if (fboundp maybe) maybe name)))
517 (let ((this-command command))
518 (run-hooks 'pre-command-hook))
520 (let ((this-command command))
521 (run-hooks 'post-command-hook))))
523 (ert-deftest company-template-c-like-templatify ()
525 (let ((text "foo(int a, short b)"))
527 (company-template-c-like-templatify text)
528 (should (equal "foo(arg0, arg1)" (buffer-string)))
529 (should (looking-at "arg0"))
530 (should (equal "int a"
531 (overlay-get (company-template-field-at) 'display))))))
533 (ert-deftest company-template-c-like-templatify-trims-after-closing-paren ()
535 (let ((text "foo(int a, short b)!@ #1334 a"))
537 (company-template-c-like-templatify text)
538 (should (equal "foo(arg0, arg1)" (buffer-string)))
539 (should (looking-at "arg0")))))
543 (defmacro company-elisp-with-buffer (contents &rest body)
547 (setq major-mode 'emacs-lisp-mode)
548 (re-search-backward "|")
550 (let ((company-elisp-detect-function-context t))
553 (ert-deftest company-elisp-candidates-predicate ()
554 (company-elisp-with-buffer
556 (should (eq (company-elisp--candidates-predicate "ba")
558 (should (eq (let (company-elisp-detect-function-context)
559 (company-elisp--candidates-predicate "ba"))
560 'company-elisp--predicate)))
561 (company-elisp-with-buffer
563 (should (eq (company-elisp--candidates-predicate "foo")
565 (should (eq (let (company-elisp-detect-function-context)
566 (company-elisp--candidates-predicate "foo"))
567 'company-elisp--predicate)))
568 (company-elisp-with-buffer
570 (should (eq (company-elisp--candidates-predicate "b")
571 'company-elisp--predicate))))
573 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
574 (company-elisp-with-buffer
575 "(def foo () \"Doo be doo `ide|"
576 (should (eq 'company-elisp--predicate
577 (company-elisp--candidates-predicate "ide")))))
579 ;; This one's also an integration test.
580 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
581 (let ((company-elisp-detect-function-context t)
582 (obarray [when what whelp])
586 (company-elisp-with-buffer
587 "(let ((foo 7) (wh| )))"
588 (should (equal '("what" "whelp")
589 (company-elisp-candidates "wh"))))
590 (company-elisp-with-buffer
591 "(cond ((null nil) (wh| )))"
592 (should (equal '("when")
593 (company-elisp-candidates "wh"))))))
595 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
596 (loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
597 ("(let (foo (bar|" "bar" boundp)
598 ("(let (foo) (bar|" "bar" fboundp))
600 (eval `(company-elisp-with-buffer
602 (should (eq ',predicate
603 (company-elisp--candidates-predicate ,prefix)))))))
605 (ert-deftest company-elisp-finds-vars ()
606 (let ((obarray [boo bar baz backquote])
610 (should (equal '("bar" "baz")
611 (company-elisp--globals "ba" 'boundp)))))
613 (ert-deftest company-elisp-finds-functions ()
614 (let ((obarray [when what whelp])
617 (should (equal '("when")
618 (company-elisp--globals "wh" 'fboundp)))))
620 (ert-deftest company-elisp-finds-things ()
621 (let ((obarray [when what whelp])
624 (should (equal '("what" "whelp" "when")
625 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
628 (ert-deftest company-elisp-locals-vars ()
629 (company-elisp-with-buffer
630 "(let ((foo 5) (bar 6))
631 (cl-labels ((borg ()))
634 (should (equal '("bar" "baz" "boo")
635 (company-elisp--locals "b" nil)))))
637 (ert-deftest company-elisp-locals-single-var ()
638 (company-elisp-with-buffer
642 (should (equal '("itk" "item")
643 (company-elisp--locals "it" nil)))))
645 (ert-deftest company-elisp-locals-funs ()
646 (company-elisp-with-buffer
647 "(cl-labels ((foo ())
651 (should (equal '("fee" "foo")
652 (sort (company-elisp--locals "f" t) 'string<)))))
654 (ert-deftest company-elisp-locals-skips-current-varlist ()
655 (company-elisp-with-buffer
658 (should (null (company-elisp--locals "f" nil)))))
660 (ert-deftest company-elisp-show-locals-first ()
661 (company-elisp-with-buffer
666 (let ((obarray [float-pi]))
667 (let (company-elisp-show-locals-first)
668 (should (eq nil (company-elisp 'sorted))))
669 (let ((company-elisp-show-locals-first t))
670 (should (eq t (company-elisp 'sorted)))
671 (should (equal '("flee" "floo" "flop" "float-pi")
672 (company-elisp-candidates "fl")))))))
674 (ert-deftest company-elisp-candidates-no-duplicates ()
675 (company-elisp-with-buffer
678 (let ((obarray [float-pi])
679 (company-elisp-show-locals-first t))
680 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
682 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
683 (company-elisp-with-buffer
685 (should (null (company-elisp 'prefix)))))
687 (ert-deftest company-elisp-should-complete-def-call ()
688 (company-elisp-with-buffer
690 (should (equal "defu" (company-elisp 'prefix)))))
692 (ert-deftest company-elisp-should-complete-in-defvar ()
693 ;; It will also complete the var name, at least for now.
694 (company-elisp-with-buffer
696 (should (equal "de" (company-elisp 'prefix)))))
698 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
699 (company-elisp-with-buffer
700 "(defsubst foobar (ba|"
701 (should (null (company-elisp 'prefix)))))
703 (ert-deftest company-elisp-prefix-in-defun-body ()
704 (company-elisp-with-buffer
706 (should (equal "" (company-elisp 'prefix)))))
710 (ert-deftest company-clang-objc-templatify ()
712 (let ((text "createBookWithTitle:andAuthor:"))
714 (company-clang-objc-templatify text)
715 (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
716 (should (looking-at "arg0"))
717 (should (null (overlay-get (company-template-field-at) 'display))))))