Neat. I noticed that you're forcing the arg lists into vectors in both make-maps and in stubfn. Since they're not being manipulated at all, you could just as easily leave them as seqs and everything will still work.
It might be a bit clearer to use reduce instead of map and merge to generate the stub maps, but YMMV. (defmacro stub [stubs & body] (let [stub-maps (reduce (fn [acc [[f & args] res]] (assoc-in acc [f args] res)) {} (partition 2 stubs))] `(binding [~@(mapcat (fn [[fname fhash]] `(~fname (fn [& args#] ('~fhash args#)))) stub-maps)] [EMAIL PROTECTED]))) I've also removed the helper function but on second thought it might be nicer to keep it. If you delegate stubbed function calls to the helper, it could be overridden to do something other than return nil when stubbed functions are called with argument lists not specified in the stub. Justin On Nov 23, 11:24 am, James Reeves <[EMAIL PROTECTED]> wrote: > 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 clojure@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---