Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix CLJS exception handling during tests

  • Loading branch information...
commit 49d6c6fd004348e7c9affefe552b68607da6a22c 1 parent ed9d84d
@kumarshantanu authored
View
15 src/basil/util.clj
@@ -19,6 +19,21 @@
x))
+(def ^{:dynamic true
+ :doc
+ "Handler fn that takes 2 args `try-f` (fn that takes no arg) and `catch-f`
+ (fn that takes the exception object as only arg.) You must rebind `try-catch`
+ in CLJS as follows:
+
+ (binding [*try-catch* (fn [try-f catch-f]
+ (try (try-f)
+ (catch js/Error err#
+ (catch-f err#)))]
+ ...)"}
+ *try-catch*
+ (fn [try-f catch-f] (assert (not "Rebind *try-catch* as per documentation"))))
+
+
;; ===== Tuple utilities =====
View
21 test-cljs-macro/clojure/test_cljs.clj
@@ -1,6 +1,7 @@
;*CLJSBUILD-MACRO-FILE*;
-(ns clojure.test-cljs)
+(ns clojure.test-cljs
+ (:use [basil.util :only [*try-catch*]]))
(defmacro deftest
@@ -23,14 +24,20 @@
(defmacro thrown?
- [ex-class & body]
- '`(try ~@body
+ "Rebind *try-catch* before calling this, so that the effect is same as:
+ `(try ~@body
(catch ~'js/Error err#
- true)))
+ true))"
+ [ex-class & body]
+ `(try-catch #(do ~@body)
+ (constantly true)))
(defmacro thrown-with-msg?
- [ex-class msg-regex & body]
- '`(try ~@body
+ "Rebind *try-catch* before calling this, so that the effect is same as:
+ `(try ~@body
(catch ~'js/Error err#
- (boolean (re-find ~msg-regex (.message err#))))))
+ (boolean (re-find ~msg-regex (.message err#)))))"
+ [ex-class msg-regex & body]
+ `(try-catch #(do ~@body)
+ (fn [err#] (boolean (re-find ~msg-regex (.message err#))))))
View
9 test-cljs/basil/run_tests.cljs
@@ -1,9 +1,14 @@
(ns basil.run-tests
- (:require [basil.core-test :as core-test]))
+ (:require [basil.core-test :as core-test]
+ [basil.util :as util]))
(defn ^:export run
[]
(.log js/console "Running CLJS tests.")
- (binding [*print-fn* (fn [& args] (doseq [each args] (.log js/console each)))]
+ (binding [*print-fn* (fn [& args] (doseq [each args]
+ (.log js/console each)))
+ util/*try-catch* (fn [try-f catch-f] (try (try-f)
+ (catch js/Error err#
+ (catch-f err#))))]
(core-test/test-ns-hook)))
Please sign in to comment.
Something went wrong with that request. Please try again.