Hi,
I’m trying to generate test cases from a JSON file. However, when I try the quote/unquote
mechanism, it compiles but doesn’t find tests.
test/input.json
{
"[1,2,3]": [ 1, 2, 3 ],
"true": true,
"{\"s\":1}": { "s": 1 }
}
tests/my_test.exs
(working, finding three tests)
This is based on the @ AndyL discussion on ExUnit Data-driven Tests. I would like to get rid of the @expression
stuff.
defmodule MyTest do
import MyTestHelper, only: [parses_to: 2]
use ExUnit.Case, async: true
with {:ok, body} <- File.read(Path.join([__DIR__, "input.json"])),
{:ok, json} <- Poison.decode(body) do
Enum.each(json, fn {expression, result} ->
@expression expression
@result result
@expression |> parses_to(@result)
end)
end
end
tests/my_test.exs
(not working, not finding any test)
defmodule MyTest do
import MyTestHelper, only: [parses_to: 2]
use ExUnit.Case, async: true
with {:ok, body} <- File.read(Path.join([__DIR__, "input.json"])),
{:ok, json} <- Poison.decode(body) do
Enum.each(json, fn {expression, result} ->
quote do
unquote(expression) |> parses_to(unquote(result))
end
end)
end
end
tests/test_helper.exs
ExUnit.start()
defmodule MyTestHelper do
use ExUnit.Case
defmacro parses_to(expression, expected) do
quote do
test "Expression \"#{unquote(expression)}\"" do
assert unquote(expected) === unquote(expression) |> Poison.decode!()
end
end
end
end