==========================
== Zhuo Hong Wei's Blog ==
==========================
Any and everything

Writing a Barebones Test Library

Here’s my latest experiment with Common Lisp, trying to write a small library for testing simple predicates. It runs a series of tests and report whether each test passed or failed, with a short summary at the end,

(defmacro make-test (name predicate)
    `(lambda () 
        (let ((p ,predicate))
            (if p 
                (format t "Test `~A` passed~%" ,name)
                (format t "Test `~A` failed~%" ,name))
        p)))

(defun run-tests (&rest tests)
       (let*  ((score (lambda (p) (if p 1 0)))
                (run-test (lambda (test) (funcall test)))
                (scores (map 'list score (map 'list run-test tests)))
                (num-tests (length scores))
                (num-passes (reduce #'+ scores)))
            (format t "~A out of ~A tests passed~%" num-passes num-tests)))

To use it I do the following,

(run-tests
    (make-test "1 == 1" (= 1 1))
    (make-test "2 == 1" (= 2 1))
    (make-test "NIL" nil)
    (make-test "t" t)
)

and finally the output looks something like,

Test `1 == 1` passed
Test `2 == 1` failed
Test `NIL` failed
Test `t` passed
2 out of 4 tests passed