[gambit-list] Unit testing fun
lowlycoder at huoyanjinjing.com
Fri May 1 04:40:06 EDT 2009
I've been thinking for a long time about the simplest unit testing
framework I can think of. I came up with the following:
~/testing$ cat unit-test.scm
(define (unit-test-create name)
(let ((unit-test-name name)
(define (add name test)
(set! tests (cons (cons name test) tests)))
(map (lambda (value)
(let ((x (car value))
(y (cdr value)))
(pp `(,x ,(y)))))
(define (dispatch . cmds)
(cond ((eq? (car cmds) 'run) (run-tests))
((eq? (car cmds) 'add) (add (cadr cmds) (caddr cmds)))
((#t (error ,(command ,cmds not supported))))))
~/testing$ cat a.scm
(define unit-tests (unit-test-create 'tests-a))
(unit-tests 'add '+ (lambda () (eq? (+ 1 1) 2)))
(unit-tests 'add '- (lambda () (eq? (- 1 1) 0)))
(unit-tests 'add '* (lambda () (eq? (* 1 1) 2)))
~/testing$ gsi -e "(load \"a.scm\") (unit-tests 'run)"
I'm almost happy with this, except that if I have a.scm and b.scm, I have to
have different names in them (I can't have them both use 'unit-tests').
Maybe I can call them 'unit-tests-a' or 'unit-tests-b' .. but then I have to
deal with folder path.
Maybe I can call them 'unit-tests-testing-a' and 'unit-tests-testing-b' but
this becomes a pain when I move files around.
What I want would be somehow, to have a variable created automatically
that's _local_ to the particular file, so that I can still run something
gsi -e "(load \"a.scm\") (magic)"
gsi -e "(include \"a.scm\") (magic)"
gsi -e "(magic1 \"a.scm\") (magic2)"
and have the proper tests run.
How can I do this?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Gambit-list