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