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