Commits

Meikel Brandmeyer committed 4315061

Added runs? test

Made the test driver to return either the test result or the special
symbol de.kotka.tap/test-failed. So the user can check, whether the
test worked out or not.

Comments (0)

Files changed (2)

de/kotka/tap/tap.clj

   (when-not (nil? desc)
     (print " - ")
     (print desc))
-  (newline))
+  (newline)
+  (flush))
 
 (let [current-test (ref 1)]
   (defn test-driver [actual qactual exp desc pred diagnose]
-    (let [r (ref nil)]
-      (if (= *mode* :skip)
-        (print-result @current-test *mode* true *skip-reason*)
-        (try
-          (let [e (exp)
-                a (actual)]
-            (let [rr (pred e a)] (dosync (ref-set r rr)))
-            (print-result @current-test *mode* @r desc)
-            (when-not @r
-              (let [es (pr-str e)
-                    as (pr-str qactual)
-                    rs (pr-str a)]
-                (diagnose es as rs))))
-          (catch Exception e
-            (print-result @current-test *mode* false desc)
-            (diag (str "Exception was thrown: " e)))))
-      (flush)
-      (dosync (commute current-test inc))
-      @r)))
+    (if (= *mode* :skip)
+      (print-result @current-test *mode* true *skip-reason*)
+      (try
+        (let [e (exp)
+              a (actual)
+              r (pred e a)]
+          (print-result @current-test *mode* r desc)
+          (when-not r
+            (let [es (pr-str e)
+                  as (pr-str qactual)
+                  rs (pr-str a)]
+              (diagnose es as rs)))
+          a)
+        (catch Exception e
+          (print-result @current-test *mode* false desc)
+          (diag (str "Exception was thrown: " e))
+          `test-failed)
+        (finally
+          (dosync (commute current-test inc)))))))
 
 (defmacro ok? [t & desc]
   `(test-driver (fn [] ~t)
                 (fn [e# a# r#]
                   (diag (.concat "Expected: " a#))
                   (diag (.concat "to throw: " e#)))))
+
+(defmacro runs? [body & desc]
+  `(test-driver (fn [] ~body)
+                (quote ~body)
+                (fn [] nil)
+                ~(first desc)
+                (fn [e# a#] true)
+                (fn [e# a# r#]
+                  (diag (.concat "Expected " a#
+                                 " to run through w/o exception.")))))
+#! /bin/sh
+#-
+# Copyright 2008 (c) Meikel Brandmeyer.
+# All rights reserved.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+. t/testlib.sh
+
+echo 1..4
+
+try '(is? 1 (runs? 1))' 'ok 1
+ok 2' 'success w/o message'
+try '(is? 1 (runs? 1 "success"))' 'ok 1 - success
+ok 2' 'success w/ message'
+
+try '(is? (symbol "de.kotka.tap" "test-failed") (runs? (do (throw (new Exception "x")) "reached")))' \
+'not ok 1
+# Exception was thrown: java.lang.Exception: x
+ok 2' 'failure w/o message'
+try '(is? (symbol "de.kotka.tap" "test-failed") (runs? (do (throw (new Exception "x")) "reached") "failure"))' \
+'not ok 1 - failure
+# Exception was thrown: java.lang.Exception: x
+ok 2' 'failure w/ message'
+
+cleanup
+
+# vim:ft=sh: