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)
31 (require 'company-elisp)
32 (require 'company-clang)
36 (ert-deftest company-sorted-keywords ()
37 "Test that keywords in `company-keywords-alist' are in alphabetical order."
38 (dolist (pair company-keywords-alist)
39 (when (consp (cdr pair))
40 (let ((prev (cadr pair)))
41 (dolist (next (cddr pair))
42 (should (not (equal prev next)))
43 (should (string< prev next))
46 (ert-deftest company-good-prefix ()
47 (let ((company-minimum-prefix-length 5)
48 company--explicit-action)
49 (should (eq t (company--good-prefix-p "!@#$%")))
50 (should (eq nil (company--good-prefix-p "abcd")))
51 (should (eq nil (company--good-prefix-p 'stop)))
52 (should (eq t (company--good-prefix-p '("foo" . 5))))
53 (should (eq nil (company--good-prefix-p '("foo" . 4))))))
55 (ert-deftest company-multi-backend-with-lambdas ()
56 (let ((company-backend
57 (list (lambda (command &optional arg &rest ignore)
60 (candidates '("a" "b"))))
61 (lambda (command &optional arg &rest ignore)
64 (candidates '("c" "d")))))))
65 (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
67 (ert-deftest company-begin-backend-failure-doesnt-break-company-backends ()
72 (company-begin-backend (lambda (command &rest ignore))))
73 (let (company-frontends
75 (list (lambda (command &optional arg)
78 (candidates '("a" "ab" "ac")))))))
80 (company-call 'complete))
81 (should (eq 3 company-candidates-length)))))
83 (ert-deftest company-require-match-explicit ()
87 (let (company-frontends
88 (company-require-match 'company-explicit-action-p)
90 (list (lambda (command &optional arg)
92 (prefix (buffer-substring (point-min) (point)))
93 (candidates '("abc" "abd")))))))
96 (let ((last-command-event ?e))
97 (company-call 'self-insert-command 1))
98 (should (eq 2 company-candidates-length))
99 (should (eq 3 (point))))))
101 (ert-deftest company-dont-require-match-when-idle ()
105 (let (company-frontends
106 (company-require-match 'company-explicit-action-p)
108 (list (lambda (command &optional arg)
110 (prefix (buffer-substring (point-min) (point)))
111 (candidates '("abc" "abd")))))))
112 (company-idle-begin (current-buffer) (selected-window)
113 (buffer-chars-modified-tick) (point))
114 (let ((last-command-event ?e))
115 (company-call 'self-insert-command 1))
116 (should (eq nil company-candidates-length))
117 (should (eq 4 (point))))))
119 (ert-deftest company-auto-complete-explicit ()
123 (let (company-frontends
124 (company-auto-complete 'company-explicit-action-p)
125 (company-auto-complete-chars '(? ))
127 (list (lambda (command &optional arg)
129 (prefix (buffer-substring (point-min) (point)))
130 (candidates '("abcd" "abef")))))))
133 (let ((last-command-event ? ))
134 (company-call 'self-insert-command 1))
135 (should (string= "abcd " (buffer-string))))))
137 (ert-deftest company-no-auto-complete-when-idle ()
141 (let (company-frontends
142 (company-auto-complete 'company-explicit-action-p)
143 (company-auto-complete-chars '(? ))
145 (list (lambda (command &optional arg)
147 (prefix (buffer-substring (point-min) (point)))
148 (candidates '("abcd" "abef")))))))
149 (company-idle-begin (current-buffer) (selected-window)
150 (buffer-chars-modified-tick) (point))
151 (let ((last-command-event ? ))
152 (company-call 'self-insert-command 1))
153 (should (string= "ab " (buffer-string))))))
155 (ert-deftest company-clears-explicit-action-when-no-matches ()
158 (let (company-frontends
160 (company-call 'manual-begin) ;; fails
161 (should (null company-candidates))
162 (should (null (company-explicit-action-p))))))
164 (ert-deftest company-pseudo-tooltip-does-not-get-displaced ()
167 (save-window-excursion
168 (set-window-buffer nil (current-buffer))
169 (save-excursion (insert " ff"))
171 (let ((company-frontends '(company-pseudo-tooltip-frontend))
172 (company-begin-commands '(self-insert-command))
174 (list (lambda (c &optional arg)
175 (case c (prefix "") (candidates '("a" "b" "c")))))))
177 (company-call 'complete))
178 (company-call 'open-line 1)
179 (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
181 (defun company-test-pseudo-tooltip-overlay-show ()
182 (save-window-excursion
183 (set-window-buffer nil (current-buffer))
184 (insert "aaaa\n bb\nccccc\nddd")
185 (search-backward "bb")
186 (let ((col-row (company--col-row))
187 (company-candidates-length 2)
188 (company-candidates '("123" "45")))
189 (company-pseudo-tooltip-show (cdr col-row) (car col-row) 0)
190 (let ((ov company-pseudo-tooltip-overlay))
191 (should (eq (overlay-get ov 'company-width) 3))
193 (should (eq (overlay-get ov 'company-height) 10))
194 (should (eq (overlay-get ov 'company-column) (car col-row)))
195 (should (string= (overlay-get ov 'company-before)
196 " 123\nc45 c\nddd\n"))))))
198 (ert-deftest company-pseudo-tooltip-overlay-show ()
201 (company-test-pseudo-tooltip-overlay-show)))
203 (ert-deftest company-pseudo-tooltip-overlay-show-with-header-line ()
206 (setq header-line-format "foo bar")
207 (company-test-pseudo-tooltip-overlay-show)))
211 (ert-deftest company-template-removed-after-the-last-jump ()
215 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
219 (company-template-add-field tpl (point) "foo")))
220 (company-call 'template-forward-field)
221 (should (= 3 (point)))
222 (company-call 'template-forward-field)
223 (should (= 7 (point)))
224 (company-call 'template-forward-field)
225 (should (= 11 (point)))
226 (should (zerop (length (overlay-get tpl 'company-template-fields))))
227 (should (null (overlay-buffer tpl))))))
229 (ert-deftest company-template-removed-after-input-and-jump ()
233 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
236 (company-template-add-field tpl (point) "bar"))
237 (company-call 'template-move-to-first tpl)
238 (should (= 3 (point)))
239 (dolist (c (string-to-list "tee"))
240 (let ((last-command-event c))
241 (company-call 'self-insert-command 1)))
242 (should (string= "{ tee }" (buffer-string)))
243 (should (overlay-buffer tpl))
244 (company-call 'template-forward-field)
245 (should (= 7 (point)))
246 (should (null (overlay-buffer tpl))))))
248 (defun company-call (name &rest args)
249 (let* ((maybe (intern (format "company-%s" name)))
250 (command (if (fboundp maybe) maybe name)))
252 (let ((this-command command))
253 (run-hooks 'post-command-hook))))
255 (ert-deftest company-template-c-like-templatify ()
257 (let ((text "foo(int a, short b)"))
259 (company-template-c-like-templatify text)
260 (should (equal "foo(arg0, arg1)" (buffer-string)))
261 (should (looking-at "arg0"))
262 (should (equal "int a"
263 (overlay-get (company-template-field-at) 'display))))))
267 (defmacro company-elisp-with-buffer (contents &rest body)
271 (setq major-mode 'emacs-lisp-mode)
272 (re-search-backward "|")
274 (let ((company-elisp-detect-function-context t))
277 (ert-deftest company-elisp-candidates-predicate ()
278 (company-elisp-with-buffer
280 (should (eq (company-elisp--candidates-predicate "ba")
282 (should (eq (let (company-elisp-detect-function-context)
283 (company-elisp--candidates-predicate "ba"))
284 'company-elisp--predicate)))
285 (company-elisp-with-buffer
287 (should (eq (company-elisp--candidates-predicate "foo")
289 (should (eq (let (company-elisp-detect-function-context)
290 (company-elisp--candidates-predicate "foo"))
291 'company-elisp--predicate)))
292 (company-elisp-with-buffer
294 (should (eq (company-elisp--candidates-predicate "b")
295 'company-elisp--predicate))))
297 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
298 (company-elisp-with-buffer
299 "(def foo () \"Doo be doo `ide|"
300 (should (eq 'company-elisp--predicate
301 (company-elisp--candidates-predicate "ide")))))
303 ;; This one's also an integration test.
304 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
305 (let ((company-elisp-detect-function-context t)
306 (obarray [when what whelp])
310 (company-elisp-with-buffer
311 "(let ((foo 7) (wh| )))"
312 (should (equal '("what" "whelp")
313 (company-elisp-candidates "wh"))))
314 (company-elisp-with-buffer
315 "(cond ((null nil) (wh| )))"
316 (should (equal '("when")
317 (company-elisp-candidates "wh"))))))
319 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
320 (loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
321 ("(let (foo (bar|" "bar" boundp)
322 ("(let (foo) (bar|" "bar" fboundp))
324 (eval `(company-elisp-with-buffer
326 (should (eq ',predicate
327 (company-elisp--candidates-predicate ,prefix)))))))
329 (ert-deftest company-elisp-finds-vars ()
330 (let ((obarray [boo bar baz backquote])
334 (should (equal '("bar" "baz")
335 (company-elisp--globals "ba" 'boundp)))))
337 (ert-deftest company-elisp-finds-functions ()
338 (let ((obarray [when what whelp])
341 (should (equal '("when")
342 (company-elisp--globals "wh" 'fboundp)))))
344 (ert-deftest company-elisp-finds-things ()
345 (let ((obarray [when what whelp])
348 (should (equal '("what" "whelp" "when")
349 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
352 (ert-deftest company-elisp-locals-vars ()
353 (company-elisp-with-buffer
354 "(let ((foo 5) (bar 6))
355 (cl-labels ((borg ()))
358 (should (equal '("bar" "baz" "boo")
359 (company-elisp--locals "b" nil)))))
361 (ert-deftest company-elisp-locals-single-var ()
362 (company-elisp-with-buffer
366 (should (equal '("itk" "item")
367 (company-elisp--locals "it" nil)))))
369 (ert-deftest company-elisp-locals-funs ()
370 (company-elisp-with-buffer
371 "(cl-labels ((foo ())
375 (should (equal '("fee" "foo")
376 (sort (company-elisp--locals "f" t) 'string<)))))
378 (ert-deftest company-elisp-locals-skips-current-varlist ()
379 (company-elisp-with-buffer
382 (should (null (company-elisp--locals "f" nil)))))
384 (ert-deftest company-elisp-show-locals-first ()
385 (company-elisp-with-buffer
390 (let ((obarray [float-pi]))
391 (let (company-elisp-show-locals-first)
392 (should (eq nil (company-elisp 'sorted))))
393 (let ((company-elisp-show-locals-first t))
394 (should (eq t (company-elisp 'sorted)))
395 (should (equal '("flee" "floo" "flop" "float-pi")
396 (company-elisp-candidates "fl")))))))
398 (ert-deftest company-elisp-candidates-no-duplicates ()
399 (company-elisp-with-buffer
402 (let ((obarray [float-pi])
403 (company-elisp-show-locals-first t))
404 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
406 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
407 (company-elisp-with-buffer
409 (should (null (company-elisp 'prefix)))))
411 (ert-deftest company-elisp-should-complete-def-call ()
412 (company-elisp-with-buffer
414 (should (equal "defu" (company-elisp 'prefix)))))
416 (ert-deftest company-elisp-should-complete-in-defvar ()
417 ;; It will also complete the var name, at least for now.
418 (company-elisp-with-buffer
420 (should (equal "de" (company-elisp 'prefix)))))
422 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
423 (company-elisp-with-buffer
424 "(defsubst foobar (ba|"
425 (should (null (company-elisp 'prefix)))))
427 (ert-deftest company-elisp-prefix-in-defun-body ()
428 (company-elisp-with-buffer
430 (should (equal "" (company-elisp 'prefix)))))
434 (ert-deftest company-clang-objc-templatify ()
436 (let ((text "createBookWithTitle:andAuthor:"))
438 (company-clang-objc-templatify text)
439 (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
440 (should (looking-at "arg0"))
441 (should (null (overlay-get (company-template-field-at) 'display))))))