Skip to content

Commit 897e693

Browse files
committed
Merge pull request #247 from justinj/improve-assert-throws
Improve assert-throws? macro
2 parents fef4f65 + 9b8425a commit 897e693

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

pixie/test.pxi

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,25 @@
6161
yr# ~y]
6262
(assert (= xr# yr#) (str (show '~x xr#) " != " (show '~y yr#)))))
6363

64-
(defmacro assert-throws? [klass msg body]
65-
`(try
66-
~body
67-
(assert false (str "Expected a " ~klass " exception: " ~msg))
68-
(catch e#
69-
(assert (= (type e#) ~klass)
70-
(str "Expected exception of class " ~klass " but got " (type e#)))
71-
(assert (= (ex-msg e#) ~msg)
72-
(str "Expected message: " ~msg " but got " (ex-msg e#))))))
64+
(defmacro assert-throws?
65+
([body]
66+
`(let [exn# (try (do ~body nil) (catch e# e#))]
67+
(assert (not (nil? exn#))
68+
(str "Expected " (pr-str (quote ~body)) " to throw an exception"))
69+
exn#))
70+
([klass body]
71+
`(let [exn# (assert-throws? ~body)]
72+
(assert (= (type exn#) ~klass)
73+
(str "Expected " (pr-str (quote ~body))
74+
" to throw exception of class " (pr-str ~klass)
75+
" but got " (pr-str (type exn#))))
76+
exn#))
77+
([klass msg body]
78+
`(let [exn# (assert-throws? ~klass ~body)]
79+
(assert (= (ex-msg exn#) ~msg)
80+
(str "Expected " (pr-str (quote ~body))
81+
" to throw exception with message " (pr-str ~msg)
82+
" but got " (pr-str (ex-msg exn#)))))))
7383

7484
(defmacro assert [x]
7585
`(let [x# ~x]

0 commit comments

Comments
 (0)