428
|
1 ;; test-harness.el --- Run Emacs Lisp test suites.
|
|
2
|
|
3 ;;; Copyright (C) 1998 Free Software Foundation, Inc.
|
826
|
4 ;;; Copyright (C) 2002 Ben Wing.
|
428
|
5
|
|
6 ;; Author: Martin Buchholz
|
|
7 ;; Keywords: testing
|
|
8
|
|
9 ;; This file is part of XEmacs.
|
|
10
|
|
11 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
12 ;; under the terms of the GNU General Public License as published by
|
|
13 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
14 ;; any later version.
|
|
15
|
|
16 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
19 ;; General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
|
22 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
24 ;; Boston, MA 02111-1307, USA.
|
|
25
|
|
26 ;;; Synched up with: Not in FSF.
|
|
27
|
|
28 ;;; Commentary:
|
|
29
|
|
30 ;;; A test suite harness for testing XEmacs.
|
|
31 ;;; The actual tests are in other files in this directory.
|
|
32 ;;; Basically you just create files of emacs-lisp, and use the
|
|
33 ;;; Assert, Check-Error, and Check-Message functions to create tests.
|
|
34 ;;; You run the tests using M-x test-emacs-test-file,
|
|
35 ;;; or $(EMACS) -batch -l .../test-harness.el -f batch-test-emacs file ...
|
|
36 ;;; which is run for you by the `make check' target in the top-level Makefile.
|
|
37
|
|
38 (require 'bytecomp)
|
|
39
|
|
40 (defvar test-harness-verbose
|
|
41 (and (not noninteractive) (> (device-baud-rate) search-slow-speed))
|
|
42 "*Non-nil means print messages describing progress of emacs-tester.")
|
|
43
|
|
44 (defvar test-harness-current-file nil)
|
|
45
|
|
46 (defvar emacs-lisp-file-regexp (purecopy "\\.el\\'")
|
|
47 "*Regexp which matches Emacs Lisp source files.")
|
|
48
|
|
49 ;;;###autoload
|
|
50 (defun test-emacs-test-file (filename)
|
|
51 "Test a file of Lisp code named FILENAME.
|
|
52 The output file's name is made by appending `c' to the end of FILENAME."
|
|
53 (interactive
|
|
54 (let ((file buffer-file-name)
|
|
55 (file-name nil)
|
|
56 (file-dir nil))
|
|
57 (and file
|
|
58 (eq (cdr (assq 'major-mode (buffer-local-variables)))
|
|
59 'emacs-lisp-mode)
|
|
60 (setq file-name (file-name-nondirectory file)
|
|
61 file-dir (file-name-directory file)))
|
|
62 (list (read-file-name "Test file: " file-dir nil nil file-name))))
|
|
63 ;; Expand now so we get the current buffer's defaults
|
|
64 (setq filename (expand-file-name filename))
|
|
65
|
|
66 ;; If we're testing a file that's in a buffer and is modified, offer
|
|
67 ;; to save it first.
|
|
68 (or noninteractive
|
|
69 (let ((b (get-file-buffer (expand-file-name filename))))
|
|
70 (if (and b (buffer-modified-p b)
|
|
71 (y-or-n-p (format "save buffer %s first? " (buffer-name b))))
|
|
72 (save-excursion (set-buffer b) (save-buffer)))))
|
|
73
|
|
74 (if (or noninteractive test-harness-verbose)
|
|
75 (message "Testing %s..." filename))
|
|
76 (let ((test-harness-current-file filename)
|
|
77 input-buffer)
|
|
78 (save-excursion
|
|
79 (setq input-buffer (get-buffer-create " *Test Input*"))
|
|
80 (set-buffer input-buffer)
|
|
81 (erase-buffer)
|
|
82 (insert-file-contents filename)
|
|
83 ;; Run hooks including the uncompression hook.
|
|
84 ;; If they change the file name, then change it for the output also.
|
|
85 (let ((buffer-file-name filename)
|
|
86 (default-major-mode 'emacs-lisp-mode)
|
|
87 (enable-local-eval nil))
|
|
88 (normal-mode)
|
|
89 (setq filename buffer-file-name)))
|
|
90 (test-harness-from-buffer input-buffer filename)
|
|
91 (kill-buffer input-buffer)
|
|
92 ))
|
|
93
|
|
94 (defun test-harness-read-from-buffer (buffer)
|
|
95 "Read forms from BUFFER, and turn it into a lambda test form."
|
|
96 (let ((body nil))
|
|
97 (goto-char (point-min) buffer)
|
|
98 (condition-case error-info
|
|
99 (while t
|
|
100 (setq body (cons (read buffer) body)))
|
|
101 (end-of-file nil)
|
|
102 (error
|
928
|
103 (princ (format "Unexpected error %S reading forms from buffer\n" error-info))))
|
428
|
104 `(lambda ()
|
|
105 (defvar passes)
|
|
106 (defvar assertion-failures)
|
|
107 (defvar no-error-failures)
|
|
108 (defvar wrong-error-failures)
|
|
109 (defvar missing-message-failures)
|
|
110 (defvar other-failures)
|
|
111
|
|
112 (defvar unexpected-test-suite-failure)
|
|
113 (defvar trick-optimizer)
|
|
114
|
|
115 ,@(nreverse body))))
|
|
116
|
|
117 (defun test-harness-from-buffer (inbuffer filename)
|
|
118 "Run tests in buffer INBUFFER, visiting FILENAME."
|
|
119 (defvar trick-optimizer)
|
|
120 (let ((passes 0)
|
|
121 (assertion-failures 0)
|
|
122 (no-error-failures 0)
|
|
123 (wrong-error-failures 0)
|
|
124 (missing-message-failures 0)
|
|
125 (other-failures 0)
|
|
126
|
|
127 (trick-optimizer nil)
|
|
128 (unexpected-test-suite-failure nil)
|
826
|
129 (debug-on-error t)
|
|
130 (pass-stream nil))
|
428
|
131 (with-output-to-temp-buffer "*Test-Log*"
|
826
|
132 (princ (format "Testing %s...\n\n" filename))
|
|
133
|
|
134 (defun Print-Failure (fmt &rest args)
|
|
135 (setq fmt (concat "FAIL: " fmt))
|
|
136 (if (noninteractive) (apply #'message fmt args))
|
|
137 (princ (concat (apply #'format fmt args) "\n")))
|
|
138
|
|
139 (defun Print-Pass (fmt &rest args)
|
|
140 (setq fmt (concat "PASS: " fmt))
|
|
141 (and test-harness-verbose
|
|
142 (princ (concat (apply #'format fmt args) "\n"))))
|
|
143
|
428
|
144
|
|
145 (defmacro Assert (assertion)
|
|
146 `(condition-case error-info
|
|
147 (progn
|
|
148 (assert ,assertion)
|
826
|
149 (Print-Pass "%S" (quote ,assertion))
|
428
|
150 (incf passes))
|
|
151 (cl-assertion-failed
|
826
|
152 (Print-Failure "Assertion failed: %S" (quote ,assertion))
|
428
|
153 (incf assertion-failures))
|
826
|
154 (t (Print-Failure "%S ==> error: %S" (quote ,assertion) error-info)
|
428
|
155 (incf other-failures)
|
|
156 )))
|
|
157
|
|
158 (defmacro Check-Error (expected-error &rest body)
|
|
159 (let ((quoted-body (if (= 1 (length body))
|
|
160 `(quote ,(car body)) `(quote (progn ,@body)))))
|
|
161 `(condition-case error-info
|
|
162 (progn
|
|
163 (setq trick-optimizer (progn ,@body))
|
826
|
164 (Print-Failure "%S executed successfully, but expected error %S"
|
428
|
165 ,quoted-body
|
826
|
166 ',expected-error)
|
428
|
167 (incf no-error-failures))
|
|
168 (,expected-error
|
826
|
169 (Print-Pass "%S ==> error %S, as expected"
|
|
170 ,quoted-body ',expected-error)
|
428
|
171 (incf passes))
|
|
172 (error
|
826
|
173 (Print-Failure "%S ==> expected error %S, got error %S instead"
|
|
174 ,quoted-body ',expected-error error-info)
|
428
|
175 (incf wrong-error-failures)))))
|
|
176
|
826
|
177 (defmacro Check-Error-Message (expected-error expected-error-regexp
|
|
178 &rest body)
|
428
|
179 (let ((quoted-body (if (= 1 (length body))
|
|
180 `(quote ,(car body)) `(quote (progn ,@body)))))
|
|
181 `(condition-case error-info
|
|
182 (progn
|
|
183 (setq trick-optimizer (progn ,@body))
|
826
|
184 (Print-Failure "%S executed successfully, but expected error %S"
|
|
185 ,quoted-body ',expected-error)
|
428
|
186 (incf no-error-failures))
|
|
187 (,expected-error
|
|
188 (let ((error-message (second error-info)))
|
|
189 (if (string-match ,expected-error-regexp error-message)
|
|
190 (progn
|
826
|
191 (Print-Pass "%S ==> error %S %S, as expected"
|
|
192 ,quoted-body error-message ',expected-error)
|
428
|
193 (incf passes))
|
826
|
194 (Print-Failure "%S ==> got error %S as expected, but error message %S did not match regexp %S"
|
|
195 ,quoted-body ',expected-error error-message ,expected-error-regexp)
|
428
|
196 (incf wrong-error-failures))))
|
|
197 (error
|
826
|
198 (Print-Failure "%S ==> expected error %S, got error %S instead"
|
|
199 ,quoted-body ',expected-error error-info)
|
428
|
200 (incf wrong-error-failures)))))
|
|
201
|
|
202
|
|
203 (defmacro Check-Message (expected-message-regexp &rest body)
|
|
204 (let ((quoted-body (if (= 1 (length body))
|
|
205 `(quote ,(car body)) `(quote (progn ,@body)))))
|
|
206 `(let ((messages ""))
|
|
207 (defadvice message (around collect activate)
|
|
208 (defvar messages)
|
|
209 (let ((msg-string (apply 'format (ad-get-args 0))))
|
|
210 (setq messages (concat messages msg-string))
|
|
211 msg-string))
|
|
212 (condition-case error-info
|
|
213 (progn
|
|
214 (setq trick-optimizer (progn ,@body))
|
|
215 (if (string-match ,expected-message-regexp messages)
|
|
216 (progn
|
826
|
217 (Print-Pass "%S ==> value %S, message %S, matching %S, as expected"
|
|
218 ,quoted-body trick-optimizer messages ',expected-message-regexp)
|
428
|
219 (incf passes))
|
826
|
220 (Print-Failure "%S ==> value %S, message %S, NOT matching expected %S"
|
|
221 ,quoted-body trick-optimizer messages
|
|
222 ',expected-message-regexp)
|
428
|
223 (incf missing-message-failures)))
|
|
224 (error
|
826
|
225 (Print-Failure "%S ==> unexpected error %S"
|
|
226 ,quoted-body error-info)
|
428
|
227 (incf other-failures)))
|
|
228 (ad-unadvise 'message))))
|
|
229
|
|
230 (defmacro Ignore-Ebola (&rest body)
|
|
231 `(let ((debug-issue-ebola-notices -42)) ,@body))
|
|
232
|
|
233 (defun Int-to-Marker (pos)
|
|
234 (save-excursion
|
|
235 (set-buffer standard-output)
|
|
236 (save-excursion
|
|
237 (goto-char pos)
|
|
238 (point-marker))))
|
|
239
|
|
240 (princ "Testing Interpreted Lisp\n\n")
|
|
241 (condition-case error-info
|
|
242 (funcall (test-harness-read-from-buffer inbuffer))
|
|
243 (error
|
|
244 (setq unexpected-test-suite-failure t)
|
|
245 (princ (format "Unexpected error %S while executing interpreted code\n"
|
|
246 error-info))
|
|
247 (message "Unexpected error %S while executing interpreted code." error-info)
|
|
248 (message "Test suite execution aborted." error-info)
|
|
249 ))
|
|
250 (princ "\nTesting Compiled Lisp\n\n")
|
|
251 (let (code)
|
|
252 (condition-case error-info
|
446
|
253 (setq code
|
|
254 ;; our lisp code is often intentionally dubious,
|
|
255 ;; so throw away _all_ the byte compiler warnings.
|
|
256 (letf (((symbol-function 'byte-compile-warn) 'ignore))
|
|
257 (byte-compile (test-harness-read-from-buffer inbuffer))))
|
428
|
258 (error
|
|
259 (princ (format "Unexpected error %S while byte-compiling code\n"
|
|
260 error-info))))
|
|
261 (condition-case error-info
|
|
262 (if code (funcall code))
|
|
263 (error
|
|
264 (princ (format "Unexpected error %S while executing byte-compiled code\n"
|
|
265 error-info))
|
|
266 (message "Unexpected error %S while executing byte-compiled code." error-info)
|
|
267 (message "Test suite execution aborted." error-info)
|
|
268 )))
|
826
|
269 (princ (format "\nSUMMARY for %s:\n" filename))
|
428
|
270 (princ (format "\t%5d passes\n" passes))
|
|
271 (princ (format "\t%5d assertion failures\n" assertion-failures))
|
|
272 (princ (format "\t%5d errors that should have been generated, but weren't\n" no-error-failures))
|
|
273 (princ (format "\t%5d wrong-error failures\n" wrong-error-failures))
|
|
274 (princ (format "\t%5d missing-message failures\n" missing-message-failures))
|
|
275 (princ (format "\t%5d other failures\n" other-failures))
|
|
276 (let* ((total (+ passes
|
|
277 assertion-failures
|
|
278 no-error-failures
|
|
279 wrong-error-failures
|
|
280 missing-message-failures
|
|
281 other-failures))
|
|
282 (basename (file-name-nondirectory filename))
|
|
283 (summary-msg
|
|
284 (if (> total 0)
|
|
285 (format "%s: %d of %d (%d%%) tests successful."
|
|
286 basename passes total (/ (* 100 passes) total))
|
|
287 (format "%s: No tests run" basename))))
|
|
288 (message "%s" summary-msg))
|
|
289 (when unexpected-test-suite-failure
|
|
290 (message "Test suite execution failed unexpectedly."))
|
|
291 (fmakunbound 'Assert)
|
|
292 (fmakunbound 'Check-Error)
|
863
|
293 (fmakunbound 'Check-Message)
|
|
294 (fmakunbound 'Check-Error-Message)
|
428
|
295 (fmakunbound 'Ignore-Ebola)
|
|
296 (fmakunbound 'Int-to-Marker)
|
|
297 )))
|
|
298
|
|
299 (defvar test-harness-results-point-max nil)
|
|
300 (defmacro displaying-emacs-test-results (&rest body)
|
|
301 `(let ((test-harness-results-point-max test-harness-results-point-max))
|
|
302 ;; Log the file name.
|
|
303 (test-harness-log-file)
|
|
304 ;; Record how much is logged now.
|
|
305 ;; We will display the log buffer if anything more is logged
|
|
306 ;; before the end of BODY.
|
|
307 (or test-harness-results-point-max
|
|
308 (save-excursion
|
|
309 (set-buffer (get-buffer-create "*Test-Log*"))
|
|
310 (setq test-harness-results-point-max (point-max))))
|
|
311 (unwind-protect
|
|
312 (condition-case error-info
|
|
313 (progn ,@body)
|
|
314 (error
|
|
315 (test-harness-report-error error-info)))
|
|
316 (save-excursion
|
|
317 ;; If there were compilation warnings, display them.
|
|
318 (set-buffer "*Test-Log*")
|
|
319 (if (= test-harness-results-point-max (point-max))
|
|
320 nil
|
|
321 (if temp-buffer-show-function
|
|
322 (let ((show-buffer (get-buffer-create "*Test-Log-Show*")))
|
|
323 (save-excursion
|
|
324 (set-buffer show-buffer)
|
|
325 (setq buffer-read-only nil)
|
|
326 (erase-buffer))
|
|
327 (copy-to-buffer show-buffer
|
|
328 (save-excursion
|
|
329 (goto-char test-harness-results-point-max)
|
|
330 (forward-line -1)
|
|
331 (point))
|
|
332 (point-max))
|
|
333 (funcall temp-buffer-show-function show-buffer))
|
|
334 (select-window
|
|
335 (prog1 (selected-window)
|
|
336 (select-window (display-buffer (current-buffer)))
|
|
337 (goto-char test-harness-results-point-max)
|
|
338 (recenter 1)))))))))
|
|
339
|
|
340 (defun batch-test-emacs-1 (file)
|
|
341 (condition-case error-info
|
|
342 (progn (test-emacs-test-file file) t)
|
|
343 (error
|
|
344 (princ ">>Error occurred processing ")
|
|
345 (princ file)
|
|
346 (princ ": ")
|
|
347 (display-error error-info nil)
|
|
348 (terpri)
|
|
349 nil)))
|
|
350
|
|
351 (defun batch-test-emacs ()
|
|
352 "Run `test-harness' on the files remaining on the command line.
|
|
353 Use this from the command line, with `-batch';
|
|
354 it won't work in an interactive Emacs.
|
|
355 Each file is processed even if an error occurred previously.
|
|
356 For example, invoke \"xemacs -batch -f batch-test-emacs tests/*.el\""
|
|
357 ;; command-line-args-left is what is left of the command line (from
|
|
358 ;; startup.el)
|
|
359 (defvar command-line-args-left) ;Avoid 'free variable' warning
|
|
360 (defvar debug-issue-ebola-notices)
|
|
361 (if (not noninteractive)
|
|
362 (error "`batch-test-emacs' is to be used only with -batch"))
|
|
363 (let ((error nil))
|
|
364 (dolist (file command-line-args-left)
|
|
365 (if (file-directory-p file)
|
|
366 (dolist (file-in-dir (directory-files file t))
|
|
367 (when (and (string-match emacs-lisp-file-regexp file-in-dir)
|
|
368 (not (or (auto-save-file-name-p file-in-dir)
|
|
369 (backup-file-name-p file-in-dir)
|
|
370 (equal (file-name-nondirectory file-in-dir)
|
|
371 "test-harness.el"))))
|
|
372 (or (batch-test-emacs-1 file-in-dir)
|
|
373 (setq error t))))
|
|
374 (or (batch-test-emacs-1 file)
|
|
375 (setq error t))))
|
|
376 ;;(message "%s" (buffer-string nil nil "*Test-Log*"))
|
|
377 (message "Done")
|
|
378 (kill-emacs (if error 1 0))))
|
|
379
|
|
380 (provide 'test-harness)
|
|
381
|
|
382 ;;; test-harness.el ends here
|