1 ;;; elisp-tests.el --- company-elisp tests
3 ;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
5 ;; Author: Dmitry Gutov
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 (require 'company-tests)
25 (require 'company-elisp)
27 (defmacro company-elisp-with-buffer (contents &rest body)
31 (setq major-mode 'emacs-lisp-mode)
32 (re-search-backward "|")
34 (let ((company-elisp-detect-function-context t))
37 (ert-deftest company-elisp-candidates-predicate ()
38 (company-elisp-with-buffer
40 (should (eq (company-elisp--candidates-predicate "ba")
42 (should (eq (let (company-elisp-detect-function-context)
43 (company-elisp--candidates-predicate "ba"))
44 'company-elisp--predicate)))
45 (company-elisp-with-buffer
47 (should (eq (company-elisp--candidates-predicate "foo")
49 (should (eq (let (company-elisp-detect-function-context)
50 (company-elisp--candidates-predicate "foo"))
51 'company-elisp--predicate)))
52 (company-elisp-with-buffer
54 (should (eq (company-elisp--candidates-predicate "b")
55 'company-elisp--predicate))))
57 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
58 (company-elisp-with-buffer
59 "(def foo () \"Doo be doo `ide|"
60 (should (eq 'company-elisp--predicate
61 (company-elisp--candidates-predicate "ide")))))
63 ;; This one's also an integration test.
64 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
65 (let ((company-elisp-detect-function-context t)
66 (obarray [when what whelp])
70 (company-elisp-with-buffer
71 "(let ((foo 7) (wh| )))"
72 (should (equal '("what" "whelp")
73 (company-elisp-candidates "wh"))))
74 (company-elisp-with-buffer
75 "(cond ((null nil) (wh| )))"
76 (should (equal '("when")
77 (company-elisp-candidates "wh"))))))
79 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
80 (cl-loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
81 ("(let (foo (bar|" "bar" boundp)
82 ("(let (foo) (bar|" "bar" fboundp))
84 (eval `(company-elisp-with-buffer
86 (should (eq ',predicate
87 (company-elisp--candidates-predicate ,prefix)))))))
89 (ert-deftest company-elisp-finds-vars ()
90 (let ((obarray [boo bar baz backquote])
94 (should (equal '("bar" "baz")
95 (company-elisp--globals "ba" 'boundp)))))
97 (ert-deftest company-elisp-finds-functions ()
98 (let ((obarray [when what whelp])
101 (should (equal '("when")
102 (company-elisp--globals "wh" 'fboundp)))))
104 (ert-deftest company-elisp-finds-things ()
105 (let ((obarray [when what whelp])
108 (should (equal '("what" "whelp" "when")
109 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
112 (ert-deftest company-elisp-locals-vars ()
113 (company-elisp-with-buffer
114 "(let ((foo 5) (bar 6))
115 (cl-labels ((borg ()))
118 (should (equal '("bar" "baz" "boo")
119 (company-elisp--locals "b" nil)))))
121 (ert-deftest company-elisp-locals-single-var ()
122 (company-elisp-with-buffer
126 (should (equal '("itk" "item")
127 (company-elisp--locals "it" nil)))))
129 (ert-deftest company-elisp-locals-funs ()
130 (company-elisp-with-buffer
131 "(cl-labels ((foo ())
135 (should (equal '("fee" "foo")
136 (sort (company-elisp--locals "f" t) 'string<)))))
138 (ert-deftest company-elisp-locals-skips-current-varlist ()
139 (company-elisp-with-buffer
142 (should (null (company-elisp--locals "f" nil)))))
144 (ert-deftest company-elisp-show-locals-first ()
145 (company-elisp-with-buffer
150 (let ((obarray [float-pi]))
151 (let (company-elisp-show-locals-first)
152 (should (eq nil (company-elisp 'sorted))))
153 (let ((company-elisp-show-locals-first t))
154 (should (eq t (company-elisp 'sorted)))
155 (should (equal '("flee" "floo" "flop" "float-pi")
156 (company-elisp-candidates "fl")))))))
158 (ert-deftest company-elisp-candidates-no-duplicates ()
159 (company-elisp-with-buffer
162 (let ((obarray [float-pi])
163 (company-elisp-show-locals-first t))
164 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
166 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
167 (company-elisp-with-buffer
169 (should (null (company-elisp 'prefix)))))
171 (ert-deftest company-elisp-should-complete-def-call ()
172 (company-elisp-with-buffer
174 (should (equal "defu" (company-elisp 'prefix)))))
176 (ert-deftest company-elisp-should-complete-in-defvar ()
177 ;; It will also complete the var name, at least for now.
178 (company-elisp-with-buffer
180 (should (equal "de" (company-elisp 'prefix)))))
182 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
183 (company-elisp-with-buffer
184 "(defsubst foobar (ba|"
185 (should (null (company-elisp 'prefix)))))
187 (ert-deftest company-elisp-prefix-in-defun-body ()
188 (company-elisp-with-buffer
190 (should (equal "" (company-elisp 'prefix)))))