guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: testing exceptions + bug?


From: Dirk Herrmann
Subject: Re: testing exceptions + bug?
Date: Fri, 23 Feb 2001 20:07:07 +0100 (MET)

> It would be a good idea, however, to look through the test files and to
> extract common patterns into a library that helps to write test cases
> conveniently.  Your expect-exception macro is a candidate for such a
> library.  However, if put into a library, expect-exception should not make
> the test code part of the test name.  If the code is longer than '(lambda
> (x x x) 1)' this can become disturbing.

I have modified your checks for exceptions such that they might be part of
some library.  Your tests have shown that we should unify upper/lower
case usage for error messages :-)  I also extracted the function that
tests for documentation, which I have often used when writing tests.  
What do you think?  (Some of the exceptions should probably go into the
corresponding test files if they are not of general interest.)

I am not sure whether my suggested paradigm to use a pass-if* or
expect-fail* prefix makes sense.  On the one hand, it makes it easier to
identify single test cases and to grep for tests that are expected for
fail.  On the other hand, if you take a look at the way the tests for
bit-extract in bit-operations.test are realized, it seems sometimes to be
just overkill to use some pass-if* analoque for _every_ test case.  But,
understanding such test code can be a challenge for itself and make it
difficult to find out what actually went wrong if an error occurs.

Best regards,
Dirk Herrmann


;;;; Convenience functions for writing tests

;;;; Testing for exceptions
(define exception:bad-bindings
  (cons 'misc-error "bad bindings"))
(define exception:bad-body
  (cons 'misc-error "bad body"))
(define exception:bad-formals
  (cons 'misc-error "bad formals"))
(define exception:bad/missing-clauses
  (cons 'misc-error "bad or missing clauses"))
(define exception:bad-var
  (cons 'misc-error "bad variable"))
(define exception:eof
  (cons 'misc-error "end of file"))
(define exception:missing/extra-expr
  (cons 'misc-error "missing or extra expression"))
(define exception:unbound-var
  (cons 'unbound-variable "Unbound variable"))
(define exception:unexpected-rparen
  (cons 'misc-error "unexpected \")\""))
(define exception:wrong-num-args
  (cons 'wrong-number-of-args "Wrong number of arguments"))
(define exception:wrong-type-arg
  (cons 'wrong-type-arg "Wrong type argument"))

(defmacro run-test-exception (name exception expect-pass body . rest)
  `(run-test ,name ,expect-pass
     (lambda ()
       (catch (car ,exception)
         (lambda ()
           ,body ,@rest #f)
         (lambda args 
           (or (not (not (string-match (cdr ,exception) (caddr args))))
               (apply throw args)))))))

(defmacro pass-if-exception (name exception body . rest)
  `(run-test-exception ,name ,exception #t ,body ,@rest))

(defmacro expect-fail-exception (name exception expression)
  `(run-test-exception ,name ,exception #f ,body ,@rest))


;;;; Testing for documentation
(use-modules (ice-9 documentation))

(define (run-test-documented expect-pass object)
  (run-test "documented?" expect-pass
    (lambda () (object-documentation object))))

(define (pass-if-documented object)
  (run-test-documented #t object))

(define (expect-fail-documented object)
  (run-test-documented #t object))




reply via email to

[Prev in Thread] Current Thread [Next in Thread]