1 ;;; company-tests.el --- company-mode tests
3 ;; Copyright (C) 2011, 2013 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/>.
30 (require 'company-keywords)
34 (ert-deftest company-sorted-keywords ()
35 "Test that keywords in `company-keywords-alist' are in alphabetical order."
36 (dolist (pair company-keywords-alist)
37 (when (consp (cdr pair))
38 (let ((prev (cadr pair)))
39 (dolist (next (cddr pair))
40 (should (not (equal prev next)))
41 (should (string< prev next))
44 (ert-deftest company-good-prefix ()
45 (let ((company-minimum-prefix-length 5)
46 company--explicit-action)
47 (should (eq t (company--good-prefix-p "!@#$%")))
48 (should (eq nil (company--good-prefix-p "abcd")))
49 (should (eq nil (company--good-prefix-p 'stop)))
50 (should (eq t (company--good-prefix-p '("foo" . 5))))
51 (should (eq nil (company--good-prefix-p '("foo" . 4))))))
53 (ert-deftest company-multi-backend-with-lambdas ()
54 (let ((company-backend
55 (list (lambda (command &optional arg &rest ignore)
58 (candidates '("a" "b"))))
59 (lambda (command &optional arg &rest ignore)
62 (candidates '("c" "d")))))))
63 (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
65 (ert-deftest company-begin-backend-failure-doesnt-break-company-backends ()
70 (company-begin-backend (lambda (command &rest ignore))))
71 (let (company-frontends
73 (list (lambda (command &optional arg)
76 (candidates '("a" "ab" "ac")))))))
78 (company-call 'complete))
79 (should (eq 3 company-candidates-length)))))
81 (ert-deftest company-require-match-explicit ()
85 (let (company-frontends
86 (company-require-match 'company-explicit-action-p)
88 (list (lambda (command &optional arg)
90 (prefix (buffer-substring (point-min) (point)))
91 (candidates '("abc" "abd")))))))
94 (let ((last-command-event ?e))
95 (company-call 'self-insert-command 1))
96 (should (eq 2 company-candidates-length))
97 (should (eq 3 (point))))))
99 (ert-deftest company-dont-require-match-when-idle ()
103 (let (company-frontends
104 (company-require-match 'company-explicit-action-p)
106 (list (lambda (command &optional arg)
108 (prefix (buffer-substring (point-min) (point)))
109 (candidates '("abc" "abd")))))))
110 (company-idle-begin (current-buffer) (selected-window)
111 (buffer-chars-modified-tick) (point))
112 (let ((last-command-event ?e))
113 (company-call 'self-insert-command 1))
114 (should (eq nil company-candidates-length))
115 (should (eq 4 (point))))))
117 (ert-deftest company-auto-complete-explicit ()
121 (let (company-frontends
122 (company-auto-complete 'company-explicit-action-p)
123 (company-auto-complete-chars '(? ))
125 (list (lambda (command &optional arg)
127 (prefix (buffer-substring (point-min) (point)))
128 (candidates '("abcd" "abef")))))))
131 (let ((last-command-event ? ))
132 (company-call 'self-insert-command 1))
133 (should (string= "abcd " (buffer-string))))))
135 (ert-deftest company-no-auto-complete-when-idle ()
139 (let (company-frontends
140 (company-auto-complete 'company-explicit-action-p)
141 (company-auto-complete-chars '(? ))
143 (list (lambda (command &optional arg)
145 (prefix (buffer-substring (point-min) (point)))
146 (candidates '("abcd" "abef")))))))
147 (company-idle-begin (current-buffer) (selected-window)
148 (buffer-chars-modified-tick) (point))
149 (let ((last-command-event ? ))
150 (company-call 'self-insert-command 1))
151 (should (string= "ab " (buffer-string))))))
153 (ert-deftest company-clears-explicit-action-when-no-matches ()
156 (let (company-frontends
158 (company-call 'manual-begin) ;; fails
159 (should (null company-candidates))
160 (should (null (company-explicit-action-p))))))
162 (ert-deftest company-pseudo-tooltip-does-not-get-displaced ()
165 (save-window-excursion
166 (set-window-buffer nil (current-buffer))
167 (save-excursion (insert " ff"))
169 (let ((company-frontends '(company-pseudo-tooltip-frontend))
170 (company-begin-commands '(self-insert-command))
172 (list (lambda (c &optional arg)
173 (case c (prefix "") (candidates '("a" "b" "c")))))))
175 (company-call 'complete))
176 (company-call 'open-line 1)
177 (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
179 (defun company-test-pseudo-tooltip-overlay-show ()
180 (save-window-excursion
181 (set-window-buffer nil (current-buffer))
182 (insert "aaaa\n bb\nccccc\nddd")
183 (search-backward "bb")
184 (let ((col-row (company--col-row))
185 (company-tooltip-limit 5)
186 (company-candidates-length 2)
187 (company-candidates '("123" "45")))
188 (company-pseudo-tooltip-show (cdr col-row) (car col-row) 0)
189 (let ((ov company-pseudo-tooltip-overlay))
190 (should (eq (overlay-get ov 'company-width) 3))
192 (should (eq (overlay-get ov 'company-height) 5))
193 (should (eq (overlay-get ov 'company-column) (car col-row)))
194 (should (string= (overlay-get ov 'company-before)
195 " 123\nc45 c\nddd\n"))))))
197 (ert-deftest company-pseudo-tooltip-overlay-show ()
200 (company-test-pseudo-tooltip-overlay-show)))
202 (ert-deftest company-pseudo-tooltip-overlay-show-with-header-line ()
205 (setq header-line-format "foo bar")
206 (company-test-pseudo-tooltip-overlay-show)))
210 (ert-deftest company-template-removed-after-the-last-jump ()
214 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
218 (company-template-add-field tpl (point) "foo")))
219 (company-call 'template-forward-field)
220 (should (= 3 (point)))
221 (company-call 'template-forward-field)
222 (should (= 7 (point)))
223 (company-call 'template-forward-field)
224 (should (= 11 (point)))
225 (should (zerop (length (overlay-get tpl 'company-template-fields))))
226 (should (null (overlay-buffer tpl))))))
228 (ert-deftest company-template-removed-after-input-and-jump ()
232 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
235 (company-template-add-field tpl (point) "bar"))
236 (company-call 'template-move-to-first tpl)
237 (should (= 3 (point)))
238 (dolist (c (string-to-list "tee"))
239 (let ((last-command-event c))
240 (company-call 'self-insert-command 1)))
241 (should (string= "{ tee }" (buffer-string)))
242 (should (overlay-buffer tpl))
243 (company-call 'template-forward-field)
244 (should (= 7 (point)))
245 (should (null (overlay-buffer tpl))))))
247 (defun company-call (name &rest args)
248 (let* ((maybe (intern (format "company-%s" name)))
249 (command (if (fboundp maybe) maybe name)))
251 (let ((this-command command))
252 (run-hooks 'post-command-hook))))
254 (ert-deftest company-template-c-like-templatify ()
256 (let ((text "foo(int a, short b)"))
258 (company-template-c-like-templatify text)
259 (should (equal "foo(arg0, arg1)" (buffer-string)))
260 (should (looking-at "arg0"))
261 (should (equal "int a"
262 (overlay-get (company-template-field-at) 'display))))))
266 (defmacro company-elisp-with-buffer (contents &rest body)
270 (setq major-mode 'emacs-lisp-mode)
271 (re-search-backward "|")
273 (let ((company-elisp-detect-function-context t))
276 (ert-deftest company-elisp-candidates-predicate ()
277 (company-elisp-with-buffer
279 (should (eq (company-elisp--candidates-predicate "ba")
281 (should (eq (let (company-elisp-detect-function-context)
282 (company-elisp--candidates-predicate "ba"))
283 'company-elisp--predicate)))
284 (company-elisp-with-buffer
286 (should (eq (company-elisp--candidates-predicate "foo")
288 (should (eq (let (company-elisp-detect-function-context)
289 (company-elisp--candidates-predicate "foo"))
290 'company-elisp--predicate)))
291 (company-elisp-with-buffer
293 (should (eq (company-elisp--candidates-predicate "b")
294 'company-elisp--predicate))))
296 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
297 (company-elisp-with-buffer
298 "(def foo () \"Doo be doo `ide|"
299 (should (eq 'company-elisp--predicate
300 (company-elisp--candidates-predicate "ide")))))
302 ;; This one's also an integration test.
303 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
304 (let ((company-elisp-detect-function-context t)
305 (obarray [when what whelp])
309 (company-elisp-with-buffer
310 "(let ((foo 7) (wh| )))"
311 (should (equal '("what" "whelp")
312 (company-elisp-candidates "wh"))))
313 (company-elisp-with-buffer
314 "(cond ((null nil) (wh| )))"
315 (should (equal '("when")
316 (company-elisp-candidates "wh"))))))
318 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
319 (loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
320 ("(let (foo (bar|" "bar" boundp)
321 ("(let (foo) (bar|" "bar" fboundp))
323 (eval `(company-elisp-with-buffer
325 (should (eq ',predicate
326 (company-elisp--candidates-predicate ,prefix)))))))
328 (ert-deftest company-elisp-finds-vars ()
329 (let ((obarray [boo bar baz backquote])
333 (should (equal '("bar" "baz")
334 (company-elisp--globals "ba" 'boundp)))))
336 (ert-deftest company-elisp-finds-functions ()
337 (let ((obarray [when what whelp])
340 (should (equal '("when")
341 (company-elisp--globals "wh" 'fboundp)))))
343 (ert-deftest company-elisp-finds-things ()
344 (let ((obarray [when what whelp])
347 (should (equal '("what" "whelp" "when")
348 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
351 (ert-deftest company-elisp-locals-vars ()
352 (company-elisp-with-buffer
353 "(let ((foo 5) (bar 6))
354 (cl-labels ((borg ()))
357 (should (equal '("bar" "baz" "boo")
358 (company-elisp--locals "b" nil)))))
360 (ert-deftest company-elisp-locals-single-var ()
361 (company-elisp-with-buffer
365 (should (equal '("itk" "item")
366 (company-elisp--locals "it" nil)))))
368 (ert-deftest company-elisp-locals-funs ()
369 (company-elisp-with-buffer
370 "(cl-labels ((foo ())
374 (should (equal '("fee" "foo")
375 (sort (company-elisp--locals "f" t) 'string<)))))
377 (ert-deftest company-elisp-locals-skips-current-varlist ()
378 (company-elisp-with-buffer
381 (should (null (company-elisp--locals "f" nil)))))
383 (ert-deftest company-elisp-show-locals-first ()
384 (company-elisp-with-buffer
389 (let ((obarray [float-pi]))
390 (let (company-elisp-show-locals-first)
391 (should (eq nil (company-elisp 'sorted))))
392 (let ((company-elisp-show-locals-first t))
393 (should (eq t (company-elisp 'sorted)))
394 (should (equal '("flee" "floo" "flop" "float-pi")
395 (company-elisp-candidates "fl")))))))
397 (ert-deftest company-elisp-candidates-no-duplicates ()
398 (company-elisp-with-buffer
401 (let ((obarray [float-pi])
402 (company-elisp-show-locals-first t))
403 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
405 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
406 (company-elisp-with-buffer
408 (should (null (company-elisp 'prefix)))))
410 (ert-deftest company-elisp-should-complete-def-call ()
411 (company-elisp-with-buffer
413 (should (equal "defu" (company-elisp 'prefix)))))
415 (ert-deftest company-elisp-should-complete-in-defvar ()
416 ;; It will also complete the var name, at least for now.
417 (company-elisp-with-buffer
419 (should (equal "de" (company-elisp 'prefix)))))
421 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
422 (company-elisp-with-buffer
423 "(defsubst foobar (ba|"
424 (should (null (company-elisp 'prefix)))))
426 (ert-deftest company-elisp-prefix-in-defun-body ()
427 (company-elisp-with-buffer
429 (should (equal "" (company-elisp 'prefix)))))
433 (ert-deftest company-clang-objc-templatify ()
435 (let ((text "createBookWithTitle:andAuthor:"))
437 (company-clang-objc-templatify text)
438 (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
439 (should (looking-at "arg0"))
440 (should (null (overlay-get (company-template-field-at) 'display))))))