dkuku

dkuku

Am I the only one who can't spot the difference in map keys

This is a proposal to make the map key mismatch errors a bit better:
Every time I have a typo It’s very challenging for me even when I understand the error:


I see this in many places - it’s the same in test results. There may be a bit easier because the colors but because the order of keys sometimes changes and the maps are often huge I need to scroll the screen multiple times.
I even changed the test formatter to show only the lines that have ansi sequences but I work on different projects and the formatter needs to be always installed. Similar with errors thrown by dialyzer or hammox.

Most Liked

cevado

cevado

but you don’t need to refactor everything at once, next time a test breaks, refactor that specific test. spread the word at the company, so they stop doing the old way and more people help to do the refactor… one day it all gonna be done. :smile:

cevado

cevado

both clauses expect a :amount key that is not present in the map that is passed as argument.

edit: not sure if your question is about the error or a better way to format this.
in the way you’re using it, it would be better to have a struct or at least use the dot notation to extract the values instead of pattern matching them on function clause:

def build(something) do
  created_at = something.created_at
  ...
end
sodapopcan

sodapopcan

Having not used LLMs myself, if that doesn’t work you might have some luck with Sourceror depending on how consistent your map assertions are.

LostKobrakai

LostKobrakai

Yeah, should be doable with sourceror, given the following plain elixir poc:

"""
defmodule MyApp.Test do
  test "abc" do
    y = 15
    assert %{a: ^y, b: 8} = call()
  end
end
"""
|> Code.string_to_quoted!(
  literal_encoder: &{:ok, {:__block__, &2, [&1]}},
  token_metadata: true,
  unescape: false
)
|> Macro.postwalk(fn
  {:assert, assert_meta, [{:=, assign_meta, [{:%{}, _, pairs}, call]}]} ->
    checks =
      Enum.map(pairs, fn {{:__block__, _meta, [key]}, value} ->
        quote do
          assert unquote(value) = response.unquote(key)
        end
      end)

    {:__block__, [],
     [
       {:assert, assert_meta, [{:=, assign_meta, [Macro.var(:response, nil), call]}]} | checks
     ]}

  other ->
    other
end)
|> Code.quoted_to_algebra(
  literal_encoder: &{:ok, {:__block__, &2, [&1]}},
  token_metadata: true,
  unescape: false
)
|> Inspect.Algebra.format(:infinity)
|> IO.iodata_to_binary()
|> IO.puts()
# defmodule MyApp.Test do
#   test "abc" do
#     y = 15
#     (assert response = call()
#     assert ^y = response.a
#     assert 8 = response.b)
#   end
# end

Not sure how to best get rid of the parenthesis for the block there though.

Where Next?

Popular in Proposals: Ideas Top

MeerKatDev
many times we do stuff like (e.g. test setups, json views in phoenix) aaa = ... bbb = ... ccc = ... %{aaa: aaa, bbb: bbb, ccc: ccc} and...
New
rhcarvalho
Hi all, I would like to gather some feedback before a more intentional proposal to add a new :depth option when specifying a Git depende...
New
rekkice
I’m building an editor integration to evaluate Elixir code in an IEx session. While Code.eval_string/3 allows tracking variable bindings,...
New
kip
Sumary of proposal DateTime.from_iso8601/3 adjusted to: set all numeric fields to the values as parsed (not shifted to UTC), preservi...
New
pdgonzalez872
Hi! There has been some discussion about hiring/jobs on here and I thought about running this by everyone. I wanted to try to help recr...
New
dli
Ecto currently supports some data-modifying WITH statements / CTEs for Postgres: Options: […] :operation - one of :all , :update_all ,...
New
winsalva
Hello all. First of all i’m running this using termux on an android phone. Running mix assets.setup shows this message 06:54:08.450 [de...
New
bartblast
This could resolve to {[a: 1, b: 2]}. Was it ever considered to allow such syntax? Notice this: {:abc, a: 1, b: 2} and this: my_fun(:abc,...
New
sezaru
When writing my code, I always find __MODULE__ very useful to use as alias of that module “inner dependencies”, ex: alias __MODULE__.{Im...
New
dkuku
This is a proposal to make the map key mismatch errors a bit better: Every time I have a typo It’s very challenging for me even when I u...
New

Other popular topics Top

SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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