]> code.delx.au - gnu-emacs-elpa/blob - test/async-tests.el
New transformer: company-sort-prefer-same-case-prefix
[gnu-emacs-elpa] / test / async-tests.el
1 ;;; async-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 (defun company-async-backend (command &optional _)
25 (pcase command
26 (`prefix "foo")
27 (`candidates
28 (cons :async
29 (lambda (cb)
30 (run-with-timer 0.05 nil
31 #'funcall cb '("abc" "abd")))))))
32
33 (ert-deftest company-call-backend-forces-sync ()
34 (let ((company-backend 'company-async-backend)
35 (company-async-timeout 0.1))
36 (should (equal '("abc" "abd") (company-call-backend 'candidates)))))
37
38 (ert-deftest company-call-backend-errors-on-timeout ()
39 (with-temp-buffer
40 (let* ((company-backend (lambda (command &optional _arg)
41 (pcase command
42 (`candidates (cons :async 'ignore)))))
43 (company-async-timeout 0.1)
44 (err (should-error (company-call-backend 'candidates "foo"))))
45 (should (string-match-p "async timeout" (cadr err))))))
46
47 (ert-deftest company-call-backend-raw-passes-return-value-verbatim ()
48 (let ((company-backend 'company-async-backend))
49 (should (equal "foo" (company-call-backend-raw 'prefix)))
50 (should (equal :async (car (company-call-backend-raw 'candidates "foo"))))
51 (should (equal 'closure (cadr (company-call-backend-raw 'candidates "foo"))))))
52
53 (ert-deftest company-manual-begin-forces-async-candidates-to-sync ()
54 (with-temp-buffer
55 (company-mode)
56 (let (company-frontends
57 company-transformers
58 (company-backends (list 'company-async-backend)))
59 (company-manual-begin)
60 (should (equal "foo" company-prefix))
61 (should (equal '("abc" "abd") company-candidates)))))
62
63 (ert-deftest company-idle-begin-allows-async-candidates ()
64 (with-temp-buffer
65 (company-mode)
66 (let (company-frontends
67 company-transformers
68 (company-backends (list 'company-async-backend)))
69 (company-idle-begin (current-buffer) (selected-window)
70 (buffer-chars-modified-tick) (point))
71 (should (null company-candidates))
72 (sleep-for 0.1)
73 (should (equal "foo" company-prefix))
74 (should (equal '("abc" "abd") company-candidates)))))
75
76 (ert-deftest company-idle-begin-cancels-async-candidates-if-buffer-changed ()
77 (with-temp-buffer
78 (company-mode)
79 (let (company-frontends
80 (company-backends (list 'company-async-backend)))
81 (company-idle-begin (current-buffer) (selected-window)
82 (buffer-chars-modified-tick) (point))
83 (should (null company-candidates))
84 (insert "a")
85 (sleep-for 0.1)
86 (should (null company-candidates))
87 (should (null company-candidates-cache))
88 (should (null company-backend)))))
89
90 (ert-deftest company-idle-begin-async-allows-immediate-callbacks ()
91 (with-temp-buffer
92 (company-mode)
93 (let (company-frontends
94 (company-backends
95 (list (lambda (command &optional arg)
96 (pcase command
97 (`prefix (buffer-substring (point-min) (point)))
98 (`candidates
99 (let ((c (all-completions arg '("abc" "def"))))
100 (cons :async
101 (lambda (cb) (funcall cb c)))))
102 (`no-cache t)))))
103 (company-minimum-prefix-length 0))
104 (company-idle-begin (current-buffer) (selected-window)
105 (buffer-chars-modified-tick) (point))
106 (should (equal '("abc" "def") company-candidates))
107 (let ((last-command-event ?a))
108 (company-call 'self-insert-command 1))
109 (should (equal '("abc") company-candidates)))))
110
111 (ert-deftest company-multi-backend-forces-prefix-to-sync ()
112 (with-temp-buffer
113 (let ((company-backend (list 'ignore
114 (lambda (command)
115 (should (eq command 'prefix))
116 (cons :async
117 (lambda (cb)
118 (run-with-timer
119 0.01 nil
120 (lambda () (funcall cb nil))))))
121 (lambda (command)
122 (should (eq command 'prefix))
123 "foo"))))
124 (should (equal "foo" (company-call-backend-raw 'prefix))))
125 (let ((company-backend (list (lambda (_command)
126 (cons :async
127 (lambda (cb)
128 (run-with-timer
129 0.01 nil
130 (lambda () (funcall cb "bar"))))))
131 (lambda (_command)
132 "foo"))))
133 (should (equal "bar" (company-call-backend-raw 'prefix))))))
134
135 (ert-deftest company-multi-backend-merges-deferred-candidates ()
136 (with-temp-buffer
137 (let* ((immediate (lambda (command &optional _)
138 (pcase command
139 (`prefix "foo")
140 (`candidates
141 (cons :async
142 (lambda (cb) (funcall cb '("f"))))))))
143 (company-backend (list 'ignore
144 (lambda (command &optional arg)
145 (pcase command
146 (`prefix "foo")
147 (`candidates
148 (should (equal arg "foo"))
149 (cons :async
150 (lambda (cb)
151 (run-with-timer
152 0.01 nil
153 (lambda () (funcall cb '("a" "b")))))))))
154 (lambda (command &optional _)
155 (pcase command
156 (`prefix "foo")
157 (`candidates '("c" "d" "e"))))
158 immediate)))
159 (should (equal :async (car (company-call-backend-raw 'candidates "foo"))))
160 (should (equal '("a" "b" "c" "d" "e" "f")
161 (company-call-backend 'candidates "foo")))
162 (let ((company-backend (list immediate)))
163 (should (equal '("f") (company-call-backend 'candidates "foo")))))))
164
165 (ert-deftest company-multi-backend-merges-deferred-candidates-2 ()
166 (with-temp-buffer
167 (let ((company-backend (list (lambda (command &optional _)
168 (pcase command
169 (`prefix "foo")
170 (`candidates
171 (cons :async
172 (lambda (cb) (funcall cb '("a" "b")))))))
173 (lambda (command &optional _)
174 (pcase command
175 (`prefix "foo")
176 (`candidates
177 (cons :async
178 (lambda (cb) (funcall cb '("c" "d")))))))
179 (lambda (command &optional _)
180 (pcase command
181 (`prefix "foo")
182 (`candidates
183 (cons :async
184 (lambda (cb) (funcall cb '("e" "f"))))))))))
185 (should (equal :async (car (company-call-backend-raw 'candidates "foo"))))
186 (should (equal '("a" "b" "c" "d" "e" "f")
187 (company-call-backend 'candidates "foo"))))))
188
189 (ert-deftest company-multi-backend-merges-deferred-candidates-3 ()
190 (with-temp-buffer
191 (let ((company-backend (list (lambda (command &optional _)
192 (pcase command
193 (`prefix "foo")
194 (`candidates
195 (cons :async
196 (lambda (cb) (funcall cb '("a" "b")))))))
197 (lambda (command &optional _)
198 (pcase command
199 (`prefix "foo")
200 (`candidates
201 (cons :async
202 (lambda (cb)
203 (run-with-timer
204 0.01 nil
205 (lambda ()
206 (funcall cb '("c" "d")))))))))
207 (lambda (command &optional _)
208 (pcase command
209 (`prefix "foo")
210 (`candidates
211 (cons :async
212 (lambda (cb)
213 (run-with-timer
214 0.01 nil
215 (lambda ()
216 (funcall cb '("e" "f"))))))))))))
217 (should (equal :async (car (company-call-backend-raw 'candidates "foo"))))
218 (should (equal '("a" "b" "c" "d" "e" "f")
219 (company-call-backend 'candidates "foo"))))))