1 ;;; core-tests.el --- company-mode tests -*- lexical-binding: t -*-
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Dmitry Gutov
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
22 (require 'company-tests)
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))))))
36 (ert-deftest company--manual-prefix-set-and-unset ()
40 (let (company-frontends
42 (list (lambda (command &optional _)
44 (prefix (buffer-substring (point-min) (point)))
45 (candidates '("abc" "abd")))))))
46 (company-manual-begin)
47 (should (equal "ab" company--manual-prefix))
49 (should (null company--manual-prefix)))))
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)))))))
69 (ert-deftest company-common-with-non-prefix-completion ()
70 (let ((company-backend #'ignore)
71 (company-prefix "abc")
73 company-candidates-length
74 company-candidates-cache
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))))
83 (ert-deftest company-multi-backend-with-lambdas ()
84 (let ((company-backend
85 (list (lambda (command &optional _ &rest _r)
88 (candidates '("a" "b"))))
89 (lambda (command &optional _ &rest _r)
92 (candidates '("c" "d")))))))
93 (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
95 (ert-deftest company-multi-backend-filters-backends-by-prefix ()
96 (let ((company-backend
97 (list (lambda (command &optional _ &rest _r)
100 (candidates '("a" "b"))))
101 (lambda (command &optional _ &rest _r)
104 (candidates '("c" "d"))))
105 (lambda (command &optional _ &rest _r)
108 (candidates '("e" "f")))))))
109 (should (equal (company-call-backend 'candidates "z") '("a" "b" "e" "f")))))
111 (ert-deftest company-multi-backend-remembers-candidate-backend ()
112 (let ((company-backend
113 (list (lambda (command &optional _)
117 (candidates '("a" "c"))
118 (post-completion "13")))
119 (lambda (command &optional _)
123 (candidates '("b" "d"))
124 (post-completion "42")))
125 (lambda (command &optional _)
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)))))))
140 (ert-deftest company-multi-backend-handles-keyword-with ()
141 (let ((primo (lambda (command &optional _)
144 (candidates '("abb" "abc" "abd")))))
145 (secundo (lambda (command &optional _)
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"))))))
156 (ert-deftest company-begin-backend-failure-doesnt-break-company-backends ()
161 (company-begin-backend #'ignore))
162 (let (company-frontends
164 (list (lambda (command &optional _)
167 (candidates '("a" "ab" "ac")))))))
169 (company-call 'complete))
170 (should (eq 3 company-candidates-length)))))
172 (ert-deftest company-require-match-explicit ()
176 (let (company-frontends
177 (company-require-match 'company-explicit-action-p)
179 (list (lambda (command &optional _)
181 (prefix (buffer-substring (point-min) (point)))
182 (candidates '("abc" "abd")))))))
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))))))
190 (ert-deftest company-dont-require-match-when-idle ()
194 (let (company-frontends
195 (company-minimum-prefix-length 2)
196 (company-require-match 'company-explicit-action-p)
198 (list (lambda (command &optional _)
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))))))
210 (ert-deftest company-dont-require-match-if-was-a-match-and-old-prefix-ended ()
214 (let (company-frontends
215 (company-require-match t)
217 (list (lambda (command &optional _)
219 (prefix (company-grab-word))
220 (candidates '("abc" "ab" "abd"))
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))))))
233 (ert-deftest company-dont-require-match-if-was-a-match-and-new-prefix-is-stop ()
237 (let (company-frontends
238 (company-require-match t)
240 (list (lambda (command &optional _)
242 (prefix (if (> (point) 2)
244 (buffer-substring (point-min) (point))))
245 (candidates '("a" "b" "c")))))))
248 (should (eq 3 company-candidates-length))
249 (let ((last-command-event ?e))
250 (company-call 'self-insert-command 1))
251 (should (not company-candidates)))))
253 (ert-deftest company-should-complete-whitelist ()
257 (let (company-frontends
258 company-begin-commands
260 (list (lambda (command &optional _)
262 (prefix (buffer-substring (point-min) (point)))
263 (candidates '("abc" "abd")))))))
264 (let ((company-continue-commands nil))
267 (company-call 'backward-delete-char 1)
268 (should (null company-candidates-length)))
269 (let ((company-continue-commands '(backward-delete-char)))
272 (company-call 'backward-delete-char 1)
273 (should (eq 2 company-candidates-length))))))
275 (ert-deftest company-should-complete-blacklist ()
279 (let (company-frontends
280 company-begin-commands
282 (list (lambda (command &optional _)
284 (prefix (buffer-substring (point-min) (point)))
285 (candidates '("abc" "abd")))))))
286 (let ((company-continue-commands '(not backward-delete-char)))
289 (company-call 'backward-delete-char 1)
290 (should (null company-candidates-length)))
291 (let ((company-continue-commands '(not backward-delete-char-untabify)))
294 (company-call 'backward-delete-char 1)
295 (should (eq 2 company-candidates-length))))))
297 (ert-deftest company-auto-complete-explicit ()
301 (let (company-frontends
302 (company-auto-complete 'company-explicit-action-p)
303 (company-auto-complete-chars '(? ))
305 (list (lambda (command &optional _)
307 (prefix (buffer-substring (point-min) (point)))
308 (candidates '("abcd" "abef")))))))
311 (let ((last-command-event ? ))
312 (company-call 'self-insert-command 1))
313 (should (string= "abcd " (buffer-string))))))
315 (ert-deftest company-no-auto-complete-when-idle ()
319 (let (company-frontends
320 (company-auto-complete 'company-explicit-action-p)
321 (company-auto-complete-chars '(? ))
322 (company-minimum-prefix-length 2)
324 (list (lambda (command &optional _)
326 (prefix (buffer-substring (point-min) (point)))
327 (candidates '("abcd" "abef")))))))
328 (company-idle-begin (current-buffer) (selected-window)
329 (buffer-chars-modified-tick) (point))
330 (let ((last-command-event ? ))
331 (company-call 'self-insert-command 1))
332 (should (string= "ab " (buffer-string))))))
334 (ert-deftest company-clears-explicit-action-when-no-matches ()
337 (let (company-frontends
339 (company-call 'manual-begin) ;; fails
340 (should (null company-candidates))
341 (should (null (company-explicit-action-p))))))
343 (ert-deftest company-ignore-case-replaces-prefix ()
346 (let (company-frontends
348 (list (lambda (command &optional _)
350 (prefix (buffer-substring (point-min) (point)))
351 (candidates '("abcd" "abef"))
356 (should (string= "ab" (buffer-string)))
358 (insert "A") ; hack, to keep it in one test
359 (company-complete-selection)
360 (should (string= "abcd" (buffer-string))))))
362 (ert-deftest company-ignore-case-with-keep-prefix ()
366 (let (company-frontends
368 (list (lambda (command &optional _)
370 (prefix (buffer-substring (point-min) (point)))
371 (candidates '("abcd" "abef"))
372 (ignore-case 'keep-prefix))))))
375 (company-complete-selection)
376 (should (string= "ABcd" (buffer-string))))))
378 (ert-deftest company-non-prefix-completion ()
382 (let (company-frontends
384 (list (lambda (command &optional _)
386 (prefix (buffer-substring (point-min) (point)))
387 (candidates '("tea-cup" "teal-color")))))))
390 (should (string= "tc" (buffer-string)))
391 (company-complete-selection)
392 (should (string= "tea-cup" (buffer-string))))))
396 (ert-deftest company-column-with-composition ()
399 (save-window-excursion
400 (set-window-buffer nil (current-buffer))
402 (compose-region 1 (1+ (length "lambda")) "\\")
403 (should (= (company--column) 4)))))
405 (ert-deftest company-column-with-line-prefix ()
408 (save-window-excursion
409 (set-window-buffer nil (current-buffer))
411 (put-text-property (point-min) (point) 'line-prefix " ")
412 (should (= (company--column) 5)))))
414 (ert-deftest company-column-with-line-prefix-on-empty-line ()
417 (save-window-excursion
418 (set-window-buffer nil (current-buffer))
421 (put-text-property (point-min) (point-max) 'line-prefix " ")
422 (should (= (company--column) 2)))))
424 (ert-deftest company-column-with-tabs ()
427 (save-window-excursion
428 (set-window-buffer nil (current-buffer))
429 (insert "|\t|\t|\t(")
431 (should (= (company--column) 25))))))
433 (ert-deftest company-row-with-header-line-format ()
436 (save-window-excursion
437 (set-window-buffer nil (current-buffer))
438 (should (= (company--row) 0))
439 (setq header-line-format "aaaaaaa")
440 (should (= (company--row) 0)))))