1 ;; test-harness.el --- Run Emacs Lisp test suites.
3 ;;; Copyright (C) 1998 Free Software Foundation, Inc.
5 ;; Author: Martin Buchholz
8 ;; This file is part of XEmacs.
10 ;; XEmacs is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; XEmacs 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 XEmacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Synched up with: Not in FSF.
29 ;;; A test suite harness for testing XEmacs.
30 ;;; The actual tests are in other files in this directory.
31 ;;; Basically you just create files of emacs-lisp, and use the
32 ;;; Assert, Check-Error, and Check-Message functions to create tests.
33 ;;; You run the tests using M-x test-emacs-test-file,
34 ;;; or $(EMACS) -batch -l .../test-harness.el -f batch-test-emacs file ...
35 ;;; which is run for you by the `make check' target in the top-level Makefile.
39 (defvar test-harness-verbose
40 (and (not noninteractive) (> (device-baud-rate) search-slow-speed))
41 "*Non-nil means print messages describing progress of emacs-tester.")
43 (defvar test-harness-current-file nil)
45 (defvar emacs-lisp-file-regexp (purecopy "\\.el\\'")
46 "*Regexp which matches Emacs Lisp source files.")
49 (defun test-emacs-test-file (filename)
50 "Test a file of Lisp code named FILENAME.
51 The output file's name is made by appending `c' to the end of FILENAME."
53 (let ((file buffer-file-name)
57 (eq (cdr (assq 'major-mode (buffer-local-variables)))
59 (setq file-name (file-name-nondirectory file)
60 file-dir (file-name-directory file)))
61 (list (read-file-name "Test file: " file-dir nil nil file-name))))
62 ;; Expand now so we get the current buffer's defaults
63 (setq filename (expand-file-name filename))
65 ;; If we're testing a file that's in a buffer and is modified, offer
68 (let ((b (get-file-buffer (expand-file-name filename))))
69 (if (and b (buffer-modified-p b)
70 (y-or-n-p (format "save buffer %s first? " (buffer-name b))))
71 (save-excursion (set-buffer b) (save-buffer)))))
73 (if (or noninteractive test-harness-verbose)
74 (message "Testing %s..." filename))
75 (let ((test-harness-current-file filename)
78 (setq input-buffer (get-buffer-create " *Test Input*"))
79 (set-buffer input-buffer)
81 (insert-file-contents filename)
82 ;; Run hooks including the uncompression hook.
83 ;; If they change the file name, then change it for the output also.
84 (let ((buffer-file-name filename)
85 (default-major-mode 'emacs-lisp-mode)
86 (enable-local-eval nil))
88 (setq filename buffer-file-name)))
89 (test-harness-from-buffer input-buffer filename)
90 (kill-buffer input-buffer)
93 (defun test-harness-read-from-buffer (buffer)
94 "Read forms from BUFFER, and turn it into a lambda test form."
96 (goto-char (point-min) buffer)
97 (condition-case error-info
99 (setq body (cons (read buffer) body)))
102 (princ (format "Unexpected error %S reading forms from buffer\n" error-info))))
105 (defvar assertion-failures)
106 (defvar no-error-failures)
107 (defvar wrong-error-failures)
108 (defvar missing-message-failures)
109 (defvar other-failures)
111 (defvar unexpected-test-suite-failure)
112 (defvar trick-optimizer)
116 (defun test-harness-from-buffer (inbuffer filename)
117 "Run tests in buffer INBUFFER, visiting FILENAME."
118 (defvar trick-optimizer)
120 (assertion-failures 0)
121 (no-error-failures 0)
122 (wrong-error-failures 0)
123 (missing-message-failures 0)
126 ;; #### perhaps this should be a defvar, and output at the very end
127 ;; OTOH, this way AC types can use a null EMACSPACKAGEPATH to find
128 ;; what stuff is needed, and ways to avoid using them
129 (skipped-test-reasons (make-hash-table :test 'equal))
131 (trick-optimizer nil)
132 (unexpected-test-suite-failure nil)
134 (with-output-to-temp-buffer "*Test-Log*"
136 (defmacro Assert (assertion)
137 `(condition-case error-info
140 (princ (format "PASS: %S" (quote ,assertion)))
144 (princ (format "FAIL: Assertion failed: %S\n" (quote ,assertion)))
145 (incf assertion-failures))
146 (t (princ (format "FAIL: %S ==> error: %S\n" (quote ,assertion) error-info))
147 (incf other-failures)
150 (defmacro Check-Error (expected-error &rest body)
151 (let ((quoted-body (if (= 1 (length body))
152 `(quote ,(car body)) `(quote (progn ,@body)))))
153 `(condition-case error-info
155 (setq trick-optimizer (progn ,@body))
156 (princ (format "FAIL: %S executed successfully, but expected error %S\n"
159 (incf no-error-failures))
161 (princ (format "PASS: %S ==> error %S, as expected\n"
162 ,quoted-body ',expected-error))
165 (princ (format "FAIL: %S ==> expected error %S, got error %S instead\n"
166 ,quoted-body ',expected-error error-info))
167 (incf wrong-error-failures)))))
169 (defmacro Check-Error-Message (expected-error expected-error-regexp &rest body)
170 (let ((quoted-body (if (= 1 (length body))
171 `(quote ,(car body)) `(quote (progn ,@body)))))
172 `(condition-case error-info
174 (setq trick-optimizer (progn ,@body))
175 (princ (format "FAIL: %S executed successfully, but expected error %S\n"
178 (incf no-error-failures))
180 (let ((error-message (second error-info)))
181 (if (string-match ,expected-error-regexp error-message)
183 (princ (format "PASS: %S ==> error %S %S, as expected\n"
184 ,quoted-body error-message ',expected-error))
186 (princ (format "FAIL: %S ==> got error %S as expected, but error message %S did not match regexp %S\n"
187 ,quoted-body ',expected-error error-message ,expected-error-regexp))
188 (incf wrong-error-failures))))
190 (princ (format "FAIL: %S ==> expected error %S, got error %S instead\n"
191 ,quoted-body ',expected-error error-info))
192 (incf wrong-error-failures)))))
194 (defun Print-Skip (test reason &optional fmt &rest args)
195 (setq fmt (concat "SKIP: %S. REASON: %S" fmt))
196 (princ (concat (apply #'format fmt test reason args) "\n")))
199 (defmacro Check-Message (expected-message-regexp &rest body)
200 (if (not (fboundp 'defadvice))
201 ;; #### This whole thing should go inside a macro Skip-Test
202 (let* ((reason "advice unavailable")
203 (count (gethash reason skipped-test-reasons)))
204 ;(message "%S: %S" reason count)
205 (puthash reason (if (null count) 1 (1+ count))
206 skipped-test-reasons)
207 `(Print-Skip ,expected-message-regexp ,reason))
208 (let ((quoted-body (if (= 1 (length body))
209 `(quote ,(car body)) `(quote (progn ,@body)))))
210 `(let ((messages ""))
211 (defadvice message (around collect activate)
213 (let ((msg-string (apply 'format (ad-get-args 0))))
214 (setq messages (concat messages msg-string))
216 (condition-case error-info
218 (setq trick-optimizer (progn ,@body))
219 (if (string-match ,expected-message-regexp messages)
221 (princ (format "PASS: %S ==> value %S, message %S, matching %S, as expected\n"
222 ,quoted-body trick-optimizer messages ',expected-message-regexp))
224 (princ (format "FAIL: %S ==> value %S, message %S, NOT matching expected %S\n"
225 ,quoted-body trick-optimizer messages ',expected-message-regexp))
226 (incf missing-message-failures)))
228 (princ (format "FAIL: %S ==> unexpected error %S\n"
229 ,quoted-body error-info))
230 (incf other-failures)))
231 (ad-unadvise 'message)))))
233 (defmacro Ignore-Ebola (&rest body)
234 `(let ((debug-issue-ebola-notices -42)) ,@body))
236 (defun Int-to-Marker (pos)
238 (set-buffer standard-output)
243 (princ "Testing Interpreted Lisp\n\n")
244 (condition-case error-info
245 (funcall (test-harness-read-from-buffer inbuffer))
247 (setq unexpected-test-suite-failure t)
248 (princ (format "Unexpected error %S while executing interpreted code\n"
250 (message "Unexpected error %S while executing interpreted code." error-info)
251 (message "Test suite execution aborted." error-info)
253 (princ "\nTesting Compiled Lisp\n\n")
255 (condition-case error-info
257 ;; our lisp code is often intentionally dubious,
258 ;; so throw away _all_ the byte compiler warnings.
259 (letf (((symbol-function 'byte-compile-warn) 'ignore))
260 (byte-compile (test-harness-read-from-buffer inbuffer))))
262 (princ (format "Unexpected error %S while byte-compiling code\n"
264 (condition-case error-info
265 (if code (funcall code))
267 (princ (format "Unexpected error %S while executing byte-compiled code\n"
269 (message "Unexpected error %S while executing byte-compiled code." error-info)
270 (message "Test suite execution aborted." error-info)
272 (princ "\nSUMMARY:\n")
273 (princ (format "\t%5d passes\n" passes))
274 (princ (format "\t%5d assertion failures\n" assertion-failures))
275 (princ (format "\t%5d errors that should have been generated, but weren't\n" no-error-failures))
276 (princ (format "\t%5d wrong-error failures\n" wrong-error-failures))
277 (princ (format "\t%5d missing-message failures\n" missing-message-failures))
278 (princ (format "\t%5d other failures\n" other-failures))
279 (let* ((total (+ passes
283 missing-message-failures
285 (basename (file-name-nondirectory filename))
288 (format "%s: %d of %d (%d%%) tests successful."
289 basename passes total (/ (* 100 passes) total))
290 (format "%s: No tests run" basename)))
292 (maphash (lambda (key value)
295 (format "\n %d tests skipped because %s"
297 skipped-test-reasons)
298 (when (> (length reasons) 1)
299 (setq summary-msg (concat summary-msg reasons "
300 Probably XEmacs cannot find your installed packages. Set EMACSPACKAGEPATH
301 to the package hierarchy root or configure with --package-path to enable
302 the skipped tests.")))
303 (message "%s" summary-msg))
304 (when unexpected-test-suite-failure
305 (message "Test suite execution failed unexpectedly."))
306 (fmakunbound 'Assert)
307 (fmakunbound 'Check-Error)
308 (fmakunbound 'Check-Message)
309 (fmakunbound 'Check-Error-Message)
310 (fmakunbound 'Ignore-Ebola)
311 (fmakunbound 'Int-to-Marker)
314 (defvar test-harness-results-point-max nil)
315 (defmacro displaying-emacs-test-results (&rest body)
316 `(let ((test-harness-results-point-max test-harness-results-point-max))
317 ;; Log the file name.
318 (test-harness-log-file)
319 ;; Record how much is logged now.
320 ;; We will display the log buffer if anything more is logged
321 ;; before the end of BODY.
322 (or test-harness-results-point-max
324 (set-buffer (get-buffer-create "*Test-Log*"))
325 (setq test-harness-results-point-max (point-max))))
327 (condition-case error-info
330 (test-harness-report-error error-info)))
332 ;; If there were compilation warnings, display them.
333 (set-buffer "*Test-Log*")
334 (if (= test-harness-results-point-max (point-max))
336 (if temp-buffer-show-function
337 (let ((show-buffer (get-buffer-create "*Test-Log-Show*")))
339 (set-buffer show-buffer)
340 (setq buffer-read-only nil)
342 (copy-to-buffer show-buffer
344 (goto-char test-harness-results-point-max)
348 (funcall temp-buffer-show-function show-buffer))
350 (prog1 (selected-window)
351 (select-window (display-buffer (current-buffer)))
352 (goto-char test-harness-results-point-max)
355 (defun batch-test-emacs-1 (file)
356 (condition-case error-info
357 (progn (test-emacs-test-file file) t)
359 (princ ">>Error occurred processing ")
362 (display-error error-info nil)
366 (defun batch-test-emacs ()
367 "Run `test-harness' on the files remaining on the command line.
368 Use this from the command line, with `-batch';
369 it won't work in an interactive Emacs.
370 Each file is processed even if an error occurred previously.
371 For example, invoke \"xemacs -batch -f batch-test-emacs tests/*.el\""
372 ;; command-line-args-left is what is left of the command line (from
374 (defvar command-line-args-left) ;Avoid 'free variable' warning
375 (defvar debug-issue-ebola-notices)
376 (if (not noninteractive)
377 (error "`batch-test-emacs' is to be used only with -batch"))
379 (dolist (file command-line-args-left)
380 (if (file-directory-p file)
381 (dolist (file-in-dir (directory-files file t))
382 (when (and (string-match emacs-lisp-file-regexp file-in-dir)
383 (not (or (auto-save-file-name-p file-in-dir)
384 (backup-file-name-p file-in-dir)
385 (equal (file-name-nondirectory file-in-dir)
386 "test-harness.el"))))
387 (or (batch-test-emacs-1 file-in-dir)
389 (or (batch-test-emacs-1 file)
391 ;;(message "%s" (buffer-string nil nil "*Test-Log*"))
393 (kill-emacs (if error 1 0))))
395 (provide 'test-harness)
397 ;;; test-harness.el ends here