]> code.delx.au - gnu-emacs-elpa/blob - company-tests.el
company-elisp-locals: Skip current varlist
[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-pseudo-tooltip-does-not-get-displaced ()
152 (with-temp-buffer
153 (save-window-excursion
154 (set-window-buffer nil (current-buffer))
155 (save-excursion (insert " ff"))
156 (company-mode)
157 (let ((company-frontends '(company-pseudo-tooltip-frontend))
158 (company-begin-commands '(self-insert-command))
159 (company-backends
160 (list (lambda (c &optional arg)
161 (case c (prefix "") (candidates '("a" "b" "c")))))))
162 (let (this-command)
163 (company-call 'complete))
164 (company-call 'open-line 1)
165 (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
166
167 (ert-deftest company-template-removed-after-the-last-jump ()
168 (with-temp-buffer
169 (insert "{ }")
170 (goto-char 2)
171 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
172 (save-excursion
173 (dotimes (i 2)
174 (insert " ")
175 (company-template-add-field tpl (point) "foo")
176 (forward-char 3)))
177 (company-call 'template-forward-field)
178 (should (= 3 (point)))
179 (company-call 'template-forward-field)
180 (should (= 7 (point)))
181 (company-call 'template-forward-field)
182 (should (= 11 (point)))
183 (should (zerop (length (overlay-get tpl 'company-template-fields))))
184 (should (null (overlay-buffer tpl))))))
185
186 (ert-deftest company-template-removed-after-input-and-jump ()
187 (with-temp-buffer
188 (insert "{ }")
189 (goto-char 2)
190 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
191 (save-excursion
192 (insert " ")
193 (company-template-add-field tpl (point) "bar"))
194 (company-call 'template-move-to-first tpl)
195 (should (= 3 (point)))
196 (dolist (c (string-to-list "tee"))
197 (let ((last-command-event c))
198 (company-call 'self-insert-command 1)))
199 (should (string= "{ tee }" (buffer-string)))
200 (should (overlay-buffer tpl))
201 (company-call 'template-forward-field)
202 (should (= 7 (point)))
203 (should (null (overlay-buffer tpl))))))
204
205 (defun company-call (name &rest args)
206 (let* ((maybe (intern (format "company-%s" name)))
207 (command (if (fboundp maybe) maybe name)))
208 (apply command args)
209 (let ((this-command command))
210 (run-hooks 'post-command-hook))))
211
212 (defmacro company-elisp-with-buffer (contents &rest body)
213 (declare (indent 0))
214 `(with-temp-buffer
215 (insert ,contents)
216 (re-search-backward "|")
217 (replace-match "")
218 ,@body))
219
220 (ert-deftest company-elisp-candidates-predicate ()
221 (company-elisp-with-buffer
222 "(foo ba|)"
223 (should (eq (let ((company-elisp-detect-function-context t))
224 (company-elisp-candidates-predicate "ba"))
225 'boundp))
226 (should (eq (let (company-elisp-detect-function-context)
227 (company-elisp-candidates-predicate "ba"))
228 'company-elisp-predicate)))
229 (company-elisp-with-buffer
230 "(foo| )"
231 (should (eq (let ((company-elisp-detect-function-context t))
232 (company-elisp-candidates-predicate "foo"))
233 'fboundp))
234 (should (eq (let (company-elisp-detect-function-context)
235 (company-elisp-candidates-predicate "foo"))
236 'company-elisp-predicate)))
237 (company-elisp-with-buffer
238 "(foo 'b|)"
239 (should (eq (let ((company-elisp-detect-function-context t))
240 (company-elisp-candidates-predicate "b"))
241 'company-elisp-predicate))))
242
243 ;; This one's also an integration test.
244 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
245 (company-elisp-with-buffer
246 "(let ((foo 7) (wh| )))"
247 (let ((obarray [when what whelp])
248 (what 1)
249 (whelp 2)
250 (wisp 3))
251 (should (equal '("what" "whelp")
252 (let ((company-elisp-detect-function-context t))
253 (company-elisp-candidates "wh")))))))
254
255 (ert-deftest company-elisp-finds-vars ()
256 (let ((obarray [boo bar baz backquote])
257 (boo t)
258 (bar t)
259 (baz t))
260 (should (equal '("bar" "baz")
261 (company-elisp-globals "ba" 'boundp)))))
262
263 (ert-deftest company-elisp-finds-functions ()
264 (let ((obarray [when what whelp])
265 (what t)
266 (whelp t))
267 (should (equal '("when")
268 (company-elisp-globals "wh" 'fboundp)))))
269
270 (ert-deftest company-elisp-finds-things ()
271 (let ((obarray [when what whelp])
272 (what t)
273 (whelp t))
274 (should (equal '("what" "whelp" "when")
275 (sort (company-elisp-globals "wh" 'company-elisp-predicate)
276 'string<)))))
277
278 (ert-deftest company-elisp-locals-vars ()
279 (company-elisp-with-buffer
280 "(let ((foo 5) (bar 6))
281 (cl-labels ((borg ()))
282 (lambda (boo baz)
283 b|)))"
284 (should (equal '("bar" "baz" "boo")
285 (company-elisp-locals "b" nil)))))
286
287 (ert-deftest company-elisp-locals-single-var ()
288 (company-elisp-with-buffer
289 "(dotimes (itk 100)
290 (dolist (item items)
291 it|))"
292 (should (equal '("itk" "item")
293 (company-elisp-locals "it" nil)))))
294
295 (ert-deftest company-elisp-locals-funs ()
296 (company-elisp-with-buffer
297 "(cl-labels ((foo ())
298 (fee ()))
299 (let ((fun 4))
300 (f| )))"
301 (should (equal '("fee" "foo")
302 (sort (company-elisp-locals "f" t) 'string<)))))
303
304 (ert-deftest company-elisp-locals-skips-current-varlist ()
305 (company-elisp-with-buffer
306 "(let ((foo 1)
307 (f| )))"
308 (should (null (company-elisp-locals "f" nil)))))