Hey everyone,
For an upcoming ElixirForum post (and a small algorithm experimentation library) I am trying to generate unit tests like below. PP
is the name of the module being tested and I’d like to only test functions whose names start with impl_
and have arity 1. @inputs_to_expected_results
is a map.
@function_names PP.__info__(:functions)
|> Enum.filter(fn {n, i} ->
Atom.to_string(n) |> String.starts_with?("impl_") and i == 1
end)
|> Enum.map(&elem(&1, 0))
@function_names
|> Enum.each(fn name ->
@inputs_to_expected_results
|> Enum.each(fn {input, expected} ->
# IO.puts("Testing #{inspect(input)}, expecting result: #{inspect(expected)}")
test inspect(input) do
assert expected == apply(PP, unquote(name), [unquote(input)])
end
end)
end)
The error I get is that the expected
variable does not exist. Tried to wrap it with unquote
like the other two variables but then I get told that the value is an invalid quoted expression.
I am stuck. What am I doing wrong?