Elixir newbie here working on a stemmer for a document indexing engine. The stemming algorithm has dozens of steps with many tests per step. I’ve got a data-driven approach that is helpful, but I think it must be possible to be even more efficient with a little meta-programming. Here is an example of one of my test modules:
defmodule StemEx.StepsTest do
use ExUnit.Case, async: true
# ----- step1a -----
step1a_vals = [
["caresses" , "caress"],
["ponies" , "poni" ],
["ties" , "ti" ],
["caress" , "caress"],
["cats" , "cat" ],
]
for [input, output] <- step1a_vals do
@input input
@output output
test "step1a: '#{input}' has output of '#{output}'" do
assert StemEx.Steps.step1a(@input) == @output
end
end
# ----- step1b -----
step1b_vals = [
["feed" , "feed" ],
["agreed" , "agree" ],
["plastered", "plaster" ],
["bled" , "bled" ],
["motoring" , "motoring"],
["sing" , "sing" ],
]
for [input, output] <- step1b_vals do
@input input
@output output
test "step1b: '#{input}' has output of '#{output}'" do
assert StemEx.Steps.step1b(@input) == @output
end
end
end
This works great but I don’t like to repetitive boilerplate in the for blocks. Ideally I could reduce the blocks to a single call - something like test_loop_for("step1b", step1b_vals).
Would it be possible to use a defmacro to auto-magically generate the loop code? Can someone post an example?? Thanks in advance.






















