]> code.delx.au - gnu-emacs-elpa/blob - test/async-tests.el
c548898514453e7f0f4aa94bf8bf998f6480f57c
[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
88 (ert-deftest company-idle-begin-async-allows-immediate-callbacks ()
89 (with-temp-buffer
90 (company-mode)
91 (let (company-frontends
92 (company-backends
93 (list (lambda (command &optional arg)
94 (pcase command
95 (`prefix (buffer-substring (point-min) (point)))
96 (`candidates
97 (let ((c (all-completions arg '("abc" "def"))))
98 (cons :async
99 (lambda (cb) (funcall cb c)))))
100 (`no-cache t)))))
101 (company-minimum-prefix-length 0))
102 (company-idle-begin (current-buffer) (selected-window)
103 (buffer-chars-modified-tick) (point))
104 (should (equal '("abc" "def") company-candidates))
105 (let ((last-command-event ?a))
106 (company-call 'self-insert-command 1))
107 (should (equal '("abc") company-candidates)))))
108
109 (ert-deftest company-multi-backend-forces-prefix-to-sync ()
110 (with-temp-buffer
111 (let ((company-backend (list 'ignore
112 (lambda (command)
113 (should (eq command 'prefix))
114 (cons :async
115 (lambda (cb)
116 (run-with-timer
117 0.01 nil
118 (lambda () (funcall cb nil))))))
119 (lambda (command)
120 (should (eq command 'prefix))
121 "foo"))))
122 (should (equal "foo" (company-call-backend-raw 'prefix))))
123 (let ((company-backend (list (lambda (_command)
124 (cons :async
125 (lambda (cb)
126 (run-with-timer
127 0.01 nil
128 (lambda () (funcall cb "bar"))))))
129 (lambda (_command)
130 "foo"))))
131 (should (equal "bar" (company-call-backend-raw 'prefix))))))
132
133 (ert-deftest company-multi-backend-merges-deferred-candidates ()
134 (with-temp-buffer
135 (let* ((immediate (lambda (command &optional _)
136 (pcase command
137 (`prefix "foo")
138 (`candidates
139 (cons :async
140 (lambda (cb) (funcall cb '("f"))))))))
141 (company-backend (list 'ignore
142 (lambda (command &optional arg)
143 (pcase command
144 (`prefix "foo")
145 (`candidates
146 (should (equal arg "foo"))
147 (cons :async
148 (lambda (cb)
149 (run-with-timer
150 0.01 nil
151 (lambda () (funcall cb '("a" "b")))))))))
152 (lambda (command &optional _)
153 (pcase command
154 (`prefix "foo")
155 (`candidates '("c" "d" "e"))))
156 immediate)))
157 (should (equal :async (car (company-call-backend-raw 'candidates "foo"))))
158 (should (equal '("a" "b" "c" "d" "e" "f")
159 (company-call-backend 'candidates "foo")))
160 (let ((company-backend (list immediate)))
161 (should (equal '("f") (company-call-backend 'candidates "foo")))))))
162
163 (ert-deftest company-multi-backend-merges-deferred-candidates-2 ()
164 (with-temp-buffer
165 (let ((company-backend (list (lambda (command &optional _)
166 (pcase command
167 (`prefix "foo")
168 (`candidates
169 (cons :async
170 (lambda (cb) (funcall cb '("a" "b")))))))
171 (lambda (command &optional _)
172 (pcase command
173 (`prefix "foo")
174 (`candidates
175 (cons :async
176 (lambda (cb) (funcall cb '("c" "d")))))))
177 (lambda (command &optional _)
178 (pcase command
179 (`prefix "foo")
180 (`candidates
181 (cons :async
182 (lambda (cb) (funcall cb '("e" "f"))))))))))
183 (should (equal :async (car (company-call-backend-raw 'candidates "foo"))))
184 (should (equal '("a" "b" "c" "d" "e" "f")
185 (company-call-backend 'candidates "foo"))))))
186
187 (ert-deftest company-multi-backend-merges-deferred-candidates-3 ()
188 (with-temp-buffer
189 (let ((company-backend (list (lambda (command &optional _)
190 (pcase command
191 (`prefix "foo")
192 (`candidates
193 (cons :async
194 (lambda (cb) (funcall cb '("a" "b")))))))
195 (lambda (command &optional _)
196 (pcase command
197 (`prefix "foo")
198 (`candidates
199 (cons :async
200 (lambda (cb)
201 (run-with-timer
202 0.01 nil
203 (lambda ()
204 (funcall cb '("c" "d")))))))))
205 (lambda (command &optional _)
206 (pcase command
207 (`prefix "foo")
208 (`candidates
209 (cons :async
210 (lambda (cb)
211 (run-with-timer
212 0.01 nil
213 (lambda ()
214 (funcall cb '("e" "f"))))))))))))
215 (should (equal :async (car (company-call-backend-raw 'candidates "foo"))))
216 (should (equal '("a" "b" "c" "d" "e" "f")
217 (company-call-backend 'candidates "foo"))))))