| 1 |
;;; Copyright (c) 2014 James M. Lawrence |
| 2 |
;;; |
| 3 |
;;; Permission is hereby granted, free of charge, to any person |
| 4 |
;;; obtaining a copy of this software and associated documentation |
| 5 |
;;; files (the "Software"), to deal in the Software without |
| 6 |
;;; restriction, including without limitation the rights to use, copy, |
| 7 |
;;; modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
;;; of the Software, and to permit persons to whom the Software is |
| 9 |
;;; furnished to do so, subject to the following conditions: |
| 10 |
;;; |
| 11 |
;;; The above copyright notice and this permission notice shall be |
| 12 |
;;; included in all copies or substantial portions of the Software. |
| 13 |
;;; |
| 14 |
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 |
;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 |
;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 |
;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 18 |
;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 |
;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
;;; DEALINGS IN THE SOFTWARE. |
| 22 |
|
| 23 |
(defpackage #:1am-ecl |
| 24 |
(:use #:cl) |
| 25 |
(:export #:test #:is #:signals #:run #:*tests*)) |
| 26 |
|
| 27 |
(in-package #:1am-ecl) |
| 28 |
|
| 29 |
(defvar *tests* nil "A list of tests; the default argument to `run'.") |
| 30 |
(defvar *pass-count* nil) |
| 31 |
(defvar *running* nil) |
| 32 |
(defvar *failed-random-state* nil) |
| 33 |
|
| 34 |
(defun %shuffle (vector) |
| 35 |
(loop for i downfrom (- (length vector) 1) to 1 |
| 36 |
do (rotatef (aref vector i) (aref vector (random (1+ i))))) |
| 37 |
vector) |
| 38 |
|
| 39 |
(defun shuffle (sequence) |
| 40 |
(%shuffle (map 'vector #'identity sequence))) |
| 41 |
|
| 42 |
(defun call-with-random-state (fn) |
| 43 |
(let ((*random-state* (or *failed-random-state* |
| 44 |
(load-time-value (make-random-state t))))) |
| 45 |
(setf *failed-random-state* (make-random-state nil)) |
| 46 |
(multiple-value-prog1 (funcall fn) |
| 47 |
(setf *failed-random-state* nil)))) |
| 48 |
|
| 49 |
(defun report (test-count pass-count) |
| 50 |
(format t "~&Success: ~s test~:p, ~s check~:p.~%" test-count pass-count)) |
| 51 |
|
| 52 |
(defun %run (fn test-count) |
| 53 |
(let ((*pass-count* 0)) |
| 54 |
(multiple-value-prog1 (call-with-random-state fn) |
| 55 |
(report test-count *pass-count*)))) |
| 56 |
|
| 57 |
(defun run (&optional (tests *tests*)) |
| 58 |
"Run each test in the sequence `tests'. Default is `*tests*'." |
| 59 |
(let ((*running* t)) |
| 60 |
(%run (lambda () (map nil #'funcall (shuffle tests))) |
| 61 |
(length tests))) |
| 62 |
(values)) |
| 63 |
|
| 64 |
(defun call-test (name fn) |
| 65 |
(format t "~&~s" name) |
| 66 |
(finish-output) |
| 67 |
(if *running* |
| 68 |
(funcall fn) |
| 69 |
(%run fn 1))) |
| 70 |
|
| 71 |
(defmacro test (name &body body) |
| 72 |
"Define a test function and add it to `*tests*'." |
| 73 |
`(progn |
| 74 |
(defun ,name () |
| 75 |
(call-test ',name (lambda () ,@body))) |
| 76 |
(pushnew ',name *tests*) |
| 77 |
',name)) |
| 78 |
|
| 79 |
(defun passed () |
| 80 |
(write-char #\.) |
| 81 |
;; Checks done outside a test run are not tallied. |
| 82 |
(when *pass-count* |
| 83 |
(incf *pass-count*)) |
| 84 |
(values)) |
| 85 |
|
| 86 |
(defmacro is (form) |
| 87 |
"Assert that `form' evaluates to non-nil." |
| 88 |
`(progn |
| 89 |
(assert ,form) |
| 90 |
(passed))) |
| 91 |
|
| 92 |
(defun %signals (expected fn) |
| 93 |
(flet ((handler (condition) |
| 94 |
(cond ((typep condition expected) |
| 95 |
(passed) |
| 96 |
(return-from %signals (values))) |
| 97 |
(t (error "Expected to signal ~s, but got ~s:~%~a" |
| 98 |
expected (type-of condition) condition))))) |
| 99 |
(handler-bind ((condition #'handler)) |
| 100 |
(funcall fn))) |
| 101 |
(error "Expected to signal ~s, but got nothing." expected)) |
| 102 |
|
| 103 |
(defmacro signals (condition &body body) |
| 104 |
"Assert that `body' signals a condition of type `condition'." |
| 105 |
`(%signals ',condition (lambda () ,@body))) |