Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test the throwing of errors in racket?

Tags:

testing

racket

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)

like image 567
forellana Avatar asked Sep 18 '10 23:09

forellana


People also ask

How do you catch an error in a racket?

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.

How do you test a racket?

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.


1 Answers

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.

like image 162
Eli Barzilay Avatar answered Oct 06 '22 00:10

Eli Barzilay