O'Reilly logo

Metaprogramming Elixir by Chris McCord

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required