]> rtime.felk.cvut.cz Git - sojka/company-mode.git/blob - company-elisp-tests.el
Run tests non-interactively
[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 (require 'company-elisp)
29
30 (defmacro company-elisp-with-buffer (contents &rest body)
31   (declare (indent 0))
32   `(with-temp-buffer
33      (insert ,contents)
34      (setq major-mode 'emacs-lisp-mode)
35      (re-search-backward "|")
36      (replace-match "")
37      (let ((company-elisp-detect-function-context t))
38        ,@body)))
39
40 (ert-deftest company-elisp-candidates-predicate ()
41   (company-elisp-with-buffer
42     "(foo ba|)"
43     (should (eq (company-elisp--candidates-predicate "ba")
44                 'boundp))
45     (should (eq (let (company-elisp-detect-function-context)
46                   (company-elisp--candidates-predicate "ba"))
47                 'company-elisp--predicate)))
48   (company-elisp-with-buffer
49     "(foo| )"
50     (should (eq (company-elisp--candidates-predicate "foo")
51                 'fboundp))
52     (should (eq (let (company-elisp-detect-function-context)
53                   (company-elisp--candidates-predicate "foo"))
54                 'company-elisp--predicate)))
55   (company-elisp-with-buffer
56     "(foo 'b|)"
57     (should (eq (company-elisp--candidates-predicate "b")
58                 'company-elisp--predicate))))
59
60 (ert-deftest company-elisp-candidates-predicate-in-docstring ()
61   (company-elisp-with-buffer
62    "(def foo () \"Doo be doo `ide|"
63    (should (eq 'company-elisp--predicate
64                (company-elisp--candidates-predicate "ide")))))
65
66 ;; This one's also an integration test.
67 (ert-deftest company-elisp-candidates-recognizes-binding-form ()
68   (let ((company-elisp-detect-function-context t)
69         (obarray [when what whelp])
70         (what 1)
71         (whelp 2)
72         (wisp 3))
73     (company-elisp-with-buffer
74       "(let ((foo 7) (wh| )))"
75       (should (equal '("what" "whelp")
76                      (company-elisp-candidates "wh"))))
77     (company-elisp-with-buffer
78       "(cond ((null nil) (wh| )))"
79       (should (equal '("when")
80                      (company-elisp-candidates "wh"))))))
81
82 (ert-deftest company-elisp-candidates-predicate-binding-without-value ()
83   (cl-loop for (text prefix predicate) in '(("(let (foo|" "foo" boundp)
84                                             ("(let (foo (bar|" "bar" boundp)
85                                             ("(let (foo) (bar|" "bar" fboundp))
86            do
87            (eval `(company-elisp-with-buffer
88                    ,text
89                    (should (eq ',predicate
90                                (company-elisp--candidates-predicate ,prefix)))))))
91
92 (ert-deftest company-elisp-finds-vars ()
93   (let ((obarray [boo bar baz backquote])
94         (boo t)
95         (bar t)
96         (baz t))
97     (should (equal '("bar" "baz")
98                    (company-elisp--globals "ba" 'boundp)))))
99
100 (ert-deftest company-elisp-finds-functions ()
101   (let ((obarray [when what whelp])
102         (what t)
103         (whelp t))
104     (should (equal '("when")
105                    (company-elisp--globals "wh" 'fboundp)))))
106
107 (ert-deftest company-elisp-finds-things ()
108   (let ((obarray [when what whelp])
109         (what t)
110         (whelp t))
111     (should (equal '("what" "whelp" "when")
112                    (sort (company-elisp--globals "wh" 'company-elisp--predicate)
113                          'string<)))))
114
115 (ert-deftest company-elisp-locals-vars ()
116   (company-elisp-with-buffer
117     "(let ((foo 5) (bar 6))
118        (cl-labels ((borg ()))
119          (lambda (boo baz)
120            b|)))"
121     (should (equal '("bar" "baz" "boo")
122                    (company-elisp--locals "b" nil)))))
123
124 (ert-deftest company-elisp-locals-single-var ()
125   (company-elisp-with-buffer
126     "(dotimes (itk 100)
127        (dolist (item items)
128          it|))"
129     (should (equal '("itk" "item")
130                    (company-elisp--locals "it" nil)))))
131
132 (ert-deftest company-elisp-locals-funs ()
133   (company-elisp-with-buffer
134     "(cl-labels ((foo ())
135                  (fee ()))
136        (let ((fun 4))
137          (f| )))"
138     (should (equal '("fee" "foo")
139                    (sort (company-elisp--locals "f" t) 'string<)))))
140
141 (ert-deftest company-elisp-locals-skips-current-varlist ()
142   (company-elisp-with-buffer
143     "(let ((foo 1)
144            (f| )))"
145     (should (null (company-elisp--locals "f" nil)))))
146
147 (ert-deftest company-elisp-show-locals-first ()
148   (company-elisp-with-buffer
149     "(let ((floo 1)
150            (flop 2)
151            (flee 3))
152        fl|)"
153     (let ((obarray [float-pi]))
154       (let (company-elisp-show-locals-first)
155         (should (eq nil (company-elisp 'sorted))))
156       (let ((company-elisp-show-locals-first t))
157         (should (eq t (company-elisp 'sorted)))
158         (should (equal '("flee" "floo" "flop" "float-pi")
159                        (company-elisp-candidates "fl")))))))
160
161 (ert-deftest company-elisp-candidates-no-duplicates ()
162   (company-elisp-with-buffer
163     "(let ((float-pi 4))
164        f|)"
165     (let ((obarray [float-pi])
166           (company-elisp-show-locals-first t))
167       (should (equal '("float-pi") (company-elisp-candidates "f"))))))
168
169 (ert-deftest company-elisp-shouldnt-complete-defun-name ()
170   (company-elisp-with-buffer
171     "(defun foob|)"
172     (should (null (company-elisp 'prefix)))))
173
174 (ert-deftest company-elisp-should-complete-def-call ()
175   (company-elisp-with-buffer
176     "(defu|"
177     (should (equal "defu" (company-elisp 'prefix)))))
178
179 (ert-deftest company-elisp-should-complete-in-defvar ()
180   ;; It will also complete the var name, at least for now.
181   (company-elisp-with-buffer
182     "(defvar abc de|"
183     (should (equal "de" (company-elisp 'prefix)))))
184
185 (ert-deftest company-elisp-shouldnt-complete-in-defun-arglist ()
186   (company-elisp-with-buffer
187     "(defsubst foobar (ba|"
188     (should (null (company-elisp 'prefix)))))
189
190 (ert-deftest company-elisp-prefix-in-defun-body ()
191   (company-elisp-with-buffer
192     "(defun foob ()|)"
193     (should (equal "" (company-elisp 'prefix)))))