AndyL
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.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #elixirconf-us
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance











Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sheharyarn
How about
Enum.each/2? I do something like this myself. Here’s how it would look:AndyL
I ended up with a solution that looks much like your example. Simpler than metaprogramming (maybe this is metaprogramming?!) Not sure exactly what is going on under the covers - but it seems to give a lot of flexibility for data-driven tests. The tests run very fast and the failure messages are easy to decipher. Looks like you can load datasets from the filesystem - I’ve got a files with ~22K test examples. Just saved a lot of typing!
AndyL
Last note on this thread: this ‘dynamic generation’ approach didn’t work with 22K examples - it times out and fails to run. Here is the approach I used - runs 22K examples in 0.3 seconds - very acceptable - and the error messages are quite good.
axelson
For anyone else that comes across this as an example of data-driven tests and wants to improve their test reporting output you can do something like the following:
This will give you pretty output like:
Whereas the default output show something more like:
Which isn’t that helpful since you can’t tell what the input is without adding some IO.puts into your test code. There’s probably more elegant ways to accomplish what I’m outlining but I think this is at least a good start!