James_E
Doctest: how to deal with inconsistent map key ordering?
Considering that the order of Map keys isn’t guaranteed, is there any reliable way to check KeyErrors in doctests?
Doctest failed: wrong message for
KeyErrorexpected:
"key {:c, 999} not found in: %{a: 10, b: 10}"actual:
"key {:c, 999} not found in: %{b: 10, a: 10}"
Most Liked
wojtekmach
On Elixir 1.19+ we can use ... to match anything until the end of the message, including multiline output. Support ellipsis in doctest exceptions by sabiwara · Pull Request #14233 · elixir-lang/elixir · GitHub
axelson
I think you may need to use assert_raise in that case:
iex> assert_raise KeyError, fn -> map.c end
As far as I can tell a “pure” doctest will always assert on the full exception text (with the caveat that multi-line exceptions are not supported: Handling multi-newline exception messages in doctests? (Elixir v1.12.0))
James_E
Thanks for the answers.
I’ll mention I also found one other workaround, which is, at least for the doctests, to pare your examples down to have only one key in the error-inspected map:
@doc """
…
iex> difference!(%{a: 10}, %{a: 11})
** (KeyError) key {:a, 11} not found in: %{a: 10}
iex> difference!(%{a: 10}, %{a: 3, b: 4})
** (KeyError) key {:b, 4} not found in: %{a: 10}
"""
axelson
Oooh! That’s really exciting ![]()







