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/>.
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-pseudo-tooltip-does-not-get-displaced ()
168 (save-window-excursion
169 (set-window-buffer nil (current-buffer))
170 (save-excursion (insert " ff"))
172 (let ((company-frontends '(company-pseudo-tooltip-frontend))
173 (company-begin-commands '(self-insert-command))
175 (list (lambda (c &optional arg)
176 (case c (prefix "") (candidates '("a" "b" "c")))))))
178 (company-call 'complete))
179 (company-call 'open-line 1)
180 (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
182 (defun company-test-pseudo-tooltip-overlay-show ()
183 (save-window-excursion
184 (set-window-buffer nil (current-buffer))
185 (insert "aaaa\n bb\nccccc\nddd")
186 (search-backward "bb")
187 (let ((col-row (company--col-row))
188 (company-candidates-length 2)
189 (company-candidates '("123" "45")))
190 (company-pseudo-tooltip-show (cdr col-row) (car col-row) 0)
191 (let ((ov company-pseudo-tooltip-overlay))
192 (should (eq (overlay-get ov 'company-width) 3))
194 (should (eq (overlay-get ov 'company-height) 10))
195 (should (eq (overlay-get ov 'company-column) (car col-row)))
196 (should (string= (overlay-get ov 'company-before)
197 " 123\nc45 c\nddd\n"))))))
199 (ert-deftest company-pseudo-tooltip-overlay-show ()
202 (company-test-pseudo-tooltip-overlay-show)))
204 (ert-deftest company-pseudo-tooltip-overlay-show-with-header-line ()
207 (setq header-line-format "foo bar")
208 (company-test-pseudo-tooltip-overlay-show)))
212 (ert-deftest company-template-removed-after-the-last-jump ()
216 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
220 (company-template-add-field tpl (point) "foo")))
221 (company-call 'template-forward-field)
222 (should (= 3 (point)))
223 (company-call 'template-forward-field)
224 (should (= 7 (point)))
225 (company-call 'template-forward-field)
226 (should (= 11 (point)))
227 (should (zerop (length (overlay-get tpl 'company-template-fields))))
228 (should (null (overlay-buffer tpl))))))
230 (ert-deftest company-template-removed-after-input-and-jump ()
234 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
237 (company-template-add-field tpl (point) "bar"))
238 (company-call 'template-move-to-first tpl)
239 (should (= 3 (point)))
240 (dolist (c (string-to-list "tee"))
241 (let ((last-command-event c))
242 (company-call 'self-insert-command 1)))
243 (should (string= "{ tee }" (buffer-string)))
244 (should (overlay-buffer tpl))
245 (company-call 'template-forward-field)
246 (should (= 7 (point)))
247 (should (null (overlay-buffer tpl))))))
249 (defun company-call (name &rest args)
250 (let* ((maybe (intern (format "company-%s" name)))
251 (command (if (fboundp maybe) maybe name)))
253 (let ((this-command command))
254 (run-hooks 'post-command-hook))))
256 (ert-deftest company-template-c-like-templatify ()
258 (let ((text "foo(int a, short b)"))
260 (company-template-c-like-templatify text)
261 (should (equal "foo(arg0, arg1)" (buffer-string)))
262 (should (looking-at "arg0"))
263 (should (equal "int a"
264 (overlay-get (company-template-field-at) 'display))))))
268 (defmacro company-elisp-with-buffer (contents &rest body)
272 (setq major-mode 'emacs-lisp-mode)
273 (re-search-backward "|")
275 (let ((company-elisp-detect-function-context t))
278 (ert-deftest company-elisp-candidates-predicate ()
279 (company-elisp-with-buffer
281 (should (eq (company-elisp--candidates-predicate "ba")
283 (should (eq (let (company-elisp-detect-function-context)
284 (company-elisp--candidates-predicate "ba"))
285 'company-elisp--predicate)))
286 (company-elisp-with-buffer
288 (should (eq (company-elisp--candidates-predicate "foo")
290 (should (eq (let (company-elisp-detect-function-context)
291 (company-elisp--candidates-predicate "foo"))
292 'company-elisp--predicate)))
293 (company-elisp-with-buffer
295 (should (eq (company-elisp--candidates-predicate "b")
296 'company-elisp--predicate))))
298 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
299 (company-elisp-with-buffer
300 "(def foo () \"Doo be doo `ide|"
301 (should (eq 'company-elisp--predicate
302 (company-elisp--candidates-predicate "ide")))))
304 ;; This one's also an integration test.
305 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
306 (let ((company-elisp-detect-function-context t)
307 (obarray [when what whelp])
311 (company-elisp-with-buffer
312 "(let ((foo 7) (wh| )))"
313 (should (equal '("what" "whelp")
314 (company-elisp-candidates "wh"))))
315 (company-elisp-with-buffer
316 "(cond ((null nil) (wh| )))"
317 (should (equal '("when")
318 (company-elisp-candidates "wh"))))))
320 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
321 (loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
322 ("(let (foo (bar|" "bar" boundp)
323 ("(let (foo) (bar|" "bar" fboundp))
325 (eval `(company-elisp-with-buffer
327 (should (eq ',predicate
328 (company-elisp--candidates-predicate ,prefix)))))))
330 (ert-deftest company-elisp-finds-vars ()
331 (let ((obarray [boo bar baz backquote])
335 (should (equal '("bar" "baz")
336 (company-elisp--globals "ba" 'boundp)))))
338 (ert-deftest company-elisp-finds-functions ()
339 (let ((obarray [when what whelp])
342 (should (equal '("when")
343 (company-elisp--globals "wh" 'fboundp)))))
345 (ert-deftest company-elisp-finds-things ()
346 (let ((obarray [when what whelp])
349 (should (equal '("what" "whelp" "when")
350 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
353 (ert-deftest company-elisp-locals-vars ()
354 (company-elisp-with-buffer
355 "(let ((foo 5) (bar 6))
356 (cl-labels ((borg ()))
359 (should (equal '("bar" "baz" "boo")
360 (company-elisp--locals "b" nil)))))
362 (ert-deftest company-elisp-locals-single-var ()
363 (company-elisp-with-buffer
367 (should (equal '("itk" "item")
368 (company-elisp--locals "it" nil)))))
370 (ert-deftest company-elisp-locals-funs ()
371 (company-elisp-with-buffer
372 "(cl-labels ((foo ())
376 (should (equal '("fee" "foo")
377 (sort (company-elisp--locals "f" t) 'string<)))))
379 (ert-deftest company-elisp-locals-skips-current-varlist ()
380 (company-elisp-with-buffer
383 (should (null (company-elisp--locals "f" nil)))))
385 (ert-deftest company-elisp-show-locals-first ()
386 (company-elisp-with-buffer
391 (let ((obarray [float-pi]))
392 (let (company-elisp-show-locals-first)
393 (should (eq nil (company-elisp 'sorted))))
394 (let ((company-elisp-show-locals-first t))
395 (should (eq t (company-elisp 'sorted)))
396 (should (equal '("flee" "floo" "flop" "float-pi")
397 (company-elisp-candidates "fl")))))))
399 (ert-deftest company-elisp-candidates-no-duplicates ()
400 (company-elisp-with-buffer
403 (let ((obarray [float-pi])
404 (company-elisp-show-locals-first t))
405 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
407 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
408 (company-elisp-with-buffer
410 (should (null (company-elisp 'prefix)))))
412 (ert-deftest company-elisp-should-complete-def-call ()
413 (company-elisp-with-buffer
415 (should (equal "defu" (company-elisp 'prefix)))))
417 (ert-deftest company-elisp-should-complete-in-defvar ()
418 ;; It will also complete the var name, at least for now.
419 (company-elisp-with-buffer
421 (should (equal "de" (company-elisp 'prefix)))))
423 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
424 (company-elisp-with-buffer
425 "(defsubst foobar (ba|"
426 (should (null (company-elisp 'prefix)))))
428 (ert-deftest company-elisp-prefix-in-defun-body ()
429 (company-elisp-with-buffer
431 (should (equal "" (company-elisp 'prefix)))))
435 (ert-deftest company-clang-objc-templatify ()
437 (let ((text "createBookWithTitle:andAuthor:"))
439 (company-clang-objc-templatify text)
440 (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
441 (should (looking-at "arg0"))
442 (should (null (overlay-get (company-template-field-at) 'display))))))