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

Extending Modules

A core purpose of macros is to inject code into modules to extend their behavior, define functions, and perform any other code generation that’s required. For our Assertion framework, our goal is to extend other modules with a test macro. The macro will accept a test-case description as a string, followed by a block of code where assertions can be made. Failure messages will be prefixed by the description to help debug the failing test cases. We’ll also define the run/0 function automatically for the caller so that all test cases can be executed by a single function call.

Our goal throughout this section is to produce the following testing DSL, which extends any module with our mini testing framework. Take a look at this code, ...

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