Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a strategy to test macros in Clojure?

Tags:

macros

clojure

I would generally make functions and would write unit tests for them. but for macros that do code transformations, how will one apply logic of macthing expected and actual value ?

because input to macros will be code and output will be code as well. this seems so dynamic to me to be able to come up with some strategy to test.

Or I can use a sample input for macro, and pass the expression to macroexpand-1 and match the output with the expected one ?

like image 365
Amogh Talpallikar Avatar asked Oct 12 '13 10:10

Amogh Talpallikar


People also ask

When should I use Clojure macros?

Clojure has a programmatic macro system which allows the compiler to be extended by user code. Macros can be used to define syntactic constructs which would require primitives or built-in support in other languages. Many core constructs of Clojure are not, in fact, primitives, but are normal macros.


1 Answers

I think there's merit in both examples.

For small substitution-style macros you can test the expanded form of a few examples. Testing frameworks like Midje help with that, it has an =expands-to=> checker.

But for more complex examples, the expanded form is often rapidly changing during development in small ways that never the less make checking the expanded form very brittle. In these examples I usually create test cases around specific uses of the macro.

like image 92
Ben Ashford Avatar answered Oct 12 '22 14:10

Ben Ashford