1 ;;; lunit.el --- simple testing framework for luna
3 ;; Copyright (C) 2000 Daiki Ueno.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
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 2, or (at
13 ;; 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 GNU Emacs; 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.
27 ;; This module is inspired by "JUnit A Cook's Tour".
28 ;; <URL:http://www.junit.org/junit/doc/cookstour/cookstour.htm>
32 ;; (luna-define-class silly-test-case (lunit-test-case))
34 ;; (luna-define-method test-1 ((case silly-test-case))
35 ;; (lunit-assert (integerp "a")))
37 ;; (luna-define-method test-2 ((case silly-test-case))
38 ;; (lunit-assert (stringp "b")))
40 ;; (with-output-to-temp-buffer "*Lunit Results*"
41 ;; (lunit (lunit-make-test-suite-from-class 'silly-test-case)))
42 ;; ______________________________________________________________________
43 ;; Starting test `silly-test-case#test-1'
44 ;; failure: (integerp "a")
45 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46 ;; ______________________________________________________________________
47 ;; Starting test `silly-test-case#test-2'
48 ;; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49 ;; 2 runs, 1 failures, 0 errors
55 (eval-when-compile (require 'cl))
61 (luna-define-class lunit-test ()
64 (luna-define-internal-accessors 'lunit-test))
66 (luna-define-generic lunit-test-number-of-tests (test)
67 "Count the number of test cases that will be run by the test.")
69 (luna-define-generic lunit-test-run (test result)
70 "Run the test and collects its result in result.")
72 (luna-define-generic lunit-test-suite-add-test (suite test)
73 "Add the test to the suite.")
78 (luna-define-class lunit-test-listener)
83 (put 'lunit-error 'error-message "test error")
84 (put 'lunit-error 'error-conditions '(lunit-error error))
86 (put 'lunit-failure 'error-message "test failure")
87 (put 'lunit-failure 'error-conditions '(lunit-failure lunit-error error))
90 (luna-define-class lunit-test-result ()
95 (luna-define-internal-accessors 'lunit-test-result))
97 (luna-define-generic lunit-test-result-run (result case)
100 (luna-define-generic lunit-test-result-notify (result message &rest args)
101 "Report the current state of execution.")
103 (luna-define-generic lunit-test-result-error (result case error)
104 "Add error to the list of errors.
105 The passed in exception caused the error.")
107 (luna-define-generic lunit-test-result-failure (result case failure)
108 "Add failure to the list of failures.
109 The passed in exception caused the failure.")
111 (luna-define-generic lunit-test-result-add-listener (result listener)
112 "Add listener to the list of listeners.")
114 (defun lunit-make-test-result (&rest listeners)
115 "Return a newly allocated `lunit-test-result' instance with LISTENERS."
116 (luna-make-entity 'lunit-test-result :listeners listeners))
118 (luna-define-method lunit-test-result-notify ((result lunit-test-result)
121 (lunit-test-result-listeners-internal result)))
122 (dolist (listener listeners)
123 (apply #'luna-send listener message listener args))))
125 (luna-define-method lunit-test-result-run ((result lunit-test-result) case)
126 (lunit-test-result-notify result 'lunit-test-listener-start case)
127 (condition-case error
128 (lunit-test-case-run case)
130 (lunit-test-result-failure result case (nth 1 error)))
132 (lunit-test-result-error result case (cdr error))))
133 (lunit-test-result-notify result 'lunit-test-listener-end case))
135 (luna-define-method lunit-test-result-error ((result lunit-test-result)
138 (lunit-test-result-errors-internal result)))
139 (setq errors (nconc errors (list (cons case error))))
140 (lunit-test-result-set-errors-internal result errors))
141 (lunit-test-result-notify result 'lunit-test-listener-error case error))
143 (luna-define-method lunit-test-result-failure ((result lunit-test-result)
146 (lunit-test-result-failures-internal result)))
147 (setq failures (nconc failures (list (cons case failure))))
148 (lunit-test-result-set-failures-internal result failures))
149 (lunit-test-result-notify result 'lunit-test-listener-failure case failure))
151 (luna-define-method lunit-test-result-add-listener ((result lunit-test-result)
154 (lunit-test-result-listeners-internal result)))
155 (setq listeners (nconc listeners (list listener)))
156 (lunit-test-result-set-listeners-internal result listeners)))
161 (luna-define-class lunit-test-case (lunit-test))
163 (luna-define-generic lunit-test-case-run (case)
164 "Run the test case.")
166 (luna-define-generic lunit-test-case-setup (case)
167 "Setup the test case.")
169 (luna-define-generic lunit-test-case-teardown (case)
170 "Clear the test case.")
172 (defun lunit-make-test-case (class name)
173 "Return a newly allocated `lunit-test-case'.
174 CLASS is a symbol for class derived from `lunit-test-case'.
175 NAME is name of the method to be tested."
176 (luna-make-entity class :name name))
178 (luna-define-method lunit-test-number-of-tests ((case lunit-test-case))
181 (luna-define-method lunit-test-run ((case lunit-test-case) result)
182 (lunit-test-result-run result case))
184 (luna-define-method lunit-test-case-setup ((case lunit-test-case)))
185 (luna-define-method lunit-test-case-teardown ((case lunit-test-case)))
187 (luna-define-method lunit-test-case-run ((case lunit-test-case))
188 (lunit-test-case-setup case)
191 (lunit-test-name-internal case))
193 (luna-find-functions case name)))
195 (error "Method \"%S\" not found" name))
196 (condition-case error
197 (funcall (car functions) case)
199 (signal (car error)(cdr error)))
201 (signal 'lunit-error error))))
202 (lunit-test-case-teardown case)))
208 (luna-define-class lunit-test-suite (lunit-test)
211 (luna-define-internal-accessors 'lunit-test-suite))
213 (defun lunit-make-test-suite (&rest tests)
214 "Return a newly allocated `lunit-test-suite' instance.
215 TESTS holds a number of instances of `lunit-test'."
216 (luna-make-entity 'lunit-test-suite :tests tests))
218 (luna-define-method lunit-test-suite-add-test ((suite lunit-test-suite) test)
219 (let ((tests (lunit-test-suite-tests-internal suite)))
220 (lunit-test-suite-set-tests-internal suite (nconc tests (list test)))))
222 (luna-define-method lunit-test-number-of-tests ((suite lunit-test-suite))
223 (let ((tests (lunit-test-suite-tests-internal suite))
226 (setq accu (+ accu (lunit-test-number-of-tests test))))
229 (luna-define-method lunit-test-run ((suite lunit-test-suite) result)
230 (let ((tests (lunit-test-suite-tests-internal suite)))
232 (lunit-test-run test result))))
237 (defmacro lunit-assert (condition-expr)
238 "Verify that CONDITION-EXPR returns non-nil; signal an error if not."
239 (let ((condition (eval condition-expr)))
241 (signal 'lunit-failure (list ',condition-expr)))))
243 (luna-define-class lunit-test-printer (lunit-test-listener))
245 (luna-define-method lunit-test-listener-error ((printer lunit-test-printer)
247 (princ (format " error: %S" error)))
249 (luna-define-method lunit-test-listener-failure ((printer lunit-test-printer)
251 (princ (format " failure: %S" failure)))
253 (luna-define-method lunit-test-listener-start ((printer lunit-test-printer)
255 (princ (format "Running `%S#%S'..."
256 (luna-class-name case)
257 (lunit-test-name-internal case))))
259 (luna-define-method lunit-test-listener-end ((printer lunit-test-printer) case)
262 (defun lunit-make-test-suite-from-class (class)
263 "Make a test suite from all test methods of the CLASS."
267 (if (and (fboundp symbol)
268 (null (get symbol 'luna-method-qualifier)))
269 (push (lunit-make-test-case class symbol) tests)))
270 (luna-class-obarray (luna-find-class class)))
271 (apply #'lunit-make-test-suite tests)))
274 "Run TEST and display the result."
276 (luna-make-entity 'lunit-test-printer))
278 (lunit-make-test-result printer)))
279 (lunit-test-run test result)
281 (lunit-test-result-failures-internal result))
283 (lunit-test-result-errors-internal result)))
284 (princ (format "%d runs, %d failures, %d errors\n"
285 (lunit-test-number-of-tests test)
289 (defvar imenu-create-index-function)
290 (defun lunit-create-index-function ()
295 (goto-char (point-min))
296 (setq imenu-generic-expression
297 '((nil "^\\s-*(def\\(un\\|subst\\|macro\\)\\s-+\\([-A-Za-z0-9+*|:]+\\)" 2)))
298 (funcall imenu-create-index-function))
299 (setq imenu-create-index-function lisp-imenu-generic-expression))))
301 (defun lunit-generate-template (file)
302 (interactive "fGenerate lunit template for: ")
304 (set-buffer (find-file-noselect file))
306 (lunit-create-index-function)))
307 (with-output-to-temp-buffer "*Lunit template*"
309 (file-name-sans-extension
310 (file-name-nondirectory file)))
312 (concat "test-" feature)))
313 (set-buffer standard-output)
316 \(require '" feature ")
318 \(luna-define-class " class " (lunit-test-case))
321 (dolist (index index-alist)
323 \(luna-define-method " class "-" (car index) " ((case " class "))
330 ;;; lunit.el ends here