(module unit-test mzscheme
(require "sutil.scm")
(provide unit-tests)
(define (pad-left S n)
(let ((l (string-length S)))
(if (>= l n)
(substr S l)
(string-append
(substr (make-string n #\space) 0 (- n l))
S))))
(define (pad-right S n)
(let ((l (string-length S)))
(if (>= l n)
(substr S l)
(string-append
S
(substr (make-string n #\space) 0 (- n l))))))
(define (test number description tester)
(let ((N (pad-left (format "~a" number) 3))
(D (pad-right description 40)))
(display (format "~a - ~a:" N D))
(flush-output)
(let ((R (with-handlers ((exn:fail? (lambda (exn)
(lambda ()
(format "exception: ~a" (exn-message exn))))))
(tester))))
(display (format "~a~%" (if (eq? R #t) "OK"
(if (eq? R #f) "NOK"
(if (procedure? R) (R) R)))))
(flush-output)
number)))
(define N 0)
(define in-tests 0)
(define (tests L)
(if (= in-tests 0) (set! N 0))
(letrec ((f (lambda (L)
(if (null? L)
#t
(begin
(set! N (+ N 1))
(test N (caar L) (cadar L))
(f (cdr L)))))))
(set! in-tests (+ in-tests 1))
(f L)
(set! in-tests (- in-tests 1)))
#t)
(define-syntax utest
(syntax-rules ()
((_ (description tester))
(list description tester))))
(define-syntax unit-tests
(syntax-rules ()
((_ t1 ...)
(tests
(list
(utest t1)
...)))))
)