James_E

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 KeyError

expected:

"key {:c, 999} not found in: %{a: 10, b: 10}"

actual:

"key {:c, 999} not found in: %{b: 10, a: 10}"

Most Liked

wojtekmach

wojtekmach

Hex Core Team

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

axelson

Scenic Core Team

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

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

axelson

Scenic Core Team

Oooh! That’s really exciting :tada:

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
clayschick
I'm trying to create a simple query to select distinct values from a column. If I only use select and distinct I get back a list of uniqu...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement