Debian Codegraph @

src/tests/2am.lisp

raw
1 ;;; Copyright (c) 2014 James M. Lawrence
2 ;;; Copyright (c) 2016 Daniel KochmaƄski
3 ;;;
4 ;;; Permission is hereby granted, free of charge, to any person
5 ;;; obtaining a copy of this software and associated documentation
6 ;;; files (the "Software"), to deal in the Software without
7 ;;; restriction, including without limitation the rights to use, copy,
8 ;;; modify, merge, publish, distribute, sublicense, and/or sell copies
9 ;;; of the Software, and to permit persons to whom the Software is
10 ;;; furnished to do so, subject to the following conditions:
11 ;;;
12 ;;; The above copyright notice and this permission notice shall be
13 ;;; included in all copies or substantial portions of the Software.
14 ;;;
15 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 ;;; DEALINGS IN THE SOFTWARE.
23
24 #| to avoid conflict with the library name package 2am-ecl |#
25 (defpackage #:2am-ecl
26 (:use #:cl)
27 (:export #:deftest #:test #:test-with-timeout #:is #:signals #:finishes
28 #:run #:suite #:*test-name* #:failed #:test-failure))
29
30 (in-package #:2am-ecl)
31
32 (defvar *tests* nil "A name of the default tests suite.")
33 (defvar *suites* (make-hash-table) "A collection of test suites.")
34 (defvar *hierarchy* (make-hash-table) "A hierarchy of test suites.")
35 (defvar *stats* nil "Collection of test statistics.")
36 (defvar *running* nil)
37 (defvar *test-name* nil)
38 (defvar *last-fail* nil)
39 (defvar *default-timeout* 60.0 "Default timeout in seconds.")
40
41 (defstruct test-stats
42 (failures (make-hash-table))
43 (crashes 0)
44 (test-count 0)
45 (pass-count 0)
46 (fail-count 0))
47
48 (define-condition test-failure (simple-condition)
49 ((test-name :initarg :name
50 :accessor test-name)))
51
52 (defun suite (&optional (name *tests* name-p) (sub nil sub-p))
53 "Sets the current suite to the `name'."
54 (assert (symbolp name))
55 (assert (typep sub 'sequence))
56 (when name-p
57 (setf *tests* name))
58 (when sub-p
59 (setf (gethash *tests* *hierarchy*) sub))
60 *tests*)
61
62 (defsetf suite (name) (tests suites)
63 "Resets the suite to contain the provided tests and suites"
64 `(progn
65 (assert (typep ,tests 'sequence))
66 (assert (typep ,suites 'sequence))
67 (setf (gethash ,name *suites*) ,tests
68 (gethash ,name *hierarchy*) ,suites)
69 tests))
70
71 (defun %shuffle (vector)
72 (loop for i downfrom (- (length vector) 1) to 1
73 do (rotatef (aref vector i) (aref vector (random (1+ i)))))
74 vector)
75
76 (defun shuffle (sequence)
77 (%shuffle (map 'vector #'identity sequence)))
78
79 (defun report (stats &aux
80 (test-count (test-stats-test-count stats))
81 (pass-count (test-stats-pass-count stats))
82 (fail-count (test-stats-fail-count stats))
83 (crashes (test-stats-crashes stats))
84 (failures (test-stats-failures stats)))
85 (let ((num-check (+ pass-count fail-count)))
86 (if *running*
87 (format t "~&Did ~s test~:p (~s crashed), ~s check~:p.~%" test-count crashes num-check)
88 (format t "~&Test ~s: ~s check~:p.~%" *test-name* num-check))
89 (unless (zerop num-check)
90 (let ((passed% (round (* 100 (/ pass-count num-check))))
91 (failed% (round (* 100 (/ fail-count num-check)))))
92 (format t " Pass: ~s (~2D%)~%" pass-count passed%)
93 (format t " Fail: ~s (~2D%)~%" fail-count failed%))))
94 (unless (= fail-count crashes 0)
95 (format t "~%Failure details:~%")
96 (format t "--------------------------------~%")
97 (maphash (lambda (test fails)
98 (format t " ~A:~%" test)
99 (dolist (fail (reverse fails))
100 (if (typep fail 'test-failure)
101 (format t " FAIL: ")
102 (format t " CRASH [~A]: " (type-of fail)))
103 (format t "~A~%" fail))
104 (format t "~&--------------------------------~%"))
105 failures)))
106
107 (defun %run (fn)
108 (let ((*stats* (make-test-stats)))
109 (multiple-value-prog1 (funcall fn)
110 (report *stats*))))
111
112 (defun %run-suite (name)
113 (let ((visited nil)
114 (functions nil))
115 (labels ((traverse (name)
116 (unless (member name visited)
117 (push name visited)
118 (push (lambda ()
119 (format t "~&--- Running test suite ~s~%" name)
120 (map nil #'funcall (shuffle
121 (gethash name *suites*))))
122 functions)
123 (map nil #'traverse (shuffle
124 (gethash name *hierarchy*))))))
125 (traverse name))
126 (nreverse functions)))
127
128 (defun run (&optional (tests (gethash nil *suites*)))
129 "Run each test in the sequence `tests'. Default is `*tests*'."
130 (let ((*running* t))
131 (etypecase tests
132 (symbol
133 (%run (lambda ()
134 (map nil #'funcall (%run-suite tests)))))
135 (list
136 (%run (lambda ()
137 (map nil #'funcall (shuffle tests)))))))
138 (values))
139
140 (defun call-test (name fn)
141 (let ((*test-name* name))
142 (format t "~&Running test ~s " *test-name*)
143 (finish-output)
144 (if *running*
145 (handler-case
146 (progn (incf (test-stats-test-count *stats*))
147 (funcall fn))
148 (serious-condition (c)
149 (write-char #\X)
150 (incf (test-stats-crashes *stats*))
151 (push c (gethash *test-name* (test-stats-failures *stats*)))))
152 (%run fn))
153 (values)))
154
155 (defmacro deftest (name () &body body)
156 `(progn
157 (defun ,name ()
158 (call-test ',name (lambda () ,@body)))
159 (pushnew ',name (gethash *tests* *suites*))
160 ',name))
161
162 (defmacro test (name &body body)
163 `(deftest ,name () ,@body))
164
165 (defun kill-processes (process-list &optional original)
166 "Kills a list of processes, which may be the difference between two lists."
167 (let ((process-list (set-difference process-list original)))
168 (when (member mp:*current-process* process-list)
169 (error "Found myself in the kill list"))
170 (mapc #'mp:process-kill process-list)
171 process-list))
172
173 #+threads
174 (defun call-test-with-timeout (name timeout fn)
175 (let* ((all-processes (mp:all-processes))
176 (finished nil)
177 (runner (mp:process-run-function
178 "runner"
179 #'(lambda (stats running)
180 (let ((*stats* stats)
181 (*running* running))
182 (call-test name fn)
183 (setf finished t)))
184 *stats* *running*)))
185 (loop with *test-name* = name
186 with timestep = 0.2
187 for time from 0.0 upto timeout by timestep
188 do (if finished
189 (return)
190 (sleep timestep))
191 finally (mp:process-kill runner)
192 (failed (make-condition 'test-failure
193 :name name
194 :format-control "Timeout after ~A seconds"
195 :format-arguments (list timeout)))
196 (return-from call-test-with-timeout))
197 (mp:process-join runner)
198 (let ((leftovers (kill-processes (mp:all-processes) all-processes)))
199 (when leftovers
200 (format t "~%;;; Stray processes: ~A~%" leftovers)))))
201
202 #+threads
203 (defmacro test-with-timeout (name-and-timeout &body body)
204 (let (name timeout)
205 (if (listp name-and-timeout)
206 (setf name (first name-and-timeout)
207 timeout (second name-and-timeout))
208 (setf name name-and-timeout
209 timeout '*default-timeout*))
210 `(progn
211 (defun ,name ()
212 (call-test-with-timeout ',name ,timeout (lambda () ,@body)))
213 (pushnew ',name (gethash *tests* *suites*))
214 ',name)))
215
216 (defun passed ()
217 (write-char #\.)
218 (when *stats*
219 (incf (test-stats-pass-count *stats*)))
220 T)
221
222 (defun failed (c)
223 (write-char #\f)
224 (when *stats*
225 (incf (test-stats-fail-count *stats*))
226 (push c (gethash *test-name* (test-stats-failures *stats*))))
227 (setf *last-fail* c)
228 nil)
229
230 (defmacro is (form &rest args
231 &aux
232 (fmt-ctrl (format nil "~s~@[~%~A~]" form (car args)))
233 (fmt-args (cdr args)))
234 "Assert that `form' evaluates to non-nil."
235 `(if ,form
236 (passed)
237 (failed (make-condition 'test-failure
238 :name *test-name*
239 :format-control ,fmt-ctrl
240 :format-arguments (list ,@fmt-args)))))
241
242 (defun %signals (expected fn &rest args)
243 (flet ((handler (condition)
244 (cond ((typep condition expected)
245 (return-from %signals (passed)))
246 ((typep condition 'serious-condition)
247 (return-from %signals
248 (let ((fmt-ctrl
249 (if args
250 (car args)
251 "Expected to signal ~s, but got ~s:~%~a"))
252 (fmt-args
253 (if args
254 (cdr args)
255 (list expected (type-of condition) condition))))
256 (failed (make-condition 'test-failure
257 :name *test-name*
258 :format-control fmt-ctrl
259 :format-arguments fmt-args)))))
260 (t #|ignore non-serious unexpected conditions|#))))
261 (handler-bind ((condition #'handler))
262 (funcall fn)))
263 (let ((fmt-ctrl (if args (car args) "Expected to signal ~s, but got nothing"))
264 (fmt-args (if args (cdr args) `(,expected))))
265 (failed (make-condition 'test-failure
266 :name *test-name*
267 :format-control fmt-ctrl
268 :format-arguments fmt-args))))
269
270 (defmacro signals (condition form &rest args)
271 "Assert that `body' signals a condition of type `condition'."
272 `(%signals ',condition (lambda () ,form) ,@args))
273
274 (defmacro finishes (form &rest args)
275 (if args
276 `(handler-case (multiple-value-prog1 ,form (passed))
277 (serious-condition (c)
278 (failed (make-condition 'test-failure
279 :name *test-name*
280 :format-control ,(car args)
281 :format-arguments (list ,@(cdr args))))))
282 `(handler-case (multiple-value-prog1 ,form (passed))
283 (serious-condition (c)
284 (failed (make-condition 'test-failure
285 :name *test-name*
286 :format-control "Expected to finish, but got ~s:~%~a"
287 :format-arguments (list (type-of c) c)))))))