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-auto-complete-explicit ()
124 (let (company-frontends
125 (company-auto-complete 'company-explicit-action-p)
126 (company-auto-complete-chars '(? ))
128 (list (lambda (command &optional arg)
130 (prefix (buffer-substring (point-min) (point)))
131 (candidates '("abcd" "abef")))))))
134 (let ((last-command-event ? ))
135 (company-call 'self-insert-command 1))
136 (should (string= "abcd " (buffer-string))))))
138 (ert-deftest company-no-auto-complete-when-idle ()
142 (let (company-frontends
143 (company-auto-complete 'company-explicit-action-p)
144 (company-auto-complete-chars '(? ))
146 (list (lambda (command &optional arg)
148 (prefix (buffer-substring (point-min) (point)))
149 (candidates '("abcd" "abef")))))))
150 (company-idle-begin (current-buffer) (selected-window)
151 (buffer-chars-modified-tick) (point))
152 (let ((last-command-event ? ))
153 (company-call 'self-insert-command 1))
154 (should (string= "ab " (buffer-string))))))
156 (ert-deftest company-clears-explicit-action-when-no-matches ()
159 (let (company-frontends
161 (company-call 'manual-begin) ;; fails
162 (should (null company-candidates))
163 (should (null (company-explicit-action-p))))))
165 (ert-deftest company-ignore-case-replaces-prefix ()
168 (let (company-frontends
169 company-end-of-buffer-workaround
171 (list (lambda (command &optional arg)
173 (prefix (buffer-substring (point-min) (point)))
174 (candidates '("abcd" "abef"))
179 (should (string= "ab" (buffer-string)))
181 (insert "A") ; hack, to keep it in one test
182 (company-complete-selection)
183 (should (string= "abcd" (buffer-string))))))
185 (ert-deftest company-ignore-case-with-keep-prefix ()
189 (let (company-frontends
191 (list (lambda (command &optional arg)
193 (prefix (buffer-substring (point-min) (point)))
194 (candidates '("abcd" "abef"))
195 (ignore-case 'keep-prefix))))))
198 (company-complete-selection)
199 (should (string= "ABcd" (buffer-string))))))
201 (ert-deftest company-non-prefix-completion ()
205 (let (company-frontends
206 company-end-of-buffer-workaround
208 (list (lambda (command &optional arg)
210 (prefix (buffer-substring (point-min) (point)))
211 (candidates '("tea-cup" "teal-color")))))))
214 (should (string= "tc" (buffer-string)))
215 (company-complete-selection)
216 (should (string= "tea-cup" (buffer-string))))))
218 (ert-deftest company-pseudo-tooltip-does-not-get-displaced ()
221 (save-window-excursion
222 (set-window-buffer nil (current-buffer))
223 (save-excursion (insert " ff"))
225 (let ((company-frontends '(company-pseudo-tooltip-frontend))
226 (company-begin-commands '(self-insert-command))
228 (list (lambda (c &optional arg)
229 (case c (prefix "") (candidates '("a" "b" "c")))))))
231 (company-call 'complete))
232 (company-call 'open-line 1)
233 (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
235 (ert-deftest company-pseudo-tooltip-overlay-show ()
238 (save-window-excursion
239 (set-window-buffer nil (current-buffer))
240 (insert "aaaa\n bb\nccccccc\nddd")
241 (search-backward "bb")
242 (let ((col (company--column))
243 (company-candidates-length 2)
244 (company-candidates '("123" "45")))
245 (company-pseudo-tooltip-show (company--row) col 0)
246 (let ((ov company-pseudo-tooltip-overlay))
248 (should (eq (overlay-get ov 'company-width) 5))
250 (should (eq (overlay-get ov 'company-height) company-tooltip-limit))
251 (should (eq (overlay-get ov 'company-column) col))
252 (should (string= (overlay-get ov 'company-after)
253 " 123 \nc 45 c\nddd\n")))))))
255 (ert-deftest company-create-lines-shows-numbers ()
256 (let ((company-show-numbers t)
257 (company-candidates '("x" "y" "z"))
258 (company-candidates-length 3))
259 (should (equal '(" x 1 " " y 2 " " z 3 ")
260 (company--create-lines 0 999)))))
262 (ert-deftest company-column-with-composition ()
265 (compose-region 1 (1+ (length "lambda")) "\\")
266 (should (= (company--column) 4))))
268 (ert-deftest company-column-with-line-prefix ()
271 (put-text-property (point-min) (point) 'line-prefix " ")
272 (should (= (company--column) 5))))
274 (ert-deftest company-column-wth-line-prefix-on-empty-line ()
278 (put-text-property (point-min) (point-max) 'line-prefix " ")
279 (should (= (company--column) 2))))
281 (ert-deftest company-plainify ()
283 (should (equal-including-properties
284 (company-plainify "\tabc\td\t")
288 (should (equal-including-properties
289 (company-plainify (propertize "foobar" 'line-prefix "-*-"))
292 (ert-deftest company-modify-line ()
293 (let ((str "-*-foobar"))
294 (should (equal-including-properties
295 (company-modify-line str "zz" 4)
297 (should (equal-including-properties
298 (company-modify-line str "xx" 0)
300 (should (equal-including-properties
301 (company-modify-line str "zz" 10)
304 (ert-deftest company-scrollbar-bounds ()
305 (should (equal nil (company--scrollbar-bounds 0 3 3)))
306 (should (equal nil (company--scrollbar-bounds 0 4 3)))
307 (should (equal '(0 . 0) (company--scrollbar-bounds 0 1 2)))
308 (should (equal '(1 . 1) (company--scrollbar-bounds 2 2 4)))
309 (should (equal '(2 . 3) (company--scrollbar-bounds 7 4 12)))
310 (should (equal '(1 . 2) (company--scrollbar-bounds 3 4 12)))
311 (should (equal '(1 . 3) (company--scrollbar-bounds 4 5 11))))
315 (ert-deftest company-template-removed-after-the-last-jump ()
319 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
323 (company-template-add-field tpl (point) "foo")))
324 (company-call 'template-forward-field)
325 (should (= 3 (point)))
326 (company-call 'template-forward-field)
327 (should (= 7 (point)))
328 (company-call 'template-forward-field)
329 (should (= 11 (point)))
330 (should (zerop (length (overlay-get tpl 'company-template-fields))))
331 (should (null (overlay-buffer tpl))))))
333 (ert-deftest company-template-removed-after-input-and-jump ()
337 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
340 (company-template-add-field tpl (point) "bar"))
341 (company-call 'template-move-to-first tpl)
342 (should (= 3 (point)))
343 (dolist (c (string-to-list "tee"))
344 (let ((last-command-event c))
345 (company-call 'self-insert-command 1)))
346 (should (string= "{ tee }" (buffer-string)))
347 (should (overlay-buffer tpl))
348 (company-call 'template-forward-field)
349 (should (= 7 (point)))
350 (should (null (overlay-buffer tpl))))))
352 (defun company-call (name &rest args)
353 (let* ((maybe (intern (format "company-%s" name)))
354 (command (if (fboundp maybe) maybe name)))
356 (let ((this-command command))
357 (run-hooks 'post-command-hook))))
359 (ert-deftest company-template-c-like-templatify ()
361 (let ((text "foo(int a, short b)"))
363 (company-template-c-like-templatify text)
364 (should (equal "foo(arg0, arg1)" (buffer-string)))
365 (should (looking-at "arg0"))
366 (should (equal "int a"
367 (overlay-get (company-template-field-at) 'display))))))
369 (ert-deftest company-template-c-like-templatify-trims-after-closing-paren ()
371 (let ((text "foo(int a, short b)!@ #1334 a"))
373 (company-template-c-like-templatify text)
374 (should (equal "foo(arg0, arg1)" (buffer-string)))
375 (should (looking-at "arg0")))))
379 (defmacro company-elisp-with-buffer (contents &rest body)
383 (setq major-mode 'emacs-lisp-mode)
384 (re-search-backward "|")
386 (let ((company-elisp-detect-function-context t))
389 (ert-deftest company-elisp-candidates-predicate ()
390 (company-elisp-with-buffer
392 (should (eq (company-elisp--candidates-predicate "ba")
394 (should (eq (let (company-elisp-detect-function-context)
395 (company-elisp--candidates-predicate "ba"))
396 'company-elisp--predicate)))
397 (company-elisp-with-buffer
399 (should (eq (company-elisp--candidates-predicate "foo")
401 (should (eq (let (company-elisp-detect-function-context)
402 (company-elisp--candidates-predicate "foo"))
403 'company-elisp--predicate)))
404 (company-elisp-with-buffer
406 (should (eq (company-elisp--candidates-predicate "b")
407 'company-elisp--predicate))))
409 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
410 (company-elisp-with-buffer
411 "(def foo () \"Doo be doo `ide|"
412 (should (eq 'company-elisp--predicate
413 (company-elisp--candidates-predicate "ide")))))
415 ;; This one's also an integration test.
416 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
417 (let ((company-elisp-detect-function-context t)
418 (obarray [when what whelp])
422 (company-elisp-with-buffer
423 "(let ((foo 7) (wh| )))"
424 (should (equal '("what" "whelp")
425 (company-elisp-candidates "wh"))))
426 (company-elisp-with-buffer
427 "(cond ((null nil) (wh| )))"
428 (should (equal '("when")
429 (company-elisp-candidates "wh"))))))
431 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
432 (loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
433 ("(let (foo (bar|" "bar" boundp)
434 ("(let (foo) (bar|" "bar" fboundp))
436 (eval `(company-elisp-with-buffer
438 (should (eq ',predicate
439 (company-elisp--candidates-predicate ,prefix)))))))
441 (ert-deftest company-elisp-finds-vars ()
442 (let ((obarray [boo bar baz backquote])
446 (should (equal '("bar" "baz")
447 (company-elisp--globals "ba" 'boundp)))))
449 (ert-deftest company-elisp-finds-functions ()
450 (let ((obarray [when what whelp])
453 (should (equal '("when")
454 (company-elisp--globals "wh" 'fboundp)))))
456 (ert-deftest company-elisp-finds-things ()
457 (let ((obarray [when what whelp])
460 (should (equal '("what" "whelp" "when")
461 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
464 (ert-deftest company-elisp-locals-vars ()
465 (company-elisp-with-buffer
466 "(let ((foo 5) (bar 6))
467 (cl-labels ((borg ()))
470 (should (equal '("bar" "baz" "boo")
471 (company-elisp--locals "b" nil)))))
473 (ert-deftest company-elisp-locals-single-var ()
474 (company-elisp-with-buffer
478 (should (equal '("itk" "item")
479 (company-elisp--locals "it" nil)))))
481 (ert-deftest company-elisp-locals-funs ()
482 (company-elisp-with-buffer
483 "(cl-labels ((foo ())
487 (should (equal '("fee" "foo")
488 (sort (company-elisp--locals "f" t) 'string<)))))
490 (ert-deftest company-elisp-locals-skips-current-varlist ()
491 (company-elisp-with-buffer
494 (should (null (company-elisp--locals "f" nil)))))
496 (ert-deftest company-elisp-show-locals-first ()
497 (company-elisp-with-buffer
502 (let ((obarray [float-pi]))
503 (let (company-elisp-show-locals-first)
504 (should (eq nil (company-elisp 'sorted))))
505 (let ((company-elisp-show-locals-first t))
506 (should (eq t (company-elisp 'sorted)))
507 (should (equal '("flee" "floo" "flop" "float-pi")
508 (company-elisp-candidates "fl")))))))
510 (ert-deftest company-elisp-candidates-no-duplicates ()
511 (company-elisp-with-buffer
514 (let ((obarray [float-pi])
515 (company-elisp-show-locals-first t))
516 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
518 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
519 (company-elisp-with-buffer
521 (should (null (company-elisp 'prefix)))))
523 (ert-deftest company-elisp-should-complete-def-call ()
524 (company-elisp-with-buffer
526 (should (equal "defu" (company-elisp 'prefix)))))
528 (ert-deftest company-elisp-should-complete-in-defvar ()
529 ;; It will also complete the var name, at least for now.
530 (company-elisp-with-buffer
532 (should (equal "de" (company-elisp 'prefix)))))
534 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
535 (company-elisp-with-buffer
536 "(defsubst foobar (ba|"
537 (should (null (company-elisp 'prefix)))))
539 (ert-deftest company-elisp-prefix-in-defun-body ()
540 (company-elisp-with-buffer
542 (should (equal "" (company-elisp 'prefix)))))
546 (ert-deftest company-clang-objc-templatify ()
548 (let ((text "createBookWithTitle:andAuthor:"))
550 (company-clang-objc-templatify text)
551 (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
552 (should (looking-at "arg0"))
553 (should (null (overlay-get (company-template-field-at) 'display))))))