]> code.delx.au - gnu-emacs-elpa/blob - packages/company/test/frontends-tests.el
Update packages/yasnippet by subtree-merging from its github-based upstream
[gnu-emacs-elpa] / packages / company / test / frontends-tests.el
1 ;;; frontends-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-pseudo-tooltip-does-not-get-displaced ()
25 :tags '(interactive)
26 (with-temp-buffer
27 (save-window-excursion
28 (set-window-buffer nil (current-buffer))
29 (save-excursion (insert " ff"))
30 (company-mode)
31 (let ((company-frontends '(company-pseudo-tooltip-frontend))
32 (company-begin-commands '(self-insert-command))
33 (company-backends
34 (list (lambda (c &optional _)
35 (cl-case c (prefix "") (candidates '("a" "b" "c")))))))
36 (let (this-command)
37 (company-call 'complete))
38 (company-call 'open-line 1)
39 (should (eq 1 (overlay-start company-pseudo-tooltip-overlay)))))))
40
41 (ert-deftest company-pseudo-tooltip-show ()
42 :tags '(interactive)
43 (with-temp-buffer
44 (save-window-excursion
45 (set-window-buffer nil (current-buffer))
46 (insert "aaaa\n bb\nccccccc\nddd")
47 (search-backward "bb")
48 (let ((col (company--column))
49 (company-candidates-length 2)
50 (company-candidates '("123" "45"))
51 (company-backend 'ignore))
52 (company-pseudo-tooltip-show (company--row) col 0)
53 (let ((ov company-pseudo-tooltip-overlay))
54 ;; With margins.
55 (should (eq (overlay-get ov 'company-width) 5))
56 ;; FIXME: Make it 2?
57 (should (eq (overlay-get ov 'company-height) company-tooltip-limit))
58 (should (eq (overlay-get ov 'company-column) col))
59 (should (string= (overlay-get ov 'company-display)
60 "\n 123 \nc 45 c\nddd\n")))))))
61
62 (ert-deftest company-pseudo-tooltip-edit-updates-width ()
63 :tags '(interactive)
64 (with-temp-buffer
65 (set-window-buffer nil (current-buffer))
66 (let ((company-candidates-length 5)
67 (company-candidates '("123" "45" "67" "89" "1011"))
68 (company-backend 'ignore)
69 (company-tooltip-limit 4)
70 (company-tooltip-offset-display 'scrollbar))
71 (company-pseudo-tooltip-show (company--row)
72 (company--column)
73 0)
74 (should (eq (overlay-get company-pseudo-tooltip-overlay 'company-width)
75 6))
76 (company-pseudo-tooltip-edit 4)
77 (should (eq (overlay-get company-pseudo-tooltip-overlay 'company-width)
78 7)))))
79
80 (ert-deftest company-preview-show-with-annotations ()
81 :tags '(interactive)
82 (with-temp-buffer
83 (save-window-excursion
84 (set-window-buffer nil (current-buffer))
85 (save-excursion (insert "\n"))
86 (let ((company-candidates-length 1)
87 (company-candidates '("123")))
88 (company-preview-show-at-point (point))
89 (let* ((ov company-preview-overlay)
90 (str (overlay-get ov 'after-string)))
91 (should (string= str "123"))
92 (should (eq (get-text-property 0 'cursor str) 1)))))))
93
94 (ert-deftest company-pseudo-tooltip-show-with-annotations ()
95 :tags '(interactive)
96 (with-temp-buffer
97 (save-window-excursion
98 (set-window-buffer nil (current-buffer))
99 (insert " ")
100 (save-excursion (insert "\n"))
101 (let ((company-candidates-length 2)
102 (company-backend (lambda (action &optional arg &rest _ignore)
103 (when (eq action 'annotation)
104 (cdr (assoc arg '(("123" . "(4)")))))))
105 (company-candidates '("123" "45"))
106 company-tooltip-align-annotations)
107 (company-pseudo-tooltip-show-at-point (point) 0)
108 (let ((ov company-pseudo-tooltip-overlay))
109 ;; With margins.
110 (should (eq (overlay-get ov 'company-width) 8))
111 (should (string= (overlay-get ov 'company-display)
112 "\n 123(4) \n 45 \n")))))))
113
114 (ert-deftest company-pseudo-tooltip-show-with-annotations-right-aligned ()
115 :tags '(interactive)
116 (with-temp-buffer
117 (save-window-excursion
118 (set-window-buffer nil (current-buffer))
119 (insert " ")
120 (save-excursion (insert "\n"))
121 (let ((company-candidates-length 3)
122 (company-backend (lambda (action &optional arg &rest _ignore)
123 (when (eq action 'annotation)
124 (cdr (assoc arg '(("123" . "(4)")
125 ("67" . "(891011)")))))))
126 (company-candidates '("123" "45" "67"))
127 (company-tooltip-align-annotations t))
128 (company-pseudo-tooltip-show-at-point (point) 0)
129 (let ((ov company-pseudo-tooltip-overlay))
130 ;; With margins.
131 (should (eq (overlay-get ov 'company-width) 13))
132 (should (string= (overlay-get ov 'company-display)
133 "\n 123 (4) \n 45 \n 67 (891011) \n")))))))
134
135 (ert-deftest company-create-lines-shows-numbers ()
136 (let ((company-show-numbers t)
137 (company-candidates '("x" "y" "z"))
138 (company-candidates-length 3)
139 (company-backend 'ignore))
140 (should (equal '(" x 1 " " y 2 " " z 3 ")
141 (company--create-lines 0 999)))))
142
143 (ert-deftest company-create-lines-truncates-annotations ()
144 (let* ((ww (company--window-width))
145 (data `(("1" . "(123)")
146 ("2" . nil)
147 ("3" . ,(concat "(" (make-string (- ww 2) ?4) ")"))
148 (,(make-string ww ?4) . "<4>")))
149 (company-candidates (mapcar #'car data))
150 (company-candidates-length 4)
151 (company-tooltip-margin 1)
152 (company-backend (lambda (cmd &optional arg)
153 (when (eq cmd 'annotation)
154 (cdr (assoc arg data)))))
155 company-tooltip-align-annotations)
156 (should (equal (list (format " 1(123)%s " (company-space-string (- ww 8)))
157 (format " 2%s " (company-space-string (- ww 3)))
158 (format " 3(444%s " (make-string (- ww 7) ?4))
159 (format " %s " (make-string (- ww 2) ?4)))
160 (company--create-lines 0 999)))
161 (let ((company-tooltip-align-annotations t))
162 (should (equal (list (format " 1%s(123) " (company-space-string (- ww 8)))
163 (format " 2%s " (company-space-string (- ww 3)))
164 (format " 3 (444%s " (make-string (- ww 8) ?4))
165 (format " %s " (make-string (- ww 2) ?4)))
166 (company--create-lines 0 999))))))
167
168 (ert-deftest company-create-lines-truncates-common-part ()
169 (let* ((ww (company--window-width))
170 (company-candidates-length 2)
171 (company-tooltip-margin 1)
172 (company-backend #'ignore))
173 (let* ((company-common (make-string (- ww 3) ?1))
174 (company-candidates `(,(concat company-common "2")
175 ,(concat company-common "3"))))
176 (should (equal (list (format " %s2 " (make-string (- ww 3) ?1))
177 (format " %s3 " (make-string (- ww 3) ?1)))
178 (company--create-lines 0 999))))
179 (let* ((company-common (make-string (- ww 2) ?1))
180 (company-candidates `(,(concat company-common "2")
181 ,(concat company-common "3"))))
182 (should (equal (list (format " %s " company-common)
183 (format " %s " company-common))
184 (company--create-lines 0 999))))
185 (let* ((company-common (make-string ww ?1))
186 (company-candidates `(,(concat company-common "2")
187 ,(concat company-common "3")))
188 (res (company--create-lines 0 999)))
189 (should (equal (list (format " %s " (make-string (- ww 2) ?1))
190 (format " %s " (make-string (- ww 2) ?1)))
191 res))
192 (should (eq 'company-tooltip-common-selection
193 (get-text-property (- ww 2) 'face
194 (car res))))
195 (should (eq 'company-tooltip-selection
196 (get-text-property (1- ww) 'face
197 (car res))))
198 )))
199
200 (ert-deftest company-create-lines-clears-out-non-printables ()
201 :tags '(interactive)
202 (let (company-show-numbers
203 (company-candidates (list
204 (decode-coding-string "avalis\351e" 'utf-8)
205 "avatar"))
206 (company-candidates-length 2)
207 (company-backend 'ignore))
208 (should (equal '(" avalis‗e "
209 " avatar ")
210 (company--create-lines 0 999)))))
211
212 (ert-deftest company-create-lines-handles-multiple-width ()
213 :tags '(interactive)
214 (let (company-show-numbers
215 (company-candidates '("蛙蛙蛙蛙" "蛙abc"))
216 (company-candidates-length 2)
217 (company-backend 'ignore))
218 (should (equal '(" 蛙蛙蛙蛙 "
219 " 蛙abc ")
220 (company--create-lines 0 999)))))
221
222 (ert-deftest company-create-lines-handles-multiple-width-in-annotation ()
223 (let* (company-show-numbers
224 (alist '(("a" . " ︸") ("b" . " ︸︸")))
225 (company-candidates (mapcar #'car alist))
226 (company-candidates-length 2)
227 (company-backend (lambda (c &optional a)
228 (when (eq c 'annotation)
229 (assoc-default a alist)))))
230 (should (equal '(" a ︸ "
231 " b ︸︸ ")
232 (company--create-lines 0 999)))))
233
234 (ert-deftest company-create-lines-with-multiple-width-and-keep-prefix ()
235 :tags '(interactive)
236 (let* (company-show-numbers
237 (company-candidates '("MIRAI発売1カ月"
238 "MIRAI発売2カ月"))
239 (company-candidates-length 2)
240 (company-prefix "MIRAI発")
241 (company-backend (lambda (c &optional _arg)
242 (pcase c
243 (`ignore-case 'keep-prefix)))))
244 (should (equal '(" MIRAI発売1カ月 "
245 " MIRAI発売2カ月 ")
246 (company--create-lines 0 999)))))
247
248 (ert-deftest company-fill-propertize-truncates-search-highlight ()
249 (let ((company-search-string "foo")
250 (company-backend #'ignore)
251 (company-prefix ""))
252 (should (equal-including-properties
253 (company-fill-propertize "barfoo" nil 6 t nil nil)
254 #("barfoo"
255 0 3 (face company-tooltip mouse-face company-tooltip-mouse)
256 3 6 (face company-tooltip-search mouse-face company-tooltip-mouse))))
257 (should (equal-including-properties
258 (company-fill-propertize "barfoo" nil 5 t "" " ")
259 #("barfo "
260 0 3 (face company-tooltip mouse-face company-tooltip-mouse)
261 3 5 (face company-tooltip-search mouse-face company-tooltip-mouse)
262 5 6 (face company-tooltip mouse-face company-tooltip-mouse))))
263 (should (equal-including-properties
264 (company-fill-propertize "barfoo" nil 3 t " " " ")
265 #(" bar "
266 0 5 (face company-tooltip mouse-face company-tooltip-mouse))))))
267
268 (ert-deftest company-column-with-composition ()
269 :tags '(interactive)
270 (with-temp-buffer
271 (save-window-excursion
272 (set-window-buffer nil (current-buffer))
273 (insert "lambda ()")
274 (compose-region 1 (1+ (length "lambda")) "\\")
275 (should (= (company--column) 4)))))
276
277 (ert-deftest company-plainify ()
278 (let ((tab-width 8))
279 (should (equal-including-properties
280 (company-plainify "\tabc\td\t")
281 (concat " "
282 "abc "
283 "d "))))
284 (should (equal-including-properties
285 (company-plainify (propertize "foobar" 'line-prefix "-*-"))
286 "-*-foobar")))
287
288 (ert-deftest company-buffer-lines-with-lines-folded ()
289 :tags '(interactive)
290 (with-temp-buffer
291 (insert (propertize "aaa\nbbb\nccc\nddd\n" 'display "aaa+\n"))
292 (insert "eee\nfff\nggg")
293 (should (equal (company-buffer-lines (point-min) (point-max))
294 '("aaa" "eee" "fff" "ggg")))))
295
296 (ert-deftest company-buffer-lines-with-multiline-display ()
297 :tags '(interactive)
298 (with-temp-buffer
299 (insert (propertize "a" 'display "bbb\nccc\ndddd\n"))
300 (insert "eee\nfff\nggg")
301 (should (equal (company-buffer-lines (point-min) (point-max))
302 '("" "" "" "eee" "fff" "ggg")))))
303
304 (ert-deftest company-buffer-lines-with-multiline-after-string-at-eob ()
305 :tags '(interactive)
306 (with-temp-buffer
307 (insert "a\nb\nc\n")
308 (let ((ov (make-overlay (point-max) (point-max) nil t t)))
309 (overlay-put ov 'after-string "~\n~\n~"))
310 (should (equal (company-buffer-lines (point-min) (point-max))
311 '("a" "b" "c")))))
312
313 (ert-deftest company-modify-line ()
314 (let ((str "-*-foobar"))
315 (should (equal-including-properties
316 (company-modify-line str "zz" 4)
317 "-*-fzzbar"))
318 (should (equal-including-properties
319 (company-modify-line str "xx" 0)
320 "xx-foobar"))
321 (should (equal-including-properties
322 (company-modify-line str "zz" 10)
323 "-*-foobar zz"))))
324
325 (ert-deftest company-scrollbar-bounds ()
326 (should (equal nil (company--scrollbar-bounds 0 3 3)))
327 (should (equal nil (company--scrollbar-bounds 0 4 3)))
328 (should (equal '(0 . 0) (company--scrollbar-bounds 0 1 2)))
329 (should (equal '(1 . 1) (company--scrollbar-bounds 2 2 4)))
330 (should (equal '(2 . 3) (company--scrollbar-bounds 7 4 12)))
331 (should (equal '(1 . 2) (company--scrollbar-bounds 3 4 12)))
332 (should (equal '(1 . 3) (company--scrollbar-bounds 4 5 11))))