]> code.delx.au - gnu-emacs-elpa/blob - company-tests.el
Revert "company--multi-backend-adapter: Use each respective backend's prefix"
[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
32 ;;; Core
33
34 (ert-deftest company-sorted-keywords ()
35 "Test that keywords in `company-keywords-alist' are in alphabetical order."
36 (dolist (pair company-keywords-alist)
37 (when (consp (cdr pair))
38 (let ((prev (cadr pair)))
39 (dolist (next (cddr pair))
40 (should (not (equal prev next)))
41 (should (string< prev next))
42 (setq prev next))))))
43
44 (ert-deftest company-good-prefix ()
45 (let ((company-minimum-prefix-length 5)
46 company--explicit-action)
47 (should (eq t (company--good-prefix-p "!@#$%")))
48 (should (eq nil (company--good-prefix-p "abcd")))
49 (should (eq nil (company--good-prefix-p 'stop)))
50 (should (eq t (company--good-prefix-p '("foo" . 5))))
51 (should (eq nil (company--good-prefix-p '("foo" . 4))))))
52
53 (ert-deftest company-multi-backend-with-lambdas ()
54 (let ((company-backend
55 (list (lambda (command &optional arg &rest ignore)
56 (case command
57 (prefix "z")
58 (candidates '("a" "b"))))
59 (lambda (command &optional arg &rest ignore)
60 (case command
61 (prefix "z")
62 (candidates '("c" "d")))))))
63 (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
64
65 (ert-deftest company-begin-backend-failure-doesnt-break-company-backends ()
66 (with-temp-buffer
67 (insert "a")
68 (company-mode)
69 (should-error
70 (company-begin-backend (lambda (command &rest ignore))))
71 (let (company-frontends
72 (company-backends
73 (list (lambda (command &optional arg)
74 (case command
75 (prefix "a")
76 (candidates '("a" "ab" "ac")))))))
77 (let (this-command)
78 (company-call 'complete))
79 (should (eq 3 company-candidates-length)))))
80
81 (ert-deftest company-require-match-explicit ()
82 (with-temp-buffer
83 (insert "ab")
84 (company-mode)
85 (let (company-frontends
86 (company-require-match 'company-explicit-action-p)
87 (company-backends
88 (list (lambda (command &optional arg)
89 (case command
90 (prefix (buffer-substring (point-min) (point)))
91 (candidates '("abc" "abd")))))))
92 (let (this-command)
93 (company-complete))
94 (let ((last-command-event ?e))
95 (company-call 'self-insert-command 1))
96 (should (eq 2 company-candidates-length))
97 (should (eq 3 (point))))))
98
99 (ert-deftest company-dont-require-match-when-idle ()
100 (with-temp-buffer
101 (insert "ab")
102 (company-mode)
103 (let (company-frontends
104 (company-require-match 'company-explicit-action-p)
105 (company-backends
106 (list (lambda (command &optional arg)
107 (case command
108 (prefix (buffer-substring (point-min) (point)))
109 (candidates '("abc" "abd")))))))
110 (company-idle-begin (current-buffer) (selected-window)
111 (buffer-chars-modified-tick) (point))
112 (let ((last-command-event ?e))
113 (company-call 'self-insert-command 1))
114 (should (eq nil company-candidates-length))
115 (should (eq 4 (point))))))
116
117 (ert-deftest company-auto-complete-explicit ()
118 (with-temp-buffer
119 (insert "ab")
120 (company-mode)
121 (let (company-frontends
122 (company-auto-complete 'company-explicit-action-p)
123 (company-auto-complete-chars '(? ))
124 (company-backends
125 (list (lambda (command &optional arg)
126 (case command
127 (prefix (buffer-substring (point-min) (point)))
128 (candidates '("abcd" "abef")))))))
129 (let (this-command)
130 (company-complete))
131 (let ((last-command-event ? ))
132 (company-call 'self-insert-command 1))
133 (should (string= "abcd " (buffer-string))))))
134
135 (ert-deftest company-no-auto-complete-when-idle ()
136 (with-temp-buffer
137 (insert "ab")
138 (company-mode)
139 (let (company-frontends
140 (company-auto-complete 'company-explicit-action-p)
141 (company-auto-complete-chars '(? ))
142 (company-backends
143 (list (lambda (command &optional arg)
144 (case command
145 (prefix (buffer-substring (point-min) (point)))
146 (candidates '("abcd" "abef")))))))
147 (company-idle-begin (current-buffer) (selected-window)
148 (buffer-chars-modified-tick) (point))
149 (let ((last-command-event ? ))
150 (company-call 'self-insert-command 1))
151 (should (string= "ab " (buffer-string))))))
152
153 (ert-deftest company-clears-explicit-action-when-no-matches ()
154 (with-temp-buffer
155 (company-mode)
156 (let (company-frontends
157 company-backends)
158 (company-call 'manual-begin) ;; fails
159 (should (null company-candidates))
160 (should (null (company-explicit-action-p))))))
161
162 (ert-deftest company-pseudo-tooltip-does-not-get-displaced ()
163 (with-temp-buffer
164 (save-window-excursion
165 (set-window-buffer nil (current-buffer))
166 (save-excursion (insert " ff"))
167 (company-mode)
168 (let ((company-frontends '(company-pseudo-tooltip-frontend))
169 (company-begin-commands '(self-insert-command))
170 (company-backends
171 (list (lambda (c &optional arg)
172 (case c (prefix "") (candidates '("a" "b" "c")))))))
173 (let (this-command)
174 (company-call 'complete))
175 (company-call 'open-line 1)
176 (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
177
178 ;;; Template
179
180 (ert-deftest company-template-removed-after-the-last-jump ()
181 (with-temp-buffer
182 (insert "{ }")
183 (goto-char 2)
184 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
185 (save-excursion
186 (dotimes (i 2)
187 (insert " ")
188 (company-template-add-field tpl (point) "foo")))
189 (company-call 'template-forward-field)
190 (should (= 3 (point)))
191 (company-call 'template-forward-field)
192 (should (= 7 (point)))
193 (company-call 'template-forward-field)
194 (should (= 11 (point)))
195 (should (zerop (length (overlay-get tpl 'company-template-fields))))
196 (should (null (overlay-buffer tpl))))))
197
198 (ert-deftest company-template-removed-after-input-and-jump ()
199 (with-temp-buffer
200 (insert "{ }")
201 (goto-char 2)
202 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
203 (save-excursion
204 (insert " ")
205 (company-template-add-field tpl (point) "bar"))
206 (company-call 'template-move-to-first tpl)
207 (should (= 3 (point)))
208 (dolist (c (string-to-list "tee"))
209 (let ((last-command-event c))
210 (company-call 'self-insert-command 1)))
211 (should (string= "{ tee }" (buffer-string)))
212 (should (overlay-buffer tpl))
213 (company-call 'template-forward-field)
214 (should (= 7 (point)))
215 (should (null (overlay-buffer tpl))))))
216
217 (defun company-call (name &rest args)
218 (let* ((maybe (intern (format "company-%s" name)))
219 (command (if (fboundp maybe) maybe name)))
220 (apply command args)
221 (let ((this-command command))
222 (run-hooks 'post-command-hook))))
223
224 (ert-deftest company-template-c-like-templatify ()
225 (with-temp-buffer
226 (let ((text "foo(int a, short b)"))
227 (insert text)
228 (company-template-c-like-templatify text)
229 (should (equal "foo(arg0, arg1)" (buffer-string)))
230 (should (looking-at "arg0"))
231 (should (equal "int a"
232 (overlay-get (company-template-field-at) 'display))))))
233
234 ;;; Elisp
235
236 (defmacro company-elisp-with-buffer (contents &rest body)
237 (declare (indent 0))
238 `(with-temp-buffer
239 (insert ,contents)
240 (setq major-mode 'emacs-lisp-mode)
241 (re-search-backward "|")
242 (replace-match "")
243 (let ((company-elisp-detect-function-context t))
244 ,@body)))
245
246 (ert-deftest company-elisp-candidates-predicate ()
247 (company-elisp-with-buffer
248 "(foo ba|)"
249 (should (eq (company-elisp--candidates-predicate "ba")
250 'boundp))
251 (should (eq (let (company-elisp-detect-function-context)
252 (company-elisp--candidates-predicate "ba"))
253 'company-elisp--predicate)))
254 (company-elisp-with-buffer
255 "(foo| )"
256 (should (eq (company-elisp--candidates-predicate "foo")
257 'fboundp))
258 (should (eq (let (company-elisp-detect-function-context)
259 (company-elisp--candidates-predicate "foo"))
260 'company-elisp--predicate)))
261 (company-elisp-with-buffer
262 "(foo 'b|)"
263 (should (eq (company-elisp--candidates-predicate "b")
264 'company-elisp--predicate))))
265
266 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
267 (company-elisp-with-buffer
268 "(def foo () \"Doo be doo `ide|"
269 (should (eq 'company-elisp--predicate
270 (company-elisp--candidates-predicate "ide")))))
271
272 ;; This one's also an integration test.
273 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
274 (let ((company-elisp-detect-function-context t)
275 (obarray [when what whelp])
276 (what 1)
277 (whelp 2)
278 (wisp 3))
279 (company-elisp-with-buffer
280 "(let ((foo 7) (wh| )))"
281 (should (equal '("what" "whelp")
282 (company-elisp-candidates "wh"))))
283 (company-elisp-with-buffer
284 "(cond ((null nil) (wh| )))"
285 (should (equal '("when")
286 (company-elisp-candidates "wh"))))))
287
288 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
289 (loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
290 ("(let (foo (bar|" "bar" boundp)
291 ("(let (foo) (bar|" "bar" fboundp))
292 do
293 (eval `(company-elisp-with-buffer
294 ,text
295 (should (eq ',predicate
296 (company-elisp--candidates-predicate ,prefix)))))))
297
298 (ert-deftest company-elisp-finds-vars ()
299 (let ((obarray [boo bar baz backquote])
300 (boo t)
301 (bar t)
302 (baz t))
303 (should (equal '("bar" "baz")
304 (company-elisp--globals "ba" 'boundp)))))
305
306 (ert-deftest company-elisp-finds-functions ()
307 (let ((obarray [when what whelp])
308 (what t)
309 (whelp t))
310 (should (equal '("when")
311 (company-elisp--globals "wh" 'fboundp)))))
312
313 (ert-deftest company-elisp-finds-things ()
314 (let ((obarray [when what whelp])
315 (what t)
316 (whelp t))
317 (should (equal '("what" "whelp" "when")
318 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
319 'string<)))))
320
321 (ert-deftest company-elisp-locals-vars ()
322 (company-elisp-with-buffer
323 "(let ((foo 5) (bar 6))
324 (cl-labels ((borg ()))
325 (lambda (boo baz)
326 b|)))"
327 (should (equal '("bar" "baz" "boo")
328 (company-elisp--locals "b" nil)))))
329
330 (ert-deftest company-elisp-locals-single-var ()
331 (company-elisp-with-buffer
332 "(dotimes (itk 100)
333 (dolist (item items)
334 it|))"
335 (should (equal '("itk" "item")
336 (company-elisp--locals "it" nil)))))
337
338 (ert-deftest company-elisp-locals-funs ()
339 (company-elisp-with-buffer
340 "(cl-labels ((foo ())
341 (fee ()))
342 (let ((fun 4))
343 (f| )))"
344 (should (equal '("fee" "foo")
345 (sort (company-elisp--locals "f" t) 'string<)))))
346
347 (ert-deftest company-elisp-locals-skips-current-varlist ()
348 (company-elisp-with-buffer
349 "(let ((foo 1)
350 (f| )))"
351 (should (null (company-elisp--locals "f" nil)))))
352
353 (ert-deftest company-elisp-show-locals-first ()
354 (company-elisp-with-buffer
355 "(let ((floo 1)
356 (flop 2)
357 (flee 3))
358 fl|)"
359 (let ((obarray [float-pi]))
360 (let (company-elisp-show-locals-first)
361 (should (eq nil (company-elisp 'sorted))))
362 (let ((company-elisp-show-locals-first t))
363 (should (eq t (company-elisp 'sorted)))
364 (should (equal '("flee" "floo" "flop" "float-pi")
365 (company-elisp-candidates "fl")))))))
366
367 (ert-deftest company-elisp-candidates-no-duplicates ()
368 (company-elisp-with-buffer
369 "(let ((float-pi 4))
370 f|)"
371 (let ((obarray [float-pi])
372 (company-elisp-show-locals-first t))
373 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
374
375 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
376 (company-elisp-with-buffer
377 "(defun foob|)"
378 (should (null (company-elisp 'prefix)))))
379
380 (ert-deftest company-elisp-should-complete-def-call ()
381 (company-elisp-with-buffer
382 "(defu|"
383 (should (equal "defu" (company-elisp 'prefix)))))
384
385 (ert-deftest company-elisp-should-complete-in-defvar ()
386 ;; It will also complete the var name, at least for now.
387 (company-elisp-with-buffer
388 "(defvar abc de|"
389 (should (equal "de" (company-elisp 'prefix)))))
390
391 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
392 (company-elisp-with-buffer
393 "(defsubst foobar (ba|"
394 (should (null (company-elisp 'prefix)))))
395
396 (ert-deftest company-elisp-prefix-in-defun-body ()
397 (company-elisp-with-buffer
398 "(defun foob ()|)"
399 (should (equal "" (company-elisp 'prefix)))))
400
401 ;;; Clang
402
403 (ert-deftest company-clang-objc-templatify ()
404 (with-temp-buffer
405 (let ((text "createBookWithTitle:andAuthor:"))
406 (insert text)
407 (company-clang-objc-templatify text)
408 (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
409 (should (looking-at "arg0"))
410 (should (null (overlay-get (company-template-field-at) 'display))))))