One way you can get what you want is to give up on the auto-gensym feature
of the #-terminated identifiers, and call gensym directly, enabling it to
be mocked out with with-redefs. E.g. instead of:
(defmacro m1
[x f]
`(let [x# ~x]
(~f x#)))
do
(defmacro m1 [x f]
(let [x-sym (gensym)]
`(let [~x-sym ~x] (~f ~x-sym))))
You can then do something like
(defn new-gensym []
(let [counter (atom 0)]
(fn [& [extra]] (symbol (str (or extra "G_") (swap! counter inc))))))
(with-redefs [gensym (new-gensym)] (macroexpand-1 '(m1 1 inc)))
(not tested)
On Fri, Mar 27, 2015 at 5:50 PM, David James <[email protected]> wrote:
> I agree with this motivation behind this request, which I explain in more
> detail here:
> http://stackoverflow.com/questions/16745135/how-to-test-a-clojure-macro-that-uses-gensyms
>
> We should be able to test the behavior *and* the macroexpansion. (Most
> things in life are not simple either/or decisions. Don't believe people
> that tell you otherwise.)
>
> On Monday, March 23, 2015 at 12:58:49 PM UTC-4, Chris Ford wrote:
>>
>> I think it's useful to think of macros as an odd form of I/O. Just as you
>> would separate out your templating from your domain functions, separate out
>> your defmacro from regular functions that just happen to manipulate
>> symbols. These functions will be easier to test.
>>
>> On 23 March 2015 at 16:23, Sean Corfield <[email protected]> wrote:
>>
>>> On Mar 22, 2015, at 7:52 PM, myguidingstar <[email protected]>
>>> wrote:
>>> > I wonder if there is any way to make macro expansion in Clojure
>>> deterministic. That would be useful in unit tests.
>>>
>>> I'd be very interested to understand your use case... Testing what the
>>> macro expands to seems like it is test the macro system itself, not your
>>> own code. Surely in a unit test you'd want to test the _behavior_ of the
>>> code instead?
>>>
>>> Sean Corfield -- (904) 302-SEAN
>>> An Architect's View -- http://corfield.org/
>>>
>>> "Perfection is the enemy of the good."
>>> -- Gustave Flaubert, French realist novelist (1821-1880)
>>>
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To post to this group, send email to [email protected]
>>> Note that posts from new members are moderated - please be patient with
>>> your first post.
>>> 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
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks, which
may be sweet, aromatic, fermented or spirit-based. ... Family and social
life also offer numerous other occasions to consume drinks for pleasure."
[Larousse, "Drink" entry]
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.