dimitarvp

dimitarvp

Cannot find or invoke local XYZ inside match -- ideas?

I am trying to make ergonomic test helpers like so:

  def violated_unique_constraint?(
        field,
        {:error,
         %Changeset{
           errors: [
             {field,
              {_,
               [
                 {:constraint, :unique},
                 {:constraint_name, _}
               ]}}
           ]
         }}
      ),
      do: true

  def violated_unique_constraint?(_, _), do: false

Which in my tests I am invoking like so:

assert violated_unique_constraint?(:country_code) =
             insert(%{country_code: "us", ...})

Which fails with:

** (CompileError) test/schema/whatever_test.exs:15 cannot find or invoke local violated_unique_constraint?/1 inside match. Only macros can be invoked in a match and they must be defined before their invocation. Called as: violated_unique_constraint?(:country_code)

You’ll notice I am matching the first parameter inside the second which AFAIK is allowed in Elixir and worked really well for me in other scenarios.

What am I doing wrong?

Marked As Solved

fuelen

fuelen

violated_unique_constraint? has arity 2, so

assert violated_unique_constraint?(:country_code, insert(%{country_code: "us", ...}))

Also Liked

lud

lud

It does not work for me:

iex(1)> defmodule X do
...(1)> def indentity(x), do: x
...(1)> end
{:module, X,
 <<70, 79, 82, 49, 0, 0, 4, 212, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 137,
   0, 0, 0, 14, 8, 69, 108, 105, 120, 105, 114, 46, 88, 8, 95, 95, 105, 110,
   102, 111, 95, 95, 10, 97, 116, 116, 114, ...>>, {:indentity, 1}}
iex(2)> require ExUnit.Assertions
ExUnit.Assertions
iex(3)> ExUnit.Assertions.assert X.identity(1) = 1
** (CompileError) iex:3: cannot invoke remote function X.identity/1 inside a match
    (stdlib 3.14) lists.erl:1358: :lists.mapfoldl/3
    (stdlib 3.14) lists.erl:1358: :lists.mapfoldl/3
    expanding macro: ExUnit.Assertions.assert/1

To be more precise, I know you can assert with a valid match expression, but that’s it. No left-side function calls (same rules as for case, receive).

lud

lud

I ran mix new dimi and then wrote this:

defmodule Dimi do

  def hello do
    :world
  end

  def indentity(x), do: x
end

and this

defmodule DimiTest do
  use ExUnit.Case
  doctest Dimi

  test "greets the world" do
    assert Dimi.hello() == :world
    assert Dimi.identity(1) = 1
  end
end

And you get the same error. Left-side function calls on match operations are not supported. And I don’t see a reason they should be supported in assert since they would not be anywhere else anyway.

Edit: also it makes sense because you can do that:

assert {:ok, data} = stuff()
do_something_with(data)

So the match has to be executed since we may want to work with the bound variables. So it has to be a valid match expression.

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement