Last active
June 8, 2017 02:50
-
-
Save metametadata/69cae3488d891f5e23d9 to your computer and use it in GitHub Desktop.
Portable Clojure/ClojureScript wrappers for (is (thrown-with-msg? ...)) test assertions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#?(:clj | |
(defn- cljs-env? | |
"Take the &env from a macro, and tell whether we are expanding into cljs. | |
Source: http://v.gd/rmKNdf" | |
[env] | |
(boolean (:ns env)))) | |
#?(:clj | |
(defmacro is-exception-thrown | |
"(is (thrown-with-msg? ...)) for specified exceptions in Clojure/ClojureScript." | |
[clj-exc-class cljs-exc-class re expr] | |
(let [is (if (cljs-env? &env) 'cljs.test/is | |
'clojure.test/is) | |
exc-class (if (cljs-env? &env) cljs-exc-class | |
clj-exc-class)] | |
`(~is (~'thrown-with-msg? ~exc-class ~re ~expr))))) | |
#?(:clj | |
(defmacro is-error-thrown | |
"(is (thrown-with-msg? ...)) for general exceptions in Clojure/ClojureScript." | |
[re expr] | |
`(is-exception-thrown java.lang.Exception js/Error ~re ~expr))) | |
#?(:clj | |
(defmacro is-assertion-error-thrown | |
"(is (thrown-with-msg? ...)) for assert exceptions in Clojure/ClojureScript." | |
[re expr] | |
`(is-exception-thrown java.lang.AssertionError js/Error ~re ~expr))) | |
;; example | |
(comment | |
(is-error-thrown | |
#"expected exception" | |
(throw (ex-info "expected exception" {}))) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment