Unit Testing Macros

341 views
Skip to first unread message

Sean

unread,
Mar 17, 2009, 6:06:46 PM3/17/09
to Clojure
Hey everyone,
I'm writing a macro library for myself, and I'm thinking about
publishing it. Before I do so, I'd like to write & run some unit
tests for it. I was hoping the crowd could chime in on how they test
macros. Any links & examples would be great. Thanks!


Cosmin Stejerean

unread,
Mar 18, 2009, 12:42:31 AM3/18/09
to clo...@googlegroups.com
Haven't gotten around to writing unit tests for macros, but if I did I think I would use macroexpand-1 and assert that it expanded into the form I was expecting. For example let's say I wanted to write a test for the 'defn' macro

If it worked correctly then the following expression should be true

(= (macroexpand-1 '(defn foo [x y] (+ x y))) 
     '(def foo (clojure.core/fn ([x y] (+ x y)))))


and it is, because (defn foo [x y] (+ x y)) does in fact expand out to (def foo (clojure.core/fn ([x y] (+ x y))))


--
Cosmin Stejerean
http://offbytwo.com

Stuart Sierra

unread,
Mar 18, 2009, 12:38:45 PM3/18/09
to Clojure
Hi Sean,

You can generally test a macro like any other function -- call it,
then check that the result is what you want. The exact form a macro
expands to is usually an implementation detail, not a behavior.

-Stuart Sierra

Sean

unread,
Mar 18, 2009, 3:14:41 PM3/18/09
to Clojure
Thanks to everyone who replied. After looking at the examples & a
night's sleep, this makes sense.

On Mar 18, 12:38 pm, Stuart Sierra <the.stuart.sie...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages