Hi

On Sat, Nov 22, 2008 at 7:32 AM, samppi <[EMAIL PROTECTED]> wrote:
>
> I'm trying to unit-test a mutli-function's methods without resorting
> to a separate test file. I can do this:
>
>  (defn foo
>    ([x] (+ x 2))
>    {:test (fn [] (= (foo 3) 4))})
>
> ...but how do I do something like this?
>
>  ; Does not work
>  (defmethod foo :mapping
>    ([x] (assoc x :a 5))
>    {:test (fn [] (= (foo {:a 3 :b 2}) {:a 5 :b 2}))})
>
> I can't find a way to attach metadata to methods. I can't find a
> function that creates a standalone method that I can define a variable
> with. Is it currently possible to somehow do this another way?

Perhaps this thread will help with the metadata part of the question:

http://groups.google.com/group/clojure/browse_thread/thread/f19ff07f8d44fe7b

It also seems to me like there's something wrong with your defmethod,
although I've only just started trying to figure out multimethods.

I think you actually need something like this:

(defmulti foo :dispatch)
(defmethod foo :mapping [x] (assoc x :a 5))

and then you need to call it like:

(foo {:dispatch :mapping :a 3 :b 2})

With the metadata, I think it should be like this:

(defmethod #^{:test (fn [] (= (foo {:dispatch :mapping :a 3 :b 2})
{:dispatch :mapping :a 5 :b 2}))} foo :mapping [x] (assoc x :a 5))

Although I think it makes more sense to attach the metadata to the
defmulti than to the defmethod.

I may be completely wrong about the above, since I've only just
started looking at metadata and multimethods, but I thought I'd try to
give you an answer seeing as nobody else has yet :)

I do seem to be having some trouble with metadata, though (using Clojure r1121):

user=> (def #^{:a 1} x 'test)
#'user/x
user=> x
test
user=> (meta x)
nil
user=> (meta (with-meta x {:a 1}))
{:a 1}
user=>

Shouldn't (meta x) return {:a 1}?

I see that this works:

(def x (with-meta 'test {:a 1}))

although it seems that would attach the metadata to 'test instead of to x.

(def #^{:a 1} x) (meta 'x) also returns nil.

What am I missing?

-- 
Michael Wood <[EMAIL PROTECTED]>

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to