]> code.delx.au - gnu-emacs-elpa/blob - company-tests.el
Check in NEWS and tests for the previous commit
[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 (with-temp-buffer
184 (save-window-excursion
185 (set-window-buffer nil (current-buffer))
186 (insert "aaaa\n bb\nccccc\nddd")
187 (search-backward "bb")
188 (let ((col (company--column))
189 (company-candidates-length 2)
190 (company-candidates '("123" "45")))
191 (company-pseudo-tooltip-show (company--row) col 0)
192 (let ((ov company-pseudo-tooltip-overlay))
193 (should (eq (overlay-get ov 'company-width) 3))
194 ;; FIXME: Make it 2?
195 (should (eq (overlay-get ov 'company-height) company-tooltip-limit))
196 (should (eq (overlay-get ov 'company-column) col))
197 (should (string= (overlay-get ov 'company-before)
198 " 123\nc45 c\nddd\n")))))))
199
200 ;;; Template
201
202 (ert-deftest company-template-removed-after-the-last-jump ()
203 (with-temp-buffer
204 (insert "{ }")
205 (goto-char 2)
206 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
207 (save-excursion
208 (dotimes (i 2)
209 (insert " ")
210 (company-template-add-field tpl (point) "foo")))
211 (company-call 'template-forward-field)
212 (should (= 3 (point)))
213 (company-call 'template-forward-field)
214 (should (= 7 (point)))
215 (company-call 'template-forward-field)
216 (should (= 11 (point)))
217 (should (zerop (length (overlay-get tpl 'company-template-fields))))
218 (should (null (overlay-buffer tpl))))))
219
220 (ert-deftest company-template-removed-after-input-and-jump ()
221 (with-temp-buffer
222 (insert "{ }")
223 (goto-char 2)
224 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
225 (save-excursion
226 (insert " ")
227 (company-template-add-field tpl (point) "bar"))
228 (company-call 'template-move-to-first tpl)
229 (should (= 3 (point)))
230 (dolist (c (string-to-list "tee"))
231 (let ((last-command-event c))
232 (company-call 'self-insert-command 1)))
233 (should (string= "{ tee }" (buffer-string)))
234 (should (overlay-buffer tpl))
235 (company-call 'template-forward-field)
236 (should (= 7 (point)))
237 (should (null (overlay-buffer tpl))))))
238
239 (defun company-call (name &rest args)
240 (let* ((maybe (intern (format "company-%s" name)))
241 (command (if (fboundp maybe) maybe name)))
242 (apply command args)
243 (let ((this-command command))
244 (run-hooks 'post-command-hook))))
245
246 (ert-deftest company-template-c-like-templatify ()
247 (with-temp-buffer
248 (let ((text "foo(int a, short b)"))
249 (insert text)
250 (company-template-c-like-templatify text)
251 (should (equal "foo(arg0, arg1)" (buffer-string)))
252 (should (looking-at "arg0"))
253 (should (equal "int a"
254 (overlay-get (company-template-field-at) 'display))))))
255
256 ;;; Elisp
257
258 (defmacro company-elisp-with-buffer (contents &rest body)
259 (declare (indent 0))
260 `(with-temp-buffer
261 (insert ,contents)
262 (setq major-mode 'emacs-lisp-mode)
263 (re-search-backward "|")
264 (replace-match "")
265 (let ((company-elisp-detect-function-context t))
266 ,@body)))
267
268 (ert-deftest company-elisp-candidates-predicate ()
269 (company-elisp-with-buffer
270 "(foo ba|)"
271 (should (eq (company-elisp--candidates-predicate "ba")
272 'boundp))
273 (should (eq (let (company-elisp-detect-function-context)
274 (company-elisp--candidates-predicate "ba"))
275 'company-elisp--predicate)))
276 (company-elisp-with-buffer
277 "(foo| )"
278 (should (eq (company-elisp--candidates-predicate "foo")
279 'fboundp))
280 (should (eq (let (company-elisp-detect-function-context)
281 (company-elisp--candidates-predicate "foo"))
282 'company-elisp--predicate)))
283 (company-elisp-with-buffer
284 "(foo 'b|)"
285 (should (eq (company-elisp--candidates-predicate "b")
286 'company-elisp--predicate))))
287
288 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
289 (company-elisp-with-buffer
290 "(def foo () \"Doo be doo `ide|"
291 (should (eq 'company-elisp--predicate
292 (company-elisp--candidates-predicate "ide")))))
293
294 ;; This one's also an integration test.
295 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
296 (let ((company-elisp-detect-function-context t)
297 (obarray [when what whelp])
298 (what 1)
299 (whelp 2)
300 (wisp 3))
301 (company-elisp-with-buffer
302 "(let ((foo 7) (wh| )))"
303 (should (equal '("what" "whelp")
304 (company-elisp-candidates "wh"))))
305 (company-elisp-with-buffer
306 "(cond ((null nil) (wh| )))"
307 (should (equal '("when")
308 (company-elisp-candidates "wh"))))))
309
310 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
311 (loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
312 ("(let (foo (bar|" "bar" boundp)
313 ("(let (foo) (bar|" "bar" fboundp))
314 do
315 (eval `(company-elisp-with-buffer
316 ,text
317 (should (eq ',predicate
318 (company-elisp--candidates-predicate ,prefix)))))))
319
320 (ert-deftest company-elisp-finds-vars ()
321 (let ((obarray [boo bar baz backquote])
322 (boo t)
323 (bar t)
324 (baz t))
325 (should (equal '("bar" "baz")
326 (company-elisp--globals "ba" 'boundp)))))
327
328 (ert-deftest company-elisp-finds-functions ()
329 (let ((obarray [when what whelp])
330 (what t)
331 (whelp t))
332 (should (equal '("when")
333 (company-elisp--globals "wh" 'fboundp)))))
334
335 (ert-deftest company-elisp-finds-things ()
336 (let ((obarray [when what whelp])
337 (what t)
338 (whelp t))
339 (should (equal '("what" "whelp" "when")
340 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
341 'string<)))))
342
343 (ert-deftest company-elisp-locals-vars ()
344 (company-elisp-with-buffer
345 "(let ((foo 5) (bar 6))
346 (cl-labels ((borg ()))
347 (lambda (boo baz)
348 b|)))"
349 (should (equal '("bar" "baz" "boo")
350 (company-elisp--locals "b" nil)))))
351
352 (ert-deftest company-elisp-locals-single-var ()
353 (company-elisp-with-buffer
354 "(dotimes (itk 100)
355 (dolist (item items)
356 it|))"
357 (should (equal '("itk" "item")
358 (company-elisp--locals "it" nil)))))
359
360 (ert-deftest company-elisp-locals-funs ()
361 (company-elisp-with-buffer
362 "(cl-labels ((foo ())
363 (fee ()))
364 (let ((fun 4))
365 (f| )))"
366 (should (equal '("fee" "foo")
367 (sort (company-elisp--locals "f" t) 'string<)))))
368
369 (ert-deftest company-elisp-locals-skips-current-varlist ()
370 (company-elisp-with-buffer
371 "(let ((foo 1)
372 (f| )))"
373 (should (null (company-elisp--locals "f" nil)))))
374
375 (ert-deftest company-elisp-show-locals-first ()
376 (company-elisp-with-buffer
377 "(let ((floo 1)
378 (flop 2)
379 (flee 3))
380 fl|)"
381 (let ((obarray [float-pi]))
382 (let (company-elisp-show-locals-first)
383 (should (eq nil (company-elisp 'sorted))))
384 (let ((company-elisp-show-locals-first t))
385 (should (eq t (company-elisp 'sorted)))
386 (should (equal '("flee" "floo" "flop" "float-pi")
387 (company-elisp-candidates "fl")))))))
388
389 (ert-deftest company-elisp-candidates-no-duplicates ()
390 (company-elisp-with-buffer
391 "(let ((float-pi 4))
392 f|)"
393 (let ((obarray [float-pi])
394 (company-elisp-show-locals-first t))
395 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
396
397 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
398 (company-elisp-with-buffer
399 "(defun foob|)"
400 (should (null (company-elisp 'prefix)))))
401
402 (ert-deftest company-elisp-should-complete-def-call ()
403 (company-elisp-with-buffer
404 "(defu|"
405 (should (equal "defu" (company-elisp 'prefix)))))
406
407 (ert-deftest company-elisp-should-complete-in-defvar ()
408 ;; It will also complete the var name, at least for now.
409 (company-elisp-with-buffer
410 "(defvar abc de|"
411 (should (equal "de" (company-elisp 'prefix)))))
412
413 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
414 (company-elisp-with-buffer
415 "(defsubst foobar (ba|"
416 (should (null (company-elisp 'prefix)))))
417
418 (ert-deftest company-elisp-prefix-in-defun-body ()
419 (company-elisp-with-buffer
420 "(defun foob ()|)"
421 (should (equal "" (company-elisp 'prefix)))))
422
423 ;;; Clang
424
425 (ert-deftest company-clang-objc-templatify ()
426 (with-temp-buffer
427 (let ((text "createBookWithTitle:andAuthor:"))
428 (insert text)
429 (company-clang-objc-templatify text)
430 (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
431 (should (looking-at "arg0"))
432 (should (null (overlay-get (company-template-field-at) 'display))))))