Skip to content

Commit 2512cba

Browse files
committed
Fix doctest example since Inspect.MapSet changed (#13366)
1 parent a99a9cf commit 2512cba

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

lib/ex_unit/lib/ex_unit/doc_test.ex

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,30 +96,30 @@ defmodule ExUnit.DocTest do
9696
values are treated as comments in Elixir code due to the leading
9797
`#` sign, they require special care when being used in doctests.
9898
99-
Imagine you have a map that contains a MapSet and is printed as:
99+
Imagine you have a map that contains a `DateTime` and is printed as:
100100
101-
%{users: #MapSet<[:foo, :bar]>}
101+
%{datetime: #DateTime<2023-06-26 09:30:00+09:00 JST Asia/Tokyo>}
102102
103103
If you try to match on such an expression, `doctest` will fail to compile.
104104
There are two ways to resolve this.
105105
106106
The first is to rely on the fact that doctest can compare internal
107107
structures as long as they are at the root. So one could write:
108108
109-
iex> map = %{users: Enum.into([:foo, :bar], MapSet.new())}
110-
iex> map.users
111-
#MapSet<[:foo, :bar]>
109+
iex> map = %{datetime: DateTime.from_naive!(~N[2023-06-26T09:30:00], "Asia/Tokyo")}
110+
iex> map.datetime
111+
#DateTime<2023-06-26 09:30:00+09:00 JST Asia/Tokyo>
112112
113113
Whenever a doctest starts with "#Name<", `doctest` will perform a string
114114
comparison. For example, the above test will perform the following match:
115115
116-
inspect(map.users) == "#MapSet<[:foo, :bar]>"
116+
inspect(map.datetime) == "#DateTime<2023-06-26 09:30:00+09:00 JST Asia/Tokyo>"
117117
118118
Alternatively, since doctest results are actually evaluated, you can have
119-
the MapSet building expression as the doctest result:
119+
the `DateTime` building expression as the doctest result:
120120
121-
iex> %{users: Enum.into([:foo, :bar], MapSet.new())}
122-
%{users: Enum.into([:foo, :bar], MapSet.new())}
121+
iex> %{datetime: DateTime.from_naive!(~N[2023-06-26T09:30:00], "Asia/Tokyo")}
122+
%{datetime: DateTime.from_naive!(~N[2023-06-26T09:30:00], "Asia/Tokyo")}
123123
124124
The downside of this approach is that the doctest result is not really
125125
what users would see in the terminal.

0 commit comments

Comments
 (0)