1 ;;; company-elisp-tests.el --- company-elisp tests
3 ;; Copyright (C) 2013-2014 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/>.
28 (defmacro company-elisp-with-buffer (contents &rest body)
32 (setq major-mode 'emacs-lisp-mode)
33 (re-search-backward "|")
35 (let ((company-elisp-detect-function-context t))
38 (ert-deftest company-elisp-candidates-predicate ()
39 (company-elisp-with-buffer
41 (should (eq (company-elisp--candidates-predicate "ba")
43 (should (eq (let (company-elisp-detect-function-context)
44 (company-elisp--candidates-predicate "ba"))
45 'company-elisp--predicate)))
46 (company-elisp-with-buffer
48 (should (eq (company-elisp--candidates-predicate "foo")
50 (should (eq (let (company-elisp-detect-function-context)
51 (company-elisp--candidates-predicate "foo"))
52 'company-elisp--predicate)))
53 (company-elisp-with-buffer
55 (should (eq (company-elisp--candidates-predicate "b")
56 'company-elisp--predicate))))
58 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
59 (company-elisp-with-buffer
60 "(def foo () \"Doo be doo `ide|"
61 (should (eq 'company-elisp--predicate
62 (company-elisp--candidates-predicate "ide")))))
64 ;; This one's also an integration test.
65 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
66 (let ((company-elisp-detect-function-context t)
67 (obarray [when what whelp])
71 (company-elisp-with-buffer
72 "(let ((foo 7) (wh| )))"
73 (should (equal '("what" "whelp")
74 (company-elisp-candidates "wh"))))
75 (company-elisp-with-buffer
76 "(cond ((null nil) (wh| )))"
77 (should (equal '("when")
78 (company-elisp-candidates "wh"))))))
80 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
81 (loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
82 ("(let (foo (bar|" "bar" boundp)
83 ("(let (foo) (bar|" "bar" fboundp))
85 (eval `(company-elisp-with-buffer
87 (should (eq ',predicate
88 (company-elisp--candidates-predicate ,prefix)))))))
90 (ert-deftest company-elisp-finds-vars ()
91 (let ((obarray [boo bar baz backquote])
95 (should (equal '("bar" "baz")
96 (company-elisp--globals "ba" 'boundp)))))
98 (ert-deftest company-elisp-finds-functions ()
99 (let ((obarray [when what whelp])
102 (should (equal '("when")
103 (company-elisp--globals "wh" 'fboundp)))))
105 (ert-deftest company-elisp-finds-things ()
106 (let ((obarray [when what whelp])
109 (should (equal '("what" "whelp" "when")
110 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
113 (ert-deftest company-elisp-locals-vars ()
114 (company-elisp-with-buffer
115 "(let ((foo 5) (bar 6))
116 (cl-labels ((borg ()))
119 (should (equal '("bar" "baz" "boo")
120 (company-elisp--locals "b" nil)))))
122 (ert-deftest company-elisp-locals-single-var ()
123 (company-elisp-with-buffer
127 (should (equal '("itk" "item")
128 (company-elisp--locals "it" nil)))))
130 (ert-deftest company-elisp-locals-funs ()
131 (company-elisp-with-buffer
132 "(cl-labels ((foo ())
136 (should (equal '("fee" "foo")
137 (sort (company-elisp--locals "f" t) 'string<)))))
139 (ert-deftest company-elisp-locals-skips-current-varlist ()
140 (company-elisp-with-buffer
143 (should (null (company-elisp--locals "f" nil)))))
145 (ert-deftest company-elisp-show-locals-first ()
146 (company-elisp-with-buffer
151 (let ((obarray [float-pi]))
152 (let (company-elisp-show-locals-first)
153 (should (eq nil (company-elisp 'sorted))))
154 (let ((company-elisp-show-locals-first t))
155 (should (eq t (company-elisp 'sorted)))
156 (should (equal '("flee" "floo" "flop" "float-pi")
157 (company-elisp-candidates "fl")))))))
159 (ert-deftest company-elisp-candidates-no-duplicates ()
160 (company-elisp-with-buffer
163 (let ((obarray [float-pi])
164 (company-elisp-show-locals-first t))
165 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
167 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
168 (company-elisp-with-buffer
170 (should (null (company-elisp 'prefix)))))
172 (ert-deftest company-elisp-should-complete-def-call ()
173 (company-elisp-with-buffer
175 (should (equal "defu" (company-elisp 'prefix)))))
177 (ert-deftest company-elisp-should-complete-in-defvar ()
178 ;; It will also complete the var name, at least for now.
179 (company-elisp-with-buffer
181 (should (equal "de" (company-elisp 'prefix)))))
183 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
184 (company-elisp-with-buffer
185 "(defsubst foobar (ba|"
186 (should (null (company-elisp 'prefix)))))
188 (ert-deftest company-elisp-prefix-in-defun-body ()
189 (company-elisp-with-buffer
191 (should (equal "" (company-elisp 'prefix)))))