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)
32 (ert-deftest company-sorted-keywords ()
33 "Test that keywords in `company-keywords-alist' are in alphabetical order."
34 (dolist (pair company-keywords-alist)
35 (when (consp (cdr pair))
36 (let ((prev (cadr pair)))
37 (dolist (next (cddr pair))
38 (should (not (equal prev next)))
39 (should (string< prev next))
42 (ert-deftest company-good-prefix ()
43 (let ((company-minimum-prefix-length 5)
44 company--explicit-action)
45 (should (eq t (company--good-prefix-p "!@#$%")))
46 (should (eq nil (company--good-prefix-p "abcd")))
47 (should (eq nil (company--good-prefix-p 'stop)))
48 (should (eq t (company--good-prefix-p '("foo" . 5))))
49 (should (eq nil (company--good-prefix-p '("foo" . 4))))))
51 (ert-deftest company-multi-backend-with-lambdas ()
52 (let ((company-backend
53 (list (lambda (command &optional arg &rest ignore)
56 (candidates '("a" "b"))))
57 (lambda (command &optional arg &rest ignore)
60 (candidates '("c" "d")))))))
61 (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
63 (ert-deftest company-begin-backend-failure-doesnt-break-company-backends ()
68 (company-begin-backend (lambda (command &rest ignore))))
69 (let (company-frontends
71 (list (lambda (command &optional arg)
74 (candidates '("a" "ab" "ac")))))))
76 (company-call 'complete))
77 (should (eq 3 company-candidates-length)))))
79 (ert-deftest company-require-match-explicit ()
83 (let (company-frontends
84 (company-require-match 'company-explicit-action-p)
86 (list (lambda (command &optional arg)
88 (prefix (buffer-substring (point-min) (point)))
89 (candidates '("abc" "abd")))))))
92 (let ((last-command-event ?e))
93 (company-call 'self-insert-command 1))
94 (should (eq 2 company-candidates-length))
95 (should (eq 3 (point))))))
97 (ert-deftest company-dont-require-match-when-idle ()
101 (let (company-frontends
102 (company-require-match 'company-explicit-action-p)
104 (list (lambda (command &optional arg)
106 (prefix (buffer-substring (point-min) (point)))
107 (candidates '("abc" "abd")))))))
108 (company-idle-begin (current-buffer) (selected-window)
109 (buffer-chars-modified-tick) (point))
110 (let ((last-command-event ?e))
111 (company-call 'self-insert-command 1))
112 (should (eq nil company-candidates-length))
113 (should (eq 4 (point))))))
115 (ert-deftest company-auto-complete-explicit ()
119 (let (company-frontends
120 (company-auto-complete 'company-explicit-action-p)
121 (company-auto-complete-chars '(? ))
123 (list (lambda (command &optional arg)
125 (prefix (buffer-substring (point-min) (point)))
126 (candidates '("abcd" "abef")))))))
129 (let ((last-command-event ? ))
130 (company-call 'self-insert-command 1))
131 (should (string= "abcd " (buffer-string))))))
133 (ert-deftest company-no-auto-complete-when-idle ()
137 (let (company-frontends
138 (company-auto-complete 'company-explicit-action-p)
139 (company-auto-complete-chars '(? ))
141 (list (lambda (command &optional arg)
143 (prefix (buffer-substring (point-min) (point)))
144 (candidates '("abcd" "abef")))))))
145 (company-idle-begin (current-buffer) (selected-window)
146 (buffer-chars-modified-tick) (point))
147 (let ((last-command-event ? ))
148 (company-call 'self-insert-command 1))
149 (should (string= "ab " (buffer-string))))))
151 (ert-deftest company-clears-explicit-action-when-no-matches ()
154 (let (company-frontends
156 (company-call 'manual-begin) ;; fails
157 (should (null company-candidates))
158 (should (null (company-explicit-action-p))))))
160 (ert-deftest company-pseudo-tooltip-does-not-get-displaced ()
162 (save-window-excursion
163 (set-window-buffer nil (current-buffer))
164 (save-excursion (insert " ff"))
166 (let ((company-frontends '(company-pseudo-tooltip-frontend))
167 (company-begin-commands '(self-insert-command))
169 (list (lambda (c &optional arg)
170 (case c (prefix "") (candidates '("a" "b" "c")))))))
172 (company-call 'complete))
173 (company-call 'open-line 1)
174 (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
176 (ert-deftest company-template-removed-after-the-last-jump ()
178 (insert "{ foo foo }")
180 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
183 (search-forward "foo")
184 (company-template-add-field tpl (match-beginning 0) (match-end 0))))
185 (company-call 'template-forward-field)
186 (should (= 3 (point)))
187 (company-call 'template-forward-field)
188 (should (= 7 (point)))
189 (company-call 'template-forward-field)
190 (should (= 11 (point)))
191 (should (zerop (length (overlay-get tpl 'company-template-fields))))
192 (should (null (overlay-buffer tpl))))))
194 (ert-deftest company-template-removed-after-input-and-jump ()
198 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
199 (company-template-add-field tpl 3 6)
200 (company-call 'template-move-to-first tpl)
201 (should (= 3 (point)))
202 (dolist (c (string-to-list "tee"))
203 (let ((last-command-event c))
204 (company-call 'self-insert-command 1)))
205 (should (string= "{ tee }" (buffer-string)))
206 (should (overlay-buffer tpl))
207 (company-call 'template-forward-field)
208 (should (= 7 (point)))
209 (should (null (overlay-buffer tpl))))))
211 (defun company-call (name &rest args)
212 (let* ((maybe (intern (format "company-%s" name)))
213 (command (if (fboundp maybe) maybe name)))
215 (let ((this-command command))
216 (run-hooks 'post-command-hook))))
218 (defmacro company-elisp-with-buffer (contents &rest body)
222 (setq major-mode 'emacs-lisp-mode)
223 (re-search-backward "|")
225 (let ((company-elisp-detect-function-context t))
228 (ert-deftest company-elisp-candidates-predicate ()
229 (company-elisp-with-buffer
231 (should (eq (company-elisp--candidates-predicate "ba")
233 (should (eq (let (company-elisp-detect-function-context)
234 (company-elisp--candidates-predicate "ba"))
235 'company-elisp--predicate)))
236 (company-elisp-with-buffer
238 (should (eq (company-elisp--candidates-predicate "foo")
240 (should (eq (let (company-elisp-detect-function-context)
241 (company-elisp--candidates-predicate "foo"))
242 'company-elisp--predicate)))
243 (company-elisp-with-buffer
245 (should (eq (company-elisp--candidates-predicate "b")
246 'company-elisp--predicate))))
248 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
249 (company-elisp-with-buffer
250 "(def foo () \"Doo be doo `ide|"
251 (should (eq 'company-elisp--predicate
252 (company-elisp--candidates-predicate "ide")))))
254 ;; This one's also an integration test.
255 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
256 (let ((company-elisp-detect-function-context t)
257 (obarray [when what whelp])
261 (company-elisp-with-buffer
262 "(let ((foo 7) (wh| )))"
263 (should (equal '("what" "whelp")
264 (company-elisp-candidates "wh"))))
265 (company-elisp-with-buffer
266 "(cond ((null nil) (wh| )))"
267 (should (equal '("when")
268 (company-elisp-candidates "wh"))))))
270 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
271 (loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
272 ("(let (foo (bar|" "bar" boundp)
273 ("(let (foo) (bar|" "bar" fboundp))
275 (eval `(company-elisp-with-buffer
277 (should (eq ',predicate
278 (company-elisp--candidates-predicate ,prefix)))))))
280 (ert-deftest company-elisp-finds-vars ()
281 (let ((obarray [boo bar baz backquote])
285 (should (equal '("bar" "baz")
286 (company-elisp--globals "ba" 'boundp)))))
288 (ert-deftest company-elisp-finds-functions ()
289 (let ((obarray [when what whelp])
292 (should (equal '("when")
293 (company-elisp--globals "wh" 'fboundp)))))
295 (ert-deftest company-elisp-finds-things ()
296 (let ((obarray [when what whelp])
299 (should (equal '("what" "whelp" "when")
300 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
303 (ert-deftest company-elisp-locals-vars ()
304 (company-elisp-with-buffer
305 "(let ((foo 5) (bar 6))
306 (cl-labels ((borg ()))
309 (should (equal '("bar" "baz" "boo")
310 (company-elisp--locals "b" nil)))))
312 (ert-deftest company-elisp-locals-single-var ()
313 (company-elisp-with-buffer
317 (should (equal '("itk" "item")
318 (company-elisp--locals "it" nil)))))
320 (ert-deftest company-elisp-locals-funs ()
321 (company-elisp-with-buffer
322 "(cl-labels ((foo ())
326 (should (equal '("fee" "foo")
327 (sort (company-elisp--locals "f" t) 'string<)))))
329 (ert-deftest company-elisp-locals-skips-current-varlist ()
330 (company-elisp-with-buffer
333 (should (null (company-elisp--locals "f" nil)))))
335 (ert-deftest company-elisp-show-locals-first ()
336 (company-elisp-with-buffer
341 (let ((obarray [float-pi]))
342 (let (company-elisp-show-locals-first)
343 (should (eq nil (company-elisp 'sorted))))
344 (let ((company-elisp-show-locals-first t))
345 (should (eq t (company-elisp 'sorted)))
346 (should (equal '("flee" "floo" "flop" "float-pi")
347 (company-elisp-candidates "fl")))))))
349 (ert-deftest company-elisp-candidates-no-duplicates ()
350 (company-elisp-with-buffer
353 (let ((obarray [float-pi])
354 (company-elisp-show-locals-first t))
355 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
357 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
358 (company-elisp-with-buffer
360 (should (null (company-elisp 'prefix)))))
362 (ert-deftest company-elisp-should-complete-def-call ()
363 (company-elisp-with-buffer
365 (should (equal "defu" (company-elisp 'prefix)))))
367 (ert-deftest company-elisp-should-complete-in-defvar ()
368 ;; It will also complete the var name, at least for now.
369 (company-elisp-with-buffer
371 (should (equal "de" (company-elisp 'prefix)))))
373 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
374 (company-elisp-with-buffer
375 "(defsubst foobar (ba|"
376 (should (null (company-elisp 'prefix)))))
378 (ert-deftest company-elisp-prefix-in-defun-body ()
379 (company-elisp-with-buffer
381 (should (equal "" (company-elisp 'prefix)))))