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