Patch: java.lang.Error support for clojure.contrib.test_is

4 views
Skip to first unread message

Paul Drummond

unread,
Oct 30, 2008, 6:35:18 AM10/30/08
to Clojure
I have been using the test-is library from clojure.contrib very
effectively - thanks Stuart and contributors!

One thing I have noticed is that errors like StackOverflowError cause
"run-tests" to terminate prematurely.

I know java.lang.Errors should not be caught in general but I think
it's acceptable for a unit testing framework.

Here is a patch to add support for errors:

Index: test_is.clj
===================================================================
--- test_is.clj (revision 227)
+++ test_is.clj (working copy)
@@ -68,6 +68,7 @@
(defcounter *assertions* count-assertion)
(defcounter *failures* count-failure)
(defcounter *exceptions* count-exception)
+(defcounter *errors* count-error)

(defmacro failure [reason message]
`(throw (new java.lang.AssertionError
@@ -125,12 +126,14 @@
`(binding [*tests* (ref 0)
*assertions* (ref 0)
*failures* (ref 0)
- *exceptions* (ref 0)]
+ *exceptions* (ref 0)
+ *errors* (ref 0)]
~@body
{:tests @*tests*
:assertions @*assertions*
:failures @*failures*
- :exceptions @*exceptions*}))
+ :exceptions @*exceptions*
+ :errors @*errors*}))

(defn- run-test-fn
"Calls the function; reports errors/exceptions."
@@ -145,8 +148,14 @@
(catch java.lang.Exception e
(count-exception)
(. *test-out* (println (str "EXCEPTION in " name ":")))
- (.printStackTrace e *test-out*))))
+ (.printStackTrace e *test-out*))
+ (catch java.lang.Error e
+ (count-error)
+ (. *test-out* (print (str "ERROR in " name ": ")))
+ (. *test-out* (println e)))))
+

+
(defn- test-var
"Finds and calls the fn in a var's :test metadata."
[v]
@@ -202,7 +211,8 @@
(println (str "\nRan " (:tests r) " tests with "
(:assertions r) " assertions.\n"
(:failures r) " failures, "
- (:exceptions r) " exceptions."))) )
+ (:exceptions r) " exceptions, "
+ (:errors r) " errors."))))

(defn test-ns
"Runs tests on all interned symbols in the namespaces
@@ -213,6 +223,7 @@
:assertions => number of assertions checked
:failures => number of failed assertions
:exceptions => number of exceptions raised
+ :errors => number of errors raised

If no namespace is given, uses *ns*."
([] (test-ns *ns*))
Reply all
Reply to author
Forward
0 new messages