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