Added treat for byte-compiler.
[ETest.git] / etest.el
blob0d298e1b5a2e846179c2f11cc482e010026b6968
1 ;;; etest.el --- Run tests and get back a hierarchical set of results.
3 ;; Copyright (C) 2008 Philip Jackson
5 ;; Author: Philip Jackson <phil@shellarchive.co.uk>
7 ;; This file is not currently part of GNU Emacs.
9 ;; This program is free software; you can redistribute it and/or
10 ;; modify it under the terms of the GNU General Public License as
11 ;; published by the Free Software Foundation; either version 2, or (at
12 ;; your option) any later version.
14 ;; This program is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program ; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 ;;; Commentary:
26 ;; etest lets you define tests in a domain specific, hierarchical
27 ;; manner and gather results in a simple, structure of the same shape.
29 ;; To install you must put the location of etest into your
30 ;; `load-path', perhaps like this:
32 ;; (add-to-list 'load-path "~/.elisp/etest")
34 ;; Then actually load etest.el:
36 ;; (require 'etest)
37 ;;
38 ;; Valid examples of etest usage might be:
40 ;; Checking (+ 1 1) yeilds a non-nil result:
41 ;; (etest '(ok (+ 1 1)))
43 ;; You can add an extra argument to the end of any test and it will be
44 ;; used as the documentation string for the test:
45 ;;
46 ;; (etest '(ok (+ 1 1) "Check 1 + 1 yeilds non-nil"))
47 ;;
48 ;; If you omit this string then one will be generated for you.
49 ;;
50 ;; Checking (+ 1 1) yeilds 2:
51 ;; (etest '(eq (+ 1 1) 2))
53 ;; To combine these you might do this:
54 ;; (etest '("Check '+' function" (ok (+ 1 1)) (eq (+ 1 1) 2)))
56 ;; The string is just a header to split things up and hopefully make
57 ;; the output more readable. You can have header groups nest as deeply
58 ;; as you like and within each as many tests as you like.
60 ;; To define your own tests the `deftest' function should be used. For
61 ;; example the following can (and is) used to test etest itself:
62 ;;
63 ;; (deftest '(eres 1)
64 ;; (lambda (test)
65 ;; (etest-ok
66 ;; (plist-get (car (etest-run (list test))) :result))))
68 ;; Used like this:
69 ;;
70 ;; (etest '(eres (ok t)))
71 ;;
72 ;; I can see if etests 'built-ins' are working.
74 (require 'etest-result-mode)
75 (require 'etest-execute)
77 (eval-when-compile (require 'cl))
79 (defvar etest-results-function 'etest-rm-refresh-buffer
80 "Function used to display the results of a run.")
82 (defvar etest-candidates-plist
83 '(eq (etest-eq 2)
84 noerror (etest-noerror 1)
85 error (etest-error 1)
86 like (etest-like 2)
87 null (etest-null 1)
88 equal (etest-equal 2)
89 eql (etest-eql 2)
90 ok (etest-ok 1)
91 todo (etest-todo 1))
92 "Plist of test candidates where PROP is the name of the new
93 test . See `deftest' for details of how to modify this.")
95 (defun deftest (details func)
96 "Define a new test. DETAILS must be a list containing the name
97 of the test and the argcount. FUNC is the actual function that
98 will be run."
99 (destructuring-bind (name argcount) details
100 (plist-put etest-candidates-plist
101 name (list func argcount))))
103 (defun etest-todo (form)
104 "Return an etest result set with :result set to t. Set
105 :todo to t and comments to the result of FORM."
106 (let ((res (prin1-to-string
107 (condition-case err (car (etest-run (list form)))
108 (error
109 err)))))
110 (list :result t
111 :comments (concat "got: " (replace-regexp-in-string "\n" "" res))
112 :todo t)))
114 (defun etest-ok (test)
115 "Simply eval TEST and pass if the result is non-nil."
116 (let ((ret (eval test))
117 (result '()))
118 (setq result (plist-put result :result (not (null ret))))
119 (setq result (plist-put result :comments (format "got: '%S'" ret)))
120 result))
122 (defun etest-equality-test (func one two)
123 "Compare two items, ONE and TWO, using the function
124 FUNC. Returns a test result."
125 (let ((one (eval one))
126 (two (eval two))
127 (res (funcall func (eval one) (eval two)))
128 (result '()))
129 (setq result (plist-put result :result res))
130 (setq result (plist-put result :comments
131 (if res
132 (format "both: '%S'" one)
133 (format "one: '%S'\ntwo: '%S'" one two))))))
135 (defun etest-null (test)
136 "Allows the use of `null' in a test."
137 (let ((ret (eval test))
138 (result '()))
139 (setq result (plist-put result :result (null ret)))
140 (setq result (plist-put result :comments (format "got: '%S'" ret)))
141 result))
143 (defun etest-eq (one two)
144 "Allows the use of `eq' in a test."
145 (etest-equality-test 'eq one two))
147 (defun etest-equal (one two)
148 "Allows the use of `equal' in a test."
149 (etest-equality-test 'equal one two))
151 (defun etest-eql (one two)
152 (etest-equality-test 'eql one two))
154 (defun etest-noerror (form)
155 "Assert FORM evals without error."
156 (let ((result (etest-error form)))
157 (plist-put result :result (not (plist-get result :result)))))
159 (defun etest-error (form)
160 "Assert FORM evals with error."
161 (let* ((result '())
162 (val (condition-case err (eval form)
163 (error
164 (setq result (list :result t
165 :comments (format "got: '%S'" err)))))))
166 (if result
167 result
168 (list :result nil
169 :comments (format "got: '%S'" val)))))
171 (defun etest-resultp (result)
172 "Check that RESULT is a vaid test result."
173 (and (plist-member result :result)
174 (booleanp (plist-get result :result))
175 (plist-member result :comments)))
177 (defun etest-like (form re)
178 "Check string is like re"
179 (let* ((i 0)
180 (match nil)
181 (re (eval re))
182 (string (eval form))
183 (comments (format " needle: '%s'\n haystack: '%s'\n" re string))
184 (res (not (not (string-match re string))))
185 (result (list :result res)))
186 (while (setq match (match-string (setq i (1+ i)) string))
187 (setq comments (concat (or comments "")
188 (format "match %3d: '%s'\n" i match))))
189 (plist-put result :comments comments)
190 result))
192 ;;;###autoload
193 (defmacro etest (&rest form)
194 "Wrapper to `etest-run'. Will popup a window displaying the
195 results of the run."
196 `(let* ((meta-info (list :pass 0
197 :fail 0
198 :timestart (current-time)
199 :timefinish 0))
200 (results (etest-run ',form meta-info)))
201 (plist-put meta-info :timefinish (current-time))
202 (when (fboundp etest-results-function)
203 (funcall etest-results-function results meta-info))
204 results))
206 (defun etest-run (form &optional meta-info)
207 "This function does all of the work where actually running the
208 tests is concerned. Takes a valid etest form and will return a
209 similarly shaped set of results. "
210 (mapcar
211 '(lambda (test)
212 (let ((name (car test)))
213 (cond
214 ((stringp name)
215 (cons name (etest-run (cdr test) meta-info)))
216 ((symbolp name)
217 (let ((cand (car (plist-get etest-candidates-plist name)))
218 (args (cdr test))
219 (argcount (cadr (plist-get etest-candidates-plist name)))
220 (doc nil))
221 (unless cand
222 (error "'%s' is not a valid name type" name))
223 (if (< (length args) argcount)
224 (error "%s needs %d arguments" cand argcount)
225 (if (and (eq (length args) (1+ argcount))
226 (stringp (car (last args))))
227 (progn
228 (setq doc (car (last args)))
229 (setq args (delq doc args)))
230 (setq doc (prin1-to-string test))))
231 (let ((results (apply cand args)))
232 (plist-put results :doc doc)
233 (when meta-info
234 (etest-meta-info-update-pass-fail results meta-info))
235 results))))))
236 form))
238 (defun etest-meta-info-update-pass-fail (result meta-info)
239 "Update the pass/fail item in the meta-info plist based on the
240 resuls in RESULT."
241 (let ((type (if (plist-get result :result) :pass :fail)))
242 (plist-put meta-info type (1+ (plist-get meta-info type)))))
244 ;; This is defined so that etest can test itself
245 (defun etest-test-tests (test result)
246 "This test is used to test ETest itself. TEST is the test to be
247 run (in ETest syntax) and RESULT is a plist of items you would
248 like to compare. See the file etest.etest for example usage."
249 (let* ((testres (car (etest-run (list test))))
250 (my-res t)
251 (res-items '(:result :comments :doc :todo))
252 (my-comments (mapconcat
253 '(lambda (item)
254 (replace-regexp-in-string "\n" " "
255 (format "%9S %S"
256 item
257 (plist-get testres item))))
258 res-items
259 "\n")))
260 (dolist (sym res-items)
261 (let ((testval (plist-get testres sym))
262 (resultval (plist-get result sym)))
263 (when (and (plist-member result sym)
264 (not (equal testval resultval)))
265 (setq my-res nil))))
266 (list :result my-res :comments my-comments)))
268 ;; Make `etest-test-tests' available
269 (deftest '(eres 2) 'etest-test-tests)
271 (provide 'etest)