DidactMacros
This pertains to when a call is made to the function ExUnit.Assertions.assert/2 from within ExUnit.Assertions.translate_operator/6 (matched against the equality_check: false argument-set), and the first argument sent to it is unquote(call).
The call made to this function is from within a quote do: block, and the first argument sent is unquote(call), with the second being a keyword list.
In the ExUnit.Assertions.assert/2 function there is an unless expression that is to execute its code in the event that unquote(call), matched to the variable value, evaluates to false.
How can value ever be false?
When does unquote(call) have a chance to be evaluated?
Code for reference:-
assert/1
defmacro assert(assertion) do
if translated = translate_assertion(:assert, assertion, __CALLER__) do
translated
else
{args, value} = extract_args(assertion, __CALLER__)
quote generated: true do
if value = unquote(value) do
value
else
raise ExUnit.AssertionError,
args: unquote(args),
expr: unquote(escape_quoted(:assert, [], assertion)),
message: "Expected truthy, got #{inspect(value)}"
end
end
end
end
translate_assertion
defp translate_assertion(:assert, {operator, meta, [_, _]} = expr, caller)
when operator in @operator do
if match?([{_, Kernel}], Macro.Env.lookup_import(caller, {operator, 2})) do
left = Macro.var(:left, __MODULE__)
right = Macro.var(:right, __MODULE__)
call = {operator, meta, [left, right]}
equality_check? = operator in [:<, :>, :!==, :!=]
message = "Assertion with #{operator} failed"
translate_operator(:assert, expr, call, message, equality_check?, caller)
end
end
translate_operator
defp translate_operator(kind, {op, meta, [left, right]} = expr, call, message, false, _caller) do
expr = escape_quoted(kind, meta, expr)
context = if op in [:===, :!==], do: :===, else: :==
quote do
left = unquote(left)
right = unquote(right)
ExUnit.Assertions.assert(unquote(call),
left: left,
right: right,
expr: unquote(expr),
message: unquote(message),
context: unquote(context)
)
end
end
assert/2
def assert(value, opts) when is_list(opts) do
unless value, do: raise(ExUnit.AssertionError, opts)
true
end
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
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 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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
Code like:
will expand to:
DidactMacros
Thanks for taking a look.
When* I try expanding something like this …
…using my own..
h = {:!=, [], [5, 5]}…then passing to a function…
…I get an AST chunk back.
If I do the unquote outside of the
quote do:I get a no variable error.al2o3cr
Functions like
unqwould need to be called from inside adefmacroto get the AST they generate turned into executable code.DidactMacros
Also noticed there was a type in there specifically,
Just going to update this in the 2nd P as this was not an area of concern, nor a source of error.
Fantastic, you’ve been solving so many of my question lol!
Just a crucial sidebar, when I do…
It outputs true however,
when I use
I get AST segments again.
I think I understand why, but I don’t want to assume.
Are the function calls getting processed, or are they all getting crammed into the macro as ASTs and then getting executed in there?
Edit: I forgot that the 3 elem tuple is going to get quoted when it’s passed as an arg to the macro, which addresses the above.
DidactMacros
The
quote do…unquoteis located in adef do:block which is quoted, so theunquoteis in a nestedquotewhich means that is quoted rather than executed.Results in…
…getting sent back to the macro that started the chain of function calls.
The macro will then subject the AST to a
macro.escape-like processing that identifies:unquoteoperators in the AST and does not escape the args (children), leaving them in their current form with :unquotestripped or generating a valid AST to describe them. This occurs because the returned AST from translate_operator is the last reachable expression in the macro, and so it must be validated as a formal AST.The macro will then insert this valid AST fragment into the over all AST at the location were it (the macro,
defmacro assert(assertion)) was called.I initial thought that evaluation was taking place at unquote and at escape, however this was corrected:
If anyone has an question about this I’d be happy to go into further detail.
If I got anything wrong please let me know.