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 ?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With