etest-result-mode.el: Added customisable faces.
[ETest.git] / etest.el
blobda092bc1114ebf7161532ac576f6bd9abc92790e
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)
76 (defvar etest-results-function 'etest-rm-refresh-buffer
77 "Function used to display the results of a run.")
79 (defvar etest-candidates-plist
80 '(eq (etest-eq 2)
81 noerror (etest-noerror 1)
82 error (etest-error 1)
83 like (etest-like 2)
84 null (etest-null 1)
85 equal (etest-equal 2)
86 eql (etest-eql 2)
87 ok (etest-ok 1))
88 "Plist of test candidates where PROP is the name of the new
89 test . See `deftest' for details of how to modify this.")
91 (defun deftest (details func)
92 "Define a new test. DETAILS must be a list containing the name
93 of the test and the argcount. FUNC is the actual function that
94 will be run."
95 (destructuring-bind (name argcount) details
96 (plist-put etest-candidates-plist
97 name (list func argcount))))
99 (defun etest-ok (test)
100 "Simply eval TEST and pass if the result is non-nil."
101 (let ((ret (eval test))
102 (result '()))
103 (setq result (plist-put result :result (not (null ret))))
104 (setq result (plist-put result :comments (format "got: '%S'" ret)))
105 result))
107 (defun etest-equality-test (func one two)
108 "Compare two items, ONE and TWO, using the function
109 FUNC. Returns a test result."
110 (let ((one (eval one))
111 (two (eval two))
112 (res (funcall func (eval one) (eval two)))
113 (result '()))
114 (setq result (plist-put result :result res))
115 (setq result (plist-put result :comments
116 (if res
117 (format "both: '%S'" one)
118 (format "one: '%S'\ntwo: '%S'" one two))))))
120 (defun etest-null (test)
121 "Allows the use of `null' in a test."
122 (etest-ok `(null ,test)))
124 (defun etest-eq (one two)
125 "Allows the use of `eq' in a test."
126 (etest-equality-test 'eq one two))
128 (defun etest-equal (one two)
129 "Allows the use of `equal' in a test."
130 (etest-equality-test 'equal one two))
132 (defun etest-eql (one two)
133 (etest-equality-test 'eql one two))
135 (defun etest-noerror (form)
136 "Assert FORM evals without error."
137 (let ((result (etest-error form)))
138 (plist-put result :result (not (plist-get result :result)))))
140 (defun etest-error (form)
141 "Assert FORM evals with error."
142 (let* ((result '())
143 (val (condition-case err (eval form)
144 (error
145 (setq result (list :result t
146 :comments (format "got: '%S'" err)))))))
147 (if result
148 result
149 (list :result nil
150 :comments (format "got: '%S'" val)))))
152 (defun etest-resultp (result)
153 "Check that RESULT is a vaid test result."
154 (and (plist-member result :result)
155 (booleanp (plist-get result :result))
156 (plist-member result :comments)))
158 (defun etest-like (form re)
159 "Check string is like re"
160 (let* ((i 0)
161 (match nil)
162 (re (eval re))
163 (string (eval form))
164 (comments (concat "searching: '" string "'\n"))
165 (res (not (not (string-match re string))))
166 (result (list :result res)))
167 (while (setq match (match-string (setq i (1+ i)) string))
168 (setq comments (concat (or comments "")
169 (format "match %3d: '%s'\n" i match))))
170 (plist-put result :comments comments)
171 result))
173 ;;;###autoload
174 (defmacro etest (&rest form)
175 "Wrapper to `etest-run'. Will popup a window displaying the
176 results of the run."
177 `(let ((results (etest-run ',form)))
178 (when (fboundp etest-results-function)
179 (funcall etest-results-function results))
180 results))
182 (defun etest-run (form)
183 "This function does all of the work where actually running the
184 tests is concerned. Takes a valid etest form and will return a
185 similarly shaped set of results. "
186 (mapcar
187 '(lambda (test)
188 (let ((name (car test)))
189 (cond
190 ((stringp name)
191 (cons name (etest-run (cdr test))))
192 ((symbolp name)
193 (let ((cand (car (plist-get etest-candidates-plist name)))
194 (args (cdr test))
195 (argcount (cadr (plist-get etest-candidates-plist name)))
196 (doc nil))
197 (unless cand
198 (error "'%s' is not a valid name type" name))
199 (if (< (length args) argcount)
200 (error "%s needs %d arguments" cand argcount)
201 (if (and (eq (length args) (1+ argcount))
202 (stringp (car (last args))))
203 (progn
204 (setq doc (car (last args)))
205 (setq args (delq doc args)))
206 (setq doc (prin1-to-string test))))
207 (plist-put (apply cand args) :doc doc))))))
208 form))
210 ;; This is defined so that etest can test itself
211 (defun etest-test-tests (test result)
212 "This test is used to test ETest itself. TEST is the test to be
213 run (in ETest syntax) and RESULT is a plist of items you would
214 like to compare. See the file etest.etest for example usage."
215 (let ((testres (car (etest-run (list test))))
216 (my-res t)
217 (my-comments ""))
218 (when (null (plist-get testres :result))
219 (setq my-comments "note: test result was nil\n"))
220 (dolist (sym '(:result :comments :doc))
221 (let ((testval (plist-get testres sym))
222 (resultval (plist-get result sym)))
223 (when (and (plist-member result sym)
224 (not (equal testval resultval)))
225 (setq my-res nil)
226 (setq my-comments
227 (concat my-comments
228 (format "got: %S from '%S'\n"
229 resultval sym))))))
230 (list :result my-res :comments my-comments)))
232 ;; Make `etest-test-tests' available
233 (deftest '(eres 2) 'etest-test-tests)
235 (provide 'etest)