Unit Tests

Unit-testing macros should be used only for cases where you need to test a bit of tricky code generation in isolation. Over-testing macros at the unit level can lead to brittle tests because we can only test the AST generated from the macro or the string of source produced. These values can be hard to match against and are subject to change, which can lead to error-prone tests that are difficult to maintain.

Let’s add unit tests to our Translator suite by testing the compile function. Our compile function was the main code-generation entry point that we delegated to from our __using__ macro. The easiest way to test that the t/3 functions were generated properly is to convert the AST to a string and test that the Elixir source matches ...

Get Metaprogramming Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.