]> code.delx.au - gnu-emacs-elpa/blob - company-tests.el
b111822bbe7316f84448b5abe061da1b4e174029
[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 (eval-when-compile (require 'cl))
29 (require 'ert)
30 (require 'company)
31 (require 'company-keywords)
32 (require 'company-elisp)
33 (require 'company-clang)
34
35 ;;; Core
36
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))
45 (setq prev next))))))
46
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))))))
55
56 (ert-deftest company-multi-backend-with-lambdas ()
57 (let ((company-backend
58 (list (lambda (command &optional arg &rest ignore)
59 (case command
60 (prefix "z")
61 (candidates '("a" "b"))))
62 (lambda (command &optional arg &rest ignore)
63 (case command
64 (prefix "z")
65 (candidates '("c" "d")))))))
66 (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
67
68 (ert-deftest company-begin-backend-failure-doesnt-break-company-backends ()
69 (with-temp-buffer
70 (insert "a")
71 (company-mode)
72 (should-error
73 (company-begin-backend (lambda (command &rest ignore))))
74 (let (company-frontends
75 (company-backends
76 (list (lambda (command &optional arg)
77 (case command
78 (prefix "a")
79 (candidates '("a" "ab" "ac")))))))
80 (let (this-command)
81 (company-call 'complete))
82 (should (eq 3 company-candidates-length)))))
83
84 (ert-deftest company-require-match-explicit ()
85 (with-temp-buffer
86 (insert "ab")
87 (company-mode)
88 (let (company-frontends
89 (company-require-match 'company-explicit-action-p)
90 (company-backends
91 (list (lambda (command &optional arg)
92 (case command
93 (prefix (buffer-substring (point-min) (point)))
94 (candidates '("abc" "abd")))))))
95 (let (this-command)
96 (company-complete))
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))))))
101
102 (ert-deftest company-dont-require-match-when-idle ()
103 (with-temp-buffer
104 (insert "ab")
105 (company-mode)
106 (let (company-frontends
107 (company-require-match 'company-explicit-action-p)
108 (company-backends
109 (list (lambda (command &optional arg)
110 (case command
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))))))
119
120 (ert-deftest company-auto-complete-explicit ()
121 (with-temp-buffer
122 (insert "ab")
123 (company-mode)
124 (let (company-frontends
125 (company-auto-complete 'company-explicit-action-p)
126 (company-auto-complete-chars '(? ))
127 (company-backends
128 (list (lambda (command &optional arg)
129 (case command
130 (prefix (buffer-substring (point-min) (point)))
131 (candidates '("abcd" "abef")))))))
132 (let (this-command)
133 (company-complete))
134 (let ((last-command-event ? ))
135 (company-call 'self-insert-command 1))
136 (should (string= "abcd " (buffer-string))))))
137
138 (ert-deftest company-no-auto-complete-when-idle ()
139 (with-temp-buffer
140 (insert "ab")
141 (company-mode)
142 (let (company-frontends
143 (company-auto-complete 'company-explicit-action-p)
144 (company-auto-complete-chars '(? ))
145 (company-backends
146 (list (lambda (command &optional arg)
147 (case command
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))))))
155
156 (ert-deftest company-clears-explicit-action-when-no-matches ()
157 (with-temp-buffer
158 (company-mode)
159 (let (company-frontends
160 company-backends)
161 (company-call 'manual-begin) ;; fails
162 (should (null company-candidates))
163 (should (null (company-explicit-action-p))))))
164
165 (ert-deftest company-ignore-case-replaces-prefix ()
166 (with-temp-buffer
167 (company-mode)
168 (let (company-frontends
169 (company-backends
170 (list (lambda (command &optional arg)
171 (case command
172 (prefix (buffer-substring (point-min) (point)))
173 (candidates '("abcd" "abef"))
174 (ignore-case t))))))
175 (insert "A")
176 (let (this-command)
177 (company-complete))
178 (should (string= "ab" (buffer-string)))
179 (delete-char -2)
180 (insert "AB") ; hack, to keep it in one test
181 (company-complete-selection)
182 (should (string= "abcd" (buffer-string))))))
183
184 (ert-deftest company-ignore-case-with-keep-prefix ()
185 (with-temp-buffer
186 (insert "AB")
187 (company-mode)
188 (let (company-frontends
189 (company-backends
190 (list (lambda (command &optional arg)
191 (case command
192 (prefix (buffer-substring (point-min) (point)))
193 (candidates '("abcd" "abef"))
194 (ignore-case 'keep-prefix))))))
195 (let (this-command)
196 (company-complete))
197 (company-complete-selection)
198 (should (string= "ABcd" (buffer-string))))))
199
200 (ert-deftest company-non-prefix-completion ()
201 (with-temp-buffer
202 (insert "tc")
203 (company-mode)
204 (let (company-frontends
205 company-end-of-buffer-workaround
206 (company-backends
207 (list (lambda (command &optional arg)
208 (case command
209 (prefix (buffer-substring (point-min) (point)))
210 (candidates '("tea-cup" "teal-color")))))))
211 (let (this-command)
212 (company-complete))
213 (should (string= "tc" (buffer-string))))))
214
215 (ert-deftest company-non-prefix-completion ()
216 (with-temp-buffer
217 (insert "tc")
218 (company-mode)
219 (let (company-frontends
220 company-end-of-buffer-workaround
221 (company-backends
222 (list (lambda (command &optional arg)
223 (case command
224 (prefix (buffer-substring (point-min) (point)))
225 (candidates '("tea-cup" "teal-color")))))))
226 (let (this-command)
227 (company-complete))
228 (should (string= "tc" (buffer-string)))
229 (company-complete-selection)
230 (should (string= "tea-cup" (buffer-string))))))
231
232 (ert-deftest company-pseudo-tooltip-does-not-get-displaced ()
233 :tags '(interactive)
234 (with-temp-buffer
235 (save-window-excursion
236 (set-window-buffer nil (current-buffer))
237 (save-excursion (insert " ff"))
238 (company-mode)
239 (let ((company-frontends '(company-pseudo-tooltip-frontend))
240 (company-begin-commands '(self-insert-command))
241 (company-backends
242 (list (lambda (c &optional arg)
243 (case c (prefix "") (candidates '("a" "b" "c")))))))
244 (let (this-command)
245 (company-call 'complete))
246 (company-call 'open-line 1)
247 (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
248
249 (ert-deftest company-pseudo-tooltip-overlay-show ()
250 :tags '(interactive)
251 (with-temp-buffer
252 (save-window-excursion
253 (set-window-buffer nil (current-buffer))
254 (insert "aaaa\n bb\nccccc\nddd")
255 (search-backward "bb")
256 (let ((col (company--column))
257 (company-candidates-length 2)
258 (company-candidates '("123" "45")))
259 (company-pseudo-tooltip-show (company--row) col 0)
260 (let ((ov company-pseudo-tooltip-overlay))
261 (should (eq (overlay-get ov 'company-width) 3))
262 ;; FIXME: Make it 2?
263 (should (eq (overlay-get ov 'company-height) company-tooltip-limit))
264 (should (eq (overlay-get ov 'company-column) col))
265 (should (string= (overlay-get ov 'company-after)
266 " 123\nc45 c\nddd\n")))))))
267
268 (ert-deftest company-create-lines-shows-numbers ()
269 (let ((company-show-numbers t)
270 (company-candidates '("x" "y" "z"))
271 (company-candidates-length 3))
272 (should (equal '("x 1" "y 2" "z 3")
273 (company--create-lines 0 999)))))
274
275 (ert-deftest company-column-with-composition ()
276 (with-temp-buffer
277 (insert "lambda ()")
278 (compose-region 1 (1+ (length "lambda")) "\\")
279 (should (= (company--column) 4))))
280
281 (ert-deftest company-column-with-line-prefix ()
282 (with-temp-buffer
283 (insert "foo")
284 (put-text-property (point-min) (point) 'line-prefix " ")
285 (should (= (company--column) 5))))
286
287 (ert-deftest company-column-wth-line-prefix-on-empty-line ()
288 (with-temp-buffer
289 (insert "\n")
290 (forward-char -1)
291 (put-text-property (point-min) (point-max) 'line-prefix " ")
292 (should (= (company--column) 2))))
293
294 (ert-deftest company-plainify ()
295 (let ((tab-width 8))
296 (should (equal-including-properties
297 (company-plainify "\tabc\td\t")
298 (concat " "
299 "abc "
300 "d "))))
301 (should (equal-including-properties
302 (company-plainify (propertize "foobar" 'line-prefix "-*-"))
303 "-*-foobar")))
304
305 (ert-deftest company-modify-line ()
306 (let ((str "-*-foobar"))
307 (should (equal-including-properties
308 (company-modify-line str "zz" 4)
309 "-*-fzzbar"))
310 (should (equal-including-properties
311 (company-modify-line str "xx" 0)
312 "xx-foobar"))
313 (should (equal-including-properties
314 (company-modify-line str "zz" 10)
315 "-*-foobar zz"))))
316
317 ;;; Template
318
319 (ert-deftest company-template-removed-after-the-last-jump ()
320 (with-temp-buffer
321 (insert "{ }")
322 (goto-char 2)
323 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
324 (save-excursion
325 (dotimes (i 2)
326 (insert " ")
327 (company-template-add-field tpl (point) "foo")))
328 (company-call 'template-forward-field)
329 (should (= 3 (point)))
330 (company-call 'template-forward-field)
331 (should (= 7 (point)))
332 (company-call 'template-forward-field)
333 (should (= 11 (point)))
334 (should (zerop (length (overlay-get tpl 'company-template-fields))))
335 (should (null (overlay-buffer tpl))))))
336
337 (ert-deftest company-template-removed-after-input-and-jump ()
338 (with-temp-buffer
339 (insert "{ }")
340 (goto-char 2)
341 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
342 (save-excursion
343 (insert " ")
344 (company-template-add-field tpl (point) "bar"))
345 (company-call 'template-move-to-first tpl)
346 (should (= 3 (point)))
347 (dolist (c (string-to-list "tee"))
348 (let ((last-command-event c))
349 (company-call 'self-insert-command 1)))
350 (should (string= "{ tee }" (buffer-string)))
351 (should (overlay-buffer tpl))
352 (company-call 'template-forward-field)
353 (should (= 7 (point)))
354 (should (null (overlay-buffer tpl))))))
355
356 (defun company-call (name &rest args)
357 (let* ((maybe (intern (format "company-%s" name)))
358 (command (if (fboundp maybe) maybe name)))
359 (apply command args)
360 (let ((this-command command))
361 (run-hooks 'post-command-hook))))
362
363 (ert-deftest company-template-c-like-templatify ()
364 (with-temp-buffer
365 (let ((text "foo(int a, short b)"))
366 (insert text)
367 (company-template-c-like-templatify text)
368 (should (equal "foo(arg0, arg1)" (buffer-string)))
369 (should (looking-at "arg0"))
370 (should (equal "int a"
371 (overlay-get (company-template-field-at) 'display))))))
372
373 (ert-deftest company-template-c-like-templatify-trims-after-closing-paren ()
374 (with-temp-buffer
375 (let ((text "foo(int a, short b)!@ #1334 a"))
376 (insert text)
377 (company-template-c-like-templatify text)
378 (should (equal "foo(arg0, arg1)" (buffer-string)))
379 (should (looking-at "arg0")))))
380
381 ;;; Elisp
382
383 (defmacro company-elisp-with-buffer (contents &rest body)
384 (declare (indent 0))
385 `(with-temp-buffer
386 (insert ,contents)
387 (setq major-mode 'emacs-lisp-mode)
388 (re-search-backward "|")
389 (replace-match "")
390 (let ((company-elisp-detect-function-context t))
391 ,@body)))
392
393 (ert-deftest company-elisp-candidates-predicate ()
394 (company-elisp-with-buffer
395 "(foo ba|)"
396 (should (eq (company-elisp--candidates-predicate "ba")
397 'boundp))
398 (should (eq (let (company-elisp-detect-function-context)
399 (company-elisp--candidates-predicate "ba"))
400 'company-elisp--predicate)))
401 (company-elisp-with-buffer
402 "(foo| )"
403 (should (eq (company-elisp--candidates-predicate "foo")
404 'fboundp))
405 (should (eq (let (company-elisp-detect-function-context)
406 (company-elisp--candidates-predicate "foo"))
407 'company-elisp--predicate)))
408 (company-elisp-with-buffer
409 "(foo 'b|)"
410 (should (eq (company-elisp--candidates-predicate "b")
411 'company-elisp--predicate))))
412
413 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
414 (company-elisp-with-buffer
415 "(def foo () \"Doo be doo `ide|"
416 (should (eq 'company-elisp--predicate
417 (company-elisp--candidates-predicate "ide")))))
418
419 ;; This one's also an integration test.
420 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
421 (let ((company-elisp-detect-function-context t)
422 (obarray [when what whelp])
423 (what 1)
424 (whelp 2)
425 (wisp 3))
426 (company-elisp-with-buffer
427 "(let ((foo 7) (wh| )))"
428 (should (equal '("what" "whelp")
429 (company-elisp-candidates "wh"))))
430 (company-elisp-with-buffer
431 "(cond ((null nil) (wh| )))"
432 (should (equal '("when")
433 (company-elisp-candidates "wh"))))))
434
435 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
436 (loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
437 ("(let (foo (bar|" "bar" boundp)
438 ("(let (foo) (bar|" "bar" fboundp))
439 do
440 (eval `(company-elisp-with-buffer
441 ,text
442 (should (eq ',predicate
443 (company-elisp--candidates-predicate ,prefix)))))))
444
445 (ert-deftest company-elisp-finds-vars ()
446 (let ((obarray [boo bar baz backquote])
447 (boo t)
448 (bar t)
449 (baz t))
450 (should (equal '("bar" "baz")
451 (company-elisp--globals "ba" 'boundp)))))
452
453 (ert-deftest company-elisp-finds-functions ()
454 (let ((obarray [when what whelp])
455 (what t)
456 (whelp t))
457 (should (equal '("when")
458 (company-elisp--globals "wh" 'fboundp)))))
459
460 (ert-deftest company-elisp-finds-things ()
461 (let ((obarray [when what whelp])
462 (what t)
463 (whelp t))
464 (should (equal '("what" "whelp" "when")
465 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
466 'string<)))))
467
468 (ert-deftest company-elisp-locals-vars ()
469 (company-elisp-with-buffer
470 "(let ((foo 5) (bar 6))
471 (cl-labels ((borg ()))
472 (lambda (boo baz)
473 b|)))"
474 (should (equal '("bar" "baz" "boo")
475 (company-elisp--locals "b" nil)))))
476
477 (ert-deftest company-elisp-locals-single-var ()
478 (company-elisp-with-buffer
479 "(dotimes (itk 100)
480 (dolist (item items)
481 it|))"
482 (should (equal '("itk" "item")
483 (company-elisp--locals "it" nil)))))
484
485 (ert-deftest company-elisp-locals-funs ()
486 (company-elisp-with-buffer
487 "(cl-labels ((foo ())
488 (fee ()))
489 (let ((fun 4))
490 (f| )))"
491 (should (equal '("fee" "foo")
492 (sort (company-elisp--locals "f" t) 'string<)))))
493
494 (ert-deftest company-elisp-locals-skips-current-varlist ()
495 (company-elisp-with-buffer
496 "(let ((foo 1)
497 (f| )))"
498 (should (null (company-elisp--locals "f" nil)))))
499
500 (ert-deftest company-elisp-show-locals-first ()
501 (company-elisp-with-buffer
502 "(let ((floo 1)
503 (flop 2)
504 (flee 3))
505 fl|)"
506 (let ((obarray [float-pi]))
507 (let (company-elisp-show-locals-first)
508 (should (eq nil (company-elisp 'sorted))))
509 (let ((company-elisp-show-locals-first t))
510 (should (eq t (company-elisp 'sorted)))
511 (should (equal '("flee" "floo" "flop" "float-pi")
512 (company-elisp-candidates "fl")))))))
513
514 (ert-deftest company-elisp-candidates-no-duplicates ()
515 (company-elisp-with-buffer
516 "(let ((float-pi 4))
517 f|)"
518 (let ((obarray [float-pi])
519 (company-elisp-show-locals-first t))
520 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
521
522 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
523 (company-elisp-with-buffer
524 "(defun foob|)"
525 (should (null (company-elisp 'prefix)))))
526
527 (ert-deftest company-elisp-should-complete-def-call ()
528 (company-elisp-with-buffer
529 "(defu|"
530 (should (equal "defu" (company-elisp 'prefix)))))
531
532 (ert-deftest company-elisp-should-complete-in-defvar ()
533 ;; It will also complete the var name, at least for now.
534 (company-elisp-with-buffer
535 "(defvar abc de|"
536 (should (equal "de" (company-elisp 'prefix)))))
537
538 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
539 (company-elisp-with-buffer
540 "(defsubst foobar (ba|"
541 (should (null (company-elisp 'prefix)))))
542
543 (ert-deftest company-elisp-prefix-in-defun-body ()
544 (company-elisp-with-buffer
545 "(defun foob ()|)"
546 (should (equal "" (company-elisp 'prefix)))))
547
548 ;;; Clang
549
550 (ert-deftest company-clang-objc-templatify ()
551 (with-temp-buffer
552 (let ((text "createBookWithTitle:andAuthor:"))
553 (insert text)
554 (company-clang-objc-templatify text)
555 (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
556 (should (looking-at "arg0"))
557 (should (null (overlay-get (company-template-field-at) 'display))))))