lok0613

lok0613

Hello all,

I’m recently doing something similar to ExUnit, keeping a bunch of variables on “setup” block and then pass it to the next block which is “test” just like below.

defmodule MyTest do
  use ExUnit.Case, async: true

  setup do
    {:ok, %{message: "hi"}}
  end

  test "my test title", %{message, msg} do
    IO.puts(msg)
  end
end

However, I didn’t managed to do this on my own.

defmodule XUnit do

    defmacro __using__(_opts) do
        quote do
            import XUnit
            
            @setups []
            @tests []
            
            @before_compile XUnit
            
            def run() do
                IO.puts("Running....")
                run_setups()
                run_displays()
            end
        end
    end
    
    defmacro setup(do: block) do
        fn_name = String.to_atom("setup")
        quote do
            def unquote(fn_name)(), do: unquote(block)
            @setups [unquote(fn_name) | @setups]
        end
    end
    
    defmacro test(message, var, do: block) do
        var = Macro.escape(var)
        
        quote bind_quoted: [message: message, var: var, block: block] do
            fn_name = String.to_atom("test " <> message)
            def unquote(fn_name)(unquote(var)), do: unquote(block)
            @tests [{fn_name, var} | @tests]
        end
    end
    
    defmacro __before_compile__(_opts) do
        quote do
            def run_setups() do
                @setups
                |> Enum.each(fn setup_fn -> apply(__MODULE__, setup_fn, []) end)
            end
            
            def run_displays() do
                @tests
                |> Enum.each(fn {test_fn, params} ->
                    apply(__MODULE__, test_fn, [params])
                end)
            end
        end
    end

end

defmodule Test do
    use XUnit
    
    setup do
        %{message: "hi"}
    end
    
    test "my test title", %{message: msg} do
        IO.puts("print from test, #{msg}")
    end

end

Test.run()

It turns out this error message.

warning: variable "msg" does not exist and is being expanded to "msg()", please use parentheses to remove the ambiguity or change the variable name
  Main.exs:64: Test

** (CompileError) Main.exs:64: undefined function msg/0
    (elixir) src/elixir_bitstring.erl:142: :elixir_bitstring.expand_expr/4
    (elixir) src/elixir_bitstring.erl:27: :elixir_bitstring.expand/8
    (elixir) src/elixir_bitstring.erl:20: :elixir_bitstring.expand/4
    expanding macro: XUnit.test/3

I tried many ways but it still not really work as my expectation. This is my first question on elixir forum, so please could anyone give me some idea how to mimic the way that ex_unit was doing?

Thanks so much

Showing Posts 1 to 8

kokolegorille

kokolegorille

Hello and welcome,

You do have a syntax error,

It should be…

test "my test title", %{message: msg} do
al2o3cr

al2o3cr

Your test macro handles arguments differently than the one in ExUnit:

https://github.com/elixir-lang/elixir/blob/83ddb923d4294470e3c8d158a5486f89f75e1481/lib/ex_unit/lib/ex_unit/case.ex#L299

I suspect that’s the cause of your immediate error.

You’ll also need to figure out how the return values of setup blocks make their way to the actual test functions; right now you’re passing the AST received by test. Enum.each is likely not the function you want here.

lok0613

lok0613 OP

this is just an example…

lok0613

lok0613 OP

really… I put Enum.each because it would be multiple test macros.

al2o3cr

al2o3cr

Example or not, unless your test cases have some kind of side-effect you won’t get any results from Enum.each besides :ok.

Kabie

Kabie

There is a trick:

defmacro test(message, param, do: block) do
  quote do
    def unquote(:"test_#{message}")(param) do
      unquote(param) = param
      unquote(block)
    end
  end
end
Kabie

Kabie

Ahh, I was overthinking, this should works already.

defmacro test(message, param, do: block) do
  quote do
    def unquote(:"test_#{message}")(unquote(param)) do
      unquote(block)
    end
  end
end

But your problem is you didn’t pass results that setup returns to tests, something like:

def run_tests() do
  setup_result = @setups
  |> Enum.reduce(%{}, fn setup_fn, params
    -> apply(__MODULE__, setup_fn, [params])
  end)

  @tests
  |> Enum.each(fn test_fn ->
    apply(__MODULE__, test_fn, [setup_result])
  end)
end
lok0613

lok0613 OP

wow that works like a charm!
Thank you @Kabie, @al2o3cr for helping. I’m actually digging into the ExUnit source code see if I could find any clue but it’s quite complex tho.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews