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