I'm sorry for spamming.. In Stuart's book - 'Programming Clojure', I
saw below paragraph:
"1. Redeļ¬ne private to mean "private for production code, but public
for serialization and unit tests".
...
I have seen Java programs that needed all these features."
So I decided to try what he referred to. I thought it will be trivial
by using macros.. But shamefully, I couldn't do this simple job.. Here
is my code, which does not work:
;;;;;;;;TRY 1;;;;;;;;;
;;defn_star.clj
(ns defn-star)
(def *test* (ref false))
(defmacro defn*
{:doc "If *test* is true, 'defn*' expands to 'defn', otherwise it
expands to 'defn-'"}
[& body]
(if (true? @*test*)
`(defn [EMAIL PROTECTED])
`(defn- [EMAIL PROTECTED])))
;;temp.clj
(ns temp
(:use defn-star))
(dosync (alter *test* #(not %)))
(defn* sym->key
{:doc "(sym->key 'abc) => :abc (sym->key nil) => nil"}
[sym]
(if (= "" (str sym))
nil
(keyword (str sym))))
;;test_temp.clj
(ns test-temp
(:use temp)
(:use (clojure.contrib test-is))
(deftest test-sym->key
(is (= (sym->key 'abc) :abc)))
(run-tests)
;;;;;;;;;;TRY 2;;;;;;;;;;
;;defn_star.clj
(ns defn-star)
(def *test* false)
(defmacro defn*
{:doc "If *test* is true, 'defn*' expands to 'defn', otherwise it
expands to 'defn-'"}
[& body]
(if (true? *test*)
`(defn [EMAIL PROTECTED])
`(defn- [EMAIL PROTECTED])))
;;temp.clj
(ns temp
(:use defn-star))
(binding [*test* true]
(defn* sym->key
{:doc "(sym->key 'abc) => :abc (sym->key nil) => nil"}
[sym]
(if (= "" (str sym))
nil
(keyword (str sym)))))
;;test_temp.clj
(ns test-temp
(:use temp)
(:use (clojure.contrib test-is))
(deftest test-sym->key
(is (= (sym->key 'abc) :abc)))
(run-tests)
In both cases, tests can not recognize 'sym->key'. Though I (:use defn-
star), it seems that *test* variable is not referred by 'temp'. Ahh..
maybe I think that I didn't properly understand about namespaces. Is
there any elegant way to achieve testing private functions? Any
suggestion appreciated.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---