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-begin-backend-failure-doesnt-break-company-backends ()
73 (company-begin-backend (lambda (command &rest ignore))))
74 (let (company-frontends
76 (list (lambda (command &optional arg)
79 (candidates '("a" "ab" "ac")))))))
81 (company-call 'complete))
82 (should (eq 3 company-candidates-length)))))
84 (ert-deftest company-require-match-explicit ()
88 (let (company-frontends
89 (company-require-match 'company-explicit-action-p)
91 (list (lambda (command &optional arg)
93 (prefix (buffer-substring (point-min) (point)))
94 (candidates '("abc" "abd")))))))
97 (let ((last-command-event ?e))
98 (company-call 'self-insert-command 1))
99 (should (eq 2 company-candidates-length))
100 (should (eq 3 (point))))))
102 (ert-deftest company-dont-require-match-when-idle ()
106 (let (company-frontends
107 (company-require-match 'company-explicit-action-p)
109 (list (lambda (command &optional arg)
111 (prefix (buffer-substring (point-min) (point)))
112 (candidates '("abc" "abd")))))))
113 (company-idle-begin (current-buffer) (selected-window)
114 (buffer-chars-modified-tick) (point))
115 (let ((last-command-event ?e))
116 (company-call 'self-insert-command 1))
117 (should (eq nil company-candidates-length))
118 (should (eq 4 (point))))))
120 (ert-deftest company-should-complete-whitelist ()
124 (let (company-frontends
125 company-begin-commands
127 (list (lambda (command &optional arg)
129 (prefix (buffer-substring (point-min) (point)))
130 (candidates '("abc" "abd")))))))
131 (let ((company-continue-commands nil))
134 (company-call 'backward-delete-char 1)
135 (should (null company-candidates-length)))
136 (let ((company-continue-commands '(backward-delete-char)))
139 (company-call 'backward-delete-char 1)
140 (should (eq 2 company-candidates-length))))))
142 (ert-deftest company-should-complete-blacklist ()
146 (let (company-frontends
147 company-begin-commands
149 (list (lambda (command &optional arg)
151 (prefix (buffer-substring (point-min) (point)))
152 (candidates '("abc" "abd")))))))
153 (let ((company-continue-commands '(not backward-delete-char)))
156 (company-call 'backward-delete-char 1)
157 (should (null company-candidates-length)))
158 (let ((company-continue-commands '(not backward-delete-char-untabify)))
161 (company-call 'backward-delete-char 1)
162 (should (eq 2 company-candidates-length))))))
164 (ert-deftest company-auto-complete-explicit ()
168 (let (company-frontends
169 (company-auto-complete 'company-explicit-action-p)
170 (company-auto-complete-chars '(? ))
172 (list (lambda (command &optional arg)
174 (prefix (buffer-substring (point-min) (point)))
175 (candidates '("abcd" "abef")))))))
178 (let ((last-command-event ? ))
179 (company-call 'self-insert-command 1))
180 (should (string= "abcd " (buffer-string))))))
182 (ert-deftest company-no-auto-complete-when-idle ()
186 (let (company-frontends
187 (company-auto-complete 'company-explicit-action-p)
188 (company-auto-complete-chars '(? ))
190 (list (lambda (command &optional arg)
192 (prefix (buffer-substring (point-min) (point)))
193 (candidates '("abcd" "abef")))))))
194 (company-idle-begin (current-buffer) (selected-window)
195 (buffer-chars-modified-tick) (point))
196 (let ((last-command-event ? ))
197 (company-call 'self-insert-command 1))
198 (should (string= "ab " (buffer-string))))))
200 (ert-deftest company-clears-explicit-action-when-no-matches ()
203 (let (company-frontends
205 (company-call 'manual-begin) ;; fails
206 (should (null company-candidates))
207 (should (null (company-explicit-action-p))))))
209 (ert-deftest company-ignore-case-replaces-prefix ()
212 (let (company-frontends
213 company-end-of-buffer-workaround
215 (list (lambda (command &optional arg)
217 (prefix (buffer-substring (point-min) (point)))
218 (candidates '("abcd" "abef"))
223 (should (string= "ab" (buffer-string)))
225 (insert "A") ; hack, to keep it in one test
226 (company-complete-selection)
227 (should (string= "abcd" (buffer-string))))))
229 (ert-deftest company-ignore-case-with-keep-prefix ()
233 (let (company-frontends
235 (list (lambda (command &optional arg)
237 (prefix (buffer-substring (point-min) (point)))
238 (candidates '("abcd" "abef"))
239 (ignore-case 'keep-prefix))))))
242 (company-complete-selection)
243 (should (string= "ABcd" (buffer-string))))))
245 (ert-deftest company-non-prefix-completion ()
249 (let (company-frontends
250 company-end-of-buffer-workaround
252 (list (lambda (command &optional arg)
254 (prefix (buffer-substring (point-min) (point)))
255 (candidates '("tea-cup" "teal-color")))))))
258 (should (string= "tc" (buffer-string)))
259 (company-complete-selection)
260 (should (string= "tea-cup" (buffer-string))))))
262 (ert-deftest company-pseudo-tooltip-does-not-get-displaced ()
265 (save-window-excursion
266 (set-window-buffer nil (current-buffer))
267 (save-excursion (insert " ff"))
269 (let ((company-frontends '(company-pseudo-tooltip-frontend))
270 (company-begin-commands '(self-insert-command))
272 (list (lambda (c &optional arg)
273 (case c (prefix "") (candidates '("a" "b" "c")))))))
275 (company-call 'complete))
276 (company-call 'open-line 1)
277 (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
279 (ert-deftest company-pseudo-tooltip-show ()
282 (save-window-excursion
283 (set-window-buffer nil (current-buffer))
284 (insert "aaaa\n bb\nccccccc\nddd")
285 (search-backward "bb")
286 (let ((col (company--column))
287 (company-candidates-length 2)
288 (company-candidates '("123" "45")))
289 (company-pseudo-tooltip-show (company--row) col 0)
290 (let ((ov company-pseudo-tooltip-overlay))
292 (should (eq (overlay-get ov 'company-width) 5))
294 (should (eq (overlay-get ov 'company-height) company-tooltip-limit))
295 (should (eq (overlay-get ov 'company-column) col))
296 (should (string= (overlay-get ov 'company-after)
297 " 123 \nc 45 c\nddd\n")))))))
299 (ert-deftest company-preview-show-with-annotations ()
302 (save-window-excursion
303 (set-window-buffer nil (current-buffer))
304 (save-excursion (insert "\n"))
305 (let ((company-candidates-length 1)
306 (company-candidates '("123")))
307 (company-preview-show-at-point (point))
308 (let ((ov company-preview-overlay))
309 (should (string= (overlay-get ov 'display) "123\n")))))))
311 (ert-deftest company-pseudo-tooltip-show-with-annotations ()
314 (save-window-excursion
315 (set-window-buffer nil (current-buffer))
317 (save-excursion (insert "\n"))
318 (let ((company-candidates-length 2)
319 (company-backend (lambda (action &optional arg &rest _ignore)
320 (when (eq action 'annotation)
321 (cdr (assoc arg '(("123" . "(4)")))))))
322 (company-candidates '("123" "45")))
323 (company-pseudo-tooltip-show-at-point (point))
324 (let ((ov company-pseudo-tooltip-overlay))
326 (should (eq (overlay-get ov 'company-width) 8))
327 (should (string= (overlay-get ov 'company-after)
328 " 123(4) \n 45 \n")))))))
330 (ert-deftest company-create-lines-shows-numbers ()
331 (let ((company-show-numbers t)
332 (company-candidates '("x" "y" "z"))
333 (company-candidates-length 3))
334 (should (equal '(" x 1 " " y 2 " " z 3 ")
335 (company--create-lines 0 999)))))
337 (ert-deftest company-column-with-composition ()
340 (compose-region 1 (1+ (length "lambda")) "\\")
341 (should (= (company--column) 4))))
343 (ert-deftest company-column-with-line-prefix ()
346 (put-text-property (point-min) (point) 'line-prefix " ")
347 (should (= (company--column) 5))))
349 (ert-deftest company-column-wth-line-prefix-on-empty-line ()
353 (put-text-property (point-min) (point-max) 'line-prefix " ")
354 (should (= (company--column) 2))))
356 (ert-deftest company-plainify ()
358 (should (equal-including-properties
359 (company-plainify "\tabc\td\t")
363 (should (equal-including-properties
364 (company-plainify (propertize "foobar" 'line-prefix "-*-"))
367 (ert-deftest company-modify-line ()
368 (let ((str "-*-foobar"))
369 (should (equal-including-properties
370 (company-modify-line str "zz" 4)
372 (should (equal-including-properties
373 (company-modify-line str "xx" 0)
375 (should (equal-including-properties
376 (company-modify-line str "zz" 10)
379 (ert-deftest company-scrollbar-bounds ()
380 (should (equal nil (company--scrollbar-bounds 0 3 3)))
381 (should (equal nil (company--scrollbar-bounds 0 4 3)))
382 (should (equal '(0 . 0) (company--scrollbar-bounds 0 1 2)))
383 (should (equal '(1 . 1) (company--scrollbar-bounds 2 2 4)))
384 (should (equal '(2 . 3) (company--scrollbar-bounds 7 4 12)))
385 (should (equal '(1 . 2) (company--scrollbar-bounds 3 4 12)))
386 (should (equal '(1 . 3) (company--scrollbar-bounds 4 5 11))))
390 (ert-deftest company-template-removed-after-the-last-jump ()
394 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
398 (company-template-add-field tpl (point) "foo")))
399 (company-call 'template-forward-field)
400 (should (= 3 (point)))
401 (company-call 'template-forward-field)
402 (should (= 7 (point)))
403 (company-call 'template-forward-field)
404 (should (= 11 (point)))
405 (should (zerop (length (overlay-get tpl 'company-template-fields))))
406 (should (null (overlay-buffer tpl))))))
408 (ert-deftest company-template-removed-after-input-and-jump ()
412 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
415 (company-template-add-field tpl (point) "bar"))
416 (company-call 'template-move-to-first tpl)
417 (should (= 3 (point)))
418 (dolist (c (string-to-list "tee"))
419 (let ((last-command-event c))
420 (company-call 'self-insert-command 1)))
421 (should (string= "{ tee }" (buffer-string)))
422 (should (overlay-buffer tpl))
423 (company-call 'template-forward-field)
424 (should (= 7 (point)))
425 (should (null (overlay-buffer tpl))))))
427 (defun company-call (name &rest args)
428 (let* ((maybe (intern (format "company-%s" name)))
429 (command (if (fboundp maybe) maybe name)))
430 (let ((this-command command))
431 (run-hooks 'pre-command-hook))
433 (let ((this-command command))
434 (run-hooks 'post-command-hook))))
436 (ert-deftest company-template-c-like-templatify ()
438 (let ((text "foo(int a, short b)"))
440 (company-template-c-like-templatify text)
441 (should (equal "foo(arg0, arg1)" (buffer-string)))
442 (should (looking-at "arg0"))
443 (should (equal "int a"
444 (overlay-get (company-template-field-at) 'display))))))
446 (ert-deftest company-template-c-like-templatify-trims-after-closing-paren ()
448 (let ((text "foo(int a, short b)!@ #1334 a"))
450 (company-template-c-like-templatify text)
451 (should (equal "foo(arg0, arg1)" (buffer-string)))
452 (should (looking-at "arg0")))))
456 (defmacro company-elisp-with-buffer (contents &rest body)
460 (setq major-mode 'emacs-lisp-mode)
461 (re-search-backward "|")
463 (let ((company-elisp-detect-function-context t))
466 (ert-deftest company-elisp-candidates-predicate ()
467 (company-elisp-with-buffer
469 (should (eq (company-elisp--candidates-predicate "ba")
471 (should (eq (let (company-elisp-detect-function-context)
472 (company-elisp--candidates-predicate "ba"))
473 'company-elisp--predicate)))
474 (company-elisp-with-buffer
476 (should (eq (company-elisp--candidates-predicate "foo")
478 (should (eq (let (company-elisp-detect-function-context)
479 (company-elisp--candidates-predicate "foo"))
480 'company-elisp--predicate)))
481 (company-elisp-with-buffer
483 (should (eq (company-elisp--candidates-predicate "b")
484 'company-elisp--predicate))))
486 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
487 (company-elisp-with-buffer
488 "(def foo () \"Doo be doo `ide|"
489 (should (eq 'company-elisp--predicate
490 (company-elisp--candidates-predicate "ide")))))
492 ;; This one's also an integration test.
493 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
494 (let ((company-elisp-detect-function-context t)
495 (obarray [when what whelp])
499 (company-elisp-with-buffer
500 "(let ((foo 7) (wh| )))"
501 (should (equal '("what" "whelp")
502 (company-elisp-candidates "wh"))))
503 (company-elisp-with-buffer
504 "(cond ((null nil) (wh| )))"
505 (should (equal '("when")
506 (company-elisp-candidates "wh"))))))
508 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
509 (loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
510 ("(let (foo (bar|" "bar" boundp)
511 ("(let (foo) (bar|" "bar" fboundp))
513 (eval `(company-elisp-with-buffer
515 (should (eq ',predicate
516 (company-elisp--candidates-predicate ,prefix)))))))
518 (ert-deftest company-elisp-finds-vars ()
519 (let ((obarray [boo bar baz backquote])
523 (should (equal '("bar" "baz")
524 (company-elisp--globals "ba" 'boundp)))))
526 (ert-deftest company-elisp-finds-functions ()
527 (let ((obarray [when what whelp])
530 (should (equal '("when")
531 (company-elisp--globals "wh" 'fboundp)))))
533 (ert-deftest company-elisp-finds-things ()
534 (let ((obarray [when what whelp])
537 (should (equal '("what" "whelp" "when")
538 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
541 (ert-deftest company-elisp-locals-vars ()
542 (company-elisp-with-buffer
543 "(let ((foo 5) (bar 6))
544 (cl-labels ((borg ()))
547 (should (equal '("bar" "baz" "boo")
548 (company-elisp--locals "b" nil)))))
550 (ert-deftest company-elisp-locals-single-var ()
551 (company-elisp-with-buffer
555 (should (equal '("itk" "item")
556 (company-elisp--locals "it" nil)))))
558 (ert-deftest company-elisp-locals-funs ()
559 (company-elisp-with-buffer
560 "(cl-labels ((foo ())
564 (should (equal '("fee" "foo")
565 (sort (company-elisp--locals "f" t) 'string<)))))
567 (ert-deftest company-elisp-locals-skips-current-varlist ()
568 (company-elisp-with-buffer
571 (should (null (company-elisp--locals "f" nil)))))
573 (ert-deftest company-elisp-show-locals-first ()
574 (company-elisp-with-buffer
579 (let ((obarray [float-pi]))
580 (let (company-elisp-show-locals-first)
581 (should (eq nil (company-elisp 'sorted))))
582 (let ((company-elisp-show-locals-first t))
583 (should (eq t (company-elisp 'sorted)))
584 (should (equal '("flee" "floo" "flop" "float-pi")
585 (company-elisp-candidates "fl")))))))
587 (ert-deftest company-elisp-candidates-no-duplicates ()
588 (company-elisp-with-buffer
591 (let ((obarray [float-pi])
592 (company-elisp-show-locals-first t))
593 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
595 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
596 (company-elisp-with-buffer
598 (should (null (company-elisp 'prefix)))))
600 (ert-deftest company-elisp-should-complete-def-call ()
601 (company-elisp-with-buffer
603 (should (equal "defu" (company-elisp 'prefix)))))
605 (ert-deftest company-elisp-should-complete-in-defvar ()
606 ;; It will also complete the var name, at least for now.
607 (company-elisp-with-buffer
609 (should (equal "de" (company-elisp 'prefix)))))
611 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
612 (company-elisp-with-buffer
613 "(defsubst foobar (ba|"
614 (should (null (company-elisp 'prefix)))))
616 (ert-deftest company-elisp-prefix-in-defun-body ()
617 (company-elisp-with-buffer
619 (should (equal "" (company-elisp 'prefix)))))
623 (ert-deftest company-clang-objc-templatify ()
625 (let ((text "createBookWithTitle:andAuthor:"))
627 (company-clang-objc-templatify text)
628 (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
629 (should (looking-at "arg0"))
630 (should (null (overlay-get (company-template-field-at) 'display))))))