fabioticconi

fabioticconi

Understanding __using__, with a practical problem

Hi all,

I’m trying to solve a practical problem - make an ExUnit “data case” with async true as default, instead of false.

So if a user does this:

use MyTestCase

Then async will be true, but if the user specifies use MyTestCase, async: false then it will be respected.

It seems to me that it should be fairly easy to inject code into the ExUnit.CaseTemplate to make it happen, but my understanding of Elixir metaprogramming is limited and have been stuck.

In my datacase, the first thing I do is to use ExUnit.CaseTemplate. This will import some callbacks and assertions and inject an __using__, overridable macro. As can be seen here: elixir/case_template.ex at v1.13.4 · elixir-lang/elixir · GitHub

Now, I thought I could do something like:

defmodule MyTestCase
  use ExUnit.CaseTemplate

  using opts do quote do
    # modify opts keyword list, put async true if not present
  end
end

But the __proxy__ function is called before evaluating the provided block (it’s the using/1 helper function in ExUnit.CaseTemplate that I’m leveraging). This means that the opts will have already been processed so I can’t know, at that point, whether the user had specified async: true or async: false.

What am I missing? Can I work with this or do I need to essentially drop the use ExUnit.CaseTemplate line and replicate what is does but in the way I need?

Marked As Solved

LostKobrakai

LostKobrakai

You likely want this:

defmodule MyTestCase
  use ExUnit.CaseTemplate

  defmacro __using__(opts) do
    opts
    |> Keyword.put_new(:async, true)
    |> super()
  end
end

For overwriting a overwritable __using__ macro, you need to implement exactly that macro. To call the injected implementation with your changes you can use super(). There’s no need to fiddle with quote in this case as you never need to convert text of code into AST.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement