]> code.delx.au - gnu-emacs-elpa/blob - company-tests.el
Move company-capf to a separate file
[gnu-emacs-elpa] / company-tests.el
1 ;;; company-tests.el --- company-mode tests
2
3 ;; Copyright (C) 2011, 2013 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22
23 ;;; Commentary:
24 ;;
25
26 ;;; Code:
27
28 (require 'ert)
29 (require 'company)
30 (require 'company-keywords)
31 (require 'company-elisp)
32 (require 'company-clang)
33
34 ;;; Core
35
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))
44 (setq prev next))))))
45
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))))))
54
55 (ert-deftest company-multi-backend-with-lambdas ()
56 (let ((company-backend
57 (list (lambda (command &optional arg &rest ignore)
58 (case command
59 (prefix "z")
60 (candidates '("a" "b"))))
61 (lambda (command &optional arg &rest ignore)
62 (case command
63 (prefix "z")
64 (candidates '("c" "d")))))))
65 (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
66
67 (ert-deftest company-begin-backend-failure-doesnt-break-company-backends ()
68 (with-temp-buffer
69 (insert "a")
70 (company-mode)
71 (should-error
72 (company-begin-backend (lambda (command &rest ignore))))
73 (let (company-frontends
74 (company-backends
75 (list (lambda (command &optional arg)
76 (case command
77 (prefix "a")
78 (candidates '("a" "ab" "ac")))))))
79 (let (this-command)
80 (company-call 'complete))
81 (should (eq 3 company-candidates-length)))))
82
83 (ert-deftest company-require-match-explicit ()
84 (with-temp-buffer
85 (insert "ab")
86 (company-mode)
87 (let (company-frontends
88 (company-require-match 'company-explicit-action-p)
89 (company-backends
90 (list (lambda (command &optional arg)
91 (case command
92 (prefix (buffer-substring (point-min) (point)))
93 (candidates '("abc" "abd")))))))
94 (let (this-command)
95 (company-complete))
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))))))
100
101 (ert-deftest company-dont-require-match-when-idle ()
102 (with-temp-buffer
103 (insert "ab")
104 (company-mode)
105 (let (company-frontends
106 (company-require-match 'company-explicit-action-p)
107 (company-backends
108 (list (lambda (command &optional arg)
109 (case command
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))))))
118
119 (ert-deftest company-auto-complete-explicit ()
120 (with-temp-buffer
121 (insert "ab")
122 (company-mode)
123 (let (company-frontends
124 (company-auto-complete 'company-explicit-action-p)
125 (company-auto-complete-chars '(? ))
126 (company-backends
127 (list (lambda (command &optional arg)
128 (case command
129 (prefix (buffer-substring (point-min) (point)))
130 (candidates '("abcd" "abef")))))))
131 (let (this-command)
132 (company-complete))
133 (let ((last-command-event ? ))
134 (company-call 'self-insert-command 1))
135 (should (string= "abcd " (buffer-string))))))
136
137 (ert-deftest company-no-auto-complete-when-idle ()
138 (with-temp-buffer
139 (insert "ab")
140 (company-mode)
141 (let (company-frontends
142 (company-auto-complete 'company-explicit-action-p)
143 (company-auto-complete-chars '(? ))
144 (company-backends
145 (list (lambda (command &optional arg)
146 (case command
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))))))
154
155 (ert-deftest company-clears-explicit-action-when-no-matches ()
156 (with-temp-buffer
157 (company-mode)
158 (let (company-frontends
159 company-backends)
160 (company-call 'manual-begin) ;; fails
161 (should (null company-candidates))
162 (should (null (company-explicit-action-p))))))
163
164 (ert-deftest company-pseudo-tooltip-does-not-get-displaced ()
165 :tags '(interactive)
166 (with-temp-buffer
167 (save-window-excursion
168 (set-window-buffer nil (current-buffer))
169 (save-excursion (insert " ff"))
170 (company-mode)
171 (let ((company-frontends '(company-pseudo-tooltip-frontend))
172 (company-begin-commands '(self-insert-command))
173 (company-backends
174 (list (lambda (c &optional arg)
175 (case c (prefix "") (candidates '("a" "b" "c")))))))
176 (let (this-command)
177 (company-call 'complete))
178 (company-call 'open-line 1)
179 (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
180
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))
192 ;; FIXME: Make it 2?
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"))))))
197
198 (ert-deftest company-pseudo-tooltip-overlay-show ()
199 :tags '(interactive)
200 (with-temp-buffer
201 (company-test-pseudo-tooltip-overlay-show)))
202
203 (ert-deftest company-pseudo-tooltip-overlay-show-with-header-line ()
204 :tags '(interactive)
205 (with-temp-buffer
206 (setq header-line-format "foo bar")
207 (company-test-pseudo-tooltip-overlay-show)))
208
209 ;;; Template
210
211 (ert-deftest company-template-removed-after-the-last-jump ()
212 (with-temp-buffer
213 (insert "{ }")
214 (goto-char 2)
215 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
216 (save-excursion
217 (dotimes (i 2)
218 (insert " ")
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))))))
228
229 (ert-deftest company-template-removed-after-input-and-jump ()
230 (with-temp-buffer
231 (insert "{ }")
232 (goto-char 2)
233 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
234 (save-excursion
235 (insert " ")
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))))))
247
248 (defun company-call (name &rest args)
249 (let* ((maybe (intern (format "company-%s" name)))
250 (command (if (fboundp maybe) maybe name)))
251 (apply command args)
252 (let ((this-command command))
253 (run-hooks 'post-command-hook))))
254
255 (ert-deftest company-template-c-like-templatify ()
256 (with-temp-buffer
257 (let ((text "foo(int a, short b)"))
258 (insert text)
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))))))
264
265 ;;; Elisp
266
267 (defmacro company-elisp-with-buffer (contents &rest body)
268 (declare (indent 0))
269 `(with-temp-buffer
270 (insert ,contents)
271 (setq major-mode 'emacs-lisp-mode)
272 (re-search-backward "|")
273 (replace-match "")
274 (let ((company-elisp-detect-function-context t))
275 ,@body)))
276
277 (ert-deftest company-elisp-candidates-predicate ()
278 (company-elisp-with-buffer
279 "(foo ba|)"
280 (should (eq (company-elisp--candidates-predicate "ba")
281 'boundp))
282 (should (eq (let (company-elisp-detect-function-context)
283 (company-elisp--candidates-predicate "ba"))
284 'company-elisp--predicate)))
285 (company-elisp-with-buffer
286 "(foo| )"
287 (should (eq (company-elisp--candidates-predicate "foo")
288 'fboundp))
289 (should (eq (let (company-elisp-detect-function-context)
290 (company-elisp--candidates-predicate "foo"))
291 'company-elisp--predicate)))
292 (company-elisp-with-buffer
293 "(foo 'b|)"
294 (should (eq (company-elisp--candidates-predicate "b")
295 'company-elisp--predicate))))
296
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")))))
302
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])
307 (what 1)
308 (whelp 2)
309 (wisp 3))
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"))))))
318
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))
323 do
324 (eval `(company-elisp-with-buffer
325 ,text
326 (should (eq ',predicate
327 (company-elisp--candidates-predicate ,prefix)))))))
328
329 (ert-deftest company-elisp-finds-vars ()
330 (let ((obarray [boo bar baz backquote])
331 (boo t)
332 (bar t)
333 (baz t))
334 (should (equal '("bar" "baz")
335 (company-elisp--globals "ba" 'boundp)))))
336
337 (ert-deftest company-elisp-finds-functions ()
338 (let ((obarray [when what whelp])
339 (what t)
340 (whelp t))
341 (should (equal '("when")
342 (company-elisp--globals "wh" 'fboundp)))))
343
344 (ert-deftest company-elisp-finds-things ()
345 (let ((obarray [when what whelp])
346 (what t)
347 (whelp t))
348 (should (equal '("what" "whelp" "when")
349 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
350 'string<)))))
351
352 (ert-deftest company-elisp-locals-vars ()
353 (company-elisp-with-buffer
354 "(let ((foo 5) (bar 6))
355 (cl-labels ((borg ()))
356 (lambda (boo baz)
357 b|)))"
358 (should (equal '("bar" "baz" "boo")
359 (company-elisp--locals "b" nil)))))
360
361 (ert-deftest company-elisp-locals-single-var ()
362 (company-elisp-with-buffer
363 "(dotimes (itk 100)
364 (dolist (item items)
365 it|))"
366 (should (equal '("itk" "item")
367 (company-elisp--locals "it" nil)))))
368
369 (ert-deftest company-elisp-locals-funs ()
370 (company-elisp-with-buffer
371 "(cl-labels ((foo ())
372 (fee ()))
373 (let ((fun 4))
374 (f| )))"
375 (should (equal '("fee" "foo")
376 (sort (company-elisp--locals "f" t) 'string<)))))
377
378 (ert-deftest company-elisp-locals-skips-current-varlist ()
379 (company-elisp-with-buffer
380 "(let ((foo 1)
381 (f| )))"
382 (should (null (company-elisp--locals "f" nil)))))
383
384 (ert-deftest company-elisp-show-locals-first ()
385 (company-elisp-with-buffer
386 "(let ((floo 1)
387 (flop 2)
388 (flee 3))
389 fl|)"
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")))))))
397
398 (ert-deftest company-elisp-candidates-no-duplicates ()
399 (company-elisp-with-buffer
400 "(let ((float-pi 4))
401 f|)"
402 (let ((obarray [float-pi])
403 (company-elisp-show-locals-first t))
404 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
405
406 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
407 (company-elisp-with-buffer
408 "(defun foob|)"
409 (should (null (company-elisp 'prefix)))))
410
411 (ert-deftest company-elisp-should-complete-def-call ()
412 (company-elisp-with-buffer
413 "(defu|"
414 (should (equal "defu" (company-elisp 'prefix)))))
415
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
419 "(defvar abc de|"
420 (should (equal "de" (company-elisp 'prefix)))))
421
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)))))
426
427 (ert-deftest company-elisp-prefix-in-defun-body ()
428 (company-elisp-with-buffer
429 "(defun foob ()|)"
430 (should (equal "" (company-elisp 'prefix)))))
431
432 ;;; Clang
433
434 (ert-deftest company-clang-objc-templatify ()
435 (with-temp-buffer
436 (let ((text "createBookWithTitle:andAuthor:"))
437 (insert text)
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))))))