Skip to content

Instantly share code, notes, and snippets.

@metametadata
Last active Jun 8, 2017
Embed
What would you like to do?
Portable Clojure/ClojureScript wrappers for (is (thrown-with-msg? ...)) test assertions
#?(: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