On Mon, Jan 26, 2009 at 10:03 PM, Daniel Jomphe <danieljom...@gmail.com> wrote:
>
> Perry Trolard wrote:
>
>> You can get the symbol that names the function from the Var's
>> metadata, like:
>>
>> user=> (:name (meta (var =)))
>> =
>
> Thank you Perry.
>
> What is (var ...)? I didn't find it in the api docs, nor in the Vars
> and Environment page.
>
> I'm yet to make your code work for my specific needs:
>
>  (defn with-test-report [test fn]
>    (let [assert# (first  test)
>          expect# (second test)
>          params# (last   test)
>          result# (assert# expect# (apply fn params#))]

You are shadowing clojure.core/fn here.  Not a problem really, but
perhaps it would be better to use a different name?  Oh, and
clojure.core/test.

>      (if result#
>        (print ".")
>        (do (newline)
>            (println (format "FAIL: %s" (:name (meta (var
> expect#)))))))
>      result#))

I've seen various code snippets where people use something# in
functions.  Is there any point to this or gensym except in a macro?

> Exception: Unable to resolve var: expect# in this context

How about using a macro.  e.g.:

user=>
(defmacro with-test-report [tst f]
  `(let [pred#    (first  ~tst)
         pname# '~(first  tst)
         expect#  (second ~tst)
         params#  (last   ~tst)
         result#  (pred# expect# (apply ~f params#))]
     (if result#
       (print ".")
       (do (newline)
           (println (format "FAIL: [%s %s]" pname# params#))))
     result#))
nil
user=> (with-test-report [= 3 [1 3]] +)

FAIL: [= [1 3]]
false

I suppose fn-with-tests could be a macro that calls a function
(with-test-report pname pred expect params f) or something like that.

-- 
Michael Wood <esiot...@gmail.com>

--~--~---------~--~----~------------~-------~--~----~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to