I want to generate a set of tests based on some external file, and avoid typing them out .
I would hope that this would work:
for {test_name, input, output} <- get_specs() do
test "test #{test_name}" do
assert process(input) == output
end
end
But the compiler complains that input
and output
are unknown.
I tried doing this with a macro:
defmacro test_row(test_name, input, output) do
quote do
test "test #{test_name}" do
assert process(unquote(input)) == unquote(output)
end
end
end
for {test_name, input, output} <- get_specs() do
test_row(test_name, input, output)
end
But then the compiler complains about not being able to find test_row
.
I have found a workaround by using module variables:
for {test_name, input, output} <- get_specs() do
@input input
@output output
test "test #{test_name}" do
assert process(@input) == @output
end
end
But it looks a bit ugly. So I wonder, is there a “canonical” way of doing this sort of thing?