aboutsummaryrefslogtreecommitdiff
path: root/src/clojure
diff options
context:
space:
mode:
authorStuart Sierra <mail@stuartsierra.com>2009-01-21 15:44:31 +0000
committerStuart Sierra <mail@stuartsierra.com>2009-01-21 15:44:31 +0000
commit46681192663f8962f8b3d5cb10fe8f89451625d9 (patch)
treeedbeb53c52bf676a6c11a989a16d552ebbb4d00e /src/clojure
parent1e1439ab3e476f1e12bdd12a14e8f45d70d4e68f (diff)
test_is.clj: new assertion "(is (thrown-with-msg? class re ...))"
Diffstat (limited to 'src/clojure')
-rw-r--r--src/clojure/contrib/test_is.clj18
-rw-r--r--src/clojure/contrib/test_is/tests.clj11
2 files changed, 27 insertions, 2 deletions
diff --git a/src/clojure/contrib/test_is.clj b/src/clojure/contrib/test_is.clj
index b729c884..2907b429 100644
--- a/src/clojure/contrib/test_is.clj
+++ b/src/clojure/contrib/test_is.clj
@@ -269,8 +269,22 @@
(report :pass ~msg '~form e#)
e#))))
-;; New assertions coming soon:
-;; * thrown-with-msg?
+(defmethod assert-expr 'thrown-with-msg? [msg form]
+ ;; (is (thrown-with-msg? c re expr))
+ ;; Asserts that evaluating expr throws an exception of class c.
+ ;; Also asserts that the message string of the exception matches
+ ;; (with re-matches) the regular expression re.
+ (let [klass (nth form 1)
+ re (nth form 2)
+ body (nthrest form 3)]
+ `(try ~@body
+ (report :fail ~msg '~form nil)
+ (catch ~klass e#
+ (let [m# (.getMessage e#)]
+ (if (re-matches ~re m#)
+ (report :pass ~msg '~form e#)
+ (report :fail ~msg '~form e#)))
+ e#))))
diff --git a/src/clojure/contrib/test_is/tests.clj b/src/clojure/contrib/test_is/tests.clj
index 2589cc77..6687ad12 100644
--- a/src/clojure/contrib/test_is/tests.clj
+++ b/src/clojure/contrib/test_is/tests.clj
@@ -41,9 +41,20 @@
(deftest can-test-thrown
(is (thrown? ArithmeticException (/ 1 0)) "Should pass")
+ ;; No exception is thrown:
(is (thrown? Exception (+ 1 1)) "Should fail")
+ ;; Wrong class of exception is thrown:
(is (thrown? ArithmeticException (throw (RuntimeException.))) "Should error"))
+(deftest can-test-thrown-with-msg
+ (is (thrown-with-msg? ArithmeticException #"Divide by zero" (/ 1 0)) "Should pass")
+ ;; Wrong message string:
+ (is (thrown-with-msg? ArithmeticException #"Something else" (/ 1 0)) "Should fail")
+ ;; No exception is thrown:
+ (is (thrown? Exception (+ 1 1)) "Should fail")
+ ;; Wrong class of exception is thrown:
+ (is (thrown-with-msg? IllegalArgumentException #"Divide by zero" (/ 1 0)) "Should error"))
+
(deftest can-catch-unexpected-exceptions
(is (= 1 (throw (Exception.))) "Should error"))