]> code.delx.au - gnu-emacs-elpa/blob - company-tests.el
016e439461b16dc54d61c1b127a875a19dc5cfad
[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-pseudo-tooltip-does-not-get-displaced ()
166 :tags '(interactive)
167 (with-temp-buffer
168 (save-window-excursion
169 (set-window-buffer nil (current-buffer))
170 (save-excursion (insert " ff"))
171 (company-mode)
172 (let ((company-frontends '(company-pseudo-tooltip-frontend))
173 (company-begin-commands '(self-insert-command))
174 (company-backends
175 (list (lambda (c &optional arg)
176 (case c (prefix "") (candidates '("a" "b" "c")))))))
177 (let (this-command)
178 (company-call 'complete))
179 (company-call 'open-line 1)
180 (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
181
182 (ert-deftest company-pseudo-tooltip-overlay-show ()
183 :tags '(interactive)
184 (with-temp-buffer
185 (save-window-excursion
186 (set-window-buffer nil (current-buffer))
187 (insert "aaaa\n bb\nccccc\nddd")
188 (search-backward "bb")
189 (let ((col (company--column))
190 (company-candidates-length 2)
191 (company-candidates '("123" "45")))
192 (company-pseudo-tooltip-show (company--row) col 0)
193 (let ((ov company-pseudo-tooltip-overlay))
194 (should (eq (overlay-get ov 'company-width) 3))
195 ;; FIXME: Make it 2?
196 (should (eq (overlay-get ov 'company-height) company-tooltip-limit))
197 (should (eq (overlay-get ov 'company-column) col))
198 (should (string= (overlay-get ov 'company-after)
199 " 123\nc45 c\nddd\n")))))))
200
201 (ert-deftest company-create-lines-shows-numbers ()
202 (let ((company-show-numbers t)
203 (company-candidates '("x" "y" "z"))
204 (company-candidates-length 3))
205 (should (equal '("x 1" "y 2" "z 3")
206 (company--create-lines 0 999)))))
207
208 (ert-deftest company-column-with-composition ()
209 (with-temp-buffer
210 (insert "lambda ()")
211 (compose-region 1 (1+ (length "lambda")) "\\")
212 (should (= (company--column) 4))))
213
214 (ert-deftest company-column-with-line-prefix ()
215 (with-temp-buffer
216 (insert "foo")
217 (put-text-property (point-min) (point) 'line-prefix " ")
218 (should (= (company--column) 5))))
219
220 (ert-deftest company-column-wth-line-prefix-on-empty-line ()
221 (with-temp-buffer
222 (insert "\n")
223 (forward-char -1)
224 (put-text-property (point-min) (point-max) 'line-prefix " ")
225 (should (= (company--column) 2))))
226
227 (ert-deftest company-plainify ()
228 (let ((tab-width 8))
229 (should (equal-including-properties
230 (company-plainify "\tabc\td\t")
231 (concat " "
232 "abc "
233 "d "))))
234 (should (equal-including-properties
235 (company-plainify (propertize "foobar" 'line-prefix "-*-"))
236 "-*-foobar")))
237
238 (ert-deftest company-modify-line ()
239 (let ((str "-*-foobar"))
240 (should (equal-including-properties
241 (company-modify-line str "zz" 4)
242 "-*-fzzbar"))
243 (should (equal-including-properties
244 (company-modify-line str "xx" 0)
245 "xx-foobar"))
246 (should (equal-including-properties
247 (company-modify-line str "zz" 10)
248 "-*-foobar zz"))))
249
250 ;;; Template
251
252 (ert-deftest company-template-removed-after-the-last-jump ()
253 (with-temp-buffer
254 (insert "{ }")
255 (goto-char 2)
256 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
257 (save-excursion
258 (dotimes (i 2)
259 (insert " ")
260 (company-template-add-field tpl (point) "foo")))
261 (company-call 'template-forward-field)
262 (should (= 3 (point)))
263 (company-call 'template-forward-field)
264 (should (= 7 (point)))
265 (company-call 'template-forward-field)
266 (should (= 11 (point)))
267 (should (zerop (length (overlay-get tpl 'company-template-fields))))
268 (should (null (overlay-buffer tpl))))))
269
270 (ert-deftest company-template-removed-after-input-and-jump ()
271 (with-temp-buffer
272 (insert "{ }")
273 (goto-char 2)
274 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
275 (save-excursion
276 (insert " ")
277 (company-template-add-field tpl (point) "bar"))
278 (company-call 'template-move-to-first tpl)
279 (should (= 3 (point)))
280 (dolist (c (string-to-list "tee"))
281 (let ((last-command-event c))
282 (company-call 'self-insert-command 1)))
283 (should (string= "{ tee }" (buffer-string)))
284 (should (overlay-buffer tpl))
285 (company-call 'template-forward-field)
286 (should (= 7 (point)))
287 (should (null (overlay-buffer tpl))))))
288
289 (defun company-call (name &rest args)
290 (let* ((maybe (intern (format "company-%s" name)))
291 (command (if (fboundp maybe) maybe name)))
292 (apply command args)
293 (let ((this-command command))
294 (run-hooks 'post-command-hook))))
295
296 (ert-deftest company-template-c-like-templatify ()
297 (with-temp-buffer
298 (let ((text "foo(int a, short b)"))
299 (insert text)
300 (company-template-c-like-templatify text)
301 (should (equal "foo(arg0, arg1)" (buffer-string)))
302 (should (looking-at "arg0"))
303 (should (equal "int a"
304 (overlay-get (company-template-field-at) 'display))))))
305
306 (ert-deftest company-template-c-like-templatify-trims-after-closing-paren ()
307 (with-temp-buffer
308 (let ((text "foo(int a, short b)!@ #1334 a"))
309 (insert text)
310 (company-template-c-like-templatify text)
311 (should (equal "foo(arg0, arg1)" (buffer-string)))
312 (should (looking-at "arg0")))))
313
314 ;;; Elisp
315
316 (defmacro company-elisp-with-buffer (contents &rest body)
317 (declare (indent 0))
318 `(with-temp-buffer
319 (insert ,contents)
320 (setq major-mode 'emacs-lisp-mode)
321 (re-search-backward "|")
322 (replace-match "")
323 (let ((company-elisp-detect-function-context t))
324 ,@body)))
325
326 (ert-deftest company-elisp-candidates-predicate ()
327 (company-elisp-with-buffer
328 "(foo ba|)"
329 (should (eq (company-elisp--candidates-predicate "ba")
330 'boundp))
331 (should (eq (let (company-elisp-detect-function-context)
332 (company-elisp--candidates-predicate "ba"))
333 'company-elisp--predicate)))
334 (company-elisp-with-buffer
335 "(foo| )"
336 (should (eq (company-elisp--candidates-predicate "foo")
337 'fboundp))
338 (should (eq (let (company-elisp-detect-function-context)
339 (company-elisp--candidates-predicate "foo"))
340 'company-elisp--predicate)))
341 (company-elisp-with-buffer
342 "(foo 'b|)"
343 (should (eq (company-elisp--candidates-predicate "b")
344 'company-elisp--predicate))))
345
346 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
347 (company-elisp-with-buffer
348 "(def foo () \"Doo be doo `ide|"
349 (should (eq 'company-elisp--predicate
350 (company-elisp--candidates-predicate "ide")))))
351
352 ;; This one's also an integration test.
353 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
354 (let ((company-elisp-detect-function-context t)
355 (obarray [when what whelp])
356 (what 1)
357 (whelp 2)
358 (wisp 3))
359 (company-elisp-with-buffer
360 "(let ((foo 7) (wh| )))"
361 (should (equal '("what" "whelp")
362 (company-elisp-candidates "wh"))))
363 (company-elisp-with-buffer
364 "(cond ((null nil) (wh| )))"
365 (should (equal '("when")
366 (company-elisp-candidates "wh"))))))
367
368 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
369 (loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
370 ("(let (foo (bar|" "bar" boundp)
371 ("(let (foo) (bar|" "bar" fboundp))
372 do
373 (eval `(company-elisp-with-buffer
374 ,text
375 (should (eq ',predicate
376 (company-elisp--candidates-predicate ,prefix)))))))
377
378 (ert-deftest company-elisp-finds-vars ()
379 (let ((obarray [boo bar baz backquote])
380 (boo t)
381 (bar t)
382 (baz t))
383 (should (equal '("bar" "baz")
384 (company-elisp--globals "ba" 'boundp)))))
385
386 (ert-deftest company-elisp-finds-functions ()
387 (let ((obarray [when what whelp])
388 (what t)
389 (whelp t))
390 (should (equal '("when")
391 (company-elisp--globals "wh" 'fboundp)))))
392
393 (ert-deftest company-elisp-finds-things ()
394 (let ((obarray [when what whelp])
395 (what t)
396 (whelp t))
397 (should (equal '("what" "whelp" "when")
398 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
399 'string<)))))
400
401 (ert-deftest company-elisp-locals-vars ()
402 (company-elisp-with-buffer
403 "(let ((foo 5) (bar 6))
404 (cl-labels ((borg ()))
405 (lambda (boo baz)
406 b|)))"
407 (should (equal '("bar" "baz" "boo")
408 (company-elisp--locals "b" nil)))))
409
410 (ert-deftest company-elisp-locals-single-var ()
411 (company-elisp-with-buffer
412 "(dotimes (itk 100)
413 (dolist (item items)
414 it|))"
415 (should (equal '("itk" "item")
416 (company-elisp--locals "it" nil)))))
417
418 (ert-deftest company-elisp-locals-funs ()
419 (company-elisp-with-buffer
420 "(cl-labels ((foo ())
421 (fee ()))
422 (let ((fun 4))
423 (f| )))"
424 (should (equal '("fee" "foo")
425 (sort (company-elisp--locals "f" t) 'string<)))))
426
427 (ert-deftest company-elisp-locals-skips-current-varlist ()
428 (company-elisp-with-buffer
429 "(let ((foo 1)
430 (f| )))"
431 (should (null (company-elisp--locals "f" nil)))))
432
433 (ert-deftest company-elisp-show-locals-first ()
434 (company-elisp-with-buffer
435 "(let ((floo 1)
436 (flop 2)
437 (flee 3))
438 fl|)"
439 (let ((obarray [float-pi]))
440 (let (company-elisp-show-locals-first)
441 (should (eq nil (company-elisp 'sorted))))
442 (let ((company-elisp-show-locals-first t))
443 (should (eq t (company-elisp 'sorted)))
444 (should (equal '("flee" "floo" "flop" "float-pi")
445 (company-elisp-candidates "fl")))))))
446
447 (ert-deftest company-elisp-candidates-no-duplicates ()
448 (company-elisp-with-buffer
449 "(let ((float-pi 4))
450 f|)"
451 (let ((obarray [float-pi])
452 (company-elisp-show-locals-first t))
453 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
454
455 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
456 (company-elisp-with-buffer
457 "(defun foob|)"
458 (should (null (company-elisp 'prefix)))))
459
460 (ert-deftest company-elisp-should-complete-def-call ()
461 (company-elisp-with-buffer
462 "(defu|"
463 (should (equal "defu" (company-elisp 'prefix)))))
464
465 (ert-deftest company-elisp-should-complete-in-defvar ()
466 ;; It will also complete the var name, at least for now.
467 (company-elisp-with-buffer
468 "(defvar abc de|"
469 (should (equal "de" (company-elisp 'prefix)))))
470
471 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
472 (company-elisp-with-buffer
473 "(defsubst foobar (ba|"
474 (should (null (company-elisp 'prefix)))))
475
476 (ert-deftest company-elisp-prefix-in-defun-body ()
477 (company-elisp-with-buffer
478 "(defun foob ()|)"
479 (should (equal "" (company-elisp 'prefix)))))
480
481 ;;; Clang
482
483 (ert-deftest company-clang-objc-templatify ()
484 (with-temp-buffer
485 (let ((text "createBookWithTitle:andAuthor:"))
486 (insert text)
487 (company-clang-objc-templatify text)
488 (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
489 (should (looking-at "arg0"))
490 (should (null (overlay-get (company-template-field-at) 'display))))))