]> code.delx.au - gnu-emacs-elpa/blob - company-tests.el
Minor tweaks
[gnu-emacs-elpa] / company-tests.el
1 ;;; company-tests.el --- company-mode tests
2
3 ;; Copyright (C) 2011, 2013-2014 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
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
23 ;;; Commentary:
24 ;;
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29 (require 'ert)
30 (require 'company)
31 (require 'company-keywords)
32 (require 'company-elisp)
33 (require 'company-clang)
34
35 ;;; Core
36
37 (ert-deftest company-sorted-keywords ()
38 "Test that keywords in `company-keywords-alist' are in alphabetical order."
39 (dolist (pair company-keywords-alist)
40 (when (consp (cdr pair))
41 (let ((prev (cadr pair)))
42 (dolist (next (cddr pair))
43 (should (not (equal prev next)))
44 (should (string< prev next))
45 (setq prev next))))))
46
47 (ert-deftest company-good-prefix ()
48 (let ((company-minimum-prefix-length 5)
49 company-abort-manual-when-too-short
50 company--manual-action ;idle begin
51 (company-selection-changed t)) ;has no effect
52 (should (eq t (company--good-prefix-p "!@#$%")))
53 (should (eq nil (company--good-prefix-p "abcd")))
54 (should (eq nil (company--good-prefix-p 'stop)))
55 (should (eq t (company--good-prefix-p '("foo" . 5))))
56 (should (eq nil (company--good-prefix-p '("foo" . 4))))
57 (should (eq t (company--good-prefix-p '("foo" . t))))))
58
59 (ert-deftest company--manual-prefix-set-and-unset ()
60 (with-temp-buffer
61 (insert "ab")
62 (company-mode)
63 (let (company-frontends
64 (company-backends
65 (list (lambda (command &optional arg)
66 (case command
67 (prefix (buffer-substring (point-min) (point)))
68 (candidates '("abc" "abd")))))))
69 (company-manual-begin)
70 (should (equal "ab" company--manual-prefix))
71 (company-abort)
72 (should (null company--manual-prefix)))))
73
74 (ert-deftest company-abort-manual-when-too-short ()
75 (let ((company-minimum-prefix-length 5)
76 (company-abort-manual-when-too-short t)
77 (company-selection-changed t)) ;has not effect
78 (let ((company--manual-action nil)) ;idle begin
79 (should (eq t (company--good-prefix-p "!@#$%")))
80 (should (eq t (company--good-prefix-p '("foo" . 5))))
81 (should (eq t (company--good-prefix-p '("foo" . t)))))
82 (let ((company--manual-action t)
83 (company--manual-prefix "abc")) ;manual begin from this prefix
84 (should (eq t (company--good-prefix-p "!@#$")))
85 (should (eq nil (company--good-prefix-p "ab")))
86 (should (eq nil (company--good-prefix-p 'stop)))
87 (should (eq t (company--good-prefix-p '("foo" . 4))))
88 (should (eq t (company--good-prefix-p "abcd")))
89 (should (eq t (company--good-prefix-p "abc")))
90 (should (eq t (company--good-prefix-p '("bar" . t)))))))
91
92 (ert-deftest company-multi-backend-with-lambdas ()
93 (let ((company-backend
94 (list (lambda (command &optional arg &rest ignore)
95 (case command
96 (prefix "z")
97 (candidates '("a" "b"))))
98 (lambda (command &optional arg &rest ignore)
99 (case command
100 (prefix "z")
101 (candidates '("c" "d")))))))
102 (should (equal (company-call-backend 'candidates "z") '("a" "b" "c" "d")))))
103
104 (ert-deftest company-multi-backend-remembers-candidate-backend ()
105 (let ((company-backend
106 (list (lambda (command &optional arg &rest ignore)
107 (case command
108 (ignore-case nil)
109 (annotation "1")
110 (candidates '("a" "c"))
111 (post-completion "13")))
112 (lambda (command &optional arg &rest ignore)
113 (case command
114 (ignore-case t)
115 (annotation "2")
116 (candidates '("b" "d"))
117 (post-completion "42"))))))
118 (let ((candidates (company-calculate-candidates nil)))
119 (should (equal candidates '("a" "b" "c" "d")))
120 (should (equal t (company-call-backend 'ignore-case)))
121 (should (equal "1" (company-call-backend 'annotation (nth 0 candidates))))
122 (should (equal "2" (company-call-backend 'annotation (nth 1 candidates))))
123 (should (equal "13" (company-call-backend 'post-completion (nth 2 candidates))))
124 (should (equal "42" (company-call-backend 'post-completion (nth 3 candidates)))))))
125
126 (ert-deftest company-multi-backend-handles-keyword-with ()
127 (let ((primo (lambda (command &optional arg)
128 (case command
129 (prefix "a")
130 (candidates '("abb" "abc" "abd")))))
131 (secundo (lambda (command &optional arg)
132 (case command
133 (prefix "a")
134 (candidates '("acc" "acd"))))))
135 (let ((company-backend (list 'ignore 'ignore :with secundo)))
136 (should (null (company-call-backend 'prefix))))
137 (let ((company-backend (list 'ignore primo :with secundo)))
138 (should (equal "a" (company-call-backend 'prefix)))
139 (should (equal '("abb" "abc" "abd" "acc" "acd")
140 (company-call-backend 'candidates "a"))))))
141
142 (ert-deftest company-begin-backend-failure-doesnt-break-company-backends ()
143 (with-temp-buffer
144 (insert "a")
145 (company-mode)
146 (should-error
147 (company-begin-backend (lambda (command &rest ignore))))
148 (let (company-frontends
149 (company-backends
150 (list (lambda (command &optional arg)
151 (case command
152 (prefix "a")
153 (candidates '("a" "ab" "ac")))))))
154 (let (this-command)
155 (company-call 'complete))
156 (should (eq 3 company-candidates-length)))))
157
158 (ert-deftest company-require-match-explicit ()
159 (with-temp-buffer
160 (insert "ab")
161 (company-mode)
162 (let (company-frontends
163 (company-require-match 'company-explicit-action-p)
164 (company-backends
165 (list (lambda (command &optional arg)
166 (case command
167 (prefix (buffer-substring (point-min) (point)))
168 (candidates '("abc" "abd")))))))
169 (let (this-command)
170 (company-complete))
171 (let ((last-command-event ?e))
172 (company-call 'self-insert-command 1))
173 (should (eq 2 company-candidates-length))
174 (should (eq 3 (point))))))
175
176 (ert-deftest company-dont-require-match-when-idle ()
177 (with-temp-buffer
178 (insert "ab")
179 (company-mode)
180 (let (company-frontends
181 (company-require-match 'company-explicit-action-p)
182 (company-backends
183 (list (lambda (command &optional arg)
184 (case command
185 (prefix (buffer-substring (point-min) (point)))
186 (candidates '("abc" "abd")))))))
187 (company-idle-begin (current-buffer) (selected-window)
188 (buffer-chars-modified-tick) (point))
189 (let ((last-command-event ?e))
190 (company-call 'self-insert-command 1))
191 (should (eq nil company-candidates-length))
192 (should (eq 4 (point))))))
193
194 (ert-deftest company-should-complete-whitelist ()
195 (with-temp-buffer
196 (insert "ab")
197 (company-mode)
198 (let (company-frontends
199 company-begin-commands
200 (company-backends
201 (list (lambda (command &optional arg)
202 (case command
203 (prefix (buffer-substring (point-min) (point)))
204 (candidates '("abc" "abd")))))))
205 (let ((company-continue-commands nil))
206 (let (this-command)
207 (company-complete))
208 (company-call 'backward-delete-char 1)
209 (should (null company-candidates-length)))
210 (let ((company-continue-commands '(backward-delete-char)))
211 (let (this-command)
212 (company-complete))
213 (company-call 'backward-delete-char 1)
214 (should (eq 2 company-candidates-length))))))
215
216 (ert-deftest company-should-complete-blacklist ()
217 (with-temp-buffer
218 (insert "ab")
219 (company-mode)
220 (let (company-frontends
221 company-begin-commands
222 (company-backends
223 (list (lambda (command &optional arg)
224 (case command
225 (prefix (buffer-substring (point-min) (point)))
226 (candidates '("abc" "abd")))))))
227 (let ((company-continue-commands '(not backward-delete-char)))
228 (let (this-command)
229 (company-complete))
230 (company-call 'backward-delete-char 1)
231 (should (null company-candidates-length)))
232 (let ((company-continue-commands '(not backward-delete-char-untabify)))
233 (let (this-command)
234 (company-complete))
235 (company-call 'backward-delete-char 1)
236 (should (eq 2 company-candidates-length))))))
237
238 (ert-deftest company-auto-complete-explicit ()
239 (with-temp-buffer
240 (insert "ab")
241 (company-mode)
242 (let (company-frontends
243 (company-auto-complete 'company-explicit-action-p)
244 (company-auto-complete-chars '(? ))
245 (company-backends
246 (list (lambda (command &optional arg)
247 (case command
248 (prefix (buffer-substring (point-min) (point)))
249 (candidates '("abcd" "abef")))))))
250 (let (this-command)
251 (company-complete))
252 (let ((last-command-event ? ))
253 (company-call 'self-insert-command 1))
254 (should (string= "abcd " (buffer-string))))))
255
256 (ert-deftest company-no-auto-complete-when-idle ()
257 (with-temp-buffer
258 (insert "ab")
259 (company-mode)
260 (let (company-frontends
261 (company-auto-complete 'company-explicit-action-p)
262 (company-auto-complete-chars '(? ))
263 (company-backends
264 (list (lambda (command &optional arg)
265 (case command
266 (prefix (buffer-substring (point-min) (point)))
267 (candidates '("abcd" "abef")))))))
268 (company-idle-begin (current-buffer) (selected-window)
269 (buffer-chars-modified-tick) (point))
270 (let ((last-command-event ? ))
271 (company-call 'self-insert-command 1))
272 (should (string= "ab " (buffer-string))))))
273
274 (ert-deftest company-clears-explicit-action-when-no-matches ()
275 (with-temp-buffer
276 (company-mode)
277 (let (company-frontends
278 company-backends)
279 (company-call 'manual-begin) ;; fails
280 (should (null company-candidates))
281 (should (null (company-explicit-action-p))))))
282
283 (ert-deftest company-ignore-case-replaces-prefix ()
284 (with-temp-buffer
285 (company-mode)
286 (let (company-frontends
287 company-end-of-buffer-workaround
288 (company-backends
289 (list (lambda (command &optional arg)
290 (case command
291 (prefix (buffer-substring (point-min) (point)))
292 (candidates '("abcd" "abef"))
293 (ignore-case t))))))
294 (insert "A")
295 (let (this-command)
296 (company-complete))
297 (should (string= "ab" (buffer-string)))
298 (delete-char -2)
299 (insert "A") ; hack, to keep it in one test
300 (company-complete-selection)
301 (should (string= "abcd" (buffer-string))))))
302
303 (ert-deftest company-ignore-case-with-keep-prefix ()
304 (with-temp-buffer
305 (insert "AB")
306 (company-mode)
307 (let (company-frontends
308 (company-backends
309 (list (lambda (command &optional arg)
310 (case command
311 (prefix (buffer-substring (point-min) (point)))
312 (candidates '("abcd" "abef"))
313 (ignore-case 'keep-prefix))))))
314 (let (this-command)
315 (company-complete))
316 (company-complete-selection)
317 (should (string= "ABcd" (buffer-string))))))
318
319 (ert-deftest company-non-prefix-completion ()
320 (with-temp-buffer
321 (insert "tc")
322 (company-mode)
323 (let (company-frontends
324 company-end-of-buffer-workaround
325 (company-backends
326 (list (lambda (command &optional arg)
327 (case command
328 (prefix (buffer-substring (point-min) (point)))
329 (candidates '("tea-cup" "teal-color")))))))
330 (let (this-command)
331 (company-complete))
332 (should (string= "tc" (buffer-string)))
333 (company-complete-selection)
334 (should (string= "tea-cup" (buffer-string))))))
335
336 (ert-deftest company-pseudo-tooltip-does-not-get-displaced ()
337 :tags '(interactive)
338 (with-temp-buffer
339 (save-window-excursion
340 (set-window-buffer nil (current-buffer))
341 (save-excursion (insert " ff"))
342 (company-mode)
343 (let ((company-frontends '(company-pseudo-tooltip-frontend))
344 (company-begin-commands '(self-insert-command))
345 (company-backends
346 (list (lambda (c &optional arg)
347 (case c (prefix "") (candidates '("a" "b" "c")))))))
348 (let (this-command)
349 (company-call 'complete))
350 (company-call 'open-line 1)
351 (should (eq 2 (overlay-start company-pseudo-tooltip-overlay)))))))
352
353 (ert-deftest company-pseudo-tooltip-show ()
354 :tags '(interactive)
355 (with-temp-buffer
356 (save-window-excursion
357 (set-window-buffer nil (current-buffer))
358 (insert "aaaa\n bb\nccccccc\nddd")
359 (search-backward "bb")
360 (let ((col (company--column))
361 (company-candidates-length 2)
362 (company-candidates '("123" "45"))
363 (company-backend 'ignore))
364 (company-pseudo-tooltip-show (company--row) col 0)
365 (let ((ov company-pseudo-tooltip-overlay))
366 ;; With margins.
367 (should (eq (overlay-get ov 'company-width) 5))
368 ;; FIXME: Make it 2?
369 (should (eq (overlay-get ov 'company-height) company-tooltip-limit))
370 (should (eq (overlay-get ov 'company-column) col))
371 (should (string= (overlay-get ov 'company-after)
372 " 123 \nc 45 c\nddd\n")))))))
373
374 (ert-deftest company-preview-show-with-annotations ()
375 :tags '(interactive)
376 (with-temp-buffer
377 (save-window-excursion
378 (set-window-buffer nil (current-buffer))
379 (save-excursion (insert "\n"))
380 (let ((company-candidates-length 1)
381 (company-candidates '("123")))
382 (company-preview-show-at-point (point))
383 (let ((ov company-preview-overlay))
384 (should (string= (overlay-get ov 'display) "123\n")))))))
385
386 (ert-deftest company-pseudo-tooltip-show-with-annotations ()
387 :tags '(interactive)
388 (with-temp-buffer
389 (save-window-excursion
390 (set-window-buffer nil (current-buffer))
391 (insert " ")
392 (save-excursion (insert "\n"))
393 (let ((company-candidates-length 2)
394 (company-backend (lambda (action &optional arg &rest _ignore)
395 (when (eq action 'annotation)
396 (cdr (assoc arg '(("123" . "(4)")))))))
397 (company-candidates '("123" "45"))
398 company-tooltip-align-annotations)
399 (company-pseudo-tooltip-show-at-point (point))
400 (let ((ov company-pseudo-tooltip-overlay))
401 ;; With margins.
402 (should (eq (overlay-get ov 'company-width) 8))
403 (should (string= (overlay-get ov 'company-after)
404 " 123(4) \n 45 \n")))))))
405
406 (ert-deftest company-pseudo-tooltip-show-with-annotations-right-aligned ()
407 :tags '(interactive)
408 (with-temp-buffer
409 (save-window-excursion
410 (set-window-buffer nil (current-buffer))
411 (insert " ")
412 (save-excursion (insert "\n"))
413 (let ((company-candidates-length 3)
414 (company-backend (lambda (action &optional arg &rest _ignore)
415 (when (eq action 'annotation)
416 (cdr (assoc arg '(("123" . "(4)")
417 ("67" . "(891011)")))))))
418 (company-candidates '("123" "45" "67"))
419 (company-tooltip-align-annotations t))
420 (company-pseudo-tooltip-show-at-point (point))
421 (let ((ov company-pseudo-tooltip-overlay))
422 ;; With margins.
423 (should (eq (overlay-get ov 'company-width) 13))
424 (should (string= (overlay-get ov 'company-after)
425 " 123 (4) \n 45 \n 67 (891011) \n")))))))
426
427 (ert-deftest company-create-lines-shows-numbers ()
428 (let ((company-show-numbers t)
429 (company-candidates '("x" "y" "z"))
430 (company-candidates-length 3)
431 (company-backend 'ignore))
432 (should (equal '(" x 1 " " y 2 " " z 3 ")
433 (company--create-lines 0 999)))))
434
435 (ert-deftest company-create-lines-truncates-annotations ()
436 (let* ((ww (company--window-width))
437 (data `(("1" . "(123)")
438 ("2" . nil)
439 ("3" . ,(concat "(" (make-string (- ww 2) ?4) ")"))
440 (,(make-string ww ?4) . "<4>")))
441 (company-candidates (mapcar #'car data))
442 (company-candidates-length 4)
443 (company-tooltip-margin 1)
444 (company-backend (lambda (cmd &optional arg)
445 (when (eq cmd 'annotation)
446 (cdr (assoc arg data)))))
447 company-tooltip-align-annotations)
448 (should (equal (list (format " 1(123)%s " (company-space-string (- ww 8)))
449 (format " 2%s " (company-space-string (- ww 3)))
450 (format " 3(444%s " (make-string (- ww 7) ?4))
451 (format " %s " (make-string (- ww 2) ?4)))
452 (company--create-lines 0 999)))
453 (let ((company-tooltip-align-annotations t))
454 (should (equal (list (format " 1%s(123) " (company-space-string (- ww 8)))
455 (format " 2%s " (company-space-string (- ww 3)))
456 (format " 3 (444%s " (make-string (- ww 8) ?4))
457 (format " %s " (make-string (- ww 2) ?4)))
458 (company--create-lines 0 999))))))
459
460 (ert-deftest company-column-with-composition ()
461 (with-temp-buffer
462 (insert "lambda ()")
463 (compose-region 1 (1+ (length "lambda")) "\\")
464 (should (= (company--column) 4))))
465
466 (ert-deftest company-column-with-line-prefix ()
467 (with-temp-buffer
468 (insert "foo")
469 (put-text-property (point-min) (point) 'line-prefix " ")
470 (should (= (company--column) 5))))
471
472 (ert-deftest company-column-wth-line-prefix-on-empty-line ()
473 (with-temp-buffer
474 (insert "\n")
475 (forward-char -1)
476 (put-text-property (point-min) (point-max) 'line-prefix " ")
477 (should (= (company--column) 2))))
478
479 (ert-deftest company-plainify ()
480 (let ((tab-width 8))
481 (should (equal-including-properties
482 (company-plainify "\tabc\td\t")
483 (concat " "
484 "abc "
485 "d "))))
486 (should (equal-including-properties
487 (company-plainify (propertize "foobar" 'line-prefix "-*-"))
488 "-*-foobar")))
489
490 (ert-deftest company-modify-line ()
491 (let ((str "-*-foobar"))
492 (should (equal-including-properties
493 (company-modify-line str "zz" 4)
494 "-*-fzzbar"))
495 (should (equal-including-properties
496 (company-modify-line str "xx" 0)
497 "xx-foobar"))
498 (should (equal-including-properties
499 (company-modify-line str "zz" 10)
500 "-*-foobar zz"))))
501
502 (ert-deftest company-scrollbar-bounds ()
503 (should (equal nil (company--scrollbar-bounds 0 3 3)))
504 (should (equal nil (company--scrollbar-bounds 0 4 3)))
505 (should (equal '(0 . 0) (company--scrollbar-bounds 0 1 2)))
506 (should (equal '(1 . 1) (company--scrollbar-bounds 2 2 4)))
507 (should (equal '(2 . 3) (company--scrollbar-bounds 7 4 12)))
508 (should (equal '(1 . 2) (company--scrollbar-bounds 3 4 12)))
509 (should (equal '(1 . 3) (company--scrollbar-bounds 4 5 11))))
510
511 ;;; Template
512
513 (ert-deftest company-template-removed-after-the-last-jump ()
514 (with-temp-buffer
515 (insert "{ }")
516 (goto-char 2)
517 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
518 (save-excursion
519 (dotimes (i 2)
520 (insert " ")
521 (company-template-add-field tpl (point) "foo")))
522 (company-call 'template-forward-field)
523 (should (= 3 (point)))
524 (company-call 'template-forward-field)
525 (should (= 7 (point)))
526 (company-call 'template-forward-field)
527 (should (= 11 (point)))
528 (should (zerop (length (overlay-get tpl 'company-template-fields))))
529 (should (null (overlay-buffer tpl))))))
530
531 (ert-deftest company-template-removed-after-input-and-jump ()
532 (with-temp-buffer
533 (insert "{ }")
534 (goto-char 2)
535 (let ((tpl (company-template-declare-template (point) (1- (point-max)))))
536 (save-excursion
537 (insert " ")
538 (company-template-add-field tpl (point) "bar"))
539 (company-call 'template-move-to-first tpl)
540 (should (= 3 (point)))
541 (dolist (c (string-to-list "tee"))
542 (let ((last-command-event c))
543 (company-call 'self-insert-command 1)))
544 (should (string= "{ tee }" (buffer-string)))
545 (should (overlay-buffer tpl))
546 (company-call 'template-forward-field)
547 (should (= 7 (point)))
548 (should (null (overlay-buffer tpl))))))
549
550 (defun company-call (name &rest args)
551 (let* ((maybe (intern (format "company-%s" name)))
552 (command (if (fboundp maybe) maybe name)))
553 (let ((this-command command))
554 (run-hooks 'pre-command-hook))
555 (apply command args)
556 (let ((this-command command))
557 (run-hooks 'post-command-hook))))
558
559 (ert-deftest company-template-c-like-templatify ()
560 (with-temp-buffer
561 (let ((text "foo(int a, short b)"))
562 (insert text)
563 (company-template-c-like-templatify text)
564 (should (equal "foo(arg0, arg1)" (buffer-string)))
565 (should (looking-at "arg0"))
566 (should (equal "int a"
567 (overlay-get (company-template-field-at) 'display))))))
568
569 (ert-deftest company-template-c-like-templatify-trims-after-closing-paren ()
570 (with-temp-buffer
571 (let ((text "foo(int a, short b)!@ #1334 a"))
572 (insert text)
573 (company-template-c-like-templatify text)
574 (should (equal "foo(arg0, arg1)" (buffer-string)))
575 (should (looking-at "arg0")))))
576
577 ;;; Elisp
578
579 (defmacro company-elisp-with-buffer (contents &rest body)
580 (declare (indent 0))
581 `(with-temp-buffer
582 (insert ,contents)
583 (setq major-mode 'emacs-lisp-mode)
584 (re-search-backward "|")
585 (replace-match "")
586 (let ((company-elisp-detect-function-context t))
587 ,@body)))
588
589 (ert-deftest company-elisp-candidates-predicate ()
590 (company-elisp-with-buffer
591 "(foo ba|)"
592 (should (eq (company-elisp--candidates-predicate "ba")
593 'boundp))
594 (should (eq (let (company-elisp-detect-function-context)
595 (company-elisp--candidates-predicate "ba"))
596 'company-elisp--predicate)))
597 (company-elisp-with-buffer
598 "(foo| )"
599 (should (eq (company-elisp--candidates-predicate "foo")
600 'fboundp))
601 (should (eq (let (company-elisp-detect-function-context)
602 (company-elisp--candidates-predicate "foo"))
603 'company-elisp--predicate)))
604 (company-elisp-with-buffer
605 "(foo 'b|)"
606 (should (eq (company-elisp--candidates-predicate "b")
607 'company-elisp--predicate))))
608
609 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
610 (company-elisp-with-buffer
611 "(def foo () \"Doo be doo `ide|"
612 (should (eq 'company-elisp--predicate
613 (company-elisp--candidates-predicate "ide")))))
614
615 ;; This one's also an integration test.
616 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
617 (let ((company-elisp-detect-function-context t)
618 (obarray [when what whelp])
619 (what 1)
620 (whelp 2)
621 (wisp 3))
622 (company-elisp-with-buffer
623 "(let ((foo 7) (wh| )))"
624 (should (equal '("what" "whelp")
625 (company-elisp-candidates "wh"))))
626 (company-elisp-with-buffer
627 "(cond ((null nil) (wh| )))"
628 (should (equal '("when")
629 (company-elisp-candidates "wh"))))))
630
631 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
632 (loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
633 ("(let (foo (bar|" "bar" boundp)
634 ("(let (foo) (bar|" "bar" fboundp))
635 do
636 (eval `(company-elisp-with-buffer
637 ,text
638 (should (eq ',predicate
639 (company-elisp--candidates-predicate ,prefix)))))))
640
641 (ert-deftest company-elisp-finds-vars ()
642 (let ((obarray [boo bar baz backquote])
643 (boo t)
644 (bar t)
645 (baz t))
646 (should (equal '("bar" "baz")
647 (company-elisp--globals "ba" 'boundp)))))
648
649 (ert-deftest company-elisp-finds-functions ()
650 (let ((obarray [when what whelp])
651 (what t)
652 (whelp t))
653 (should (equal '("when")
654 (company-elisp--globals "wh" 'fboundp)))))
655
656 (ert-deftest company-elisp-finds-things ()
657 (let ((obarray [when what whelp])
658 (what t)
659 (whelp t))
660 (should (equal '("what" "whelp" "when")
661 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
662 'string<)))))
663
664 (ert-deftest company-elisp-locals-vars ()
665 (company-elisp-with-buffer
666 "(let ((foo 5) (bar 6))
667 (cl-labels ((borg ()))
668 (lambda (boo baz)
669 b|)))"
670 (should (equal '("bar" "baz" "boo")
671 (company-elisp--locals "b" nil)))))
672
673 (ert-deftest company-elisp-locals-single-var ()
674 (company-elisp-with-buffer
675 "(dotimes (itk 100)
676 (dolist (item items)
677 it|))"
678 (should (equal '("itk" "item")
679 (company-elisp--locals "it" nil)))))
680
681 (ert-deftest company-elisp-locals-funs ()
682 (company-elisp-with-buffer
683 "(cl-labels ((foo ())
684 (fee ()))
685 (let ((fun 4))
686 (f| )))"
687 (should (equal '("fee" "foo")
688 (sort (company-elisp--locals "f" t) 'string<)))))
689
690 (ert-deftest company-elisp-locals-skips-current-varlist ()
691 (company-elisp-with-buffer
692 "(let ((foo 1)
693 (f| )))"
694 (should (null (company-elisp--locals "f" nil)))))
695
696 (ert-deftest company-elisp-show-locals-first ()
697 (company-elisp-with-buffer
698 "(let ((floo 1)
699 (flop 2)
700 (flee 3))
701 fl|)"
702 (let ((obarray [float-pi]))
703 (let (company-elisp-show-locals-first)
704 (should (eq nil (company-elisp 'sorted))))
705 (let ((company-elisp-show-locals-first t))
706 (should (eq t (company-elisp 'sorted)))
707 (should (equal '("flee" "floo" "flop" "float-pi")
708 (company-elisp-candidates "fl")))))))
709
710 (ert-deftest company-elisp-candidates-no-duplicates ()
711 (company-elisp-with-buffer
712 "(let ((float-pi 4))
713 f|)"
714 (let ((obarray [float-pi])
715 (company-elisp-show-locals-first t))
716 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
717
718 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
719 (company-elisp-with-buffer
720 "(defun foob|)"
721 (should (null (company-elisp 'prefix)))))
722
723 (ert-deftest company-elisp-should-complete-def-call ()
724 (company-elisp-with-buffer
725 "(defu|"
726 (should (equal "defu" (company-elisp 'prefix)))))
727
728 (ert-deftest company-elisp-should-complete-in-defvar ()
729 ;; It will also complete the var name, at least for now.
730 (company-elisp-with-buffer
731 "(defvar abc de|"
732 (should (equal "de" (company-elisp 'prefix)))))
733
734 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
735 (company-elisp-with-buffer
736 "(defsubst foobar (ba|"
737 (should (null (company-elisp 'prefix)))))
738
739 (ert-deftest company-elisp-prefix-in-defun-body ()
740 (company-elisp-with-buffer
741 "(defun foob ()|)"
742 (should (equal "" (company-elisp 'prefix)))))
743
744 ;;; Clang
745
746 (ert-deftest company-clang-objc-templatify ()
747 (with-temp-buffer
748 (let ((text "createBookWithTitle:andAuthor:"))
749 (insert text)
750 (company-clang-objc-templatify text)
751 (should (equal "createBookWithTitle:arg0 andAuthor:arg1" (buffer-string)))
752 (should (looking-at "arg0"))
753 (should (null (overlay-get (company-template-field-at) 'display))))))