]> code.delx.au - gnu-emacs-elpa/blob - test/elisp-tests.el
Make a new release
[gnu-emacs-elpa] / test / elisp-tests.el
1 ;;; elisp-tests.el --- company-elisp tests
2
3 ;; Copyright (C) 2013-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 ;;; Code:
23
24 (require 'company-tests)
25 (require 'company-elisp)
26
27 (defmacro company-elisp-with-buffer (contents &rest body)
28 (declare (indent 0))
29 `(with-temp-buffer
30 (insert ,contents)
31 (setq major-mode 'emacs-lisp-mode)
32 (re-search-backward "|")
33 (replace-match "")
34 (let ((company-elisp-detect-function-context t))
35 ,@body)))
36
37 (ert-deftest company-elisp-candidates-predicate ()
38 (company-elisp-with-buffer
39 "(foo ba|)"
40 (should (eq (company-elisp--candidates-predicate "ba")
41 'boundp))
42 (should (eq (let (company-elisp-detect-function-context)
43 (company-elisp--candidates-predicate "ba"))
44 'company-elisp--predicate)))
45 (company-elisp-with-buffer
46 "(foo| )"
47 (should (eq (company-elisp--candidates-predicate "foo")
48 'fboundp))
49 (should (eq (let (company-elisp-detect-function-context)
50 (company-elisp--candidates-predicate "foo"))
51 'company-elisp--predicate)))
52 (company-elisp-with-buffer
53 "(foo 'b|)"
54 (should (eq (company-elisp--candidates-predicate "b")
55 'company-elisp--predicate))))
56
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")))))
62
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])
67 (what 1)
68 (whelp 2)
69 (wisp 3))
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"))))))
78
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))
83 do
84 (eval `(company-elisp-with-buffer
85 ,text
86 (should (eq ',predicate
87 (company-elisp--candidates-predicate ,prefix)))))))
88
89 (ert-deftest company-elisp-finds-vars ()
90 (let ((obarray [boo bar baz backquote])
91 (boo t)
92 (bar t)
93 (baz t))
94 (should (equal '("bar" "baz")
95 (company-elisp--globals "ba" 'boundp)))))
96
97 (ert-deftest company-elisp-finds-functions ()
98 (let ((obarray [when what whelp])
99 (what t)
100 (whelp t))
101 (should (equal '("when")
102 (company-elisp--globals "wh" 'fboundp)))))
103
104 (ert-deftest company-elisp-finds-things ()
105 (let ((obarray [when what whelp])
106 (what t)
107 (whelp t))
108 (should (equal '("what" "whelp" "when")
109 (sort (company-elisp--globals "wh" 'company-elisp--predicate)
110 'string<)))))
111
112 (ert-deftest company-elisp-locals-vars ()
113 (company-elisp-with-buffer
114 "(let ((foo 5) (bar 6))
115 (cl-labels ((borg ()))
116 (lambda (boo baz)
117 b|)))"
118 (should (equal '("bar" "baz" "boo")
119 (company-elisp--locals "b" nil)))))
120
121 (ert-deftest company-elisp-locals-single-var ()
122 (company-elisp-with-buffer
123 "(dotimes (itk 100)
124 (dolist (item items)
125 it|))"
126 (should (equal '("itk" "item")
127 (company-elisp--locals "it" nil)))))
128
129 (ert-deftest company-elisp-locals-funs ()
130 (company-elisp-with-buffer
131 "(cl-labels ((foo ())
132 (fee ()))
133 (let ((fun 4))
134 (f| )))"
135 (should (equal '("fee" "foo")
136 (sort (company-elisp--locals "f" t) 'string<)))))
137
138 (ert-deftest company-elisp-locals-skips-current-varlist ()
139 (company-elisp-with-buffer
140 "(let ((foo 1)
141 (f| )))"
142 (should (null (company-elisp--locals "f" nil)))))
143
144 (ert-deftest company-elisp-show-locals-first ()
145 (company-elisp-with-buffer
146 "(let ((floo 1)
147 (flop 2)
148 (flee 3))
149 fl|)"
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")))))))
157
158 (ert-deftest company-elisp-candidates-no-duplicates ()
159 (company-elisp-with-buffer
160 "(let ((float-pi 4))
161 f|)"
162 (let ((obarray [float-pi])
163 (company-elisp-show-locals-first t))
164 (should (equal '("float-pi") (company-elisp-candidates "f"))))))
165
166 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
167 (company-elisp-with-buffer
168 "(defun foob|)"
169 (should (null (company-elisp 'prefix)))))
170
171 (ert-deftest company-elisp-should-complete-def-call ()
172 (company-elisp-with-buffer
173 "(defu|"
174 (should (equal "defu" (company-elisp 'prefix)))))
175
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
179 "(defvar abc de|"
180 (should (equal "de" (company-elisp 'prefix)))))
181
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)))))
186
187 (ert-deftest company-elisp-prefix-in-defun-body ()
188 (company-elisp-with-buffer
189 "(defun foob ()|)"
190 (should (equal "" (company-elisp 'prefix)))))