In case anyone's interested, I've created a macro for stubbing
existing functions. I created it for my Fact unit testing library, but
it could be used with any unit testing framework, such as the test-is
library in clojure.contrib. Since stubbing is an important part of
isolating functions for the purpose of unit testing, it's possible
some people might find this useful:
(defn stubfn
"Given a map of argument vectors and return values, construct
a function to return the value associated with the key of
arguments."
[result-map]
(fn [& args] (result-map (vec args))))
(defmacro stub
"Create function stubs for isolated unit tests.
e.g. (stub [(f 1 2) 3
(f 3 2) 5]
(= (+ (f 1 2) (f 3 2))
8))"
[stubs & body]
(let [stub-pairs (partition 2 stubs)
make-maps (fn [[[f & args] ret]] {f {(vec args) ret}})
bind-stub (fn [[f clauses]] [f `(stubfn ~clauses)])]
`(binding
[~@(mapcat bind-stub
(apply merge-with merge
(map make-maps stub-pairs)))]
[EMAIL PROTECTED])))
- James
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---