1 ;;; ert-x-tests.el --- Tests for ert-x.el
3 ;; Copyright (C) 2008, 2010-2015 Free Software Foundation, Inc.
5 ;; Author: Phil Hagelberg
6 ;; Christian Ohler <ohler@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; This program is free software: you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation, either version 3 of the
13 ;; License, or (at your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see `http://www.gnu.org/licenses/'.
25 ;; This file is part of ERT, the Emacs Lisp Regression Testing tool.
26 ;; See ert.el or the texinfo manual for more details.
37 (ert-deftest ert-test-buffer-string-reindented
()
38 (ert-with-test-buffer (:name
"well-indented")
39 (insert (concat "(hello (world\n"
42 (should (equal (ert-buffer-string-reindented) (buffer-string))))
43 (ert-with-test-buffer (:name
"badly-indented")
44 (insert (concat "(hello\n"
47 (should-not (equal (ert-buffer-string-reindented) (buffer-string)))))
49 (defun ert--hash-table-to-alist (table)
51 (maphash (lambda (key value
)
52 (push (cons key value
) accu
))
56 (ert-deftest ert-test-test-buffers
()
63 (ert-with-test-buffer (:name
"foo")
65 "[*]Test buffer (ert-test-test-buffers): foo[*]"
67 (setq buffer-1
(current-buffer))))))
72 (ert-with-test-buffer (:name
"bar")
74 "[*]Test buffer (ert-test-test-buffers): bar[*]"
76 (setq buffer-2
(current-buffer))
77 (ert-fail "fail for test"))))))
78 (let ((ert--test-buffers (make-hash-table :weakness t
)))
79 (ert-run-tests `(member ,test-1
,test-2
) #'ignore
)
80 (should (equal (ert--hash-table-to-alist ert--test-buffers
)
82 (should-not (buffer-live-p buffer-1
))
83 (should (buffer-live-p buffer-2
))))))
86 (ert-deftest ert-filter-string
()
87 (should (equal (ert-filter-string "foo bar baz" "quux")
89 (should (equal (ert-filter-string "foo bar baz" "bar")
92 (ert-deftest ert-propertized-string
()
93 (should (ert-equal-including-properties
94 (ert-propertized-string "a" '(a b
) "b" '(c t
) "cd")
95 #("abcd" 1 2 (a b
) 2 4 (c t
))))
96 (should (ert-equal-including-properties
97 (ert-propertized-string "foo " '(face italic
) "bar" " baz" nil
99 #("foo bar baz quux" 4 11 (face italic
)))))
102 ;;; Tests for ERT itself that require test features from ert-x.el.
104 (ert-deftest ert-test-run-tests-interactively-2
()
105 :tags
'(:causes-redisplay
)
106 (let* ((passing-test (make-ert-test :name
'passing-test
107 :body
(lambda () (ert-pass))))
108 (failing-test (make-ert-test :name
'failing-test
110 (ert-info ((propertize "foo\nbar"
113 "failure message")))))
114 (skipped-test (make-ert-test :name
'skipped-test
115 :body
(lambda () (ert-skip
117 (ert-debug-on-error nil
)
118 (buffer-name (generate-new-buffer-name "*ert-test-run-tests*"))
121 (lambda (format-string &rest args
)
122 (push (apply #'format format-string args
) messages
))))
123 (cl-flet ((expected-string (with-font-lock-p)
124 (ert-propertized-string
125 "Selector: (member <passing-test> <failing-test> "
128 "Failed: 1 (1 unexpected)\n"
134 `(category ,(button-category-symbol
135 'ert--results-progress-bar-button
)
137 face
,(if with-font-lock-p
138 'ert-test-result-unexpected
141 `(category ,(button-category-symbol
142 'ert--results-expand-collapse-button
)
144 face
,(if with-font-lock-p
145 'ert-test-result-unexpected
148 `(category ,(button-category-symbol
149 'ert--test-name-button
)
151 ert-test-name failing-test
)
153 nil
"\n Info: " '(a b
) "foo\n"
155 nil
"\n (ert-test-failed \"failure message\")\n\n\n"
157 (save-window-excursion
159 (let ((case-fold-search nil
))
160 (ert-run-tests-interactively
161 `(member ,passing-test
,failing-test
,skipped-test
) buffer-name
163 (should (equal messages
`(,(concat
164 "Ran 3 tests, 1 results were "
165 "as expected, 1 unexpected, "
167 (with-current-buffer buffer-name
169 (should (ert-equal-including-properties
170 (ert-filter-string (buffer-string)
171 '("Started at:\\(.*\\)$" 1)
172 '("Finished at:\\(.*\\)$" 1))
173 (expected-string nil
)))
174 ;; `font-lock-mode' only works if interactive, so
176 (let ((noninteractive nil
))
178 (should (ert-equal-including-properties
179 (ert-filter-string (buffer-string)
180 '("Started at:\\(.*\\)$" 1)
181 '("Finished at:\\(.*\\)$" 1))
182 (expected-string t
)))))
183 (when (get-buffer buffer-name
)
184 (kill-buffer buffer-name
)))))))
186 (ert-deftest ert-test-describe-test
()
187 "Tests `ert-describe-test'."
188 (save-window-excursion
189 (ert-with-buffer-renamed ("*Help*")
190 (if (< emacs-major-version
24)
191 (should (equal (should-error (ert-describe-test 'ert-describe-test
))
192 '(error "Requires Emacs 24")))
193 (ert-describe-test 'ert-test-describe-test
)
194 (with-current-buffer "*Help*"
195 (let ((case-fold-search nil
))
196 (should (string-match (concat
197 "\\`ert-test-describe-test is a test"
199 " ['`‘]ert-x-tests.elc?['’]\\.\n\n"
200 "Tests ['`‘]ert-describe-test['’]\\.\n\\'")
201 (buffer-string)))))))))
203 (ert-deftest ert-test-message-log-truncation
()
204 :tags
'(:causes-redisplay
)
205 (let ((test (make-ert-test
207 ;; Emacs would combine messages if we
208 ;; generate the same message multiple
215 (ert-with-buffer-renamed ("*Messages*")
216 (let ((message-log-max 2))
217 (setq result
(ert-run-test test
)))
218 (should (equal (with-current-buffer "*Messages*"
221 (should (equal (ert-test-result-messages result
) "a\nb\nc\nd\n")))))
223 (ert-deftest ert-test-builtin-message-log-flushing
()
224 "This test attempts to demonstrate that there is no way to
225 force immediate truncation of the *Messages* buffer from Lisp
226 \(and hence justifies the existence of
227 `ert--force-message-log-buffer-truncation'\): The only way that
228 came to my mind was \(message \"\"\), which doesn't have the
230 :tags
'(:causes-redisplay
)
231 (ert-with-buffer-renamed ("*Messages*")
232 (with-current-buffer "*Messages*"
233 (should (equal (buffer-string) ""))
234 ;; We used to get sporadic failures in this test that involved
235 ;; a spurious newline at the beginning of the buffer, before
236 ;; the first message. Below, we print a message and erase the
237 ;; buffer since this seems to eliminate the sporadic failures.
240 (should (equal (buffer-string) ""))
241 (let ((message-log-max 2))
242 (let ((message-log-max t
))
243 (cl-loop for i below
4 do
245 (should (equal (buffer-string) "0\n1\n2\n3\n")))
246 (should (equal (buffer-string) "0\n1\n2\n3\n"))
248 (should (equal (buffer-string) "0\n1\n2\n3\n"))
249 (message "Test message")
250 (should (equal (buffer-string) "3\nTest message\n"))))))
252 (ert-deftest ert-test-force-message-log-buffer-truncation
()
253 :tags
'(:causes-redisplay
)
255 (cl-loop for i below
3 do
257 ;; Uses the implicit messages buffer truncation implemented
260 (ert-with-buffer-renamed ("*Messages*")
261 (let ((message-log-max x
))
263 (with-current-buffer "*Messages*"
265 ;; Uses our lisp reimplementation.
267 (ert-with-buffer-renamed ("*Messages*")
268 (let ((message-log-max t
))
270 (let ((message-log-max x
))
271 (ert--force-message-log-buffer-truncation))
272 (with-current-buffer "*Messages*"
274 (cl-loop for x in
'(0 1 2 3 4 t
) do
275 (should (equal (c x
) (lisp x
))))))
278 (provide 'ert-x-tests
)
280 ;;; ert-x-tests.el ends here