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.
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
Sharp-eyed readers may spot that refunded_at is matched to nil in one and bound to refunded_at in the other, but it’s the same kind of difficulty you’re highlighting with the test failure message.
This is just one example and it’s not only pattern matching related. In tests or dialyzer errors the comparison is between 2 maps that are very similar and you may get it randomly because something started to fail. This was always causing issues for me and I wanted to raise it. I’m happy to implement it when we have a good idea how to handle this.
this specific formatting only happens on function clause errors, i don’t think it can be better formatted.
for tests if you’re using ExUnit it shows only the diff with it highlighted if you’re asserting a pattern match. my suggestion in that case is to do something like:
Whoa, cool! I would have not been able to throw that together so quickly Sourceror should take care of the parens, no? Not actually sure. I guess not because we’re writing new code Removing them manually wouldn’t be the biggest deal considering the time-savings of code-rewriting (though I shouldn’t say that as it’s not my project we’re talking about).
I only pushed it because this is how I solved this problem for myself. I was still learning at the time so I had a tiny project and didn’t need to do any programatic code-rewriting, but I started writing single assertions per key for this very reason.
** (FunctionClauseError) no function clause matching in TestMap.a/1
The following arguments were given to TestMap.a/1:
# 1
%{pies: :pies, kot: :kot, mysz: :mysz, slon: :slon, zyrafa: :zyrafa, zaba: :zaba, ryba: :ryba, kon: :kura}
map.ex:2: TestMap.a/1
map.ex:8: (file)
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.