]> code.delx.au - gnu-emacs-elpa/blob - test/core-tests.el
Reorganize tests layout
[gnu-emacs-elpa] / test / core-tests.el
1 ;;; core-tests.el --- company-mode tests -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Dmitry Gutov
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 (require 'company-tests)
23
24 (ert-deftest company-good-prefix ()
25 (let ((company-minimum-prefix-length 5)
26 company-abort-manual-when-too-short
27 company--manual-action ;idle begin
28 (company-selection-changed t)) ;has no effect
29 (should (eq t (company--good-prefix-p "!@#$%")))
30 (should (eq nil (company--good-prefix-p "abcd")))
31 (should (eq nil (company--good-prefix-p 'stop)))
32 (should (eq t (company--good-prefix-p '("foo" . 5))))
33 (should (eq nil (company--good-prefix-p '("foo" . 4))))
34 (should (eq t (company--good-prefix-p '("foo" . t))))))
35
36 (ert-deftest company--manual-prefix-set-and-unset ()
37 (with-temp-buffer
38 (insert "ab")
39 (company-mode)
40 (let (company-frontends
41 (company-backends
42 (list (lambda (command &optional _)
43 (cl-case command
44 (prefix (buffer-substring (point-min) (point)))
45 (candidates '("abc" "abd")))))))
46 (company-manual-begin)
47 (should (equal "ab" company--manual-prefix))
48 (company-abort)
49 (should (null company--manual-prefix)))))
50
51 (ert-deftest company-abort-manual-when-too-short ()
52 (let ((company-minimum-prefix-length 5)
53 (company-abort-manual-when-too-short t)
54 (company-selection-changed t)) ;has not effect
55 (let ((company--manual-action nil)) ;idle begin
56 (should (eq t (company--good-prefix-p "!@#$%")))
57 (should (eq t (company--good-prefix-p '("foo" . 5))))
58 (should (eq t (company--good-prefix-p '("foo" . t)))))
59 (let ((company--manual-action t)
60 (company--manual-prefix "abc")) ;manual begin from this prefix
61 (should (eq t (company--good-prefix-p "!@#$")))
62 (should (eq nil (company--good-prefix-p "ab")))
63 (should (eq nil (company--good-prefix-p 'stop)))
64 (should (eq t (company--good-prefix-p '("foo" . 4))))
65 (should (eq t (company--good-prefix-p "abcd")))
66 (should (eq t (company--good-prefix-p "abc")))
67 (should (eq t (company--good-prefix-p '("bar" . t)))))))
68
69 (ert-deftest company-common-with-non-prefix-completion ()
70 (let ((company-backend #'ignore)
71 (company-prefix "abc")
72 company-candidates
73 company-candidates-length
74 company-candidates-cache
75 company-common)
76 (company-update-candidates '("abc" "def-abc"))
77 (should (null company-common))
78 (company-update-candidates '("abc" "abe-c"))
79 (should (null company-common))
80 (company-update-candidates '("abcd" "abcde" "abcdf"))
81 (should (equal "abcd" company-common))))
82
83 (ert-deftest company-multi-backend-with-lambdas ()
84 (let ((company-backend
85 (list (lambda (command &optional _ &rest _r)
86 (cl-case command
87 (prefix "z")
88 (candidates '("a" "b"))))
89 (lambda (command &optional _ &rest _r)
90 (cl-case command
91 (prefix "z")
92 (candidates '("c" "d")))))))
93 (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
94
95 (ert-deftest company-multi-backend-filters-backends-by-prefix ()
96 (let ((company-backend
97 (list (lambda (command &optional _ &rest _r)
98 (cl-case command
99 (prefix (cons "z" t))
100 (candidates '("a" "b"))))
101 (lambda (command &optional _ &rest _r)
102 (cl-case command
103 (prefix "t")
104 (candidates '("c" "d"))))
105 (lambda (command &optional _ &rest _r)
106 (cl-case command
107 (prefix "z")
108 (candidates '("e" "f")))))))
109 (should (equal (company-call-backend 'candidates "z") '("a" "b" "e" "f")))))
110
111 (ert-deftest company-multi-backend-remembers-candidate-backend ()
112 (let ((company-backend
113 (list (lambda (command &optional _)
114 (cl-case command
115 (ignore-case nil)
116 (annotation "1")
117 (candidates '("a" "c"))
118 (post-completion "13")))
119 (lambda (command &optional _)
120 (cl-case command
121 (ignore-case t)
122 (annotation "2")
123 (candidates '("b" "d"))
124 (post-completion "42")))
125 (lambda (command &optional _)
126 (cl-case command
127 (annotation "3")
128 (candidates '("e"))
129 (post-completion "74"))))))
130 (let ((candidates (company-calculate-candidates nil)))
131 (should (equal candidates '("a" "b" "c" "d" "e")))
132 (should (equal t (company-call-backend 'ignore-case)))
133 (should (equal "1" (company-call-backend 'annotation (nth 0 candidates))))
134 (should (equal "2" (company-call-backend 'annotation (nth 1 candidates))))
135 (should (equal "13" (company-call-backend 'post-completion (nth 2 candidates))))
136 (should (equal "42" (company-call-backend 'post-completion (nth 3 candidates))))
137 (should (equal "3" (company-call-backend 'annotation (nth 4 candidates))))
138 (should (equal "74" (company-call-backend 'post-completion (nth 4 candidates)))))))
139
140 (ert-deftest company-multi-backend-handles-keyword-with ()
141 (let ((primo (lambda (command &optional _)
142 (cl-case command
143 (prefix "a")
144 (candidates '("abb" "abc" "abd")))))
145 (secundo (lambda (command &optional _)
146 (cl-case command
147 (prefix "a")
148 (candidates '("acc" "acd"))))))
149 (let ((company-backend (list 'ignore 'ignore :with secundo)))
150 (should (null (company-call-backend 'prefix))))
151 (let ((company-backend (list 'ignore primo :with secundo)))
152 (should (equal "a" (company-call-backend 'prefix)))
153 (should (equal '("abb" "abc" "abd" "acc" "acd")
154 (company-call-backend 'candidates "a"))))))
155
156 (ert-deftest company-begin-backend-failure-doesnt-break-company-backends ()
157 (with-temp-buffer
158 (insert "a")
159 (company-mode)
160 (should-error
161 (company-begin-backend #'ignore))
162 (let (company-frontends
163 (company-backends
164 (list (lambda (command &optional _)
165 (cl-case command
166 (prefix "a")
167 (candidates '("a" "ab" "ac")))))))
168 (let (this-command)
169 (company-call 'complete))
170 (should (eq 3 company-candidates-length)))))
171
172 (ert-deftest company-require-match-explicit ()
173 (with-temp-buffer
174 (insert "ab")
175 (company-mode)
176 (let (company-frontends
177 (company-require-match 'company-explicit-action-p)
178 (company-backends
179 (list (lambda (command &optional _)
180 (cl-case command
181 (prefix (buffer-substring (point-min) (point)))
182 (candidates '("abc" "abd")))))))
183 (let (this-command)
184 (company-complete))
185 (let ((last-command-event ?e))
186 (company-call 'self-insert-command 1))
187 (should (eq 2 company-candidates-length))
188 (should (eq 3 (point))))))
189
190 (ert-deftest company-dont-require-match-when-idle ()
191 (with-temp-buffer
192 (insert "ab")
193 (company-mode)
194 (let (company-frontends
195 (company-minimum-prefix-length 2)
196 (company-require-match 'company-explicit-action-p)
197 (company-backends
198 (list (lambda (command &optional _)
199 (cl-case command
200 (prefix (buffer-substring (point-min) (point)))
201 (candidates '("abc" "abd")))))))
202 (company-idle-begin (current-buffer) (selected-window)
203 (buffer-chars-modified-tick) (point))
204 (should (eq 2 company-candidates-length))
205 (let ((last-command-event ?e))
206 (company-call 'self-insert-command 1))
207 (should (eq nil company-candidates-length))
208 (should (eq 4 (point))))))
209
210 (ert-deftest company-dont-require-match-if-old-prefix-ended-and-was-a-match ()
211 (with-temp-buffer
212 (insert "ab")
213 (company-mode)
214 (let (company-frontends
215 (company-require-match 'company-explicit-action-p)
216 (company-backends
217 (list (lambda (command &optional _)
218 (cl-case command
219 (prefix (company-grab-word))
220 (candidates '("abc" "ab" "abd"))
221 (sorted t))))))
222 (let (this-command)
223 (company-complete))
224 (let ((last-command-event ?e))
225 (company-call 'self-insert-command 1))
226 (should (eq 3 company-candidates-length))
227 (should (eq 3 (point)))
228 (let ((last-command-event ? ))
229 (company-call 'self-insert-command 1))
230 (should (null company-candidates-length))
231 (should (eq 4 (point))))))
232
233 (ert-deftest company-should-complete-whitelist ()
234 (with-temp-buffer
235 (insert "ab")
236 (company-mode)
237 (let (company-frontends
238 company-begin-commands
239 (company-backends
240 (list (lambda (command &optional _)
241 (cl-case command
242 (prefix (buffer-substring (point-min) (point)))
243 (candidates '("abc" "abd")))))))
244 (let ((company-continue-commands nil))
245 (let (this-command)
246 (company-complete))
247 (company-call 'backward-delete-char 1)
248 (should (null company-candidates-length)))
249 (let ((company-continue-commands '(backward-delete-char)))
250 (let (this-command)
251 (company-complete))
252 (company-call 'backward-delete-char 1)
253 (should (eq 2 company-candidates-length))))))
254
255 (ert-deftest company-should-complete-blacklist ()
256 (with-temp-buffer
257 (insert "ab")
258 (company-mode)
259 (let (company-frontends
260 company-begin-commands
261 (company-backends
262 (list (lambda (command &optional _)
263 (cl-case command
264 (prefix (buffer-substring (point-min) (point)))
265 (candidates '("abc" "abd")))))))
266 (let ((company-continue-commands '(not backward-delete-char)))
267 (let (this-command)
268 (company-complete))
269 (company-call 'backward-delete-char 1)
270 (should (null company-candidates-length)))
271 (let ((company-continue-commands '(not backward-delete-char-untabify)))
272 (let (this-command)
273 (company-complete))
274 (company-call 'backward-delete-char 1)
275 (should (eq 2 company-candidates-length))))))
276
277 (ert-deftest company-auto-complete-explicit ()
278 (with-temp-buffer
279 (insert "ab")
280 (company-mode)
281 (let (company-frontends
282 (company-auto-complete 'company-explicit-action-p)
283 (company-auto-complete-chars '(? ))
284 (company-backends
285 (list (lambda (command &optional _)
286 (cl-case command
287 (prefix (buffer-substring (point-min) (point)))
288 (candidates '("abcd" "abef")))))))
289 (let (this-command)
290 (company-complete))
291 (let ((last-command-event ? ))
292 (company-call 'self-insert-command 1))
293 (should (string= "abcd " (buffer-string))))))
294
295 (ert-deftest company-no-auto-complete-when-idle ()
296 (with-temp-buffer
297 (insert "ab")
298 (company-mode)
299 (let (company-frontends
300 (company-auto-complete 'company-explicit-action-p)
301 (company-auto-complete-chars '(? ))
302 (company-minimum-prefix-length 2)
303 (company-backends
304 (list (lambda (command &optional _)
305 (cl-case command
306 (prefix (buffer-substring (point-min) (point)))
307 (candidates '("abcd" "abef")))))))
308 (company-idle-begin (current-buffer) (selected-window)
309 (buffer-chars-modified-tick) (point))
310 (let ((last-command-event ? ))
311 (company-call 'self-insert-command 1))
312 (should (string= "ab " (buffer-string))))))
313
314 (ert-deftest company-clears-explicit-action-when-no-matches ()
315 (with-temp-buffer
316 (company-mode)
317 (let (company-frontends
318 company-backends)
319 (company-call 'manual-begin) ;; fails
320 (should (null company-candidates))
321 (should (null (company-explicit-action-p))))))
322
323 (ert-deftest company-ignore-case-replaces-prefix ()
324 (with-temp-buffer
325 (company-mode)
326 (let (company-frontends
327 (company-backends
328 (list (lambda (command &optional _)
329 (cl-case command
330 (prefix (buffer-substring (point-min) (point)))
331 (candidates '("abcd" "abef"))
332 (ignore-case t))))))
333 (insert "A")
334 (let (this-command)
335 (company-complete))
336 (should (string= "ab" (buffer-string)))
337 (delete-char -2)
338 (insert "A") ; hack, to keep it in one test
339 (company-complete-selection)
340 (should (string= "abcd" (buffer-string))))))
341
342 (ert-deftest company-ignore-case-with-keep-prefix ()
343 (with-temp-buffer
344 (insert "AB")
345 (company-mode)
346 (let (company-frontends
347 (company-backends
348 (list (lambda (command &optional _)
349 (cl-case command
350 (prefix (buffer-substring (point-min) (point)))
351 (candidates '("abcd" "abef"))
352 (ignore-case 'keep-prefix))))))
353 (let (this-command)
354 (company-complete))
355 (company-complete-selection)
356 (should (string= "ABcd" (buffer-string))))))
357
358 (ert-deftest company-non-prefix-completion ()
359 (with-temp-buffer
360 (insert "tc")
361 (company-mode)
362 (let (company-frontends
363 (company-backends
364 (list (lambda (command &optional _)
365 (cl-case command
366 (prefix (buffer-substring (point-min) (point)))
367 (candidates '("tea-cup" "teal-color")))))))
368 (let (this-command)
369 (company-complete))
370 (should (string= "tc" (buffer-string)))
371 (company-complete-selection)
372 (should (string= "tea-cup" (buffer-string))))))
373
374 ;;; Row and column
375
376 (ert-deftest company-column-with-composition ()
377 :tags '(interactive)
378 (with-temp-buffer
379 (save-window-excursion
380 (set-window-buffer nil (current-buffer))
381 (insert "lambda ()")
382 (compose-region 1 (1+ (length "lambda")) "\\")
383 (should (= (company--column) 4)))))
384
385 (ert-deftest company-column-with-line-prefix ()
386 :tags '(interactive)
387 (with-temp-buffer
388 (save-window-excursion
389 (set-window-buffer nil (current-buffer))
390 (insert "foo")
391 (put-text-property (point-min) (point) 'line-prefix " ")
392 (should (= (company--column) 5)))))
393
394 (ert-deftest company-column-with-line-prefix-on-empty-line ()
395 :tags '(interactive)
396 (with-temp-buffer
397 (save-window-excursion
398 (set-window-buffer nil (current-buffer))
399 (insert "\n")
400 (forward-char -1)
401 (put-text-property (point-min) (point-max) 'line-prefix " ")
402 (should (= (company--column) 2)))))
403
404 (ert-deftest company-column-with-tabs ()
405 :tags '(interactive)
406 (with-temp-buffer
407 (save-window-excursion
408 (set-window-buffer nil (current-buffer))
409 (insert "|\t|\t|\t(")
410 (let ((tab-width 8))
411 (should (= (company--column) 25))))))
412
413 (ert-deftest company-row-with-header-line-format ()
414 :tags '(interactive)
415 (with-temp-buffer
416 (save-window-excursion
417 (set-window-buffer nil (current-buffer))
418 (should (= (company--row) 0))
419 (setq header-line-format "aaaaaaa")
420 (should (= (company--row) 0)))))