Simple macro coding trick for accessing a user defined variable

Hello All,

I am reading “Metaprogramming Elixir” by C. McCord and I am wondering how to enhance its chapter 2 example on the ExUnit test macro (see below). I would like to be able to access a “context” variable in test call, as in

> test "bla", context do
>  .... context[var] ....
> end

However, the current unquote(test_block) is definitely not sufficient. I guess that some Macro.escape could help, but …

The following code is given by Chris for the simple test macro (without any context) :

> defmodule Assertion do
>   defmacro __using__(_options) do
>     quote do
>       import unquote(__MODULE__)
>       Module.register_attribute __MODULE__, :tests, accumulate: true
>       @before_compile unquote(__MODULE__)
>     end
>   end
> 
>   defmacro __before_compile__(_env) do
>     quote do
>       def run, do: Assertion.Test.run(@tests, __MODULE__)     
>     end
>   end
> 
>   defmacro test(description, do: test_block) do
>     test_func = String.to_atom(description)
>     quote do
>       @tests {unquote(test_func), unquote(description)}
>       def unquote(test_func)(), do: unquote(test_block)
>     end
>   end
> 
>   defmacro assert({operator, _, [lhs, rhs]}) do
>     quote bind_quoted: [operator: operator, lhs: lhs, rhs: rhs] do
>       Assertion.Test.assert(operator, lhs, rhs)
>     end
>   end
> end

Can you help me on improving this exemple by introducing the capability to access data from inside the test macro via a declared “context” variable in the test call as in the ExUnit framework ?

Thanks a lot.
Laurent