diff options
author | Stuart Halloway <stu@thinkrelevance.com> | 2010-12-17 10:58:39 -0500 |
---|---|---|
committer | Stuart Halloway <stu@thinkrelevance.com> | 2010-12-17 17:08:46 -0500 |
commit | 4e677b34b33219811d66aa9dcb335145439dbecc (patch) | |
tree | 932d2bdc81f2d16ba71ea9691a2cad250eb81551 | |
parent | 2853173b2632e87772f932f9aa80206004db5f83 (diff) |
coalesce test helpers into clojure.test-helper, so new contributors can find them easily. cleanup from #300
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
-rw-r--r-- | test/clojure/test_clojure/control.clj | 2 | ||||
-rw-r--r-- | test/clojure/test_clojure/def.clj | 2 | ||||
-rw-r--r-- | test/clojure/test_clojure/genclass.clj | 2 | ||||
-rw-r--r-- | test/clojure/test_clojure/helpers.clj | 86 | ||||
-rw-r--r-- | test/clojure/test_clojure/logic.clj | 2 | ||||
-rw-r--r-- | test/clojure/test_clojure/metadata.clj | 2 | ||||
-rw-r--r-- | test/clojure/test_clojure/multimethods.clj | 2 | ||||
-rw-r--r-- | test/clojure/test_clojure/protocols.clj | 2 | ||||
-rw-r--r-- | test/clojure/test_clojure/rt.clj | 2 | ||||
-rw-r--r-- | test/clojure/test_helper.clj | 78 |
10 files changed, 85 insertions, 95 deletions
diff --git a/test/clojure/test_clojure/control.clj b/test/clojure/test_clojure/control.clj index 746da981..868d6ed7 100644 --- a/test/clojure/test_clojure/control.clj +++ b/test/clojure/test_clojure/control.clj @@ -14,7 +14,7 @@ (ns clojure.test-clojure.control (:use clojure.test - [clojure.test-clojure.helpers :only (exception)])) + [clojure.test-helper :only (exception)])) ;; *** Helper functions *** diff --git a/test/clojure/test_clojure/def.clj b/test/clojure/test_clojure/def.clj index 3207ff43..a9b04498 100644 --- a/test/clojure/test_clojure/def.clj +++ b/test/clojure/test_clojure/def.clj @@ -7,7 +7,7 @@ ; You must not remove this notice, or any other, from this software. (ns clojure.test-clojure.def - (:use clojure.test clojure.test-clojure.helpers + (:use clojure.test clojure.test-helper clojure.test-clojure.protocols)) (deftest defn-error-messages diff --git a/test/clojure/test_clojure/genclass.clj b/test/clojure/test_clojure/genclass.clj index 20f4a692..8745d0db 100644 --- a/test/clojure/test_clojure/genclass.clj +++ b/test/clojure/test_clojure/genclass.clj @@ -9,7 +9,7 @@ (ns ^{:doc "Tests for clojure.core/gen-class" :author "Stuart Halloway, Daniel Solano Gómez"} clojure.test-clojure.genclass - (:use clojure.test clojure.test-clojure.helpers) + (:use clojure.test clojure.test-helper) (:import [clojure.test_clojure.genclass.examples ExampleClass ExampleAnnotationClass] [java.lang.annotation ElementType diff --git a/test/clojure/test_clojure/helpers.clj b/test/clojure/test_clojure/helpers.clj deleted file mode 100644 index 42fba780..00000000 --- a/test/clojure/test_clojure/helpers.clj +++ /dev/null @@ -1,86 +0,0 @@ -; The use and distribution terms for this software are covered by the -; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) -; which can be found in the file epl-v10.html at the root of this distribution. -; By using this software in any fashion, you are agreeing to be bound by -; the terms of this license. -; You must not remove this notice, or any other, from this software. - -; Author: Stuart Halloway - -(ns clojure.test-clojure.helpers - (:use clojure.test)) - -(defn temp-ns - "Create and return a temporary ns, using clojure.core + uses" - [& uses] - (binding [*ns* *ns*] - (in-ns (gensym)) - (apply clojure.core/use 'clojure.core uses) - *ns*)) - -(defmacro eval-in-temp-ns [& forms] - `(binding [*ns* *ns*] - (in-ns (gensym)) - (clojure.core/use 'clojure.core) - (eval - '(do ~@forms)))) - -(defn causes - [^Throwable throwable] - (loop [causes [] - t throwable] - (if t (recur (conj causes t) (.getCause t)) causes))) - -;; this is how I wish clojure.test/thrown? worked... -;; Does body throw expected exception, anywhere in the .getCause chain? -(defmethod assert-expr 'fails-with-cause? - [msg [_ exception-class msg-re & body :as form]] - `(try - ~@body - (report {:type :fail, :message ~msg, :expected '~form, :actual nil}) - (catch Throwable t# - (if (some (fn [cause#] - (and - (= ~exception-class (class cause#)) - (re-find ~msg-re (.getMessage cause#)))) - (causes t#)) - (report {:type :pass, :message ~msg, - :expected '~form, :actual t#}) - (report {:type :fail, :message ~msg, - :expected '~form, :actual t#}))))) - - -(defn get-field - "Access to private or protected field. field-name is a symbol or - keyword." - ([klass field-name] - (get-field klass field-name nil)) - ([klass field-name inst] - (-> klass (.getDeclaredField (name field-name)) - (doto (.setAccessible true)) - (.get inst)))) - -(defn set-var-roots - [maplike] - (doseq [[var val] maplike] - (alter-var-root var (fn [_] val)))) - -(defn with-var-roots* - "Temporarily set var roots, run block, then put original roots back." - [root-map f & args] - (let [originals (doall (map (fn [[var _]] [var @var]) root-map))] - (set-var-roots root-map) - (try - (apply f args) - (finally - (set-var-roots originals))))) - -(defmacro with-var-roots - [root-map & body] - `(with-var-roots* ~root-map (fn [] ~@body))) - -(defn exception - "Use this function to ensure that execution of a program doesn't - reach certain point." - [] - (throw (new Exception "Exception which should never occur"))) diff --git a/test/clojure/test_clojure/logic.clj b/test/clojure/test_clojure/logic.clj index 98f2447a..73db4d4b 100644 --- a/test/clojure/test_clojure/logic.clj +++ b/test/clojure/test_clojure/logic.clj @@ -13,7 +13,7 @@ (ns clojure.test-clojure.logic (:use clojure.test - [clojure.test-clojure.helpers :only (exception)])) + [clojure.test-helper :only (exception)])) ;; *** Tests *** diff --git a/test/clojure/test_clojure/metadata.clj b/test/clojure/test_clojure/metadata.clj index e19ef823..7719f061 100644 --- a/test/clojure/test_clojure/metadata.clj +++ b/test/clojure/test_clojure/metadata.clj @@ -10,7 +10,7 @@ (ns clojure.test-clojure.metadata (:use clojure.test - [clojure.test-clojure.helpers :only (eval-in-temp-ns)])) + [clojure.test-helper :only (eval-in-temp-ns)])) (def public-namespaces '[clojure.core diff --git a/test/clojure/test_clojure/multimethods.clj b/test/clojure/test_clojure/multimethods.clj index 77c5ea7e..01ff8a76 100644 --- a/test/clojure/test_clojure/multimethods.clj +++ b/test/clojure/test_clojure/multimethods.clj @@ -9,7 +9,7 @@ ; Author: Frantisek Sodomka, Robert Lachlan (ns clojure.test-clojure.multimethods - (:use clojure.test [clojure.test-clojure.helpers :only (with-var-roots)]) + (:use clojure.test [clojure.test-helper :only (with-var-roots)]) (:require [clojure.set :as set])) ; http://clojure.org/multimethods diff --git a/test/clojure/test_clojure/protocols.clj b/test/clojure/test_clojure/protocols.clj index 32cca655..b28f84ec 100644 --- a/test/clojure/test_clojure/protocols.clj +++ b/test/clojure/test_clojure/protocols.clj @@ -12,7 +12,7 @@ (:use clojure.test clojure.test-clojure.protocols.examples) (:require [clojure.test-clojure.protocols.more-examples :as other] [clojure.set :as set] - clojure.test-clojure.helpers) + clojure.test-helper) (:import [clojure.test_clojure.protocols.examples ExampleInterface])) ;; temporary hack until I decide how to cleanly reload protocol diff --git a/test/clojure/test_clojure/rt.clj b/test/clojure/test_clojure/rt.clj index f3a9b08b..478cacb7 100644 --- a/test/clojure/test_clojure/rt.clj +++ b/test/clojure/test_clojure/rt.clj @@ -9,7 +9,7 @@ ; Author: Stuart Halloway (ns clojure.test-clojure.rt - (:use clojure.test clojure.test-clojure.helpers)) + (:use clojure.test clojure.test-helper)) (defmacro with-err-print-writer "Evaluate with err pointing to a temporary PrintWriter, and diff --git a/test/clojure/test_helper.clj b/test/clojure/test_helper.clj index 7e7cfb55..b5d11f43 100644 --- a/test/clojure/test_helper.clj +++ b/test/clojure/test_helper.clj @@ -15,7 +15,83 @@ ;; tomfaulhaber (gmail) ;; Created 04 November 2010 -(ns clojure.test-helper) +(ns clojure.test-helper + (:use clojure.test)) (let [nl (System/getProperty "line.separator")] (defn platform-newlines [s] (.replace s "\n" nl))) + +(defn temp-ns + "Create and return a temporary ns, using clojure.core + uses" + [& uses] + (binding [*ns* *ns*] + (in-ns (gensym)) + (apply clojure.core/use 'clojure.core uses) + *ns*)) + +(defmacro eval-in-temp-ns [& forms] + `(binding [*ns* *ns*] + (in-ns (gensym)) + (clojure.core/use 'clojure.core) + (eval + '(do ~@forms)))) + +(defn causes + [^Throwable throwable] + (loop [causes [] + t throwable] + (if t (recur (conj causes t) (.getCause t)) causes))) + +;; this is how I wish clojure.test/thrown? worked... +;; Does body throw expected exception, anywhere in the .getCause chain? +(defmethod assert-expr 'fails-with-cause? + [msg [_ exception-class msg-re & body :as form]] + `(try + ~@body + (report {:type :fail, :message ~msg, :expected '~form, :actual nil}) + (catch Throwable t# + (if (some (fn [cause#] + (and + (= ~exception-class (class cause#)) + (re-find ~msg-re (.getMessage cause#)))) + (causes t#)) + (report {:type :pass, :message ~msg, + :expected '~form, :actual t#}) + (report {:type :fail, :message ~msg, + :expected '~form, :actual t#}))))) + + +(defn get-field + "Access to private or protected field. field-name is a symbol or + keyword." + ([klass field-name] + (get-field klass field-name nil)) + ([klass field-name inst] + (-> klass (.getDeclaredField (name field-name)) + (doto (.setAccessible true)) + (.get inst)))) + +(defn set-var-roots + [maplike] + (doseq [[var val] maplike] + (alter-var-root var (fn [_] val)))) + +(defn with-var-roots* + "Temporarily set var roots, run block, then put original roots back." + [root-map f & args] + (let [originals (doall (map (fn [[var _]] [var @var]) root-map))] + (set-var-roots root-map) + (try + (apply f args) + (finally + (set-var-roots originals))))) + +(defmacro with-var-roots + [root-map & body] + `(with-var-roots* ~root-map (fn [] ~@body))) + +(defn exception + "Use this function to ensure that execution of a program doesn't + reach certain point." + [] + (throw (new Exception "Exception which should never occur"))) |