August 2014
Intermediate to advanced
120 pages
2h 45m
English
Another common use case for macros is to limit the reach of any errors we encounter during evaluation. Unit testing libraries typically have to do this in order to capture failures while continuing to run other tests. In clojure.test, for instance, the is macro uses an internal macro try-expr to catch exceptions and report them to the test-running infrastructure.
| context/try_expr.clj | |
| | (defmacro try-expr [msg form] |
| | `(try ~(assert-expr msg form) |
| | (catch Throwable t# |
| | (do-report {:type :error, :message ~msg, |
| | :expected '~form, :actual t#})))) |
| | (defmacro is |
| | ([form] `(is ~form nil)) |
| | ([form msg] `(try-expr ~msg ~form))) |
So when the form is actually evaluated within the assert-expr, if an unexpected exception propagates ...
Read now
Unlock full access