Enable error_context in assertions#15309
Conversation
|
|
||
| """ | ||
| defmacro assert({:=, meta, [left, right]} = assertion) do | ||
| code = escape_quoted(:assert, meta, mark_as_generated(assertion)) |
There was a problem hiding this comment.
Since assert/1 was a macro but assert/2 was a function, I needed to move things around a bit to enable diffing within assert/2, hence the decent size of this PR
Assisted-by: opencode:OpenAI/GPT-5.4
e8684d2 to
57c9f6c
Compare
|
Hi @dtwiers! Thank you for the PR! Unfortunately, this pull request may lead to breakages, in case someone was expecting assert/2 to always be a function. Furthermore, this pull request has one slight issue, which is that the options are compile-time only. For example, this works: But not this: Which is something we generally try to avoid in Elixir (although in this case it is something we can easily solve, exactly because of the function/macro semantics). So I understand the use case you are asking for here but unfortunately I don't think this is the way to go. One option I thought of is to have some sort of |
This enables 2 features within assertions:
assert foo == bar, diff: true, message: "this must be a string, and is intended to be the main error message"will give the error message as-is, but will also enable the same beautiful diffing we've enjoyed inassert/1.assert foo == bar, error_context: %{this: "could be any shape"}, which impliesdiff: true, also enables the diffing output, but adds an additionalcontext: <whatever you put here>, which I have found to be extremely handy if you want to assert something in a loop (you put the iterator value in theerror_contextand you know which item failed). You can still usemessage: "my main error message"with it too, if you want more detailsThoughts anyone? I asked about this as a proposal all the way back in September and finally got around to implementing it. It's not only my first PR into Elixir, it's my first PR into any major FOSS project.