I'm currently working in some racket programs, specifically in the PLAI's language of the Programming Languages: Application and Interpretation Book, and there is a function called test, i was wondering how to test the error throwing in racket? Does anyone knows how to do this?
Greetings
(I'm not a native english speaker, i hope this question can be understand)
To catch an exception, use the with-handlers form: (with-handlers ([predicate-expr handler-expr] ...) Each predicate-expr in a handler determines a kind of exception that is caught by the with-handlers form, and the value representing the exception is passed to the handler procedure produced by handler-expr.
To test that an expression reports an expected error, use test/exn. The test/exn form's section expression should produce a string, and test/exn checks that an error is reported where the string occurs in the error message. You can only test for errors that your program specifically reports using the error function.
There is test/exn
that you can use to test error messages, for example:
(test/exn (error "foo") "foo")
but note that the docs say that it can test only exceptions that were explicitly raised by your code. The idea is that your code should check for errors and raise them, otherwise you have a bug.
(For testing of "real" racket code, see rackunit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With