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

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.