DidactMacros

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

Showing Posts 1 to 5

al2o3cr

al2o3cr

Code like:

assert 3 * 14 == 6 * 7

will expand to:

      left = 3 * 14
      right = 6 * 7

      ExUnit.Assertions.assert(left == right,
        left: left,
        right: right,
        expr: #<ast omitted>,
        message: "Assertion with operator == failed",
        context: #<__CALLER__ omitted>
      )
DidactMacros

DidactMacros OP

Thanks for taking a look.

When* I try expanding something like this …

{operator, meta, [left, right]}

…using my own..

h = {:!=, [], [5, 5]}

…then passing to a function…

defmodule Tst do
 def unq(p) do
	quote do: unq2 unquote p
 end
 def unq2(g), do: IO.puts g
end

…I get an AST chunk back.

If I do the unquote outside of the quote do: I get a no variable error.

error: undefined variable "p"
└─ iex:200: Tst (module)

** (CompileError) iex: cannot compile module Tst (errors have been logged)
    (elixir 1.16.0) src/elixir_expand.erl:389: :elixir_expand.expand/3
    (elixir 1.16.0) src/elixir_expand.erl:599: :elixir_expand.expand_arg/3
    (elixir 1.16.0) src/elixir_expand.erl:531: :elixir_expand.expand_list/5
    (elixir 1.16.0) src/elixir_expand.erl:437: :elixir_expand.expand/3
    (elixir 1.16.0) src/elixir_expand.erl:599: :elixir_expand.expand_arg/3
    (elixir 1.16.0) src/elixir_expand.erl:615: :elixir_expand.mapfold/5
    (elixir 1.16.0) src/elixir_expand.erl:608: :elixir_expand.expand_args/3
    iex:194: (file)
al2o3cr

al2o3cr

Functions like unq would need to be called from inside a defmacro to get the AST they generate turned into executable code.

DidactMacros

DidactMacros OP

Also noticed there was a type in there specifically,

def unq2(g), do: IO.puts p

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…

defmodule Tst do
 defmacro unqm do
	unq({:==, [], [5, 5]})
 end
 def unq(p) do
	quote do: Tst.unq2 (unquote p)
 end
 def unq2(g), do: IO.inspect g
end
require Tst
Tst.unqm

It outputs true however,

when I use

defmodule Tst do
 defmacro unqm(k) do
	unq(k)
 end
 def unq(p) do
	quote do: Tst.unq2 (unquote p)
 end
 def unq2(g), do: IO.inspect g
end
require Tst
Tst.unqm {:!=, [], [5, 5]}

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

DidactMacros OP

The quote dounquote is located in a def do: block which is quoted, so the unquote is in a nested quote which means that is quoted rather than executed.

  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

Results in…

    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

…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 :unquote operators in the AST and does not escape the args (children), leaving them in their current form with :unquote stripped 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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews