Skip to content

Instantly share code, notes, and snippets.

@metametadata
Last active June 8, 2017 02:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metametadata/69cae3488d891f5e23d9 to your computer and use it in GitHub Desktop.
Save metametadata/69cae3488d891f5e23d9 to your computer and use it in GitHub Desktop.
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