diff options
author | Stuart Halloway <stu@thinkrelevance.com> | 2009-12-30 13:09:51 -0500 |
---|---|---|
committer | Stuart Halloway <stu@thinkrelevance.com> | 2009-12-30 13:09:51 -0500 |
commit | f24f6412a1cee7015285856e5e4fc6ddc3974439 (patch) | |
tree | 6c54832dc5f72866f58b8010ef0c5cdbf451c11a | |
parent | 3764e2e6a5b135b44c0b5971ad16c4eb230b2eba (diff) |
clean up temporary namespaces, fixes #54
-rw-r--r-- | src/clojure/contrib/test_contrib.clj | 2 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/with_ns_test.clj | 19 | ||||
-rw-r--r-- | src/clojure/contrib/with_ns.clj | 13 |
3 files changed, 27 insertions, 7 deletions
diff --git a/src/clojure/contrib/test_contrib.clj b/src/clojure/contrib/test_contrib.clj index 724445e0..69b624a7 100644 --- a/src/clojure/contrib/test_contrib.clj +++ b/src/clojure/contrib/test_contrib.clj @@ -21,7 +21,7 @@ :pprint.cl-format :str-utils :shell-out :test-graph :test-dataflow :test-java-utils :test-lazy-seqs :test-trace :test-jmx :java-utils :mock-test :mock-test.test-adapter-test - :seq-utils-test]) + :seq-utils-test :with-ns-test]) (def test-namespaces (concat diff --git a/src/clojure/contrib/test_contrib/with_ns_test.clj b/src/clojure/contrib/test_contrib/with_ns_test.clj new file mode 100644 index 00000000..09137bc0 --- /dev/null +++ b/src/clojure/contrib/test_contrib/with_ns_test.clj @@ -0,0 +1,19 @@ +(ns clojure.contrib.test-contrib.with-ns-test + (:use clojure.test + clojure.contrib.with-ns + [clojure.contrib.seq-utils :only (includes?)])) + +(deftest test-namespace-gets-removed + (let [all-ns-names (fn [] (map #(.name %) (all-ns)))] + (testing "unexceptional return" + (let [ns-name (with-temp-ns (ns-name *ns*))] + (is (not (includes? (all-ns-names) ns-name))))) + (testing "when an exception is thrown" + (let [ns-name-str + (try + (with-temp-ns + (throw (RuntimeException. (str (ns-name *ns*))))) + (catch clojure.lang.Compiler$CompilerException e + (-> e .getCause .getMessage)))] + (is (re-find #"^sym.*$" ns-name-str)) + (is (not (includes? (all-ns-names) (symbol ns-name-str)))))))) diff --git a/src/clojure/contrib/with_ns.clj b/src/clojure/contrib/with_ns.clj index be33dd7c..d874a4a5 100644 --- a/src/clojure/contrib/with_ns.clj +++ b/src/clojure/contrib/with_ns.clj @@ -29,9 +29,10 @@ "Evaluates body in an anonymous namespace, which is then immediately removed. The temporary namespace will 'refer' clojure.core." [& body] - `(do (create-ns 'sym#) - (let [result# (with-ns 'sym# - (clojure.core/refer-clojure) - ~@body)] - (remove-ns 'sym#) - result#))) + `(try + (create-ns 'sym#) + (let [result# (with-ns 'sym# + (clojure.core/refer-clojure) + ~@body)] + result#) + (finally (remove-ns 'sym#)))) |