[gambit-list] Unit testing fun

lowly coder lowlycoder at huoyanjinjing.com
Fri May 1 04:40:06 EDT 2009


Hi Gambiters,

  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)
        (tests '()))

    (define (add name test)
      (set! tests (cons (cons name test) tests)))

    (define (run-tests)
      (map (lambda (value)
             (let ((x (car value))
                   (y (cdr value)))
               (pp `(,x ,(y)))))
           (reverse tests)))

    (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))))))

    dispatch))
~/testing$ cat a.scm
(include "unit-test.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)"
(+ #t)
(- #t)
(* #f)



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
like:

gsi -e "(load \"a.scm\") (magic)"
or
gsi -e "(include \"a.scm\") (magic)"
or
gsi -e "(magic1 \"a.scm\") (magic2)"

and have the proper tests run.

How can I do this?

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20090501/2f0d13c6/attachment.htm>


More information about the Gambit-list mailing list